| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use app\models\StoreCloud;
- use app\models\StoreSyncExtLog;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\MerchantForm;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //商品同步
- class GoodsJob extends BaseObject implements JobInterface
- {
- public array $goods_id;
- public array $to_store_id;
- public int $from_store_id;
- public function execute($queue)
- {
- try {
- $goods_id = $this->goods_id;
- $to_store_id = $this->to_store_id;
- $from_store_id = $this->from_store_id;
- if (is_array($to_store_id)) {
- if (!is_array($goods_id)) {
- $goods_id = [$goods_id];
- }
- $goodsCache = [];
- foreach ($to_store_id as $store_id_item) {
- foreach ($goods_id as $goods_id_item) {
- // 商品分类处理
- // $cacheCat = $this->doGoodsCat($from_store_id, $store_id_item);
- // 商品规格处理
- $goods = Goods::find()->where([
- 'store_id' => $from_store_id,
- 'id' => $goods_id_item
- ])->andWhere(['<=', 'md_food_id', 0])->asArray()->one();
- $goodsCache = [];
- $store_cloud_id = StoreCloud::findOne(['store_id' => $store_id_item, 'is_delete' => 0]);
- if ($goods) {
- list($attrGroupCache, $attrCache) = $this->doGoodsAttr($from_store_id, $store_id_item, $goods['attr']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $store_id_item,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT,
- 'from_id' => $goods['id']
- ]);
- $goods_id_cache = $goods['id'];
- unset($goods['id']);
- $g = Goods::findOne($storeSyncExtLog->to_id ?? 0);
- $is_new = 0;
- if (!$g) {
- $g = new Goods();
- $is_new = 1;
- }
- if (!empty($goods['attr'])) {
- $attrs = \json_decode($goods['attr'], true);
- foreach ($attrs as &$attr) {
- if ($attr['attr_list']) {
- foreach ($attr['attr_list'] as &$att) {
- if (isset($att['attr_id']) && isset($attrCache[$att['attr_id']])) {
- $att['attr_id'] = $attrCache[$att['attr_id']];
- }
- }
- }
- }
- $goods['attr'] = \json_encode($attrs);
- }
- //选品更新自动更新商城销售价格
- $repeat_update_goods_price = \app\models\Option::get('repeat_update_goods_price', $store_id_item, 'store')['value'];
- if (!intval($repeat_update_goods_price) && !$is_new && !empty($g->attr)) {
- $goods['attr'] = $g->attr;
- }
- $deliveryRulesExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $store_id_item,
- 'type' => StoreSyncExtLog::TYPE_POSTAGE_RULES,
- 'from_id' => $goods['delivery_rules_id']
- ]);
- $goods['store_id'] = $store_id_item;
- $g->setAttributes($goods, false);
- $g->cloud_goods_id = $goods['cloud_goods_id'];
- $g->is_wholesale = $goods['is_wholesale'];
- $g->cloud_supplier_id = $goods['cloud_supplier_id'];
- $g->delivery_rules_id = $deliveryRulesExtLog->to_id ?? 0;
- $g->is_delete = $goods['is_delete'];
- $g->unit = $goods['unit'];
- $g->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $store_id_item, $goods_id_cache, $g->id, StoreSyncExtLog::TYPE_PRODUCT);
- $goodsCache[$goods_id_cache] = $g->id;
- if ($g->cloud_goods_id > 0 && !intval($g->is_wholesale)) {
- try {
- $form = new GoodsForm();
- $form->id = $g->cloud_goods_id;
- $form->store_id = $store_id_item;
- $goodsInfo = $form->saveCloudGoods();
- $merchantForm = new MerchantForm();
- $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0;
- $r = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'], $goodsInfo['goods_id'], $store_id_item);
- } catch (\Exception $e) {
- }
- }
- // 商品分类处理
- $goodsCats = GoodsCat::find()->where(['store_id' => $from_store_id, 'goods_id' => $goods_id_item])->asArray()->all();
- GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $goodsCache[$goods_id_cache]]);//增加删除标识
- $goodsCatOpen = false;
- foreach ($goodsCats as $goodsCat) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $store_id_item,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT,
- 'from_id' => $goodsCat['id']
- ]);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLogCat = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $store_id_item,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $goodsCat['cat_id']
- ]);
- if (!empty($storeSyncExtLogCat->to_id)) {
- $id = $goodsCat['id'];
- $cat = GoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsCat();
- $goodsCat['goods_id'] = $goodsCache[$goods_id_cache];
- $goodsCat['cat_id'] = $storeSyncExtLogCat->to_id;
- $goodsCat['store_id'] = $store_id_item;
- unset($goodsCat['id']);
- $cat->setAttributes($goodsCat, false);
- $cat->is_delete = $goodsCat['is_delete'];
- $cat->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $store_id_item, $id, $cat->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT);
- $goodsCatOpen = true;
- }
- }
- if ($goodsCatOpen) {
- //删除
- GoodsCat::deleteAll(['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);
- } else {
- GoodsCat::updateAll(['is_delete' => 0], ['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);//增加删除标识
- }
- // 商品组图处理
- $goodsPic = GoodsPic::find()->where(['goods_id' => $goods_id_item])->asArray()->all();
- GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $goodsCache[$goods_id_cache]]);//增加删除标识
- $goodsPicOpen = false;
- foreach ($goodsPic as $pic) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $store_id_item,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC,
- 'from_id' => $pic['id']
- ]);
- $newPic = GoodsPic::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsPic();
- $newPic->goods_id = $goodsCache[$goods_id_cache];
- $newPic->pic_url = $pic['pic_url'];
- $newPic->is_delete = $pic['is_delete'];
- $newPic->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $store_id_item, $pic['id'], $newPic->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC);
- $goodsPicOpen = true;
- }
- if ($goodsPicOpen) {
- //删除
- GoodsPic::deleteAll(['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);
- } else {
- GoodsPic::updateAll(['is_delete' => 0], ['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);//增加删除标识
- }
- }
- }
- }
- }
- } catch (\Exception $e) {
- debug_log($e->getMessage() . ' ' . $e->getLine(). ' ' . $e->getFile(), 'queuePush.log');
- }
- }
- // 商品分类创建
- private function doGoodsCat($from_store_id, $to_store_id)
- {
- $cats = Cat::find()->where([
- 'store_id' => $from_store_id,
- 'parent_id' => 0,
- ])->asArray()->all();
- $catCache = [];
- foreach ($cats as $cat) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $cat['id']
- ]);
- $catC = $cat;
- unset($catC['id']);
- $c = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
- $catC['store_id'] = $to_store_id;
- $c->setAttributes($catC, false);
- $c->is_delete = $catC['is_delete'];
- $c->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat['id'], $c->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
- $parent_1_id = $catCache[$cat['id']] = $c->id;
- $cats1 = Cat::find()->where([
- 'store_id' => $from_store_id,
- 'parent_id' => $cat['id'],
- ])->asArray()->all();
- foreach ($cats1 as $cat1) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $cat1['id']
- ]);
- $catC1 = $cat1;
- unset($catC1['id']);
- $c1 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
- $catC1['store_id'] = $to_store_id;
- $catC1['parent_id'] = $parent_1_id;
- $c1->setAttributes($catC1, false);
- $c1->is_delete = $catC1['is_delete'];
- $c1->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat1['id'], $c1->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
- $parent_2_id = $catCache[$cat1['id']] = $c1->id;
- $cats2 = Cat::find()->where([
- 'store_id' => $from_store_id,
- 'parent_id' => $cat1['id'],
- ])->asArray()->all();
- foreach ($cats2 as $cat2) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $cat2['id']
- ]);
- $catC2 = $cat2;
- unset($catC2['id']);
- $c2 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
- $catC2['store_id'] = $to_store_id;
- $catC2['parent_id'] = $parent_2_id;
- $c2->setAttributes($catC2, false);
- $c2->is_delete = $catC2['is_delete'];
- $c2->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat2['id'], $c2->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
- $catCache[$cat2['id']] = $c2->id;
- }
- }
- }
- return $catCache;
- }
- // 商品规格创建
- private function doGoodsAttr($from_store_id, $to_store_id, $attrs = null)
- {
- $attrGroupCache = $attrCache = [];
- $attr_id = [];
- if ($attrs) {
- $attrs = json_decode($attrs, true);
- foreach ($attrs as $attr_item) {
- $attr_id_item = array_column($attr_item['attr_list'], 'attr_id');
- $attr_id = array_merge($attr_id, $attr_id_item);
- }
- }
- if ($attr_id) {
- $attr_id = array_unique($attr_id);
- foreach ($attr_id as $attr_id_item) {
- $attrGroup = null;
- $old_attr = Attr::findOne($attr_id_item);
- if ($old_attr) {
- $old_attr_group = AttrGroup::findOne($old_attr->attr_group_id);
- //判断是否为当前商城同步过此商品
- $storeSyncAttrGroupExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP,
- 'from_id' => $old_attr_group->id
- ]);
- $attrGroup = AttrGroup::findOne($storeSyncAttrGroupExtLog->to_id ?? 0) ?: new AttrGroup();
- $attrGroup->store_id = $to_store_id;
- $attrGroup->attr_group_name = $old_attr_group->attr_group_name;
- $attrGroup->is_delete = $old_attr_group->is_delete;
- $attrGroup->save();
- $attrGroupCache[$old_attr_group->id] = $attrGroup->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr_group->id, $attrGroup->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP);
- }
- //判断是否为当前商城同步过此商品
- $storeSyncAttrExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR,
- 'from_id' => $attr_id_item
- ]);
- $attr = Attr::findOne($storeSyncAttrExtLog->to_id ?? 0) ?: new Attr();
- $attr->attr_group_id = $attrGroup->id ?? 0;
- $attr->attr_name = $old_attr->attr_name;
- $attr->is_delete = $old_attr->is_delete;
- $attr->is_default = 0;
- $attr->save();
- $attrCache[$old_attr->id] = $attr->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr->id, $attr->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR);
- }
- }
- return [$attrGroupCache, $attrCache];
- }
- }
|