DistAppearanceVariable.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. class DistAppearanceVariable extends Model
  7. {
  8. use HasDateTimeFormatter;
  9. protected $table = 'dist_appearance_variable';
  10. protected $fillable = ['dist_id', 'appearance_id', 'variable_name','variable_value','variable_type','created_at','updated_at','template_id'];
  11. /*
  12. * 关联到分销商表
  13. */
  14. public function distributor()
  15. {
  16. return $this->hasOne(DistAdminDistributor::class, 'id', 'dist_id');
  17. }
  18. /*
  19. * 关联到外观表
  20. */
  21. public function appearance()
  22. {
  23. return $this->hasOne(DistAppearance::class, 'id', 'appearance_id');
  24. }
  25. /*
  26. * 把原始变量复制给分销商
  27. */
  28. public static function copyAppearanceVariable($appearanceId, $distId){
  29. $distId = intval($distId);
  30. $appearanceId = intval($appearanceId);
  31. $count = self::where('dist_id', $distId)->where('appearance_id', $appearanceId)->count();
  32. if ($count > 0) {
  33. return;
  34. }
  35. //复制
  36. DB::statement("
  37. INSERT INTO `dist_appearance_variable` (`dist_id`, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, `created_at`, `updated_at`, `template_id`,`variable_code`)
  38. SELECT {$distId}, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, NOW(), NOW(), `template_id`,`variable_code`
  39. FROM `dist_appearance_variable`
  40. WHERE `dist_id` = 0 AND `appearance_id` = {$appearanceId};
  41. ");
  42. //更新模版ID
  43. $tmpModel = new DistAppearanceTemplate();
  44. $tmpRow = $tmpModel->select('id','dist_id','file_path','file_name','parent_id')->wherein('dist_id',[0,$distId])->where('appearance_id',$appearanceId)->get();
  45. $variableRows = self::select('id','dist_id','appearance_id','variable_name','template_id')->where('dist_id', $distId)->where('appearance_id', $appearanceId)->get();
  46. if ($variableRows) {
  47. $updateData = [];
  48. foreach ($variableRows as $row) {
  49. if ($row->template_id > 0) {
  50. $newTemplateId = self::findTemplateId($row->template_id,$tmpRow);
  51. if ($newTemplateId > -1) {
  52. $updateData[] = ['id' => $row->id, 'template_id' => $newTemplateId];
  53. }
  54. }
  55. }
  56. //批量更新
  57. if ($updateData) {
  58. self::upsert($updateData,['id'],['template_id']);
  59. }
  60. }
  61. }
  62. private static function findTemplateId($oldTemplateId,&$tmpRow)
  63. {
  64. if (!$tmpRow) {return -1;}
  65. $oldRow = null;
  66. foreach ($tmpRow as $row) {
  67. if ($row->id == $oldTemplateId && $row->dist_id == 0) {
  68. $oldRow = $row;
  69. }
  70. }
  71. if ($oldRow) {
  72. foreach ($tmpRow as $row) {
  73. if ($oldRow->file_path == $row->file_path && $row->dist_id > 0) {
  74. return $row->id;
  75. }
  76. }
  77. }
  78. return -1;
  79. }
  80. }