TopicSettingJob.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\StoreSyncExtLog;
  4. use app\models\Topic;
  5. use app\models\TopicType;
  6. use yii\base\BaseObject;
  7. use yii\queue\JobInterface;
  8. //专题
  9. class TopicSettingJob extends BaseObject implements JobInterface
  10. {
  11. public array $to_store_id;
  12. public int $from_store_id;
  13. public function execute($queue)
  14. {
  15. // TODO: Implement execute() method.
  16. $from_store_id = $this->from_store_id;
  17. $to_store_id = $this->to_store_id;
  18. foreach ($to_store_id as $store_id_item) {
  19. $type_result = $this->topicTypeSetting($from_store_id, $store_id_item);
  20. $typeCache = [];
  21. if ($type_result['code'] === 0) {
  22. $typeCache = $type_result['data']['typeCache'];
  23. }
  24. $result = $this->topicContentSetting($from_store_id, $store_id_item, $typeCache);
  25. }
  26. }
  27. private function topicTypeSetting($fromStoreId, $toId) {
  28. try {
  29. $topic_type_list = TopicType::find()->where(['store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
  30. $typeCache = [];
  31. foreach ($topic_type_list as $topic_type_item) {
  32. $id = $topic_type_item['id'];
  33. unset($topic_type_item['id']);
  34. //判断是否为当前商城同步过此商品
  35. $storeSyncExtLog = StoreSyncExtLog::findOne([
  36. 'from_store_id' => $fromStoreId,
  37. 'to_store_id' => $toId,
  38. 'type' => StoreSyncExtLog::TYPE_TOPIC_TYPE,
  39. 'from_id' => $id
  40. ]);
  41. $TopicType = TopicType::findOne($storeSyncExtLog->to_id ?? 0) ?: new TopicType();
  42. $TopicType->attributes = $topic_type_item;
  43. $TopicType->store_id = $toId;
  44. $TopicType->is_delete = $topic_type_item['is_delete'];
  45. if (!$TopicType->save()) {
  46. throw new \Exception(json_encode($TopicType->errors, JSON_UNESCAPED_UNICODE));
  47. };
  48. $typeCache[$id] = $TopicType->id;
  49. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $TopicType->id, StoreSyncExtLog::TYPE_TOPIC_TYPE);
  50. }
  51. return [
  52. 'code' => 0,
  53. 'msg' => '同步成功',
  54. 'data' => [
  55. 'typeCache' => $typeCache
  56. ]
  57. ];
  58. } catch (\Exception $e) {
  59. return [
  60. 'code' => 1,
  61. 'msg' => $e->getMessage()
  62. ];
  63. }
  64. }
  65. private function topicContentSetting($fromStoreId, $toId, $typeCache) {
  66. try {
  67. $topic_list = Topic::find()->where(['store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
  68. foreach ($topic_list as $topic_item) {
  69. $id = $topic_item['id'];
  70. unset($topic_item['id']);
  71. //判断是否为当前商城同步过此商品
  72. $storeSyncExtLog = StoreSyncExtLog::findOne([
  73. 'from_store_id' => $fromStoreId,
  74. 'to_store_id' => $toId,
  75. 'type' => StoreSyncExtLog::TYPE_TOPIC,
  76. 'from_id' => $id
  77. ]);
  78. $Topic = Topic::findOne($storeSyncExtLog->to_id ?? 0) ?: new Topic();
  79. $Topic->attributes = $topic_item;
  80. $Topic->store_id = $toId;
  81. $Topic->type = $typeCache[$topic_item['type']];
  82. $Topic->is_delete = $topic_item['is_delete'];
  83. if (!$Topic->save()) {
  84. throw new \Exception(json_encode($Topic->errors, JSON_UNESCAPED_UNICODE));
  85. };
  86. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $Topic->id, StoreSyncExtLog::TYPE_TOPIC);
  87. }
  88. return [
  89. 'code' => 0,
  90. 'msg' => '同步成功'
  91. ];
  92. } catch (\Exception $e) {
  93. return [
  94. 'code' => 1,
  95. 'msg' => $e->getMessage()
  96. ];
  97. }
  98. }
  99. }