CutPriceActivityJob.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\ActivityCutPrice;
  4. use app\models\ActivityCutPriceCat;
  5. use app\models\ActivityCutPriceGoods;
  6. use app\models\Cat;
  7. use app\models\Goods;
  8. use app\models\StoreSyncExtLog;
  9. use yii\base\BaseObject;
  10. use yii\queue\JobInterface;
  11. //同步砍价
  12. class CutPriceActivityJob extends BaseObject implements JobInterface
  13. {
  14. public array $activity_id;
  15. public array $to_store_id;
  16. public int $from_store_id;
  17. public function execute($queue)
  18. {
  19. // TODO: Implement execute() method.
  20. $from_store_id = $this->from_store_id;
  21. $to_store_id = $this->to_store_id;
  22. $activity_id = $this->activity_id;
  23. foreach ($to_store_id as $store_id_item) {
  24. $cacheActivity = $this->doCopyCutPrice($from_store_id, $store_id_item, $activity_id);
  25. $cacheCat = $this->doCopyCutPriceCat($from_store_id, $store_id_item);
  26. $cache = $this->doCopyCutPriceGoods($from_store_id, $store_id_item, $cacheActivity, $cacheCat);
  27. }
  28. }
  29. public function doCopyCutPrice($from_store_id, $to_store_id, $activity_id) {
  30. try {
  31. $list = ActivityCutPrice::find()
  32. ->where([
  33. 'store_id' => $from_store_id,
  34. 'id' => $activity_id
  35. ])->orderBy(['id' => SORT_ASC])->all();
  36. $cache = [];
  37. foreach ($list as $value) {
  38. $id = $value['id'];
  39. $attr = $value->attributes;
  40. unset($attr['id']);
  41. //判断是否为当前商城同步过此商品
  42. $storeSyncExtLog = StoreSyncExtLog::findOne([
  43. 'from_store_id' => $from_store_id,
  44. 'to_store_id' => $to_store_id,
  45. 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE,
  46. 'from_id' => $value['id']
  47. ]);
  48. $attr['store_id'] = $to_store_id;
  49. $attr['goods_ids'] = '';
  50. $model = ActivityCutPrice::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPrice();
  51. $model->setAttributes($attr, false);
  52. $model->is_delete = $attr['is_delete'];
  53. $model->save();
  54. $cache[$id] = $model->id;
  55. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE);
  56. }
  57. return $cache;
  58. } catch (\Exception $e) {
  59. return [
  60. 'code' => 1,
  61. 'msg' => $e->getMessage()
  62. ];
  63. }
  64. }
  65. public function doCopyCutPriceCat($from_store_id, $to_store_id) {
  66. try {
  67. $list = ActivityCutPriceCat::find()
  68. ->where([
  69. 'store_id' => $from_store_id,
  70. ])->orderBy(['id' => SORT_ASC])->all();
  71. $cache = [];
  72. foreach ($list as $value) {
  73. //判断是否为当前商城同步过此商品
  74. $storeSyncExtLog = StoreSyncExtLog::findOne([
  75. 'from_store_id' => $from_store_id,
  76. 'to_store_id' => $to_store_id,
  77. 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT,
  78. 'from_id' => $value['id']
  79. ]);
  80. $id = $value['id'];
  81. $attr = $value->attributes;
  82. unset($attr['id']);
  83. $attr['store_id'] = $to_store_id;
  84. $model = ActivityCutPriceCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceCat();
  85. $model->setAttributes($attr, false);
  86. $model->is_delete = $attr['is_delete'];
  87. $model->save();
  88. $cache[$id] = $model->id;
  89. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT);
  90. }
  91. return $cache;
  92. } catch(\Exception $e) {
  93. return [
  94. 'code' => 1,
  95. 'msg' => $e->getMessage()
  96. ];
  97. }
  98. }
  99. public function doCopyCutPriceGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat) {
  100. try {
  101. $list = ActivityCutPriceGoods::find()
  102. ->where([
  103. 'store_id' => $from_store_id
  104. ])->orderBy(['id' => SORT_ASC])->all();
  105. $goods = [];
  106. $cache = [];
  107. foreach ($list as $value) {
  108. $id = $value['id'];
  109. $attr = $value->attributes;
  110. unset($attr['id']);
  111. $attr['store_id'] = $to_store_id;
  112. $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
  113. $cat = Cat::findOne(['id' => $attr['cat_id'], 'is_delete' => 0]);
  114. if (!$cat) {
  115. continue;
  116. }
  117. $attr['cat_id'] = $cacheCat[$attr['cat_id']];
  118. $goods = Goods::findOne(['id' => $attr['goods_id'], 'is_delete' => 0]);
  119. if (!$goods) {
  120. continue;
  121. }
  122. $storeSyncExtLog = StoreSyncExtLog::findOne([
  123. 'from_store_id' => $from_store_id,
  124. 'to_store_id' => $to_store_id,
  125. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  126. 'from_id' => $attr['goods_id']
  127. ]);
  128. if ($storeSyncExtLog && !$storeSyncExtLog->to_id) {
  129. continue;
  130. }
  131. $attr['goods_id'] = $storeSyncExtLog->to_id;
  132. $agattr = json_decode($value['attr'], true);
  133. $goodsNew = Goods::findOne($attr['goods_id']);
  134. $gattrNew = json_decode($goodsNew['attr'], true);
  135. $agattrNew = [];
  136. if ($agattr) {
  137. foreach($agattr as $k => $item){
  138. $attrNames = array_column($item['attr_list'], 'attr_name');
  139. // var_dump($attrNames);
  140. if ($gattrNew) {
  141. foreach($gattrNew as $v){
  142. $attrNamesNew = array_column($v['attr_list'], 'attr_name');
  143. // var_dump($attrNamesNew);
  144. if($attrNames === $attrNamesNew){
  145. // var_dump('$attrNames == $attrNamesNew');
  146. $item['attr_list'] = $v['attr_list'];
  147. }
  148. }
  149. }
  150. $agattrNew[] = $item;
  151. }
  152. }
  153. $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
  154. //判断是否为当前商城同步过此商品
  155. $storeSyncExtLog = StoreSyncExtLog::findOne([
  156. 'from_store_id' => $from_store_id,
  157. 'to_store_id' => $to_store_id,
  158. 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS,
  159. 'from_id' => $value['id']
  160. ]);
  161. $goods[$attr['activity_id']] = $goods[$attr['activity_id']] ?? [];
  162. array_push($goods[$attr['activity_id']], $attr['goods_id']);
  163. $model = ActivityCutPriceGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceGoods();
  164. $model->setAttributes($attr, false);
  165. $model->is_delete = $attr['is_delete'];
  166. $model->save();
  167. $cache[$id] = $model->id;
  168. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS);
  169. }
  170. foreach($goods as $aid => $gids){
  171. $ac = ActivityCutPrice::findOne($aid);
  172. $ac->goods_ids = implode(',', $gids);
  173. $ac->save();
  174. }
  175. return $cache;
  176. } catch(\Exception $e) {
  177. return [
  178. 'code' => 1,
  179. 'msg' => $e->getMessage()
  180. ];
  181. }
  182. }
  183. }