DistAppearanceTemplate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\DistAppearanceTemplate as Model;
  4. use App\Models\SiteAppearanceTemplate;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. use Illuminate\Support\Carbon;
  7. class DistAppearanceTemplate extends EloquentRepository
  8. {
  9. /**
  10. * Model.
  11. *
  12. * @var string
  13. */
  14. protected $eloquentClass = Model::class;
  15. public static function getContent($appearanceId,$distId,$templateCode)
  16. {
  17. $appearanceId = intval($appearanceId);
  18. $distId = intval($distId);
  19. $data = Model::where('appearance_id', $appearanceId)->where('dist_id', $distId)->where('template_code', $templateCode)->first();
  20. if ($data) {
  21. return $data->content;
  22. }
  23. return '';
  24. }
  25. public static function getTemplateTree($appearance_id,$dist_id)
  26. {
  27. $data = Model::where('appearance_id', $appearance_id)->where('dist_id', $dist_id)->orderBy('file_name', 'asc')->get(['id', 'file_name', 'file_path','template_code']);
  28. return self::buildTree($data->toArray());
  29. }
  30. public static function buildTree(array $data) {
  31. $result = [];
  32. foreach ($data as $item) {
  33. // Extract the path and file information
  34. $path = $item['file_path'];
  35. $id = $item['id'];
  36. $file_name = $item['file_name'];
  37. // Initialize a new entry for this path if it doesn't exist
  38. if (!isset($result[$path])) {
  39. $result[$path] = [
  40. 'id' => 0, // default, as there's no parent ID
  41. 'dist_id' => 0, // assuming dist_id for path
  42. 'file_name' => basename($path), // using the directory name for file_name
  43. 'children' => []
  44. ];
  45. }
  46. // Add the file to the children of the respective path
  47. $result[$path]['children'][] = [
  48. 'id' => $id,
  49. 'dist_id' => 0, // assuming dist_id
  50. 'file_name' => $file_name,
  51. 'template_code' => $item['template_code']
  52. ];
  53. }
  54. $result = array_values($result);
  55. return $result;
  56. }
  57. /*
  58. * 保存模板内容
  59. */
  60. public static function saveContent($appearanceId,$distId,$templateCode, $content)
  61. {
  62. $appearanceId = intval($appearanceId);
  63. $distId = intval($distId);
  64. $data = Model::where('dist_id', $distId)->where('appearance_id', $appearanceId)->where('appearance_id', $appearanceId)->where('template_code', $templateCode)->first();
  65. if ($data) {
  66. $currentContent = $content;
  67. $previousContent = $data->content;
  68. //保存
  69. $data->content = $content;
  70. $data->save();
  71. //加入模版修改日志
  72. DistAppearanceTemplateLog::insertLog($appearanceId,$distId,$templateCode,$currentContent,$previousContent);
  73. return true;
  74. }
  75. return false;
  76. }
  77. /*
  78. * 请空指定模板
  79. */
  80. public static function deleteTemplates($appearanceId,$distId) {
  81. Model::deleteTemplates($appearanceId, $distId);
  82. return true;
  83. }
  84. /*
  85. * 插入模版文件夹
  86. */
  87. // public static function insertTemplateFolder($distId,$appearanceId,$filePath,$fileName,$parentId) {
  88. // $self = new self();
  89. // $model = $self->model();
  90. // $model->dist_id = $distId;
  91. // $model->appearance_id = $appearanceId;
  92. // $model->file_name = $fileName;
  93. // $model->parent_id = $parentId;
  94. // $model->file_type = 0;
  95. // $model->file_path = $filePath;
  96. // $model->template_code = uniqueCode('');
  97. // $model->save();
  98. // // 获取插入数据的 ID
  99. // $insertedId = $model->id;
  100. // return $insertedId;
  101. // }
  102. /*
  103. * 插入模版文件内容
  104. */
  105. public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$content) {
  106. $self = new self();
  107. $model = $self->model();
  108. $model->dist_id = $distId;
  109. $model->appearance_id = $appearanceId;
  110. $model->file_name = $fileName;
  111. $model->file_path = $filePath;
  112. $model->content = $content;
  113. $model->template_code = uniqueCode('');
  114. $model->save();
  115. // 获取插入数据的 ID
  116. $insertedId = $model->id;
  117. return $insertedId;
  118. }
  119. /*
  120. * 把原始模板复制给分销商
  121. */
  122. public static function copyTemplateToDist($appearanceId,$distId) {
  123. return Model::copyTemplateToDist($appearanceId, $distId);
  124. }
  125. /*
  126. * 同步模版到正式表上
  127. */
  128. public static function syncAppearanceTemplates($appearanceId,$distId)
  129. {
  130. $model = new Model();
  131. return $model->syncAppearanceTemplates($appearanceId,$distId);
  132. }
  133. }