| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\BookingGoodsCat;
- use app\models\BookingGoodsExt;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //同步预约商品
- class BookingHomeJob extends BaseObject implements JobInterface
- {
- 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;
- foreach ($to_store_id as $store_id_item) {
- //预约分类
- $cat_cache = $this->bookingHomeGoodsCat($from_store_id, $store_id_item);
- //预约商品与预约分类修改绑定关系
- $ext_result = $this->bookingHomeGoodsCatBind($from_store_id, $store_id_item, $cat_cache );
- }
- }
- //预约商品分类
- private function bookingHomeGoodsCat($fromStoreId, $toId) {
- try {
- $goodsCatCache = [];
- $goods_cat_ = BookingGoodsCat::find()->where(['store_id' => $fromStoreId])->asArray()->all();
- foreach ($goods_cat_ as $goods_cat_item) {
- $id = $goods_cat_item['id'];
- unset($goods_cat_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT,
- 'from_id' => $id
- ]);
- $goods_cat = BookingGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsCat();
- $goods_cat->attributes = $goods_cat_item;
- $goods_cat->store_id = $toId;
- $goods_cat->name = $goods_cat_item['name'];
- $goods_cat->pic_url = $goods_cat_item['pic_url'];
- $goods_cat->sort = $goods_cat_item['sort'];
- $goods_cat->is_show = $goods_cat_item['is_show'];
- $goods_cat->is_delete = $goods_cat_item['is_delete'];
- if (!$goods_cat->save()) {
- throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- $goodsCatCache[$id] = $goods_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_cat->id, StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT);
- }
- return $goodsCatCache;
- } catch (\Exception $e) {
- return [];
- }
- }
- //预约商品与预约分类修改绑定关系
- private function bookingHomeGoodsCatBind($fromStoreId, $toId, $cat_cache) {
- try {
- $goodsExt = BookingGoodsExt::find()->where(['store_id' => $fromStoreId])->asArray()->all();
- foreach ($goodsExt as $ext_item) {
- $id = $ext_item['id'];
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS,
- 'from_id' => $id
- ]);
- $storeSyncGoodsLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT,
- 'from_id' => $ext_item['goods_id']
- ]);
- if ($storeSyncGoodsLog && !$storeSyncGoodsLog->to_id) {
- continue;
- }
- unset($ext_item['id']);
- $goods_ext = BookingGoodsExt::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsExt();
- $goods_ext->store_id = $toId;
- $goods_ext->goods_id = $storeSyncGoodsLog->to_id;
- $goods_ext->cat_id = $cat_cache[$ext_item['cat_id']];
- if (!$goods_ext->save()) {
- throw new \Exception(json_encode($goods_ext->errors, JSON_UNESCAPED_UNICODE));
- }
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_ext->id, StoreSyncExtLog::TYPE_BOOKING_GOODS);
- }
- return [
- 'code' => 0,
- 'msg' => 'success'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|