DistAppearanceTemplate.php 6.9 KB

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