DistAppearanceTemplate.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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' => trim($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,$data->file_name,$data->file_path,$templateCode,$currentContent,$previousContent);
  73. return true;
  74. }
  75. return false;
  76. }
  77. public static function addDelTree($appearanceId,$distId,$fileId,$fileName, $filePath,$type) {
  78. if (empty($appearanceId) || empty($distId)) {
  79. return ['status' => 0,'msg' => 'Missing required parameters'];
  80. }
  81. $model = new Model();
  82. if ($type == 'add') {
  83. $count = $model->where('appearance_id', $appearanceId)->where('dist_id', $distId)->where('file_name', $fileName)->count();
  84. if ($count > 0) {
  85. return ['status' => 0,'msg' => 'File name already exists'];
  86. }
  87. $filePath = empty($filePath)?'':$filePath;
  88. $model = new Model();
  89. $model->dist_id = $distId;
  90. $model->appearance_id = $appearanceId;
  91. $model->file_name = $fileName;
  92. $model->file_path = $filePath;
  93. $model->content = '';
  94. $model->template_code = uniqueCode('');
  95. $model->save();
  96. return ['status' => 1];
  97. } else {
  98. $row = $model->where('appearance_id', $appearanceId)->where('dist_id', $distId)->where('id', $fileId)->first();
  99. if ($row) {
  100. $row->delete();
  101. //加入模版修改日志
  102. DistAppearanceTemplateLog::insertLog($appearanceId,$distId,$row->file_name,$row->file_path,$row->template_code,'',$row->content);
  103. return ['status' => 1];
  104. }
  105. return ['status' => 0,'msg' => 'File ID not found'];
  106. }
  107. }
  108. /*
  109. * 请空指定模板
  110. */
  111. public static function deleteTemplates($appearanceId,$distId) {
  112. Model::deleteTemplates($appearanceId, $distId);
  113. return true;
  114. }
  115. /*
  116. * 插入模版文件夹
  117. */
  118. // public static function insertTemplateFolder($distId,$appearanceId,$filePath,$fileName,$parentId) {
  119. // $self = new self();
  120. // $model = $self->model();
  121. // $model->dist_id = $distId;
  122. // $model->appearance_id = $appearanceId;
  123. // $model->file_name = $fileName;
  124. // $model->parent_id = $parentId;
  125. // $model->file_type = 0;
  126. // $model->file_path = $filePath;
  127. // $model->template_code = uniqueCode('');
  128. // $model->save();
  129. // // 获取插入数据的 ID
  130. // $insertedId = $model->id;
  131. // return $insertedId;
  132. // }
  133. /*
  134. * 插入模版文件内容
  135. */
  136. public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$content) {
  137. $self = new self();
  138. $model = $self->model();
  139. $model->dist_id = $distId;
  140. $model->appearance_id = $appearanceId;
  141. $model->file_name = $fileName;
  142. $model->file_path = $filePath;
  143. $model->content = $content;
  144. $model->template_code = uniqueCode('');
  145. $model->save();
  146. // 获取插入数据的 ID
  147. $insertedId = $model->id;
  148. return $insertedId;
  149. }
  150. /*
  151. * 把原始模板复制给分销商
  152. */
  153. public static function copyTemplateToDist($appearanceId,$distId) {
  154. return Model::copyTemplateToDist($appearanceId, $distId);
  155. }
  156. /*
  157. * 同步模版到正式表上
  158. */
  159. public static function syncAppearanceTemplates($appearanceId,$distId)
  160. {
  161. $model = new Model();
  162. return $model->syncAppearanceTemplates($appearanceId,$distId);
  163. }
  164. }