| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\AboutArticle;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //同步文章
- class ArticleSettingJob 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) {
- $result = $this->articleSetting($from_store_id, $store_id_item);
- }
- }
- /** 同步文章start **/
- private function articleSetting($fromStoreId, $toId) {
- try {
- $article_list = AboutArticle::find()->where(['store_id' => $fromStoreId])->andWhere(['<>', 'type', 1])->asArray()->all();
- foreach ($article_list as $article_item) {
- $id = $article_item['id'];
- unset($article_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_ARTICLE,
- 'from_id' => $id
- ]);
- $aboutArticle = AboutArticle::findOne($storeSyncExtLog->to_id ?? 0) ?: new AboutArticle();
- $aboutArticle->attributes = $article_item;
- $aboutArticle->store_id = $toId;
- $aboutArticle->type = $article_item['type'];
- $aboutArticle->is_delete = $article_item['is_delete'];
- if (!$aboutArticle->save()) {
- throw new \Exception(json_encode($aboutArticle->errors, JSON_UNESCAPED_UNICODE));
- };
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $aboutArticle->id, StoreSyncExtLog::TYPE_ARTICLE);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步文章end **/
- }
|