ArticleSettingJob.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\AboutArticle;
  4. use app\models\StoreSyncExtLog;
  5. use yii\base\BaseObject;
  6. use yii\queue\JobInterface;
  7. //同步文章
  8. class ArticleSettingJob extends BaseObject implements JobInterface
  9. {
  10. public array $to_store_id;
  11. public int $from_store_id;
  12. public function execute($queue)
  13. {
  14. // TODO: Implement execute() method.
  15. $from_store_id = $this->from_store_id;
  16. $to_store_id = $this->to_store_id;
  17. foreach ($to_store_id as $store_id_item) {
  18. $result = $this->articleSetting($from_store_id, $store_id_item);
  19. }
  20. }
  21. /** 同步文章start **/
  22. private function articleSetting($fromStoreId, $toId) {
  23. try {
  24. $article_list = AboutArticle::find()->where(['store_id' => $fromStoreId])->andWhere(['<>', 'type', 1])->asArray()->all();
  25. foreach ($article_list as $article_item) {
  26. $id = $article_item['id'];
  27. unset($article_item['id']);
  28. //判断是否为当前商城同步过此商品
  29. $storeSyncExtLog = StoreSyncExtLog::findOne([
  30. 'from_store_id' => $fromStoreId,
  31. 'to_store_id' => $toId,
  32. 'type' => StoreSyncExtLog::TYPE_ARTICLE,
  33. 'from_id' => $id
  34. ]);
  35. $aboutArticle = AboutArticle::findOne($storeSyncExtLog->to_id ?? 0) ?: new AboutArticle();
  36. $aboutArticle->attributes = $article_item;
  37. $aboutArticle->store_id = $toId;
  38. $aboutArticle->type = $article_item['type'];
  39. $aboutArticle->is_delete = $article_item['is_delete'];
  40. if (!$aboutArticle->save()) {
  41. throw new \Exception(json_encode($aboutArticle->errors, JSON_UNESCAPED_UNICODE));
  42. };
  43. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $aboutArticle->id, StoreSyncExtLog::TYPE_ARTICLE);
  44. }
  45. return [
  46. 'code' => 0,
  47. 'msg' => '同步成功'
  48. ];
  49. } catch (\Exception $e) {
  50. return [
  51. 'code' => 1,
  52. 'msg' => $e->getMessage()
  53. ];
  54. }
  55. }
  56. /** 同步文章end **/
  57. }