GoodsCatJob.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\Cat;
  4. use app\models\StoreSyncExtLog;
  5. use yii\base\BaseObject;
  6. use yii\queue\JobInterface;
  7. class GoodsCatJob extends BaseObject implements JobInterface
  8. {
  9. public array $cat_id;
  10. public array $to_store_id;
  11. public int $from_store_id;
  12. public function execute($queue)
  13. {
  14. $from_store_id = $this->from_store_id;
  15. $to_store_id = $this->to_store_id;
  16. $cat_id = $this->cat_id;
  17. foreach ($to_store_id as $store_id_item) {
  18. //商品分类
  19. $this->handleGoodsCat($from_store_id, $store_id_item, $cat_id);
  20. }
  21. }
  22. private function handleGoodsCat($from_store_id, $to_store_id, $cat_id) {
  23. $cats = Cat::find()->where([
  24. 'store_id' => $from_store_id,
  25. 'id' => $cat_id,
  26. ])->asArray()->all();
  27. foreach ($cats as $cat) {
  28. $catC = $cat;
  29. //判断是否为当前商城同步过此商品
  30. $storeSyncExtLog = StoreSyncExtLog::findOne([
  31. 'from_store_id' => $from_store_id,
  32. 'to_store_id' => $to_store_id,
  33. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  34. 'from_id' => $cat['id']
  35. ]);
  36. $c = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  37. //判断是否为当前商城同步过此商品
  38. $storeSyncExtLogCatParent = StoreSyncExtLog::findOne([
  39. 'from_store_id' => $from_store_id,
  40. 'to_store_id' => $to_store_id,
  41. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  42. 'from_id' => $cat['parent_id']
  43. ]);
  44. unset($catC['id']);
  45. $catC['store_id'] = $to_store_id;
  46. $c->setAttributes($catC, false);
  47. $c->parent_id = $storeSyncExtLogCatParent->to_id ?? 0;
  48. $c->save();
  49. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat['id'], $c->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  50. }
  51. }
  52. }