BookingHomeJob.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\BookingGoodsCat;
  4. use app\models\BookingGoodsExt;
  5. use app\models\StoreSyncExtLog;
  6. use yii\base\BaseObject;
  7. use yii\queue\JobInterface;
  8. //同步预约商品
  9. class BookingHomeJob 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. $cat_cache = $this->bookingHomeGoodsCat($from_store_id, $store_id_item);
  21. //预约商品与预约分类修改绑定关系
  22. $ext_result = $this->bookingHomeGoodsCatBind($from_store_id, $store_id_item, $cat_cache );
  23. }
  24. }
  25. //预约商品分类
  26. private function bookingHomeGoodsCat($fromStoreId, $toId) {
  27. try {
  28. $goodsCatCache = [];
  29. $goods_cat_ = BookingGoodsCat::find()->where(['store_id' => $fromStoreId])->asArray()->all();
  30. foreach ($goods_cat_ as $goods_cat_item) {
  31. $id = $goods_cat_item['id'];
  32. unset($goods_cat_item['id']);
  33. //判断是否为当前商城同步过此商品
  34. $storeSyncExtLog = StoreSyncExtLog::findOne([
  35. 'from_store_id' => $fromStoreId,
  36. 'to_store_id' => $toId,
  37. 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT,
  38. 'from_id' => $id
  39. ]);
  40. $goods_cat = BookingGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsCat();
  41. $goods_cat->attributes = $goods_cat_item;
  42. $goods_cat->store_id = $toId;
  43. $goods_cat->name = $goods_cat_item['name'];
  44. $goods_cat->pic_url = $goods_cat_item['pic_url'];
  45. $goods_cat->sort = $goods_cat_item['sort'];
  46. $goods_cat->is_show = $goods_cat_item['is_show'];
  47. $goods_cat->is_delete = $goods_cat_item['is_delete'];
  48. if (!$goods_cat->save()) {
  49. throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
  50. }
  51. $goodsCatCache[$id] = $goods_cat->id;
  52. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_cat->id, StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT);
  53. }
  54. return $goodsCatCache;
  55. } catch (\Exception $e) {
  56. return [];
  57. }
  58. }
  59. //预约商品与预约分类修改绑定关系
  60. private function bookingHomeGoodsCatBind($fromStoreId, $toId, $cat_cache) {
  61. try {
  62. $goodsExt = BookingGoodsExt::find()->where(['store_id' => $fromStoreId])->asArray()->all();
  63. foreach ($goodsExt as $ext_item) {
  64. $id = $ext_item['id'];
  65. //判断是否为当前商城同步过此商品
  66. $storeSyncExtLog = StoreSyncExtLog::findOne([
  67. 'from_store_id' => $fromStoreId,
  68. 'to_store_id' => $toId,
  69. 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS,
  70. 'from_id' => $id
  71. ]);
  72. $storeSyncGoodsLog = StoreSyncExtLog::findOne([
  73. 'from_store_id' => $fromStoreId,
  74. 'to_store_id' => $toId,
  75. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  76. 'from_id' => $ext_item['goods_id']
  77. ]);
  78. if ($storeSyncGoodsLog && !$storeSyncGoodsLog->to_id) {
  79. continue;
  80. }
  81. unset($ext_item['id']);
  82. $goods_ext = BookingGoodsExt::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsExt();
  83. $goods_ext->store_id = $toId;
  84. $goods_ext->goods_id = $storeSyncGoodsLog->to_id;
  85. $goods_ext->cat_id = $cat_cache[$ext_item['cat_id']];
  86. if (!$goods_ext->save()) {
  87. throw new \Exception(json_encode($goods_ext->errors, JSON_UNESCAPED_UNICODE));
  88. }
  89. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_ext->id, StoreSyncExtLog::TYPE_BOOKING_GOODS);
  90. }
  91. return [
  92. 'code' => 0,
  93. 'msg' => 'success'
  94. ];
  95. } catch (\Exception $e) {
  96. return [
  97. 'code' => 1,
  98. 'msg' => $e->getMessage()
  99. ];
  100. }
  101. }
  102. }