ClearController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. namespace console\controllers;
  3. use bizGhs\clear\classes\OrderCgClearClass;
  4. use bizGhs\clear\models\Clear;
  5. use bizGhs\order\classes\OrderClearClass;
  6. use bizGhs\order\classes\OrderClass;
  7. use bizHd\purchase\classes\PurchaseClass;
  8. use bizHd\purchase\classes\PurchaseClearClass;
  9. use common\components\dict;
  10. use Yii;
  11. use yii\console\Controller;
  12. use yii\console\ExitCode;
  13. class ClearController extends Controller
  14. {
  15. /** @var int 是否仅预览,1=是 */
  16. public $dryRun = 0;
  17. /** @var int 每批扫描 xhClear 条数(宜小,减轻锁与内存) */
  18. public $batchSize = 50;
  19. /** @var int 每批之间休眠毫秒数,降低对线上库压力 */
  20. public $sleepMs = 100;
  21. /** @var int 指定结账单 ID,0=全部 */
  22. public $clearId = 0;
  23. /** @var int 是否重置进度从最大 id 重跑,1=是 */
  24. public $reset = 0;
  25. /** @var int 本次最多处理 xhClear 条数,0=不限制 */
  26. public $limit = 0;
  27. /** @var int 每处理多少条输出一次进度,0=仅每批汇总 */
  28. public $logEvery = 500;
  29. /** @var int 每种 clearStyle 抽样条数(check-order-cg-clear-sample 默认 50000) */
  30. public $samplePerStyle = 50000;
  31. /** @var int 抽样检查最多打印的问题明细条数 */
  32. public $showIssues = 30;
  33. /** @var string 回填进度文件后缀(内部使用) */
  34. private $backfillProgressKey = '';
  35. public function options($actionID)
  36. {
  37. $options = array_merge(parent::options($actionID), [
  38. 'dryRun',
  39. 'batchSize',
  40. 'sleepMs',
  41. 'clearId',
  42. 'reset',
  43. 'limit',
  44. 'logEvery',
  45. ]);
  46. if ($actionID === 'check-order-cg-clear-sample') {
  47. $options[] = 'samplePerStyle';
  48. $options[] = 'showIssues';
  49. }
  50. return $options;
  51. }
  52. //更新 ssh 2023503
  53. public function actionUpdate()
  54. {
  55. ini_set('memory_limit', '2045M');
  56. set_time_limit(0);
  57. $list = OrderClearClass::getAllByCondition(['status' => 2], null, '*', null, true);
  58. if (!empty($list)) {
  59. foreach ($list as $clear) {
  60. $payTime = $clear->payTime ?? '';
  61. $saleIds = $clear->saleIds ?? '';
  62. $ids = explode(',', trim($saleIds));
  63. print_r($ids);
  64. if (!empty($ids)) {
  65. OrderClass::updateByIds($ids, ['clearTime' => $payTime]);
  66. }
  67. $purchaseIds = $clear->purchaseIds ?? '';
  68. $purchaseIds = explode(',', $purchaseIds);
  69. print_r($purchaseIds);
  70. if (!empty($purchaseIds)) {
  71. PurchaseClass::updateByIds($purchaseIds, ['clearTime' => $payTime]);
  72. }
  73. echo "-----------------\n";
  74. }
  75. }
  76. }
  77. /**
  78. * 抽样检查 xhOrderCgClear:clearStyle 1/2/3 各抽 N 条(默认 50000)
  79. *
  80. * 检查漏补、旧格式等问题;不检 amount=0(由 patch-order-cg-clear-amount 处理)
  81. *
  82. * 用法:
  83. * php yii clear/check-order-cg-clear-sample
  84. * php yii clear/check-order-cg-clear-sample --samplePerStyle=50000
  85. * php yii clear/check-order-cg-clear-sample --dryRun=0 # 发现问题顺带漏补
  86. */
  87. public function actionCheckOrderCgClearSample()
  88. {
  89. if (!$this->isCliOptionPassed('dryRun')) {
  90. $this->dryRun = 1;
  91. }
  92. return $this->runSampleCheckOrderCgClear();
  93. }
  94. /**
  95. * 补写 xhOrderCgClear.amount
  96. *
  97. * 1. 从 xhOrderCgClear 查 amount=0
  98. * 2. 按 orderId/cgId + clearStyle 反查源单 actPrice
  99. * 3. 按关系行 id 更新(且仍满足 amount=0 才写,避免覆盖已有值)
  100. *
  101. * 用法:
  102. * php yii clear/patch-order-cg-clear-amount --dryRun=1 --limit=1000
  103. * php yii clear/patch-order-cg-clear-amount --reset=1 --batchSize=100 --sleepMs=200
  104. */
  105. public function actionPatchOrderCgClearAmount()
  106. {
  107. ini_set('memory_limit', '512M');
  108. set_time_limit(0);
  109. if ($this->batchSize < 1) {
  110. $this->stderr("batchSize 必须大于 0\n");
  111. return ExitCode::UNSPECIFIED_ERROR;
  112. }
  113. $this->backfillProgressKey = 'amount';
  114. if ($this->reset) {
  115. $this->clearBackfillProgress();
  116. $this->stdout("已重置补 amount 进度\n");
  117. }
  118. $cursorId = $this->loadBackfillProgress();
  119. $total = 0;
  120. $updated = 0;
  121. $skipped = 0;
  122. $batchNo = 0;
  123. $this->stdout(sprintf(
  124. "补 amount | 1查xhOrderCgClear.amount=0 → 2反查actPrice → 3更新 | dryRun=%d batchSize=%d sleepMs=%d limit=%d | id<%s\n",
  125. (int)$this->dryRun,
  126. $this->batchSize,
  127. $this->sleepMs,
  128. (int)$this->limit,
  129. $cursorId === null ? 'max' : (string)$cursorId
  130. ));
  131. while (true) {
  132. if ($this->limit > 0 && $total >= $this->limit) {
  133. break;
  134. }
  135. $fetchLimit = $this->batchSize;
  136. if ($this->limit > 0) {
  137. $fetchLimit = min($fetchLimit, $this->limit - $total);
  138. if ($fetchLimit < 1) {
  139. break;
  140. }
  141. }
  142. $rows = OrderCgClearClass::fetchZeroAmountRelationBatch($cursorId, $fetchLimit);
  143. if (empty($rows)) {
  144. break;
  145. }
  146. $batchNo++;
  147. $batchMinId = null;
  148. $result = OrderCgClearClass::patchZeroAmountRows($rows, (bool)$this->dryRun, (bool)$this->dryRun);
  149. $batchUpdated = intval($result['updated'] ?? 0);
  150. $batchSkipped = intval($result['skipped'] ?? 0);
  151. if ($this->dryRun && !empty($result['details'])) {
  152. foreach ($result['details'] as $detail) {
  153. if (($detail['status'] ?? '') === 'would_update') {
  154. $this->stdout(sprintf(
  155. " 预览 id=%d clearId=%d orderId=%d cgId=%d => amount=%s\n",
  156. (int)$detail['id'],
  157. (int)$detail['clearId'],
  158. (int)$detail['orderId'],
  159. (int)$detail['cgId'],
  160. $detail['amount']
  161. ));
  162. }
  163. }
  164. }
  165. $updated += $batchUpdated;
  166. $skipped += $batchSkipped;
  167. $total += count($rows);
  168. foreach ($rows as $row) {
  169. $batchMinId = intval($row['id']);
  170. $cursorId = $batchMinId;
  171. }
  172. if (!$this->dryRun && $batchMinId !== null) {
  173. $this->saveBackfillProgress($batchMinId);
  174. }
  175. $this->stdout(sprintf(
  176. "第 %d 批:amount=0 行 %d 条,补写 %d,跳过 %d,checkpoint id=%d\n",
  177. $batchNo,
  178. count($rows),
  179. $batchUpdated,
  180. $batchSkipped,
  181. (int)$batchMinId
  182. ));
  183. if (count($rows) < $fetchLimit) {
  184. break;
  185. }
  186. $this->sleepBetweenBatch();
  187. }
  188. if (!$this->dryRun && $cursorId !== null) {
  189. $this->stdout("进度已保存,下次从 id < {$cursorId} 继续\n");
  190. }
  191. $this->stdout(sprintf(
  192. "完成:处理 amount=0 行 %d 条,补写 %d,跳过 %d\n",
  193. $total,
  194. $updated,
  195. $skipped
  196. ));
  197. return ExitCode::OK;
  198. }
  199. /**
  200. * 漏补 xhOrderCgClear:clearStyle 1(hd2Gys) + 2(gys2Hd) + 3(gys2KmGys) 统一扫描
  201. *
  202. * 仅处理:无关系行 / 关系行不足 / amount 为空 / gys2KmGys 旧格式;已完整则跳过。
  203. *
  204. * 用法:
  205. * php yii clear/backfill-order-cg-clear --dryRun=1 --limit=1000
  206. * php yii clear/backfill-order-cg-clear --reset=1 --batchSize=50 --sleepMs=200
  207. * php yii clear/backfill-order-cg-clear --clearId=123
  208. */
  209. public function actionBackfillOrderCgClear()
  210. {
  211. return $this->runBackfillOrderCgClear(
  212. OrderCgClearClass::getRelationClearStyles(),
  213. 'all',
  214. 'hd2Gys(1)+gys2Hd(2)+gys2KmGys(3)',
  215. true
  216. );
  217. }
  218. /**
  219. * @deprecated 请改用 backfill-order-cg-clear(已合并 1+2+3)
  220. */
  221. public function actionBackfillOrderCgClearHdGys()
  222. {
  223. $this->stderr("已合并至: php yii clear/backfill-order-cg-clear\n");
  224. return $this->runBackfillOrderCgClear(
  225. [
  226. intval(dict::getDict('clearStyle', 'hd2Gys')),
  227. intval(dict::getDict('clearStyle', 'gys2Hd')),
  228. ],
  229. 'hd_gys',
  230. 'hd2Gys(1)+gys2Hd(2)',
  231. false
  232. );
  233. }
  234. /**
  235. * @deprecated 请改用 backfill-order-cg-clear(已合并 1+2+3)
  236. */
  237. public function actionBackfillOrderCgClearKmGys()
  238. {
  239. $this->stderr("已合并至: php yii clear/backfill-order-cg-clear\n");
  240. return $this->runBackfillOrderCgClear(
  241. [intval(dict::getDict('clearStyle', 'gys2KmGys'))],
  242. 'km_gys',
  243. 'gys2KmGys(3)',
  244. true
  245. );
  246. }
  247. /**
  248. * 迁移 gys2KmGys 关系行:orderId/cgId 旧格式 -> cgId 存 xhGhsCgOrder、orderId=0
  249. *
  250. * 用法:
  251. * php yii clear/migrate-gys2-km-gys-order-cg-clear --dryRun=1
  252. * php yii clear/migrate-gys2-km-gys-order-cg-clear
  253. *
  254. * 注意:若仍存在唯一索引 order_id_clear_id(clearId,orderId),请先执行 rebuild-order-cg-clear-index
  255. */
  256. public function actionMigrateGys2KmGysOrderCgClear()
  257. {
  258. set_time_limit(0);
  259. $this->stdout("迁移 gys2KmGys 关系行 | dryRun={$this->dryRun} batchSize={$this->batchSize}\n");
  260. $count = OrderCgClearClass::migrateLegacyGys2KmGysRows((bool)$this->dryRun, $this->batchSize, $this->sleepMs);
  261. $this->stdout(($this->dryRun ? '预览' : '完成') . ":处理 {$count} 行\n");
  262. return ExitCode::OK;
  263. }
  264. /**
  265. * 重建 xhOrderCgClear 唯一索引为 (clearId, cgId, orderId)
  266. *
  267. * 用法:
  268. * php yii clear/rebuild-order-cg-clear-index --dryRun=1
  269. * php yii clear/rebuild-order-cg-clear-index
  270. */
  271. public function actionRebuildOrderCgClearIndex()
  272. {
  273. set_time_limit(0);
  274. if ($this->dryRun) {
  275. $info = OrderCgClearClass::rebuildRelationUniqueIndex(true);
  276. $this->stdout(sprintf(
  277. "预览:旧索引=%s 新索引=%s 待迁移旧格式行=%d\n",
  278. !empty($info['hasLegacyIndex']) ? '有' : '无',
  279. !empty($info['hasNewIndex']) ? '有' : '无',
  280. (int)($info['legacyRows'] ?? 0)
  281. ));
  282. return ExitCode::OK;
  283. }
  284. $this->stdout("开始重建唯一索引 uk_clear_cg_order(clearId,cgId,orderId)\n");
  285. $result = OrderCgClearClass::rebuildRelationUniqueIndex(false);
  286. $this->stdout(sprintf(
  287. "完成:删旧索引=%s 建新索引=%s 迁移旧格式行=%d\n",
  288. !empty($result['droppedLegacyIndex']) ? '是' : '否',
  289. !empty($result['addedNewIndex']) ? '是' : '否',
  290. (int)($result['migratedLegacyRows'] ?? 0)
  291. ));
  292. return ExitCode::OK;
  293. }
  294. /**
  295. * clearStyle 1/2/3 各抽样 samplePerStyle 条,检查关系表漏补等问题(不含 amount=0)
  296. */
  297. private function runSampleCheckOrderCgClear()
  298. {
  299. ini_set('memory_limit', '512M');
  300. set_time_limit(0);
  301. $sampleSize = intval($this->samplePerStyle);
  302. if ($sampleSize < 1) {
  303. $this->stderr("samplePerStyle 必须大于 0\n");
  304. return ExitCode::UNSPECIFIED_ERROR;
  305. }
  306. $styleLabels = [
  307. intval(dict::getDict('clearStyle', 'hd2Gys')) => 'hd2Gys(1)',
  308. intval(dict::getDict('clearStyle', 'gys2Hd')) => 'gys2Hd(2)',
  309. intval(dict::getDict('clearStyle', 'gys2KmGys')) => 'gys2KmGys(3)',
  310. ];
  311. $this->stdout(sprintf(
  312. "抽样检查 xhOrderCgClear | 各 clearStyle 抽 %d 条(id DESC) | 不检 amount=0 | dryRun=%d | 最多展示 %d 条问题明细\n",
  313. $sampleSize,
  314. (int)$this->dryRun,
  315. (int)$this->showIssues
  316. ));
  317. $totalSampled = 0;
  318. $totalIssues = 0;
  319. $totalFixed = 0;
  320. $allIssues = [];
  321. $reasonStats = [];
  322. foreach ($styleLabels as $clearStyle => $label) {
  323. $list = $this->fetchClearSampleByStyle($clearStyle, $sampleSize);
  324. $sampled = count($list);
  325. $totalSampled += $sampled;
  326. if ($sampled === 0) {
  327. $this->stdout("{$label}:无数据\n");
  328. continue;
  329. }
  330. $clearIds = array_map('intval', array_column($list, 'id'));
  331. $relationCountMap = [];
  332. $legacyGys2KmGysClearIds = [];
  333. foreach (array_chunk($clearIds, 2000) as $clearIdChunk) {
  334. $relationCountMap += OrderCgClearClass::getRelationCountMap($clearIdChunk);
  335. $legacyGys2KmGysClearIds = array_merge(
  336. $legacyGys2KmGysClearIds,
  337. OrderCgClearClass::getClearIdsWithLegacyGys2KmGysRows($clearIdChunk)
  338. );
  339. }
  340. $legacyGys2KmGysClearIds = array_values(array_unique(array_map('intval', $legacyGys2KmGysClearIds)));
  341. $styleIssues = 0;
  342. $styleFixed = 0;
  343. foreach ($list as $clear) {
  344. $currentClearId = intval($clear['id'] ?? 0);
  345. $relationCount = $relationCountMap[$currentClearId] ?? 0;
  346. $issue = OrderCgClearClass::detectBackfillIssue(
  347. $clear,
  348. $relationCount,
  349. [],
  350. $legacyGys2KmGysClearIds,
  351. false
  352. );
  353. if ($issue === null) {
  354. continue;
  355. }
  356. $styleIssues++;
  357. $totalIssues++;
  358. $reason = $issue['reason'];
  359. $reasonStats[$reason] = ($reasonStats[$reason] ?? 0) + 1;
  360. $allIssues[] = [
  361. 'clearId' => $currentClearId,
  362. 'clearStyle' => $clearStyle,
  363. 'label' => $label,
  364. 'reason' => $reason,
  365. 'relationCount' => $issue['relationCount'],
  366. 'expected' => $issue['expected'],
  367. ];
  368. if (!$this->dryRun) {
  369. $count = OrderCgClearClass::backfillFromClear($clear, true);
  370. if ($count > 0) {
  371. $styleFixed++;
  372. $totalFixed += $count;
  373. }
  374. }
  375. }
  376. $this->stdout(sprintf(
  377. "%s:抽样 %d 条,问题 %d 条%s\n",
  378. $label,
  379. $sampled,
  380. $styleIssues,
  381. !$this->dryRun ? ",已写入关系 {$styleFixed} 行" : ''
  382. ));
  383. }
  384. if (!empty($reasonStats)) {
  385. $this->stdout("问题类型汇总:\n");
  386. foreach ($reasonStats as $reason => $cnt) {
  387. $this->stdout(" {$reason}:{$cnt}\n");
  388. }
  389. }
  390. $showLimit = max(0, intval($this->showIssues));
  391. if ($showLimit > 0 && !empty($allIssues)) {
  392. $this->stdout("问题明细(前 {$showLimit} 条):\n");
  393. foreach (array_slice($allIssues, 0, $showLimit) as $item) {
  394. $this->stdout(sprintf(
  395. " clearId=%d %s %s 已有=%d 应有≈%d\n",
  396. $item['clearId'],
  397. $item['label'],
  398. $item['reason'],
  399. $item['relationCount'],
  400. $item['expected']
  401. ));
  402. }
  403. if (count($allIssues) > $showLimit) {
  404. $this->stdout(' ... 还有 ' . (count($allIssues) - $showLimit) . " 条未展示\n");
  405. }
  406. }
  407. $this->stdout(sprintf(
  408. "检查完成:共抽样 %d 条,发现问题结账单 %d 个%s\n",
  409. $totalSampled,
  410. $totalIssues,
  411. $totalIssues === 0 ? ',未发现漏补' : ''
  412. ));
  413. if (!$this->dryRun && $totalFixed > 0) {
  414. $this->stdout("已漏补写入关系 {$totalFixed} 行\n");
  415. }
  416. if ($this->dryRun && $totalIssues > 0) {
  417. $this->stdout("请加 --dryRun=0 执行漏补,或运行 backfill-order-cg-clear --reset=1 全量漏补\n");
  418. }
  419. if ($totalIssues === 0) {
  420. $this->stdout("amount=0 请单独跑: php yii clear/patch-order-cg-clear-amount\n");
  421. }
  422. return $totalIssues > 0 ? ExitCode::UNSPECIFIED_ERROR : ExitCode::OK;
  423. }
  424. private function fetchClearSampleByStyle($clearStyle, $limit)
  425. {
  426. return Yii::$app->db->createCommand(
  427. 'SELECT id, orderSn, saleIds, purchaseIds, status, clearStyle FROM ' . Clear::tableName()
  428. . ' WHERE clearStyle = :style ORDER BY id DESC LIMIT ' . intval($limit)
  429. )->bindValue(':style', intval($clearStyle))->queryAll();
  430. }
  431. /**
  432. * @param int[] $clearStyles
  433. * @param string $progressKey 进度文件后缀
  434. * @param string $label 日志标签
  435. * @param bool $checkLegacyGys2KmGys 是否检测 gys2KmGys 旧格式行
  436. */
  437. private function runBackfillOrderCgClear(array $clearStyles, $progressKey, $label, $checkLegacyGys2KmGys)
  438. {
  439. ini_set('memory_limit', '512M');
  440. set_time_limit(0);
  441. if ($this->batchSize < 1) {
  442. $this->stderr("batchSize 必须大于 0\n");
  443. return ExitCode::UNSPECIFIED_ERROR;
  444. }
  445. if ($this->sleepMs < 0) {
  446. $this->stderr("sleepMs 不能小于 0\n");
  447. return ExitCode::UNSPECIFIED_ERROR;
  448. }
  449. $this->backfillProgressKey = $progressKey;
  450. if ($this->reset && $this->clearId <= 0) {
  451. $this->clearBackfillProgress();
  452. $this->stdout("已重置回填进度 [{$progressKey}]\n");
  453. }
  454. $cursorId = $this->clearId > 0 ? null : $this->loadBackfillProgress();
  455. $total = 0;
  456. $inserted = 0;
  457. $skipped = 0;
  458. $needBackfill = 0;
  459. $batchNo = 0;
  460. $styleStats = [];
  461. $this->stdout(sprintf(
  462. "开始漏补 xhOrderCgClear [%s] | id DESC 分批 | dryRun=%d batchSize=%d sleepMs=%d limit=%d | 续跑 id<%s | 进度=%s\n",
  463. $label,
  464. (int)$this->dryRun,
  465. $this->batchSize,
  466. $this->sleepMs,
  467. (int)$this->limit,
  468. $cursorId === null ? 'max' : (string)$cursorId,
  469. $this->getBackfillProgressFile()
  470. ));
  471. while (true) {
  472. if ($this->limit > 0 && $total >= $this->limit) {
  473. break;
  474. }
  475. $fetchLimit = $this->batchSize;
  476. if ($this->limit > 0) {
  477. $fetchLimit = min($fetchLimit, $this->limit - $total);
  478. if ($fetchLimit < 1) {
  479. break;
  480. }
  481. }
  482. $list = $this->fetchClearBatch($clearStyles, $cursorId, $fetchLimit);
  483. if (empty($list)) {
  484. break;
  485. }
  486. $batchNo++;
  487. $batchMinId = null;
  488. $clearIds = array_map('intval', array_column($list, 'id'));
  489. $emptyAmountClearIds = OrderCgClearClass::getClearIdsWithEmptyAmount($clearIds);
  490. $legacyGys2KmGysClearIds = $checkLegacyGys2KmGys
  491. ? OrderCgClearClass::getClearIdsWithLegacyGys2KmGysRows($clearIds)
  492. : [];
  493. $relationCountMap = OrderCgClearClass::getRelationCountMap($clearIds);
  494. foreach ($list as $clear) {
  495. $total++;
  496. $currentClearId = intval($clear['id'] ?? 0);
  497. $clearStyle = intval($clear['clearStyle'] ?? 0);
  498. $batchMinId = $currentClearId;
  499. $cursorId = $currentClearId;
  500. if (!isset($styleStats[$clearStyle])) {
  501. $styleStats[$clearStyle] = ['scan' => 0, 'need' => 0, 'skip' => 0];
  502. }
  503. $styleStats[$clearStyle]['scan']++;
  504. $saleIds = trim($clear['saleIds'] ?? '');
  505. $purchaseIds = trim($clear['purchaseIds'] ?? '');
  506. if ($saleIds === '' && $purchaseIds === '') {
  507. $skipped++;
  508. $styleStats[$clearStyle]['skip']++;
  509. continue;
  510. }
  511. $relationCount = $relationCountMap[$currentClearId] ?? 0;
  512. if (OrderCgClearClass::shouldSkipBackfill(
  513. $clear,
  514. $relationCount,
  515. $emptyAmountClearIds,
  516. $legacyGys2KmGysClearIds
  517. )) {
  518. $skipped++;
  519. $styleStats[$clearStyle]['skip']++;
  520. continue;
  521. }
  522. $needBackfill++;
  523. $styleStats[$clearStyle]['need']++;
  524. if ($this->dryRun) {
  525. $expected = OrderCgClearClass::expectedRelationCountFromClear($clear);
  526. $this->stdout(sprintf(
  527. "需漏补 clearId=%d clearStyle=%d 已有关系=%d 应有≈%d\n",
  528. $currentClearId,
  529. $clearStyle,
  530. $relationCount,
  531. $expected
  532. ));
  533. if ($this->logEvery > 0 && $needBackfill % $this->logEvery === 0) {
  534. $this->stdout("dryRun 需漏补累计 {$needBackfill} 条\n");
  535. }
  536. continue;
  537. }
  538. $count = OrderCgClearClass::backfillFromClear($clear, true);
  539. if ($count > 0) {
  540. $inserted += $count;
  541. } else {
  542. $skipped++;
  543. $styleStats[$clearStyle]['skip']++;
  544. $styleStats[$clearStyle]['need']--;
  545. $needBackfill--;
  546. }
  547. if ($this->logEvery > 0 && $total % $this->logEvery === 0) {
  548. $this->stdout("进度:扫描 {$total},需漏补 {$needBackfill},写入关系 {$inserted} 行,跳过 {$skipped},当前 clearId={$currentClearId}\n");
  549. }
  550. }
  551. if (!$this->dryRun && $this->clearId <= 0 && $batchMinId !== null) {
  552. $this->saveBackfillProgress($batchMinId);
  553. }
  554. $this->stdout(sprintf(
  555. "第 %d 批完成:本批 %d 条,累计扫描 %d,需漏补 %d,写入关系 %d 行,跳过 %d,checkpoint id=%d\n",
  556. $batchNo,
  557. count($list),
  558. $total,
  559. $needBackfill,
  560. $inserted,
  561. $skipped,
  562. (int)$batchMinId
  563. ));
  564. if ($this->clearId > 0 || count($list) < $fetchLimit) {
  565. break;
  566. }
  567. $this->sleepBetweenBatch();
  568. }
  569. if (!$this->dryRun && $this->clearId <= 0 && $cursorId !== null) {
  570. $this->stdout("进度已保存,下次从 id < {$cursorId} 继续\n");
  571. }
  572. $this->stdout(sprintf(
  573. "全部完成 [%s]:扫描 %d 条 xhClear,需漏补 %d 条,写入 %d 行关系,跳过 %d 条\n",
  574. $label,
  575. $total,
  576. $needBackfill,
  577. $inserted,
  578. $skipped
  579. ));
  580. if (!empty($styleStats)) {
  581. ksort($styleStats);
  582. foreach ($styleStats as $style => $stat) {
  583. $this->stdout(sprintf(
  584. " clearStyle=%d:扫描 %d,需漏补 %d,跳过 %d\n",
  585. $style,
  586. (int)$stat['scan'],
  587. (int)$stat['need'],
  588. (int)$stat['skip']
  589. ));
  590. }
  591. }
  592. return ExitCode::OK;
  593. }
  594. /**
  595. * 按主键倒序轻量拉取一批 xhClear(只查回填所需字段,LIMIT 走主键索引)
  596. */
  597. private function fetchClearBatch(array $clearStyles, $cursorId, $limit)
  598. {
  599. $clearStyles = array_values(array_unique(array_filter(array_map('intval', $clearStyles))));
  600. if (empty($clearStyles)) {
  601. return [];
  602. }
  603. $inParts = [];
  604. $params = [];
  605. foreach ($clearStyles as $idx => $style) {
  606. $key = ':s' . $idx;
  607. $inParts[] = $key;
  608. $params[$key] = $style;
  609. }
  610. $inSql = implode(',', $inParts);
  611. if ($this->clearId > 0) {
  612. $params[':id'] = intval($this->clearId);
  613. $row = Yii::$app->db->createCommand(
  614. 'SELECT id, orderSn, saleIds, purchaseIds, status, clearStyle FROM ' . Clear::tableName()
  615. . ' WHERE id = :id AND clearStyle IN (' . $inSql . ') LIMIT 1'
  616. )->bindValues($params)->queryOne();
  617. return $row ? [$row] : [];
  618. }
  619. $sql = 'SELECT id, orderSn, saleIds, purchaseIds, status, clearStyle FROM ' . Clear::tableName()
  620. . ' WHERE clearStyle IN (' . $inSql . ')';
  621. if ($cursorId !== null) {
  622. $sql .= ' AND id < :cursorId';
  623. $params[':cursorId'] = intval($cursorId);
  624. }
  625. $sql .= ' ORDER BY id DESC LIMIT ' . intval($limit);
  626. return Yii::$app->db->createCommand($sql)->bindValues($params)->queryAll();
  627. }
  628. private function sleepBetweenBatch()
  629. {
  630. if ($this->sleepMs > 0) {
  631. usleep($this->sleepMs * 1000);
  632. }
  633. }
  634. private function getBackfillProgressFile()
  635. {
  636. $key = $this->backfillProgressKey !== '' ? $this->backfillProgressKey : 'all';
  637. return Yii::getAlias('@console/runtime/order_cg_clear_backfill_' . $key . '.last_id');
  638. }
  639. private function loadBackfillProgress()
  640. {
  641. $file = $this->getBackfillProgressFile();
  642. if (!is_file($file)) {
  643. return null;
  644. }
  645. $id = intval(trim((string)file_get_contents($file)));
  646. return $id > 0 ? $id : null;
  647. }
  648. private function saveBackfillProgress($clearId)
  649. {
  650. if ($this->dryRun || $this->clearId > 0) {
  651. return;
  652. }
  653. $clearId = intval($clearId);
  654. if ($clearId <= 0) {
  655. return;
  656. }
  657. $file = $this->getBackfillProgressFile();
  658. $dir = dirname($file);
  659. if (!is_dir($dir)) {
  660. mkdir($dir, 0777, true);
  661. }
  662. file_put_contents($file, (string)$clearId);
  663. }
  664. private function clearBackfillProgress()
  665. {
  666. $file = $this->getBackfillProgressFile();
  667. if (is_file($file)) {
  668. unlink($file);
  669. }
  670. }
  671. private function isCliOptionPassed($name)
  672. {
  673. foreach ($_SERVER['argv'] ?? [] as $arg) {
  674. if ($arg === '--' . $name || strpos($arg, '--' . $name . '=') === 0) {
  675. return true;
  676. }
  677. }
  678. return false;
  679. }
  680. }