| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\ActivityCutPrice;
- use app\models\ActivityCutPriceCat;
- use app\models\ActivityCutPriceGoods;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //同步砍价
- class CutPriceActivityJob extends BaseObject implements JobInterface
- {
- public array $activity_id;
- public array $to_store_id;
- public int $from_store_id;
- public function execute($queue)
- {
- // TODO: Implement execute() method.
- $from_store_id = $this->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()
- ];
- }
- }
- }
|