| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\StoreSyncExtLog;
- use app\models\Topic;
- use app\models\TopicType;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //专题
- class TopicSettingJob 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) {
- $type_result = $this->topicTypeSetting($from_store_id, $store_id_item);
- $typeCache = [];
- if ($type_result['code'] === 0) {
- $typeCache = $type_result['data']['typeCache'];
- }
- $result = $this->topicContentSetting($from_store_id, $store_id_item, $typeCache);
- }
- }
- private function topicTypeSetting($fromStoreId, $toId) {
- try {
- $topic_type_list = TopicType::find()->where(['store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
- $typeCache = [];
- foreach ($topic_type_list as $topic_type_item) {
- $id = $topic_type_item['id'];
- unset($topic_type_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_TOPIC_TYPE,
- 'from_id' => $id
- ]);
- $TopicType = TopicType::findOne($storeSyncExtLog->to_id ?? 0) ?: new TopicType();
- $TopicType->attributes = $topic_type_item;
- $TopicType->store_id = $toId;
- $TopicType->is_delete = $topic_type_item['is_delete'];
- if (!$TopicType->save()) {
- throw new \Exception(json_encode($TopicType->errors, JSON_UNESCAPED_UNICODE));
- };
- $typeCache[$id] = $TopicType->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $TopicType->id, StoreSyncExtLog::TYPE_TOPIC_TYPE);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功',
- 'data' => [
- 'typeCache' => $typeCache
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function topicContentSetting($fromStoreId, $toId, $typeCache) {
- try {
- $topic_list = Topic::find()->where(['store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
- foreach ($topic_list as $topic_item) {
- $id = $topic_item['id'];
- unset($topic_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_TOPIC,
- 'from_id' => $id
- ]);
- $Topic = Topic::findOne($storeSyncExtLog->to_id ?? 0) ?: new Topic();
- $Topic->attributes = $topic_item;
- $Topic->store_id = $toId;
- $Topic->type = $typeCache[$topic_item['type']];
- $Topic->is_delete = $topic_item['is_delete'];
- if (!$Topic->save()) {
- throw new \Exception(json_encode($Topic->errors, JSON_UNESCAPED_UNICODE));
- };
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $Topic->id, StoreSyncExtLog::TYPE_TOPIC);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|