123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Models;
- use Dcat\Admin\Traits\HasDateTimeFormatter;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- class DistAppearanceVariable extends Model
- {
- use HasDateTimeFormatter;
- protected $table = 'dist_appearance_variable';
- protected $fillable = ['dist_id', 'appearance_id', 'variable_name','variable_value','variable_type','created_at','updated_at','template_id'];
- /*
- * 关联到分销商表
- */
- public function distributor()
- {
- return $this->hasOne(DistAdminDistributor::class, 'id', 'dist_id');
- }
- /*
- * 关联到外观表
- */
- public function appearance()
- {
- return $this->hasOne(DistAppearance::class, 'id', 'appearance_id');
- }
- /*
- * 把原始变量复制给分销商
- */
- public static function copyAppearanceVariable($appearanceId, $distId){
- $distId = intval($distId);
- $appearanceId = intval($appearanceId);
- $count = self::where('dist_id', $distId)->where('appearance_id', $appearanceId)->count();
- if ($count > 0) {
- return;
- }
- //复制
- DB::statement("
- INSERT INTO `dist_appearance_variable` (`dist_id`, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, `created_at`, `updated_at`, `template_id`,`variable_code`)
- SELECT {$distId}, `appearance_id`, `variable_name`, `variable_value`, `variable_type`, NOW(), NOW(), `template_id`,`variable_code`
- FROM `dist_appearance_variable`
- WHERE `dist_id` = 0 AND `appearance_id` = {$appearanceId};
- ");
- //更新模版ID
- $tmpModel = new DistAppearanceTemplate();
- $tmpRow = $tmpModel->select('id','dist_id','file_path','file_name','parent_id')->wherein('dist_id',[0,$distId])->where('appearance_id',$appearanceId)->get();
- $variableRows = self::select('id','dist_id','appearance_id','variable_name','template_id')->where('dist_id', $distId)->where('appearance_id', $appearanceId)->get();
- if ($variableRows) {
- $updateData = [];
- foreach ($variableRows as $row) {
- if ($row->template_id > 0) {
- $newTemplateId = self::findTemplateId($row->template_id,$tmpRow);
- if ($newTemplateId > -1) {
- $updateData[] = ['id' => $row->id, 'template_id' => $newTemplateId];
- }
- }
- }
- //批量更新
- if ($updateData) {
- self::upsert($updateData,['id'],['template_id']);
- }
- }
- }
- private static function findTemplateId($oldTemplateId,&$tmpRow)
- {
- if (!$tmpRow) {return -1;}
- $oldRow = null;
- foreach ($tmpRow as $row) {
- if ($row->id == $oldTemplateId && $row->dist_id == 0) {
- $oldRow = $row;
- }
- }
- if ($oldRow) {
- foreach ($tmpRow as $row) {
- if ($oldRow->file_path == $row->file_path && $row->dist_id > 0) {
- return $row->id;
- }
- }
- }
- return -1;
- }
- }
|