IntegralStoreJob.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\Goods;
  4. use app\models\GoodsCat;
  5. use app\models\NewIntegralCat;
  6. use app\models\StoreSyncExtLog;
  7. use yii\base\BaseObject;
  8. use yii\queue\JobInterface;
  9. //积分商城同步
  10. class IntegralStoreJob extends BaseObject implements JobInterface
  11. {
  12. public array $to_store_id;
  13. public int $from_store_id;
  14. public function execute($queue)
  15. {
  16. // TODO: Implement execute() method.
  17. $from_store_id = $this->from_store_id;
  18. $to_store_id = $this->to_store_id;
  19. foreach ($to_store_id as $store_id_item) {
  20. //商品分类
  21. $goods_cat_result = $this->integralGoodsCatCopy($from_store_id, $store_id_item);
  22. }
  23. }
  24. //商品分类
  25. private function integralGoodsCatCopy($fromStoreId, $toId) {
  26. try {
  27. $integral_cat_list = NewIntegralCat::find()->where(['store_id' => $fromStoreId])->asArray()->all();
  28. $integral_cat_arr = [];
  29. $goods_id = Goods::find()->where(['store_id' => $toId, 'is_delete' => 0, 'product_type' => Goods::GOODS_TYPE_INTEGRAL])
  30. ->select('id')->asArray()->column();
  31. foreach ($integral_cat_list as $integral_cat_item) {
  32. $id = $integral_cat_item['id'];
  33. unset($integral_cat_item['id']);
  34. //判断是否为当前商城同步过此商品
  35. $storeSyncExtLog = StoreSyncExtLog::findOne([
  36. 'from_store_id' => $fromStoreId,
  37. 'to_store_id' => $toId,
  38. 'type' => StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT,
  39. 'from_id' => $id
  40. ]);
  41. $integral_cat = NewIntegralCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewIntegralCat();
  42. $integral_cat->attributes = $integral_cat_item;
  43. $integral_cat->store_id = $toId;
  44. $integral_cat->created_at = time();
  45. $integral_cat->updated_at = time();
  46. $integral_cat->is_delete = $integral_cat_item['is_delete'];
  47. if (!$integral_cat->save()) {
  48. throw new \Exception(json_encode($integral_cat->errors, JSON_UNESCAPED_UNICODE));
  49. }
  50. $integral_cat_arr[$id] = $integral_cat->id;
  51. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $integral_cat->id, StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT);
  52. $goods_cat_list = GoodsCat::find()->where(['goods_id' => $goods_id, 'cat_id' => $integral_cat_item['id'], 'is_delete' => 0, 'store_id' => $toId])
  53. ->asArray()->all();
  54. foreach ($goods_cat_list as $goods_cat_item) {
  55. $goods_cat = GoodsCat::findOne($goods_cat_item['id']);
  56. $goods_cat->cat_id = $integral_cat->id;
  57. if (!$goods_cat->save()) {
  58. throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
  59. }
  60. }
  61. }
  62. return [
  63. 'code' => 0,
  64. 'msg' => '同步完成',
  65. 'data' => [
  66. 'integral_cat_arr' => $integral_cat_arr
  67. ]
  68. ];
  69. } catch (\Exception $e) {
  70. return [
  71. 'code' => 1,
  72. 'msg' => $e->getMessage()
  73. ];
  74. }
  75. }
  76. }