GoodsJob.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\Attr;
  4. use app\models\AttrGroup;
  5. use app\models\Cat;
  6. use app\models\Goods;
  7. use app\models\GoodsCat;
  8. use app\models\GoodsPic;
  9. use app\models\StoreCloud;
  10. use app\models\StoreSyncExtLog;
  11. use app\modules\admin\models\GoodsForm;
  12. use app\modules\admin\models\MerchantForm;
  13. use yii\base\BaseObject;
  14. use yii\queue\JobInterface;
  15. //商品同步
  16. class GoodsJob extends BaseObject implements JobInterface
  17. {
  18. public array $goods_id;
  19. public array $to_store_id;
  20. public int $from_store_id;
  21. public function execute($queue)
  22. {
  23. try {
  24. $goods_id = $this->goods_id;
  25. $to_store_id = $this->to_store_id;
  26. $from_store_id = $this->from_store_id;
  27. if (is_array($to_store_id)) {
  28. if (!is_array($goods_id)) {
  29. $goods_id = [$goods_id];
  30. }
  31. $goodsCache = [];
  32. foreach ($to_store_id as $store_id_item) {
  33. foreach ($goods_id as $goods_id_item) {
  34. // 商品分类处理
  35. // $cacheCat = $this->doGoodsCat($from_store_id, $store_id_item);
  36. // 商品规格处理
  37. $goods = Goods::find()->where([
  38. 'store_id' => $from_store_id,
  39. 'id' => $goods_id_item
  40. ])->andWhere(['<=', 'md_food_id', 0])->asArray()->one();
  41. $goodsCache = [];
  42. $store_cloud_id = StoreCloud::findOne(['store_id' => $store_id_item, 'is_delete' => 0]);
  43. if ($goods) {
  44. list($attrGroupCache, $attrCache) = $this->doGoodsAttr($from_store_id, $store_id_item, $goods['attr']);
  45. //判断是否为当前商城同步过此商品
  46. $storeSyncExtLog = StoreSyncExtLog::findOne([
  47. 'from_store_id' => $from_store_id,
  48. 'to_store_id' => $store_id_item,
  49. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  50. 'from_id' => $goods['id']
  51. ]);
  52. $goods_id_cache = $goods['id'];
  53. unset($goods['id']);
  54. $g = Goods::findOne($storeSyncExtLog->to_id ?? 0);
  55. $is_new = 0;
  56. if (!$g) {
  57. $g = new Goods();
  58. $is_new = 1;
  59. }
  60. if (!empty($goods['attr'])) {
  61. $attrs = \json_decode($goods['attr'], true);
  62. foreach ($attrs as &$attr) {
  63. if ($attr['attr_list']) {
  64. foreach ($attr['attr_list'] as &$att) {
  65. if (isset($att['attr_id']) && isset($attrCache[$att['attr_id']])) {
  66. $att['attr_id'] = $attrCache[$att['attr_id']];
  67. }
  68. }
  69. }
  70. }
  71. $goods['attr'] = \json_encode($attrs);
  72. }
  73. //选品更新自动更新商城销售价格
  74. $repeat_update_goods_price = \app\models\Option::get('repeat_update_goods_price', $store_id_item, 'store')['value'];
  75. if (!intval($repeat_update_goods_price) && !$is_new && !empty($g->attr)) {
  76. $goods['attr'] = $g->attr;
  77. }
  78. $deliveryRulesExtLog = StoreSyncExtLog::findOne([
  79. 'from_store_id' => $from_store_id,
  80. 'to_store_id' => $store_id_item,
  81. 'type' => StoreSyncExtLog::TYPE_POSTAGE_RULES,
  82. 'from_id' => $goods['delivery_rules_id']
  83. ]);
  84. $goods['store_id'] = $store_id_item;
  85. $g->setAttributes($goods, false);
  86. $g->cloud_goods_id = $goods['cloud_goods_id'];
  87. $g->is_wholesale = $goods['is_wholesale'];
  88. $g->cloud_supplier_id = $goods['cloud_supplier_id'];
  89. $g->delivery_rules_id = $deliveryRulesExtLog->to_id ?? 0;
  90. $g->is_delete = $goods['is_delete'];
  91. $g->unit = $goods['unit'];
  92. $g->save();
  93. (new StoreSyncExtLog())::handleData($from_store_id, $store_id_item, $goods_id_cache, $g->id, StoreSyncExtLog::TYPE_PRODUCT);
  94. $goodsCache[$goods_id_cache] = $g->id;
  95. if ($g->cloud_goods_id > 0 && !intval($g->is_wholesale)) {
  96. try {
  97. $form = new GoodsForm();
  98. $form->id = $g->cloud_goods_id;
  99. $form->store_id = $store_id_item;
  100. $goodsInfo = $form->saveCloudGoods();
  101. $merchantForm = new MerchantForm();
  102. $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0;
  103. $r = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'], $goodsInfo['goods_id'], $store_id_item);
  104. } catch (\Exception $e) {
  105. }
  106. }
  107. // 商品分类处理
  108. $goodsCats = GoodsCat::find()->where(['store_id' => $from_store_id, 'goods_id' => $goods_id_item])->asArray()->all();
  109. GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $goodsCache[$goods_id_cache]]);//增加删除标识
  110. $goodsCatOpen = false;
  111. foreach ($goodsCats as $goodsCat) {
  112. //判断是否为当前商城同步过此商品
  113. $storeSyncExtLog = StoreSyncExtLog::findOne([
  114. 'from_store_id' => $from_store_id,
  115. 'to_store_id' => $store_id_item,
  116. 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT,
  117. 'from_id' => $goodsCat['id']
  118. ]);
  119. //判断是否为当前商城同步过此商品
  120. $storeSyncExtLogCat = StoreSyncExtLog::findOne([
  121. 'from_store_id' => $from_store_id,
  122. 'to_store_id' => $store_id_item,
  123. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  124. 'from_id' => $goodsCat['cat_id']
  125. ]);
  126. if (!empty($storeSyncExtLogCat->to_id)) {
  127. $id = $goodsCat['id'];
  128. $cat = GoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsCat();
  129. $goodsCat['goods_id'] = $goodsCache[$goods_id_cache];
  130. $goodsCat['cat_id'] = $storeSyncExtLogCat->to_id;
  131. $goodsCat['store_id'] = $store_id_item;
  132. unset($goodsCat['id']);
  133. $cat->setAttributes($goodsCat, false);
  134. $cat->is_delete = $goodsCat['is_delete'];
  135. $cat->save();
  136. (new StoreSyncExtLog())::handleData($from_store_id, $store_id_item, $id, $cat->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT);
  137. $goodsCatOpen = true;
  138. }
  139. }
  140. if ($goodsCatOpen) {
  141. //删除
  142. GoodsCat::deleteAll(['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);
  143. } else {
  144. GoodsCat::updateAll(['is_delete' => 0], ['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);//增加删除标识
  145. }
  146. // 商品组图处理
  147. $goodsPic = GoodsPic::find()->where(['goods_id' => $goods_id_item])->asArray()->all();
  148. GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $goodsCache[$goods_id_cache]]);//增加删除标识
  149. $goodsPicOpen = false;
  150. foreach ($goodsPic as $pic) {
  151. //判断是否为当前商城同步过此商品
  152. $storeSyncExtLog = StoreSyncExtLog::findOne([
  153. 'from_store_id' => $from_store_id,
  154. 'to_store_id' => $store_id_item,
  155. 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC,
  156. 'from_id' => $pic['id']
  157. ]);
  158. $newPic = GoodsPic::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsPic();
  159. $newPic->goods_id = $goodsCache[$goods_id_cache];
  160. $newPic->pic_url = $pic['pic_url'];
  161. $newPic->is_delete = $pic['is_delete'];
  162. $newPic->save();
  163. (new StoreSyncExtLog())::handleData($from_store_id, $store_id_item, $pic['id'], $newPic->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC);
  164. $goodsPicOpen = true;
  165. }
  166. if ($goodsPicOpen) {
  167. //删除
  168. GoodsPic::deleteAll(['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);
  169. } else {
  170. GoodsPic::updateAll(['is_delete' => 0], ['goods_id' => $goodsCache[$goods_id_cache], 'is_delete' => 1]);//增加删除标识
  171. }
  172. }
  173. }
  174. }
  175. }
  176. } catch (\Exception $e) {
  177. debug_log($e->getMessage() . ' ' . $e->getLine(). ' ' . $e->getFile(), 'queuePush.log');
  178. }
  179. }
  180. // 商品分类创建
  181. private function doGoodsCat($from_store_id, $to_store_id)
  182. {
  183. $cats = Cat::find()->where([
  184. 'store_id' => $from_store_id,
  185. 'parent_id' => 0,
  186. ])->asArray()->all();
  187. $catCache = [];
  188. foreach ($cats as $cat) {
  189. //判断是否为当前商城同步过此商品
  190. $storeSyncExtLog = StoreSyncExtLog::findOne([
  191. 'from_store_id' => $from_store_id,
  192. 'to_store_id' => $to_store_id,
  193. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  194. 'from_id' => $cat['id']
  195. ]);
  196. $catC = $cat;
  197. unset($catC['id']);
  198. $c = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  199. $catC['store_id'] = $to_store_id;
  200. $c->setAttributes($catC, false);
  201. $c->is_delete = $catC['is_delete'];
  202. $c->save();
  203. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat['id'], $c->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  204. $parent_1_id = $catCache[$cat['id']] = $c->id;
  205. $cats1 = Cat::find()->where([
  206. 'store_id' => $from_store_id,
  207. 'parent_id' => $cat['id'],
  208. ])->asArray()->all();
  209. foreach ($cats1 as $cat1) {
  210. //判断是否为当前商城同步过此商品
  211. $storeSyncExtLog = StoreSyncExtLog::findOne([
  212. 'from_store_id' => $from_store_id,
  213. 'to_store_id' => $to_store_id,
  214. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  215. 'from_id' => $cat1['id']
  216. ]);
  217. $catC1 = $cat1;
  218. unset($catC1['id']);
  219. $c1 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  220. $catC1['store_id'] = $to_store_id;
  221. $catC1['parent_id'] = $parent_1_id;
  222. $c1->setAttributes($catC1, false);
  223. $c1->is_delete = $catC1['is_delete'];
  224. $c1->save();
  225. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat1['id'], $c1->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  226. $parent_2_id = $catCache[$cat1['id']] = $c1->id;
  227. $cats2 = Cat::find()->where([
  228. 'store_id' => $from_store_id,
  229. 'parent_id' => $cat1['id'],
  230. ])->asArray()->all();
  231. foreach ($cats2 as $cat2) {
  232. //判断是否为当前商城同步过此商品
  233. $storeSyncExtLog = StoreSyncExtLog::findOne([
  234. 'from_store_id' => $from_store_id,
  235. 'to_store_id' => $to_store_id,
  236. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  237. 'from_id' => $cat2['id']
  238. ]);
  239. $catC2 = $cat2;
  240. unset($catC2['id']);
  241. $c2 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  242. $catC2['store_id'] = $to_store_id;
  243. $catC2['parent_id'] = $parent_2_id;
  244. $c2->setAttributes($catC2, false);
  245. $c2->is_delete = $catC2['is_delete'];
  246. $c2->save();
  247. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat2['id'], $c2->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  248. $catCache[$cat2['id']] = $c2->id;
  249. }
  250. }
  251. }
  252. return $catCache;
  253. }
  254. // 商品规格创建
  255. private function doGoodsAttr($from_store_id, $to_store_id, $attrs = null)
  256. {
  257. $attrGroupCache = $attrCache = [];
  258. $attr_id = [];
  259. if ($attrs) {
  260. $attrs = json_decode($attrs, true);
  261. foreach ($attrs as $attr_item) {
  262. $attr_id_item = array_column($attr_item['attr_list'], 'attr_id');
  263. $attr_id = array_merge($attr_id, $attr_id_item);
  264. }
  265. }
  266. if ($attr_id) {
  267. $attr_id = array_unique($attr_id);
  268. foreach ($attr_id as $attr_id_item) {
  269. $attrGroup = null;
  270. $old_attr = Attr::findOne($attr_id_item);
  271. if ($old_attr) {
  272. $old_attr_group = AttrGroup::findOne($old_attr->attr_group_id);
  273. //判断是否为当前商城同步过此商品
  274. $storeSyncAttrGroupExtLog = StoreSyncExtLog::findOne([
  275. 'from_store_id' => $from_store_id,
  276. 'to_store_id' => $to_store_id,
  277. 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP,
  278. 'from_id' => $old_attr_group->id
  279. ]);
  280. $attrGroup = AttrGroup::findOne($storeSyncAttrGroupExtLog->to_id ?? 0) ?: new AttrGroup();
  281. $attrGroup->store_id = $to_store_id;
  282. $attrGroup->attr_group_name = $old_attr_group->attr_group_name;
  283. $attrGroup->is_delete = $old_attr_group->is_delete;
  284. $attrGroup->save();
  285. $attrGroupCache[$old_attr_group->id] = $attrGroup->id;
  286. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr_group->id, $attrGroup->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP);
  287. }
  288. //判断是否为当前商城同步过此商品
  289. $storeSyncAttrExtLog = StoreSyncExtLog::findOne([
  290. 'from_store_id' => $from_store_id,
  291. 'to_store_id' => $to_store_id,
  292. 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR,
  293. 'from_id' => $attr_id_item
  294. ]);
  295. $attr = Attr::findOne($storeSyncAttrExtLog->to_id ?? 0) ?: new Attr();
  296. $attr->attr_group_id = $attrGroup->id ?? 0;
  297. $attr->attr_name = $old_attr->attr_name;
  298. $attr->is_delete = $old_attr->is_delete;
  299. $attr->is_default = 0;
  300. $attr->save();
  301. $attrCache[$old_attr->id] = $attr->id;
  302. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr->id, $attr->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR);
  303. }
  304. }
  305. return [$attrGroupCache, $attrCache];
  306. }
  307. }