DistAppearanceVariable.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_code`,`variable_code`)
  38. SELECT {$distId}, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, NOW(), NOW(), `template_code`,`variable_code`
  39. FROM `dist_appearance_variable`
  40. WHERE `dist_id` = 0 AND `appearance_id` = {$appearanceId};
  41. ");
  42. }
  43. public static function deleteVariable($appearanceId,$distId)
  44. {
  45. return self::where('appearance_id', $appearanceId)->where('dist_id', $distId)->delete();
  46. }
  47. }