VideoGoodsJob.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\StoreSyncExtLog;
  4. use app\models\VideoGoods;
  5. use app\models\VideoGoodsCat;
  6. use yii\base\BaseObject;
  7. use yii\queue\JobInterface;
  8. //短视频
  9. class VideoGoodsJob 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. //同步商品
  20. $goods_result = $this->videoGoodsCopy($from_store_id, $store_id_item);
  21. //同步分类
  22. $goods_cat_result = $this->videoGoodsCatCopy($from_store_id, $store_id_item);
  23. $video_cat_cache = [];
  24. if ($goods_cat_result['code'] === 0) {
  25. $video_cat_cache = $goods_cat_result['data']['video_cat_cache'];
  26. }
  27. }
  28. }
  29. //同步商品
  30. private function videoGoodsCopy($from_store_id, $to_store_id) {
  31. try {
  32. $video_goods_list = VideoGoods::find()->where(['store_id' => $from_store_id])->asArray()->all();
  33. foreach ($video_goods_list as $video_goods_item) {
  34. $video_goods_id = $video_goods_item['id'];
  35. $storeSyncGoodsLog = StoreSyncExtLog::findOne([
  36. 'from_store_id' => $from_store_id,
  37. 'to_store_id' => $to_store_id,
  38. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  39. 'from_id' => $video_goods_item['goods_id']
  40. ]);
  41. if ($storeSyncGoodsLog && !$storeSyncGoodsLog->to_id) {
  42. continue;
  43. }
  44. unset($video_goods_item['id']);
  45. //判断是否为当前商城同步过此商品
  46. $storeSyncExtLog = StoreSyncExtLog::findOne([
  47. 'from_store_id' => $from_store_id,
  48. 'to_store_id' => $to_store_id,
  49. 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS,
  50. 'from_id' => $video_goods_id
  51. ]);
  52. $video_goods = VideoGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoods();
  53. $video_goods->store_id = $to_store_id;
  54. $video_goods->status = $video_goods_item['status'];
  55. $video_goods->goods_id = $storeSyncGoodsLog->to_id;
  56. $video_goods->created_at = time();
  57. $video_goods->updated_at = time();
  58. $video_goods->is_delete = $video_goods_item['is_delete'];
  59. $video_goods->save();
  60. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $video_goods_id, $video_goods->id, StoreSyncExtLog::TYPE_VIDEO_GOODS);
  61. }
  62. return [
  63. 'code' => 0,
  64. 'msg' => '同步成功'
  65. ];
  66. } catch (\Exception $e) {
  67. return [
  68. 'code' => 1,
  69. 'msg' => $e->getMessage()
  70. ];
  71. }
  72. }
  73. //同步短视频分类
  74. private function videoGoodsCatCopy($from_store_id, $to_store_id) {
  75. try {
  76. $video_cat_cache = [];
  77. $video_goods_list = VideoGoodsCat::find()->where(['store_id' => $from_store_id])->asArray()->all();
  78. foreach ($video_goods_list as $video_goods_item) {
  79. $id = $video_goods_item['id'];
  80. unset($video_goods_item['id']);
  81. //判断是否为当前商城同步过此商品
  82. $storeSyncExtLog = StoreSyncExtLog::findOne([
  83. 'from_store_id' => $from_store_id,
  84. 'to_store_id' => $to_store_id,
  85. 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT,
  86. 'from_id' => $id
  87. ]);
  88. $video_goods_cat = VideoGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoodsCat();
  89. $video_goods_cat->attributes = $video_goods_item;
  90. $video_goods_cat->store_id = $to_store_id;
  91. $video_goods_cat->created_at = time();
  92. $video_goods_cat->updated_at = time();
  93. $video_goods_cat->is_delete = $video_goods_item['is_delete'];
  94. if (!$video_goods_cat->save()) {
  95. throw new \Exception(json_encode($video_goods_cat, JSON_UNESCAPED_UNICODE));
  96. }
  97. $video_cat_cache[$id] = $video_goods_cat->id;
  98. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $video_goods_cat->id, StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT);
  99. }
  100. return [
  101. 'code' => 0,
  102. 'msg' => '同步成功',
  103. 'data' => [
  104. 'video_cat_cache' => $video_cat_cache
  105. ]
  106. ];
  107. } catch (\Exception $e) {
  108. return [
  109. 'code' => 1,
  110. 'msg' => $e->getMessage()
  111. ];
  112. }
  113. }
  114. }