DistAppearanceVariable.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 DistAppearanceVariable extends Model
  8. {
  9. use HasDateTimeFormatter;
  10. protected $table = 'dist_appearance_variable';
  11. protected $fillable = ['dist_id', 'appearance_id', 'variable_name','variable_value','variable_type','created_at','updated_at','template_id'];
  12. /*
  13. * 关联到分销商表
  14. */
  15. public function distributor()
  16. {
  17. return $this->hasOne(DistAdminDistributor::class, 'id', 'dist_id');
  18. }
  19. /*
  20. * 关联到外观表
  21. */
  22. public function appearance()
  23. {
  24. return $this->hasOne(DistAppearance::class, 'id', 'appearance_id');
  25. }
  26. /*
  27. * 把原始变量复制给分销商
  28. */
  29. public static function copyAppearanceVariable($appearanceId, $distId){
  30. $distId = intval($distId);
  31. $appearanceId = intval($appearanceId);
  32. $count = self::where('dist_id', $distId)->where('appearance_id', $appearanceId)->count();
  33. if ($count > 0) {
  34. return;
  35. }
  36. $baseDistId = config('dictionary.base_dist_id');
  37. //复制
  38. DB::statement("
  39. INSERT INTO `dist_appearance_variable` (`dist_id`, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, `created_at`, `updated_at`, `template_code`,`variable_code`)
  40. SELECT {$distId}, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, NOW(), NOW(), `template_code`,`variable_code`
  41. FROM `dist_appearance_variable`
  42. WHERE `dist_id` = {$baseDistId} AND `appearance_id` = {$appearanceId};
  43. ");
  44. }
  45. public static function deleteVariable($appearanceId,$distId)
  46. {
  47. return self::where('appearance_id', $appearanceId)->where('dist_id', $distId)->delete();
  48. }
  49. public function syncAppearanceVariables($appearanceId,$distId) {
  50. $appearanceId = intval($appearanceId);
  51. $distId = intval($distId);
  52. // Define criteria for filtering
  53. $criteria = ['dist_id' => $distId, 'appearance_id' => $appearanceId];
  54. $tmpModel = $this;
  55. // Retrieve records from both tables
  56. $tempVariables = $tmpModel
  57. ->where($criteria)
  58. ->get();
  59. $siteModel = new SiteAppearanceVariable();
  60. $siteVariables = $siteModel
  61. ->where($criteria)
  62. ->get()
  63. ->keyBy('id'); // Use IDs as keys for easier comparison
  64. foreach ($tempVariables as $tempVariable) {
  65. $siteVariable = $siteVariables->get($tempVariable->id);
  66. if ($siteVariable) {
  67. // If record exists in `site_appearance_variable`, check for updates
  68. if ($tempVariable->updated_at > $siteVariable->updated_at) {
  69. $siteModel
  70. ->where('id', $siteVariable->id)
  71. ->update([
  72. 'variable_name' => $tempVariable->variable_name,
  73. 'variable_value' => $tempVariable->variable_value,
  74. 'variable_type' => $tempVariable->variable_type,
  75. 'template_code' => $tempVariable->template_code,
  76. 'variable_code' => $tempVariable->variable_code,
  77. 'updated_at' => Carbon::now(),
  78. ]);
  79. }
  80. } else {
  81. // If record does not exist, insert it
  82. $siteModel->insert([
  83. 'id' => $tempVariable->id,
  84. 'dist_id' => $tempVariable->dist_id,
  85. 'appearance_id' => $tempVariable->appearance_id,
  86. 'variable_name' => $tempVariable->variable_name,
  87. 'variable_value' => $tempVariable->variable_value,
  88. 'variable_type' => $tempVariable->variable_type,
  89. 'template_code' => $tempVariable->template_code,
  90. 'variable_code' => $tempVariable->variable_code,
  91. 'created_at' => Carbon::now(),
  92. 'updated_at' => Carbon::now(),
  93. ]);
  94. }
  95. }
  96. // Delete records from `site_appearance_variable` that don’t match `dist_id=1` and `appearance_id=1`
  97. $siteModel
  98. ->where($criteria)
  99. ->whereNotIn('id', $tempVariables->pluck('id')->toArray())
  100. ->delete();
  101. }
  102. }