DistAppearanceTemplate.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Dcat\Admin\Traits\HasDateTimeFormatter;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. class DistAppearanceTemplate extends Model
  8. {
  9. use HasDateTimeFormatter;
  10. protected $table = 'dist_appearance_template';
  11. /*
  12. * 把原始模板复制给分销商
  13. */
  14. public static function copyTemplateToDist($appearanceId,$distId) {
  15. $appearanceId = intval($appearanceId);
  16. $distId = intval($distId);
  17. $count = self::where('dist_id', $distId)->where('appearance_id', $appearanceId)->count();
  18. if ($count > 0) {
  19. return;
  20. }
  21. $baseDistId = config('dictionary.base_dist_id');
  22. //复制
  23. DB::statement("
  24. INSERT INTO `dist_appearance_template` (`dist_id`, `appearance_id`, `file_name`, `file_path`, `content`, `created_at`, `updated_at`, `template_code`)
  25. SELECT {$distId}, `appearance_id`, `file_name`, `file_path`, `content`, NOW(), NOW(), `template_code`
  26. FROM `dist_appearance_template`
  27. WHERE `dist_id` = {$baseDistId} AND `appearance_id` = {$appearanceId};
  28. ");
  29. }
  30. public static function deleteTemplates($appearanceId,$distId) {
  31. self::where('dist_id',$distId)->where('appearance_id',$appearanceId)->delete();
  32. return true;
  33. }
  34. public function syncAppearanceTemplates($appearanceId,$distId)
  35. {
  36. $appearanceId = intval($appearanceId);
  37. $distId = intval($distId);
  38. $criteria = ['dist_id' => $distId, 'appearance_id' => $appearanceId];
  39. $tmpModel = $this;
  40. $tempRecords = $tmpModel
  41. ->where($criteria)
  42. ->get();
  43. $siteModel = new SiteAppearanceTemplate();
  44. $siteRecords = $siteModel
  45. ->where($criteria)
  46. ->get()
  47. ->keyBy('id'); // Use IDs as keys for easier comparison
  48. foreach ($tempRecords as $tempRecord) {
  49. $siteRecord = $siteRecords->get($tempRecord->id);
  50. if ($siteRecord) {
  51. // If record exists in `site_appearance_template`, check for updates
  52. if ($tempRecord->updated_at > $siteRecord->updated_at) {
  53. $siteModel->where('id', $siteRecord->id)
  54. ->update([
  55. 'file_name' => $tempRecord->file_name,
  56. 'file_path' => $tempRecord->file_path,
  57. 'content' => $tempRecord->content,
  58. 'updated_at' => Carbon::now(),
  59. ]);
  60. }
  61. } else {
  62. // If record does not exist, insert it
  63. $siteModel->insert([
  64. 'id' => $tempRecord->id,
  65. 'dist_id' => $tempRecord->dist_id,
  66. 'appearance_id' => $tempRecord->appearance_id,
  67. 'file_name' => $tempRecord->file_name,
  68. 'file_path' => $tempRecord->file_path,
  69. 'content' => $tempRecord->content,
  70. 'created_at' => Carbon::now(),
  71. 'updated_at' => Carbon::now(),
  72. 'template_code' => $tempRecord->template_code,
  73. ]);
  74. }
  75. }
  76. // Delete records from `site_appearance_template` that don’t match `dist_id=1` and `appearance_id=1`
  77. $siteModel
  78. ->where($criteria)
  79. ->whereNotIn('id', $tempRecords->pluck('id')->toArray())
  80. ->delete();
  81. }
  82. }