DeliveryRulesJob.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\PostageRules;
  4. use app\models\StoreSyncExtLog;
  5. use yii\base\BaseObject;
  6. use yii\queue\JobInterface;
  7. class DeliveryRulesJob extends BaseObject implements JobInterface
  8. {
  9. public array $to_store_id;
  10. public int $from_store_id;
  11. public array $delivery_rules_id;
  12. public function execute($queue)
  13. {
  14. $to_store_id = $this->to_store_id;
  15. $from_store_id = $this->from_store_id;
  16. $delivery_rules_id = $this->delivery_rules_id;
  17. $postageRulesArr = PostageRules::find()->where(['id' => $delivery_rules_id])->all();
  18. if ($postageRulesArr) {
  19. foreach ($to_store_id as $store_id_item) {
  20. $this->handle($from_store_id, $store_id_item, $postageRulesArr);
  21. }
  22. }
  23. }
  24. private function handle($from_store_id, $to_store_id, $postageRulesArr) {
  25. foreach ($postageRulesArr as $postageRules) {
  26. //判断是否为当前商城同步过此商品
  27. $storeSyncExtLog = StoreSyncExtLog::findOne([
  28. 'from_store_id' => $from_store_id,
  29. 'to_store_id' => $to_store_id,
  30. 'type' => StoreSyncExtLog::TYPE_POSTAGE_RULES,
  31. 'from_id' => $postageRules->id
  32. ]);
  33. if (intval($postageRules->is_enable)) {
  34. PostageRules::updateAll(['is_enable' => 0], ['is_delete' => 0, 'store_id' => $to_store_id]);
  35. }
  36. $toPostageRules = PostageRules::findOne($storeSyncExtLog->to_id ?? 0) ?: new PostageRules();
  37. $toPostageRules->attributes = $postageRules->attributes;
  38. $toPostageRules->store_id = $to_store_id;
  39. $toPostageRules->mch_id = 0;
  40. if ($toPostageRules->save()) {
  41. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $postageRules->id, $toPostageRules->id, StoreSyncExtLog::TYPE_POSTAGE_RULES);
  42. }
  43. }
  44. }
  45. }