DistAppearanceVariable.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\DistAppearanceVariable as Model;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. use App\Models\SiteAppearanceVariable;
  7. use Illuminate\Support\Carbon;
  8. class DistAppearanceVariable extends EloquentRepository
  9. {
  10. /**
  11. * Model.
  12. *
  13. * @var string
  14. */
  15. protected $eloquentClass = Model::class;
  16. public static function getVariableRow($distId,$appearanceId,$templateId)
  17. {
  18. $model = new Model();
  19. $variable = $model->where('dist_id',$distId)->where('appearance_id',$appearanceId)->whereIn('template_id',[0,$templateId])->get();
  20. if($variable){
  21. return $variable;
  22. }else{
  23. return '';
  24. }
  25. }
  26. /*
  27. * 把原始变量复制给分销商
  28. */
  29. public static function copyAppearanceVariable($appearanceId, $distId){
  30. Model::copyAppearanceVariable($appearanceId, $distId);
  31. }
  32. /*
  33. * 删除分销商主题变量
  34. */
  35. public static function deleteVariable($appearanceId,$distId)
  36. {
  37. return Model::deleteVariable($appearanceId, $distId);
  38. }
  39. /*
  40. * 同步变量到正式表
  41. */
  42. public static function syncAppearanceVariables($appearanceId,$distId) {
  43. $appearanceId = intval($appearanceId);
  44. $distId = intval($distId);
  45. // Define criteria for filtering
  46. $criteria = ['dist_id' => $distId, 'appearance_id' => $appearanceId];
  47. $tmpModel = new Model();
  48. // Retrieve records from both tables
  49. $tempVariables = $tmpModel
  50. ->where($criteria)
  51. ->get();
  52. $siteModel = new SiteAppearanceVariable();
  53. $siteVariables = $siteModel
  54. ->where($criteria)
  55. ->get()
  56. ->keyBy('id'); // Use IDs as keys for easier comparison
  57. foreach ($tempVariables as $tempVariable) {
  58. $siteVariable = $siteVariables->get($tempVariable->id);
  59. if ($siteVariable) {
  60. // If record exists in `site_appearance_variable`, check for updates
  61. if ($tempVariable->updated_at > $siteVariable->updated_at) {
  62. $siteModel
  63. ->where('id', $siteVariable->id)
  64. ->update([
  65. 'variable_name' => $tempVariable->variable_name,
  66. 'variable_value' => $tempVariable->variable_value,
  67. 'variable_type' => $tempVariable->variable_type,
  68. 'template_code' => $tempVariable->template_code,
  69. 'variable_code' => $tempVariable->variable_code,
  70. 'updated_at' => Carbon::now(),
  71. ]);
  72. }
  73. } else {
  74. // If record does not exist, insert it
  75. $siteModel->insert([
  76. 'id' => $tempVariable->id,
  77. 'dist_id' => $tempVariable->dist_id,
  78. 'appearance_id' => $tempVariable->appearance_id,
  79. 'variable_name' => $tempVariable->variable_name,
  80. 'variable_value' => $tempVariable->variable_value,
  81. 'variable_type' => $tempVariable->variable_type,
  82. 'template_code' => $tempVariable->template_code,
  83. 'variable_code' => $tempVariable->variable_code,
  84. 'created_at' => Carbon::now(),
  85. 'updated_at' => Carbon::now(),
  86. ]);
  87. }
  88. }
  89. // Delete records from `site_appearance_variable` that don’t match `dist_id=1` and `appearance_id=1`
  90. $siteModel
  91. ->where($criteria)
  92. ->whereNotIn('id', $tempVariables->pluck('id')->toArray())
  93. ->delete();
  94. }
  95. }