| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\NewIntegralCat;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //积分商城同步
- class IntegralStoreJob 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_cat_result = $this->integralGoodsCatCopy($from_store_id, $store_id_item);
- }
- }
- //商品分类
- private function integralGoodsCatCopy($fromStoreId, $toId) {
- try {
- $integral_cat_list = NewIntegralCat::find()->where(['store_id' => $fromStoreId])->asArray()->all();
- $integral_cat_arr = [];
- $goods_id = Goods::find()->where(['store_id' => $toId, 'is_delete' => 0, 'product_type' => Goods::GOODS_TYPE_INTEGRAL])
- ->select('id')->asArray()->column();
- foreach ($integral_cat_list as $integral_cat_item) {
- $id = $integral_cat_item['id'];
- unset($integral_cat_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT,
- 'from_id' => $id
- ]);
- $integral_cat = NewIntegralCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewIntegralCat();
- $integral_cat->attributes = $integral_cat_item;
- $integral_cat->store_id = $toId;
- $integral_cat->created_at = time();
- $integral_cat->updated_at = time();
- $integral_cat->is_delete = $integral_cat_item['is_delete'];
- if (!$integral_cat->save()) {
- throw new \Exception(json_encode($integral_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- $integral_cat_arr[$id] = $integral_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $integral_cat->id, StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT);
- $goods_cat_list = GoodsCat::find()->where(['goods_id' => $goods_id, 'cat_id' => $integral_cat_item['id'], 'is_delete' => 0, 'store_id' => $toId])
- ->asArray()->all();
- foreach ($goods_cat_list as $goods_cat_item) {
- $goods_cat = GoodsCat::findOne($goods_cat_item['id']);
- $goods_cat->cat_id = $integral_cat->id;
- if (!$goods_cat->save()) {
- throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '同步完成',
- 'data' => [
- 'integral_cat_arr' => $integral_cat_arr
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|