| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?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 = [
- 25119, 2644,10652,2084,89473,50
- ];
- /** @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;
- }
- $countWhere = $this->buildOrderCountWhere($mainId, $shopId, $customId, $distId);
- $updateWhere = $this->buildOrderUpdateWhere($mainId, $shopId, $customId, $distId);
- if ($this->dryRun) {
- $count = (int)OrderClass::getCount($countWhere);
- if ($count > 0) {
- $customName = $custom['name'] ?? '';
- $this->stdout("[dryRun] mainId={$mainId} shopId={$shopId} customId={$customId} {$customName} distId={$distId} 订单 {$count} 条\n");
- $updated += $count;
- }
- continue;
- }
- $affected = Order::updateAll(['customDistId' => $distId], $updateWhere);
- 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;
- }
- /**
- * updateAll 用的 Yii 标准条件
- */
- private function buildOrderUpdateWhere($mainId, $shopId, $customId, $distId)
- {
- $base = [
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'customId' => $customId,
- ];
- if ($this->all) {
- return $base;
- }
- return ['and', $base, ['!=', 'customDistId', $distId]];
- }
- /**
- * conditionQuery 用的等值/不等条件(仅 dryRun 统计)
- */
- private function buildOrderCountWhere($mainId, $shopId, $customId, $distId)
- {
- $where = [
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'customId' => $customId,
- ];
- if (!$this->all) {
- $where['customDistId!='] = $distId;
- }
- return $where;
- }
- }
|