CreatedMdGoods.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\jobs;
  8. use app\models\Goods;
  9. use app\models\GoodsCat;
  10. use app\models\GoodsFullMinus;
  11. use app\models\GoodsPic;
  12. use app\models\Md;
  13. use yii\base\BaseObject;
  14. use yii\queue\JobInterface;
  15. /**
  16. * 同步独立运营门店商品
  17. */
  18. class CreatedMdGoods extends BaseObject implements JobInterface
  19. {
  20. public int $goods_id;
  21. public int $status;
  22. public function execute($queue)
  23. {
  24. try {
  25. $goods_id = $this->goods_id;
  26. $status = $this->status; // 是否删除
  27. $goods = Goods::findOne(['id' => $goods_id, 'md_food_id' => 0]);
  28. if ($goods) {
  29. if ($status) {
  30. GoodsCat::updateAll(['is_delete' => 1], ['old_goods_id' => $goods_id]);
  31. } else {
  32. $md_list = Md::find()->where(['store_id' => $goods->store_id, 'is_delete' => 0, 'is_single' => 1])
  33. ->select('id')->asArray()->all();
  34. foreach ($md_list as $item) {
  35. $goods_ = Goods::findOne(['md_food_id' => $item['id'], 'is_delete' => 0, 'old_goods_id' => $goods_id]);
  36. $status_ = $goods->status;
  37. if ($goods_) {
  38. $status_ = $goods_->status;
  39. }
  40. $goods_ = $goods_ ?? new Goods();
  41. $goods_->attributes = $goods;
  42. $goods_->old_goods_id = $goods_id;
  43. $goods_->status = $status_;
  44. if ($goods_->save()) {
  45. //同步分类
  46. $goods_cat = GoodsCat::find()->where(['goods_id' => $goods_id, 'is_delete' => 0])->asArray()->all();
  47. GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $goods_->id, 'is_delete' => 0]);
  48. foreach ($goods_cat as $cat) {
  49. $goods_cat_ = new GoodsCat();
  50. $goods_cat_->attributes = $cat;
  51. $goods_cat_->goods_id = $goods_->id;
  52. $goods_cat_->save();
  53. }
  54. //同步满减
  55. $goods_full_minus = GoodsFullMinus::find()->where(['goods_id' => $goods_id, 'is_delete' => 0])->asArray()->all();
  56. GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $goods_->id, 'is_delete' => 0]);
  57. foreach ($goods_full_minus as $cat) {
  58. $goods_full_minus_ = new GoodsFullMinus();
  59. $goods_full_minus_->attributes = $cat;
  60. $goods_full_minus_->goods_id = $goods_->id;
  61. $goods_full_minus_->save();
  62. }
  63. //同步商品图片
  64. $goods_pic = GoodsPic::find()->where(['goods_id' => $goods_id, 'is_delete' => 0])->asArray()->all();
  65. GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $goods_->id, 'is_delete' => 0]);
  66. foreach ($goods_pic as $cat) {
  67. $goods_pic_ = new GoodsPic();
  68. $goods_pic_->attributes = $cat;
  69. $goods_pic_->goods_id = $goods_->id;
  70. $goods_pic_->save();
  71. }
  72. }
  73. }
  74. }
  75. }
  76. } catch (\Exception $e) {
  77. debug_log('门店同步商品处理:' . $e->getMessage() . $e->getLine() . $e->getFile(),'md_goods.log');
  78. }
  79. }
  80. }