TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'], ] ] ]; } public static function doCopyStatus($id = 0, $doCopy = 0) { $model = self::findOne($id); $type = $model->type; $type = json_decode($type, true); if($model->finished != 1 && $doCopy){ self::doCopy($model->from_store_id, $model->to_store_id, $type, $id); } $model = self::findOne($id); return [ 'code' => 0, 'msg' => 'ok', 'data' => [ 'model' => $model, ], ]; } // type //home/category/product/center/all public static function doCopy($from_store_id = 0, $to_store_id = 0, $type = ['home'], $id = 0) { if($from_store_id == $to_store_id){ return [ 'code' => 1, 'msg' => '不能选择自己店铺的模板', ]; } $res = [ 'code' => 0, 'msg' => 'success', ]; if($id){ $model = self::findOne($id); }else{ $type = json_encode($type); $model = new self(); $model->from_store_id = $from_store_id; $model->to_store_id = $to_store_id; $model->type = $type; $model->save(); } $res['data'] = $model; if(!$id){ // return $res; } if($model->finished == 1){ return $res; } $finished = 1; $type = $model->type; $type = json_decode($type, true); if(!$id){ if(array_intersect(['delete_goods'], $type)){ $copy = $model->doDelGoods($to_store_id); //debug_log(['doDelGoods' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } if(array_intersect(['delete_cat'], $type)){ $copy = $model->doDelCat($to_store_id); //debug_log(['doDelCat' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } } if(array_intersect(['home', 'all'], $type)){ $copy = $model->doCopyHome(); //debug_log(['doCopyHome' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } if(array_intersect(['center', 'all'], $type)){ $copy = $model->doCopyCenter(); //debug_log(['doCopyCenter' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } if(array_intersect(['footnav', 'all'], $type)){ $copy = $model->doCopyFootnav(); //debug_log(['doCopyFootnav' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } if(array_intersect(['category', 'all'], $type)){ $copy = $model->doCopyCategory(); //debug_log(['doCopyCategory' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } if(array_intersect(['product', 'all'], $type)){ $copy = $model->doCopyProduct(); //debug_log(['doCopyProduct' => $finished, 'copy' => $copy], 'store_copy.log'); if(!$copy && $finished){ $finished = 0; } } $model->finished = $finished; $model->save(); return $res; } public function doDelGoods($store_id) { Goods::updateAll(['is_delete' => 1], ['store_id' => $store_id]); return true; } public function doDelCat($store_id) { Cat::updateAll(['is_delete' => 1], ['store_id' => $store_id]); return true; } public function doCopyHome() { $home_list = json_decode($this->home_list, true); if(!empty($home_list['from']) && !empty($home_list['to'])){ if(count($home_list['from']) == count($home_list['to'])){ return true; } } $params = [ 'is_delete' => 0, 'is_index' => 1, 'store_id' => $this->from_store_id, ]; $template = NewDiyTemplate::findOne($params); $ids = [$template->id]; // $controls = NewDiyTemplate::find() // ->where([ // 'store_id' => $this->from_store_id, // 'is_delete' => 0, // 'type' => 100 // ])->one(); // $ids = [$controls->id]; $home_list = [ 'from' => $ids, ]; $attr = $template->attributes; $params = [ 'is_delete' => 0, 'is_index' => 1, 'store_id' => $this->to_store_id, ]; $template = NewDiyTemplate::findOne($params); if ($template) { $template->is_index = 0; $template->save(); } unset($attr['id']); $attr['store_id'] = $this->to_store_id; $model = new NewDiyTemplate(); $model->attributes = $attr; $model->save(); $ids = [$model->id]; // $attr = $controls->attributes; // unset($attr['id']); // $attr['store_id'] = $this->to_store_id; // $model = new NewDiyTemplate(); // $model->attributes = $attr; // $model->save(); // $ids[] = $model->id; $home_list['to'] = $ids; $this->home_list = json_encode($home_list); $this->save(); return true; } public function doCopyCenter() { $center_list = json_decode($this->center_list, true); if(!empty($center_list['from']) && !empty($center_list['to'])){ if(count($center_list['from']) == count($center_list['to'])){ return true; } } $list = NewDiyTemplate::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, 'name' => 'system' ])->orderBy(['id' => SORT_ASC])->asArray()->all(); $template = null; foreach ($list as $value) { $itemp = json_decode($value['template'], true); if($itemp['system_type'] == 'my'){ $template = $value; break; } } $ids = [$template['id']]; $center_list = [ 'from' => $ids, ]; $attr = $template; $list = NewDiyTemplate::find() ->where([ 'store_id' => $this->to_store_id, 'is_delete' => 0, 'name' => 'system' ])->orderBy(['id' => SORT_ASC])->all(); foreach ($list as $value) { $itemp = json_decode($value['template'], true); if($itemp['system_type'] == 'my'){ $value->is_delete = 1; $value->save(); } } unset($attr['id']); $attr['store_id'] = $this->to_store_id; $model = new NewDiyTemplate(); $model->attributes = $attr; $model->save(); $ids = [$model->id]; $center_list['to'] = $ids; $this->center_list = json_encode($center_list); $this->save(); return true; } public function doCopyFootnav() { $footnav_list = json_decode($this->footnav_list, true); if(!empty($footnav_list['from']) && !empty($footnav_list['to'])){ if(count($footnav_list['from']) == count($footnav_list['to'])){ return true; } } $template = NewDiyTemplate::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, 'name' => 'controls' ])->orderBy(['id' => SORT_ASC])->asArray()->one(); // $template = null; $copyItem = []; $itemp = json_decode($template['template'], true); if($itemp){ foreach ($itemp as $widget) { if($widget['controlComName'] == 'widgetNavControl'){ $copyItem = $widget; break; } } } $ids = [$template['id']]; $footnav_list = [ 'from' => $ids, ]; $attr = $template; $template = NewDiyTemplate::find() ->where([ 'store_id' => $this->to_store_id, 'is_delete' => 0, 'name' => 'controls' ])->orderBy(['id' => SORT_ASC])->one(); $itemp = json_decode($template['template'], true); if($itemp){ foreach ($itemp as &$widget) { if($widget['controlComName'] == 'widgetNavControl'){ $widget = $copyItem; break; } } } $template->template = json_encode($itemp); $template->save(); $ids = [$template->id]; $footnav_list['to'] = $ids; $this->footnav_list = json_encode($footnav_list); $this->save(); return true; } public function doCopyCategory() { $diy_category_list = json_decode($this->diy_category_list, true); if(!empty($diy_category_list['from']) && !empty($diy_category_list['to'])){ if(count($diy_category_list['from']) == count($diy_category_list['to'])){ return true; } } $list = NewDiyTemplate::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, 'name' => 'system' ])->orderBy(['id' => SORT_ASC])->asArray()->all(); $template = null; $goods_d_template = null; foreach ($list as $value) { $itemp = json_decode($value['template'], true); if($itemp['system_type'] == 'classify'){ $template = $value; } elseif ($itemp['system_type'] == 'goodsDetail') { $goods_d_template = $value; } } $ids = [$template['id']]; $diy_category_list = [ 'from' => $ids, ]; $attr = $template; $goods_d_attr = $goods_d_template; $list = NewDiyTemplate::find() ->where([ 'store_id' => $this->to_store_id, 'is_delete' => 0, 'name' => 'system' ])->orderBy(['id' => SORT_ASC])->all(); foreach ($list as $value) { $itemp = json_decode($value['template'], true); if($itemp['system_type'] == 'classify'){ $value->is_delete = 1; $value->save(); } elseif ($itemp['system_type'] == 'goodsDetail') { $value->is_delete = 1; $value->save(); } } unset($attr['id']); unset($goods_d_attr['id']); $attr['store_id'] = $this->to_store_id; $goods_d_attr['store_id'] = $this->to_store_id; $model = new NewDiyTemplate(); $model->attributes = $attr; $model->save(); $ids = [$model->id]; $model = new NewDiyTemplate(); $model->attributes = $goods_d_attr; $model->save(); $diy_category_list['to'] = $ids; $this->diy_category_list = json_encode($diy_category_list); $this->save(); return true; } public function doCopyProductCate() { $key_value_id = []; try { $category_list = json_decode($this->category_list, true); if(!empty($category_list['from']) && !empty($category_list['to'])){ if(count($category_list['from']) == count($category_list['to'])){ return true; } } $list = Cat::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, 'parent_id' => 0 ])->orderBy(['id' => SORT_ASC])->all(); // $category_list = [ // 'from' => array_column($list, 'id'), // ]; $ids = []; foreach ($list as $value) { $attr = $value->attributes; $from_attr_id = $attr['id']; $key_value_id[$from_attr_id] = 0; unset($attr['id']); $attr['store_id'] = $this->to_store_id; $attr['parent_id'] = 0; $model = new Cat(); $model->attributes = $attr; $model->save(); $ids[] = $model->id; $key_value_id[$from_attr_id] = $model->id; } /*debug_log([ 'key_value_id' => $key_value_id, 'type' => 'category_list' ],'store_copy.log');*/ $list = Cat::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, ])->andWhere(['>', 'parent_id', 0])->orderBy(['id' => SORT_ASC])->all(); // $category_list = [ // 'from' => array_column($list, 'id'), // ]; foreach ($list as $value) { $attr = $value->attributes; $from_attr_id = $attr['id']; $key_value_id[$from_attr_id] = 0; unset($attr['id']); $attr['store_id'] = $this->to_store_id; /*debug_log([ 'from_attr_id' => $from_attr_id, 'parent_id' => $attr['parent_id'], 'real' => $key_value_id[$attr['parent_id']], 'type' => 'category_list' ],'store_copy.log');*/ if ($attr['parent_id'] > 0) { $attr['parent_id'] = $key_value_id[$attr['parent_id']] ?? 0; } $model = new Cat(); $model->attributes = $attr; $model->save(); $ids[] = $model->id; $key_value_id[$from_attr_id] = $model->id; } $category_list['to'] = $ids; $this->category_list = json_encode($category_list); if (!$this->save()) { throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE)); } } catch (\Exception $e) { /*debug_log([ 'message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'type' => 'category_list' ],'store_copy.log');*/ $category_list = json_decode($this->category_list, true); $category_list['message'] = $e->getMessage(); $category_list['line'] = $e->getLine(); $category_list['file'] = $e->getFile(); $this->category_list = json_encode($category_list, JSON_UNESCAPED_UNICODE); $this->save(); } return $key_value_id; } public function doCopyProductAttr() { $attr_group_key_value_id = []; $attr_key_value_id = []; try { $attr_group_list = json_decode($this->attr_group_list, true); if(!empty($attr_group_list['from']) && !empty($attr_group_list['to'])){ if(count($attr_group_list['from']) == count($attr_group_list['to'])){ return true; } } $list = AttrGroup::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, ])->orderBy(['id' => SORT_ASC])->all(); $attr_group_list = [ 'from' => array_column($list, 'id'), ]; $ids = []; foreach ($list as $value) { $attr = $value->attributes; $old_attr_group_id = $attr['id']; unset($attr['id']); $attr['store_id'] = $this->to_store_id; $model = new AttrGroup(); $model->attributes = $attr; $model->save(); $ids[] = $model->id; $attr_group_key_value_id[$old_attr_group_id] = $model->id; } $attr_group_list['to'] = $ids; $this->attr_group_list = json_encode($attr_group_list); if (!$this->save()) { throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE)); } } catch (\Exception $e) { /*debug_log([ 'message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'type' => 'attr_group_list' ],'store_copy.log');*/ $attr_group_list = json_decode($this->attr_group_list, true); $attr_group_list['message'] = $e->getMessage(); $attr_group_list['line'] = $e->getLine(); $attr_group_list['file'] = $e->getFile(); $this->attr_group_list = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE); $this->save(); } try { $attr_list = json_decode($this->attr_list, true); if(!empty($attr_list['from']) && !empty($attr_list['to'])){ if(count($attr_list['from']) == count($attr_list['to'])){ return; } } $list = Attr::find() ->where([ 'attr_group_id' => $attr_group_list['from'], 'is_delete' => 0, ])->orderBy(['id' => SORT_ASC])->all(); $ids = []; foreach ($list as $value) { $ids[] = $value->id; } $attr_list = [ 'from' => $ids, ]; $ids = []; foreach ($list as $value) { $attr = $value->attributes; $old_attr_id = $attr['id']; unset($attr['id']); $attr['attr_group_id'] = $attr_group_key_value_id[$attr['attr_group_id']] ?? 0; $model = new Attr(); $model->attributes = $attr; $model->save(); $ids[] = $model->id; $attr_key_value_id[$old_attr_id] = $model->id; } $attr_list['to'] = $ids; $this->attr_list = json_encode($attr_list); if (!$this->save()) { throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE)); } } catch (\Exception $e) { /*debug_log([ 'message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'type' => 'attr_list' ],'store_copy.log');*/ $attr_list = json_decode($this->attr_list, true); $attr_list['message'] = $e->getMessage(); $attr_list['line'] = $e->getLine(); $attr_list['file'] = $e->getFile(); $this->attr_list = json_encode($attr_list, JSON_UNESCAPED_UNICODE); $this->save(); } return [ 'attr_group_key_value_id' => $attr_group_key_value_id, 'attr_key_value_id' => $attr_key_value_id, ]; } public function doCopyProduct() { try { $product_cate = $this->doCopyProductCate(); $product_attr = $this->doCopyProductAttr(); $goods_ids = json_decode($this->goods_ids, true); if(!empty($goods_ids['from'])){ if(empty($goods_ids['to'])){ $goods_ids['to'] = []; } if(count($goods_ids['from']) <= count($goods_ids['to'])){ return true; } }else{ $list = Goods::find() ->where([ 'store_id' => $this->from_store_id, 'is_delete' => 0, ])->andWhere(['<=', 'md_food_id', 0])->select('id')->orderBy(['id' => SORT_ASC])->all(); $ids = []; foreach ($list as $value) { $ids[] = $value->id; } $goods_ids = [ 'from' => $ids, ]; if(empty($ids)){ $goods_ids['to'] = []; } $this->goods_ids = json_encode($goods_ids); $this->save(); if(empty($ids)){ return true; } // $queue = queue_push(new \app\jobs\StoreCopyJob(['id' => $this->id])); // \Yii::error(['queue StoreCopyJob in', $queue]); // return; } $attr_list = json_decode($this->attr_list, true); $category_list = json_decode($this->category_list, true); $ids = []; $icount = 20; $store_cloud_id = StoreCloud::findOne(['store_id' => $this->to_store_id, 'is_delete' => 0]); foreach (array_slice($goods_ids['from'], count($goods_ids['to'] ?? [])) as $k => $value) { // if($k >= $icount){ // $queue = queue_push(new \app\jobs\StoreCopyJob(['id' => $this->id])); // \Yii::error(['queue StoreCopyJob in', $queue]); // return; // } $goods = Goods::findOne($value)->toArray(); $attr = $goods; $fromId = $attr['id']; unset($attr['id']); $attr['store_id'] = $this->to_store_id; $gattr = json_decode($attr['attr'], true); // var_dump($attr_list); foreach($gattr as &$gatt){ foreach($gatt['attr_list'] as &$att){ $att['attr_id'] = $product_attr['attr_key_value_id'][$att['attr_id']]; $att['share_commission_first'] = 0; $att['share_commission_second'] = 0; $att['share_commission_third'] = 0; } } $attr['attr'] = json_encode($gattr); $attr['full_cut'] = '{"pieces":0,"forehead":0}'; $attr['integral'] = '{"give":0,"forehead":0}'; $attr['verify_card_id'] = ''; $model = new Goods(); // var_dump($value, $attr); $model->setAttributes($attr, false); // var_dump($model->attributes);die; $model->save(); if ($model->cloud_goods_id > 0) { $form = new GoodsForm(); $form->id = $model->cloud_goods_id; $form->store_id = $model->store_id; $goodsInfo = $form->saveCloudGoods(); $merchantForm = new MerchantForm(); $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0; $result = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id'], $model->store_id); //debug_log($result, 'store_copy.log'); } // var_dump($model);die; $goods_ids['to'][] = $model->id; $this->goods_ids = json_encode($goods_ids); $this->save(); $cats = GoodsCat::findAll(['goods_id' => $fromId, 'is_delete' => 0]); foreach ($cats as $cat) { $attr = $cat->attributes; unset($attr['id']); $attr['store_id'] = $this->to_store_id; $attr['goods_id'] = $model->id; $attr['cat_id'] = $product_cate[$attr['cat_id']]; $modelCat = new GoodsCat(); $modelCat->attributes = $attr; $modelCat->save(); } $gpics = GoodsPic::findAll(['goods_id' => $fromId, 'is_delete' => 0]); foreach ($gpics as $gpic) { $attr = $gpic->attributes; unset($attr['id']); $attr['goods_id'] = $model->id; $modelPic = new GoodsPic(); $modelPic->attributes = $attr; $modelPic->save(); } } // if (empty($goods_cache) && empty($cate_cache)) { // return false; // } else { $cate_ = json_decode($this->category_list, true); $cate_cache = array_combine($cate_['from'], $cate_['to']); $goods_cache = array_combine($goods_ids['from'], $goods_ids['to']); $params = [ 'is_delete' => 0, 'is_index' => 1, 'store_id' => $this->to_store_id, ]; $template = NewDiyTemplate::findOne($params); $template_ = \json_decode($template->template, true); if (!$template_) { return; } foreach ($template_['templates'] as &$item) { // 头部导航 if ($item['type'] == 'fastNav') { $item = $this->replaceFastNav($item, $goods_cache, $cate_cache); } // 图片轮播 if ($item['type'] == 'banner') { $item = $this->replaceBanner($item, $goods_cache, $cate_cache); } // 魔方导航 if ($item['type'] == 'cubeNav') { $item = $this->replaceCubeNav($item, $goods_cache, $cate_cache); } // 魔方图片 if ($item['type'] == 'container') { $item = $this->replaceContainer($item, $goods_cache, $cate_cache); } // 产品展示 if ($item['type'] == 'productShow') { //debug_log($item, 'template.log'); $item = $this->replaceProductShow($item, $goods_cache, $cate_cache); //debug_log($item, 'template.log'); } //产品分类 if ($item['type'] == 'productClassify') { $item = $this->replaceProductClassify($item, $goods_cache, $cate_cache); } // 倒计时 if ($item['type'] == 'marketCountDown') { $item = $this->replaceMarketCountDown($item, $goods_cache, $cate_cache); } // 首页推荐 if ($item['type'] == 'indexRecommend') { // Todo 该模块数据有误,后期解决 // $item = $this->replaceIndexRecommend($item, $goodsCache, $cacheCat); } } $template->template = json_encode($template_, JSON_UNESCAPED_UNICODE); $template->save(); // } return true; } catch (\Exception $e) { /*debug_log([ 'message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'type' => 'product_list' ],'store_copy.log');*/ $category_list = json_decode($this->category_list, true); $category_list['message'] = $e->getMessage(); $category_list['line'] = $e->getLine(); $category_list['file'] = $e->getFile(); return true; } } // 替换头部导航 private function replaceFastNav($data, $goodsCache, $cacheCat) { foreach ($data['params']['list'] as &$item) { $item = $this->replaceLink($item, $goodsCache, $cacheCat); } return $data; } // 替换图片轮播 private function replaceBanner($data, $goodsCache, $cacheCat) { foreach ($data['params']['bannerList'] as &$item) { $item = $this->replaceLink($item, $goodsCache, $cacheCat); } return $data; } // 替换魔方导航 private function replaceCubeNav($data, $goodsCache, $cacheCat) { foreach ($data['params']['list'] as &$item) { $item = $this->replaceLink($item, $goodsCache, $cacheCat); } return $data; } // 替换魔方图片 private function replaceContainer($data, $goodsCache, $cacheCat) { foreach ($data['params']['cubeCoverList'] as &$item) { $item = $this->replaceLink($item, $goodsCache, $cacheCat); } return $data; } // 替换产品展示 private function replaceProductShow($data, $goodsCache, $cacheCat) { // 替换分类id if (isset($data['params']['classifyData']) && isset($cacheCat[$data['params']['classifyData']])) { $data['params']['classifyData'] = $cacheCat[$data['params']['classifyData']]; } // 替换分类商品id if (isset($data['params']['classifyArr'])) { foreach ($data['params']['classifyArr'] as &$item) { if (isset($goodsCache[$item['id']])) { $item['id'] = $goodsCache[$item['id']]; } } } // 替换商品id if (isset($data['params']['selectData'])) { foreach ($data['params']['selectData'] as &$item) { if (isset($goodsCache[$item['id']])) { $item['id'] = $goodsCache[$item['id']]; } } } return $data; } // 替换产品分类 private function replaceProductClassify($data, $goodsCache, $cacheCat) { foreach ($data['params']['classifyList'] as &$item) { // 替换分类 if (empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) { $item['id'] = $cacheCat[$item['classifyId']]; $item['classifyId'] = $cacheCat[$item['classifyId']]; } // 替换商品id foreach ($item['data'] as &$value) { if (isset($goodsCache[$value['id']])) { $value['id'] = $goodsCache[$value['id']]; } } } return $data; } // 替换倒计时 private function replaceMarketCountDown($data, $goodsCache, $cacheCat) { $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat); return $data; } private function replaceLink($item, $goodsCache, $cacheCat) { if (!isset($item['link']['link'])) { return $item; } // 处理分类id if ( strpos($item['link']['link'], '/pages/cat/cat?cat_id=') !== false && $item['link']['params'][0]['value'] != '' && isset($cacheCat[$item['link']['params'][0]['value']]) ) { $item['link']['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$item['link']['params'][0]['value']]; $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']]; } // 处理商品列表 if ( strpos($item['link']['link'], '/other/list/list?cat_id=') !== false && $item['link']['params'][0]['value'] != '' && isset($cacheCat[$item['link']['params'][0]['value']]) ) { $item['link']['link'] = '/other/list/list?cat_id=' . $cacheCat[$item['link']['params'][0]['value']]; $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']]; } // 处理商品详情 if ( strpos($item['link']['link'], '/goods/goods/goods?id=') !== false && $item['link']['params'][0]['value'] != '' && isset($goodsCache[$item['link']['params'][0]['value']]) ) { $item['link']['link'] = '/goods/goods/goods?id=' . $goodsCache[$item['link']['params'][0]['value']]; $item['link']['params'][0]['value'] = $goodsCache[$item['link']['params'][0]['value']]; } return $item; } }