from_store_id; $to_store_id = $this->to_store_id; $activity_id = $this->activity_id; foreach ($to_store_id as $store_id_item) { $cacheActivity = $this->doCopyCutPrice($from_store_id, $store_id_item, $activity_id); $cacheCat = $this->doCopyCutPriceCat($from_store_id, $store_id_item); $cache = $this->doCopyCutPriceGoods($from_store_id, $store_id_item, $cacheActivity, $cacheCat); } } public function doCopyCutPrice($from_store_id, $to_store_id, $activity_id) { try { $list = ActivityCutPrice::find() ->where([ 'store_id' => $from_store_id, 'id' => $activity_id ])->orderBy(['id' => SORT_ASC])->all(); $cache = []; foreach ($list as $value) { $id = $value['id']; $attr = $value->attributes; unset($attr['id']); //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE, 'from_id' => $value['id'] ]); $attr['store_id'] = $to_store_id; $attr['goods_ids'] = ''; $model = ActivityCutPrice::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPrice(); $model->setAttributes($attr, false); $model->is_delete = $attr['is_delete']; $model->save(); $cache[$id] = $model->id; (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE); } return $cache; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function doCopyCutPriceCat($from_store_id, $to_store_id) { try { $list = ActivityCutPriceCat::find() ->where([ 'store_id' => $from_store_id, ])->orderBy(['id' => SORT_ASC])->all(); $cache = []; foreach ($list as $value) { //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT, 'from_id' => $value['id'] ]); $id = $value['id']; $attr = $value->attributes; unset($attr['id']); $attr['store_id'] = $to_store_id; $model = ActivityCutPriceCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceCat(); $model->setAttributes($attr, false); $model->is_delete = $attr['is_delete']; $model->save(); $cache[$id] = $model->id; (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT); } return $cache; } catch(\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function doCopyCutPriceGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat) { try { $list = ActivityCutPriceGoods::find() ->where([ 'store_id' => $from_store_id ])->orderBy(['id' => SORT_ASC])->all(); $goods = []; $cache = []; foreach ($list as $value) { $id = $value['id']; $attr = $value->attributes; unset($attr['id']); $attr['store_id'] = $to_store_id; $attr['activity_id'] = $cacheActivity[$attr['activity_id']]; $cat = Cat::findOne(['id' => $attr['cat_id'], 'is_delete' => 0]); if (!$cat) { continue; } $attr['cat_id'] = $cacheCat[$attr['cat_id']]; $goods = Goods::findOne(['id' => $attr['goods_id'], 'is_delete' => 0]); if (!$goods) { continue; } $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_PRODUCT, 'from_id' => $attr['goods_id'] ]); if ($storeSyncExtLog && !$storeSyncExtLog->to_id) { continue; } $attr['goods_id'] = $storeSyncExtLog->to_id; $agattr = json_decode($value['attr'], true); $goodsNew = Goods::findOne($attr['goods_id']); $gattrNew = json_decode($goodsNew['attr'], true); $agattrNew = []; if ($agattr) { foreach($agattr as $k => $item){ $attrNames = array_column($item['attr_list'], 'attr_name'); // var_dump($attrNames); if ($gattrNew) { foreach($gattrNew as $v){ $attrNamesNew = array_column($v['attr_list'], 'attr_name'); // var_dump($attrNamesNew); if($attrNames === $attrNamesNew){ // var_dump('$attrNames == $attrNamesNew'); $item['attr_list'] = $v['attr_list']; } } } $agattrNew[] = $item; } } $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE); //判断是否为当前商城同步过此商品 $storeSyncExtLog = StoreSyncExtLog::findOne([ 'from_store_id' => $from_store_id, 'to_store_id' => $to_store_id, 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS, 'from_id' => $value['id'] ]); $goods[$attr['activity_id']] = $goods[$attr['activity_id']] ?? []; array_push($goods[$attr['activity_id']], $attr['goods_id']); $model = ActivityCutPriceGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceGoods(); $model->setAttributes($attr, false); $model->is_delete = $attr['is_delete']; $model->save(); $cache[$id] = $model->id; (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS); } foreach($goods as $aid => $gids){ $ac = ActivityCutPrice::findOne($aid); $ac->goods_ids = implode(',', $gids); $ac->save(); } return $cache; } catch(\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }