DistAppearanceTemplate.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_path', '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. $data->content = $content;
  67. $data->save();
  68. //加入模版修改日志
  69. DistAppearanceTemplateLog::insertLog($appearanceId,$distId,$templateCode,$content);
  70. return true;
  71. }
  72. return false;
  73. }
  74. /*
  75. * 请空指定模板
  76. */
  77. public static function deleteTemplates($appearanceId,$distId) {
  78. $self = new self();
  79. $self->model()->where('dist_id',$distId)->where('appearance_id',$appearanceId)->delete();
  80. return true;
  81. }
  82. /*
  83. * 插入模版文件夹
  84. */
  85. // public static function insertTemplateFolder($distId,$appearanceId,$filePath,$fileName,$parentId) {
  86. // $self = new self();
  87. // $model = $self->model();
  88. // $model->dist_id = $distId;
  89. // $model->appearance_id = $appearanceId;
  90. // $model->file_name = $fileName;
  91. // $model->parent_id = $parentId;
  92. // $model->file_type = 0;
  93. // $model->file_path = $filePath;
  94. // $model->template_code = uniqueCode('');
  95. // $model->save();
  96. // // 获取插入数据的 ID
  97. // $insertedId = $model->id;
  98. // return $insertedId;
  99. // }
  100. /*
  101. * 插入模版文件内容
  102. */
  103. public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$content) {
  104. $self = new self();
  105. $model = $self->model();
  106. $model->dist_id = $distId;
  107. $model->appearance_id = $appearanceId;
  108. $model->file_name = $fileName;
  109. $model->file_path = $filePath;
  110. $model->content = $content;
  111. $model->template_code = uniqueCode('');
  112. $model->save();
  113. // 获取插入数据的 ID
  114. $insertedId = $model->id;
  115. return $insertedId;
  116. }
  117. /*
  118. * 把原始模板复制给分销商
  119. */
  120. public static function copyTemplateToDist($appearanceId,$distId) {
  121. return Model::copyTemplateToDist($appearanceId, $distId);
  122. }
  123. /*
  124. * 同步模版到正式表上
  125. */
  126. public static function syncAppearanceTemplates($appearanceId,$distId)
  127. {
  128. $appearanceId = intval($appearanceId);
  129. $distId = intval($distId);
  130. $criteria = ['dist_id' => $distId, 'appearance_id' => $appearanceId];
  131. $tmpModel = new Model();
  132. $tempRecords = $tmpModel
  133. ->where($criteria)
  134. ->get();
  135. $siteModel = new SiteAppearanceTemplate();
  136. $siteRecords = $siteModel
  137. ->where($criteria)
  138. ->get()
  139. ->keyBy('id'); // Use IDs as keys for easier comparison
  140. foreach ($tempRecords as $tempRecord) {
  141. $siteRecord = $siteRecords->get($tempRecord->id);
  142. if ($siteRecord) {
  143. // If record exists in `site_appearance_template`, check for updates
  144. if ($tempRecord->updated_at > $siteRecord->updated_at) {
  145. $siteModel->where('id', $siteRecord->id)
  146. ->update([
  147. 'file_name' => $tempRecord->file_name,
  148. 'file_path' => $tempRecord->file_path,
  149. 'content' => $tempRecord->content,
  150. 'updated_at' => Carbon::now(),
  151. ]);
  152. }
  153. } else {
  154. // If record does not exist, insert it
  155. $siteModel->insert([
  156. 'id' => $tempRecord->id,
  157. 'dist_id' => $tempRecord->dist_id,
  158. 'appearance_id' => $tempRecord->appearance_id,
  159. 'file_name' => $tempRecord->file_name,
  160. 'file_path' => $tempRecord->file_path,
  161. 'content' => $tempRecord->content,
  162. 'created_at' => Carbon::now(),
  163. 'updated_at' => Carbon::now(),
  164. 'template_code' => $tempRecord->template_code,
  165. ]);
  166. }
  167. }
  168. // Delete records from `site_appearance_template` that don’t match `dist_id=1` and `appearance_id=1`
  169. $siteModel
  170. ->where($criteria)
  171. ->whereNotIn('id', $tempRecords->pluck('id')->toArray())
  172. ->delete();
  173. }
  174. /*
  175. * 发布模版与变量
  176. */
  177. public static function publish($appearanceId,$distId) {
  178. //同步模版
  179. self::syncAppearanceTemplates($appearanceId,$distId);
  180. //同步变量
  181. DistAppearanceVariable::syncAppearanceVariables($appearanceId, $distId);
  182. return true;
  183. }
  184. }