| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\StoreSyncExtLog;
- use app\models\VideoGoods;
- use app\models\VideoGoodsCat;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //短视频
- class VideoGoodsJob 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) {
- //同步商品
- $goods_result = $this->videoGoodsCopy($from_store_id, $store_id_item);
- //同步分类
- $goods_cat_result = $this->videoGoodsCatCopy($from_store_id, $store_id_item);
- $video_cat_cache = [];
- if ($goods_cat_result['code'] === 0) {
- $video_cat_cache = $goods_cat_result['data']['video_cat_cache'];
- }
- }
- }
- //同步商品
- private function videoGoodsCopy($from_store_id, $to_store_id) {
- try {
- $video_goods_list = VideoGoods::find()->where(['store_id' => $from_store_id])->asArray()->all();
- foreach ($video_goods_list as $video_goods_item) {
- $video_goods_id = $video_goods_item['id'];
- $storeSyncGoodsLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT,
- 'from_id' => $video_goods_item['goods_id']
- ]);
- if ($storeSyncGoodsLog && !$storeSyncGoodsLog->to_id) {
- continue;
- }
- unset($video_goods_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS,
- 'from_id' => $video_goods_id
- ]);
- $video_goods = VideoGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoods();
- $video_goods->store_id = $to_store_id;
- $video_goods->status = $video_goods_item['status'];
- $video_goods->goods_id = $storeSyncGoodsLog->to_id;
- $video_goods->created_at = time();
- $video_goods->updated_at = time();
- $video_goods->is_delete = $video_goods_item['is_delete'];
- $video_goods->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $video_goods_id, $video_goods->id, StoreSyncExtLog::TYPE_VIDEO_GOODS);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步短视频分类
- private function videoGoodsCatCopy($from_store_id, $to_store_id) {
- try {
- $video_cat_cache = [];
- $video_goods_list = VideoGoodsCat::find()->where(['store_id' => $from_store_id])->asArray()->all();
- foreach ($video_goods_list as $video_goods_item) {
- $id = $video_goods_item['id'];
- unset($video_goods_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT,
- 'from_id' => $id
- ]);
- $video_goods_cat = VideoGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoodsCat();
- $video_goods_cat->attributes = $video_goods_item;
- $video_goods_cat->store_id = $to_store_id;
- $video_goods_cat->created_at = time();
- $video_goods_cat->updated_at = time();
- $video_goods_cat->is_delete = $video_goods_item['is_delete'];
- if (!$video_goods_cat->save()) {
- throw new \Exception(json_encode($video_goods_cat, JSON_UNESCAPED_UNICODE));
- }
- $video_cat_cache[$id] = $video_goods_cat->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $video_goods_cat->id, StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功',
- 'data' => [
- 'video_cat_cache' => $video_cat_cache
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|