shish 2 月之前
父節點
當前提交
280ca36dc4
共有 1 個文件被更改,包括 194 次插入0 次删除
  1. 194 0
      console/controllers/CustomDistController.php

+ 194 - 0
console/controllers/CustomDistController.php

@@ -0,0 +1,194 @@
+<?php
+
+namespace console\controllers;
+
+use biz\shop\classes\ShopClass;
+use bizGhs\custom\models\Custom;
+use bizGhs\order\classes\OrderClass;
+use bizGhs\order\models\Order;
+use common\components\dict;
+use Yii;
+use yii\console\Controller;
+use yii\console\ExitCode;
+
+/**
+ * 按 mainId 补更新 xhGhsOrder.customDistId
+ *
+ * 流程:
+ * 1. 查出 mainId 下所有门店
+ * 2. 查出各门店客户及 distId
+ * 3. 更新该门店、该客户订单的 customDistId
+ *
+ * 用法:
+ *   php yii custom-dist/fill-ghs-order
+ *   php yii custom-dist/fill-ghs-order --dryRun=1
+ *   php yii custom-dist/fill-ghs-order --batchSize=200 --sleepMs=100
+ *
+ * mainId 在下方 $fillMainIds 中配置。
+ */
+class CustomDistController extends Controller
+{
+    /**
+     * 需要补更新 customDistId 的 mainId 列表(在此填写)
+     * @var int[]
+     */
+    private static $fillMainIds = [
+        644
+    ];
+
+    /** @var int 每批处理的客户数 */
+    public $batchSize = 200;
+
+    /** @var int 每批之间的休眠毫秒数 */
+    public $sleepMs = 100;
+
+    /** @var int 是否仅预览,1=是 */
+    public $dryRun = 0;
+
+    /** @var int 是否强制全部覆盖,1=是(默认只修不一致的) */
+    public $all = 0;
+
+    /** @var int 每个 mainId 最多更新订单条数,0=不限制 */
+    public $limit = 0;
+
+    public function options($actionID)
+    {
+        return array_merge(parent::options($actionID), [
+            'batchSize',
+            'sleepMs',
+            'dryRun',
+            'all',
+            'limit',
+        ]);
+    }
+
+    public function actionFillGhsOrder()
+    {
+        ini_set('memory_limit', '512M');
+        set_time_limit(0);
+
+        $mainIdList = array_values(array_unique(array_filter(array_map('intval', self::$fillMainIds))));
+        if (empty($mainIdList)) {
+            $this->stderr("请在 CustomDistController::\$fillMainIds 中配置 mainId\n");
+            return ExitCode::UNSPECIFIED_ERROR;
+        }
+        if ($this->batchSize < 1) {
+            $this->stderr("batchSize 必须大于 0\n");
+            return ExitCode::UNSPECIFIED_ERROR;
+        }
+
+        $this->stdout(sprintf(
+            "开始补更新 %s.customDistId,mainIds: %s,batchSize: %d,dryRun: %d,all: %d\n",
+            Order::tableName(),
+            implode(',', $mainIdList),
+            $this->batchSize,
+            (int)$this->dryRun,
+            (int)$this->all
+        ));
+
+        $totalUpdated = 0;
+        foreach ($mainIdList as $mainId) {
+            $updated = $this->fillMainId((int)$mainId);
+            $totalUpdated += $updated;
+            $this->stdout("mainId={$mainId} 完成,更新订单 {$updated} 条\n");
+        }
+
+        $this->stdout("全部完成,合计更新订单 {$totalUpdated} 条\n");
+        return ExitCode::OK;
+    }
+
+    private function fillMainId($mainId)
+    {
+        $ghsPtStyle = dict::getDict('ptStyle', 'ghs');
+        $shopList = ShopClass::getAllByCondition(
+            ['mainId' => $mainId, 'ptStyle' => $ghsPtStyle],
+            null,
+            'id,shopName',
+            null,
+            true
+        );
+        if (empty($shopList)) {
+            $this->stdout("mainId={$mainId} 没有找到门店,跳过\n");
+            return 0;
+        }
+
+        $updated = 0;
+        foreach ($shopList as $shop) {
+            if ($this->limit > 0 && $updated >= $this->limit) {
+                break;
+            }
+            $shopId = (int)($shop->id ?? 0);
+            if ($shopId <= 0) {
+                continue;
+            }
+            $shopName = $shop->shopName ?? '';
+            $this->stdout("mainId={$mainId} shopId={$shopId} {$shopName} 开始处理客户\n");
+
+            $shopUpdated = $this->fillShop($mainId, $shopId, $updated);
+            $updated += $shopUpdated;
+            $this->stdout("mainId={$mainId} shopId={$shopId} 更新订单 {$shopUpdated} 条\n");
+        }
+
+        return $updated;
+    }
+
+    private function fillShop($mainId, $shopId, $mainUpdated)
+    {
+        $updated = 0;
+        $query = new \yii\db\Query();
+        $query->from(Custom::tableName());
+        $query->where(['ownShopId' => $shopId]);
+        $query->orderBy('id asc');
+
+        foreach ($query->batch($this->batchSize) as $customerList) {
+            if ($this->limit > 0 && ($mainUpdated + $updated) >= $this->limit) {
+                break;
+            }
+            if (empty($customerList)) {
+                continue;
+            }
+
+            foreach ($customerList as $custom) {
+                $customId = (int)($custom['id'] ?? 0);
+                $distId = (int)($custom['distId'] ?? 0);
+                if ($customId <= 0) {
+                    continue;
+                }
+
+                $where = [
+                    'mainId' => $mainId,
+                    'shopId' => $shopId,
+                    'customId' => $customId,
+                ];
+                if (!$this->all) {
+                    $where['customDistId'] = ['<>', $distId];
+                }
+
+                if ($this->dryRun) {
+                    $count = (int)OrderClass::getCount($where);
+                    if ($count > 0) {
+                        $customName = $custom['name'] ?? '';
+                        $this->stdout("[dryRun] mainId={$mainId} shopId={$shopId} customId={$customId} {$customName} distId={$distId} 订单 {$count} 条\n");
+                        $updated += $count;
+                    }
+                    continue;
+                }
+
+                $affected = OrderClass::updateByCondition($where, ['customDistId' => $distId]);
+                if ($affected > 0) {
+                    $updated += (int)$affected;
+                }
+
+                if ($this->limit > 0 && ($mainUpdated + $updated) >= $this->limit) {
+                    break 2;
+                }
+            }
+
+            if ($this->sleepMs > 0) {
+                usleep($this->sleepMs * 1000);
+            }
+        }
+
+        return $updated;
+    }
+}