DistAppearanceTemplate.php 4.4 KB

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