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; } }