ClearController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 默认 5000) */
  30. public $samplePerStyle = 5000;
  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 条(默认 5000),检查是否有漏补问题
  79. *
  80. * 用法:
  81. * php yii clear/check-order-cg-clear-sample
  82. * php yii clear/check-order-cg-clear-sample --samplePerStyle=5000
  83. * php yii clear/check-order-cg-clear-sample --dryRun=0 # 发现问题顺带漏补
  84. */
  85. public function actionCheckOrderCgClearSample()
  86. {
  87. if (!$this->isCliOptionPassed('dryRun')) {
  88. $this->dryRun = 1;
  89. }
  90. return $this->runSampleCheckOrderCgClear();
  91. }
  92. /**
  93. * 漏补 xhOrderCgClear:clearStyle 1(hd2Gys) + 2(gys2Hd) + 3(gys2KmGys) 统一扫描
  94. *
  95. * 仅处理:无关系行 / 关系行不足 / amount 为空 / gys2KmGys 旧格式;已完整则跳过。
  96. *
  97. * 用法:
  98. * php yii clear/backfill-order-cg-clear --dryRun=1 --limit=1000
  99. * php yii clear/backfill-order-cg-clear --reset=1 --batchSize=50 --sleepMs=200
  100. * php yii clear/backfill-order-cg-clear --clearId=123
  101. */
  102. public function actionBackfillOrderCgClear()
  103. {
  104. return $this->runBackfillOrderCgClear(
  105. OrderCgClearClass::getRelationClearStyles(),
  106. 'all',
  107. 'hd2Gys(1)+gys2Hd(2)+gys2KmGys(3)',
  108. true
  109. );
  110. }
  111. /**
  112. * @deprecated 请改用 backfill-order-cg-clear(已合并 1+2+3)
  113. */
  114. public function actionBackfillOrderCgClearHdGys()
  115. {
  116. $this->stderr("已合并至: php yii clear/backfill-order-cg-clear\n");
  117. return $this->runBackfillOrderCgClear(
  118. [
  119. intval(dict::getDict('clearStyle', 'hd2Gys')),
  120. intval(dict::getDict('clearStyle', 'gys2Hd')),
  121. ],
  122. 'hd_gys',
  123. 'hd2Gys(1)+gys2Hd(2)',
  124. false
  125. );
  126. }
  127. /**
  128. * @deprecated 请改用 backfill-order-cg-clear(已合并 1+2+3)
  129. */
  130. public function actionBackfillOrderCgClearKmGys()
  131. {
  132. $this->stderr("已合并至: php yii clear/backfill-order-cg-clear\n");
  133. return $this->runBackfillOrderCgClear(
  134. [intval(dict::getDict('clearStyle', 'gys2KmGys'))],
  135. 'km_gys',
  136. 'gys2KmGys(3)',
  137. true
  138. );
  139. }
  140. /**
  141. * 迁移 gys2KmGys 关系行:orderId/cgId 旧格式 -> cgId 存 xhGhsCgOrder、orderId=0
  142. *
  143. * 用法:
  144. * php yii clear/migrate-gys2-km-gys-order-cg-clear --dryRun=1
  145. * php yii clear/migrate-gys2-km-gys-order-cg-clear
  146. *
  147. * 注意:若仍存在唯一索引 order_id_clear_id(clearId,orderId),请先执行 rebuild-order-cg-clear-index
  148. */
  149. public function actionMigrateGys2KmGysOrderCgClear()
  150. {
  151. set_time_limit(0);
  152. $this->stdout("迁移 gys2KmGys 关系行 | dryRun={$this->dryRun} batchSize={$this->batchSize}\n");
  153. $count = OrderCgClearClass::migrateLegacyGys2KmGysRows((bool)$this->dryRun, $this->batchSize, $this->sleepMs);
  154. $this->stdout(($this->dryRun ? '预览' : '完成') . ":处理 {$count} 行\n");
  155. return ExitCode::OK;
  156. }
  157. /**
  158. * 重建 xhOrderCgClear 唯一索引为 (clearId, cgId, orderId)
  159. *
  160. * 用法:
  161. * php yii clear/rebuild-order-cg-clear-index --dryRun=1
  162. * php yii clear/rebuild-order-cg-clear-index
  163. */
  164. public function actionRebuildOrderCgClearIndex()
  165. {
  166. set_time_limit(0);
  167. if ($this->dryRun) {
  168. $info = OrderCgClearClass::rebuildRelationUniqueIndex(true);
  169. $this->stdout(sprintf(
  170. "预览:旧索引=%s 新索引=%s 待迁移旧格式行=%d\n",
  171. !empty($info['hasLegacyIndex']) ? '有' : '无',
  172. !empty($info['hasNewIndex']) ? '有' : '无',
  173. (int)($info['legacyRows'] ?? 0)
  174. ));
  175. return ExitCode::OK;
  176. }
  177. $this->stdout("开始重建唯一索引 uk_clear_cg_order(clearId,cgId,orderId)\n");
  178. $result = OrderCgClearClass::rebuildRelationUniqueIndex(false);
  179. $this->stdout(sprintf(
  180. "完成:删旧索引=%s 建新索引=%s 迁移旧格式行=%d\n",
  181. !empty($result['droppedLegacyIndex']) ? '是' : '否',
  182. !empty($result['addedNewIndex']) ? '是' : '否',
  183. (int)($result['migratedLegacyRows'] ?? 0)
  184. ));
  185. return ExitCode::OK;
  186. }
  187. /**
  188. * clearStyle 1/2/3 各抽样 samplePerStyle 条,检查关系表是否有漏补问题
  189. */
  190. private function runSampleCheckOrderCgClear()
  191. {
  192. ini_set('memory_limit', '512M');
  193. set_time_limit(0);
  194. $sampleSize = intval($this->samplePerStyle);
  195. if ($sampleSize < 1) {
  196. $this->stderr("samplePerStyle 必须大于 0\n");
  197. return ExitCode::UNSPECIFIED_ERROR;
  198. }
  199. $styleLabels = [
  200. intval(dict::getDict('clearStyle', 'hd2Gys')) => 'hd2Gys(1)',
  201. intval(dict::getDict('clearStyle', 'gys2Hd')) => 'gys2Hd(2)',
  202. intval(dict::getDict('clearStyle', 'gys2KmGys')) => 'gys2KmGys(3)',
  203. ];
  204. $this->stdout(sprintf(
  205. "抽样检查 xhOrderCgClear | 各 clearStyle 抽 %d 条(id DESC) | dryRun=%d | 最多展示 %d 条问题明细\n",
  206. $sampleSize,
  207. (int)$this->dryRun,
  208. (int)$this->showIssues
  209. ));
  210. $totalSampled = 0;
  211. $totalIssues = 0;
  212. $totalFixed = 0;
  213. $allIssues = [];
  214. $reasonStats = [];
  215. foreach ($styleLabels as $clearStyle => $label) {
  216. $list = $this->fetchClearSampleByStyle($clearStyle, $sampleSize);
  217. $sampled = count($list);
  218. $totalSampled += $sampled;
  219. if ($sampled === 0) {
  220. $this->stdout("{$label}:无数据\n");
  221. continue;
  222. }
  223. $clearIds = array_map('intval', array_column($list, 'id'));
  224. $emptyAmountClearIds = OrderCgClearClass::getClearIdsWithEmptyAmount($clearIds);
  225. $legacyGys2KmGysClearIds = OrderCgClearClass::getClearIdsWithLegacyGys2KmGysRows($clearIds);
  226. $relationCountMap = OrderCgClearClass::getRelationCountMap($clearIds);
  227. $styleIssues = 0;
  228. $styleFixed = 0;
  229. foreach ($list as $clear) {
  230. $currentClearId = intval($clear['id'] ?? 0);
  231. $relationCount = $relationCountMap[$currentClearId] ?? 0;
  232. $issue = OrderCgClearClass::detectBackfillIssue(
  233. $clear,
  234. $relationCount,
  235. $emptyAmountClearIds,
  236. $legacyGys2KmGysClearIds
  237. );
  238. if ($issue === null) {
  239. continue;
  240. }
  241. $styleIssues++;
  242. $totalIssues++;
  243. $reason = $issue['reason'];
  244. $reasonStats[$reason] = ($reasonStats[$reason] ?? 0) + 1;
  245. $allIssues[] = [
  246. 'clearId' => $currentClearId,
  247. 'clearStyle' => $clearStyle,
  248. 'label' => $label,
  249. 'reason' => $reason,
  250. 'relationCount' => $issue['relationCount'],
  251. 'expected' => $issue['expected'],
  252. ];
  253. if (!$this->dryRun) {
  254. $count = OrderCgClearClass::backfillFromClear($clear, true);
  255. if ($count > 0) {
  256. $styleFixed++;
  257. $totalFixed += $count;
  258. }
  259. }
  260. }
  261. $this->stdout(sprintf(
  262. "%s:抽样 %d 条,问题 %d 条%s\n",
  263. $label,
  264. $sampled,
  265. $styleIssues,
  266. !$this->dryRun ? ",已写入关系 {$styleFixed} 行" : ''
  267. ));
  268. }
  269. if (!empty($reasonStats)) {
  270. $this->stdout("问题类型汇总:\n");
  271. foreach ($reasonStats as $reason => $cnt) {
  272. $this->stdout(" {$reason}:{$cnt}\n");
  273. }
  274. }
  275. $showLimit = max(0, intval($this->showIssues));
  276. if ($showLimit > 0 && !empty($allIssues)) {
  277. $this->stdout("问题明细(前 {$showLimit} 条):\n");
  278. foreach (array_slice($allIssues, 0, $showLimit) as $item) {
  279. $this->stdout(sprintf(
  280. " clearId=%d %s %s 已有=%d 应有≈%d\n",
  281. $item['clearId'],
  282. $item['label'],
  283. $item['reason'],
  284. $item['relationCount'],
  285. $item['expected']
  286. ));
  287. }
  288. if (count($allIssues) > $showLimit) {
  289. $this->stdout(' ... 还有 ' . (count($allIssues) - $showLimit) . " 条未展示\n");
  290. }
  291. }
  292. $this->stdout(sprintf(
  293. "检查完成:共抽样 %d 条,发现问题结账单 %d 个%s\n",
  294. $totalSampled,
  295. $totalIssues,
  296. $totalIssues === 0 ? ',未发现漏补' : ''
  297. ));
  298. if (!$this->dryRun && $totalFixed > 0) {
  299. $this->stdout("已漏补写入关系 {$totalFixed} 行\n");
  300. }
  301. if ($this->dryRun && $totalIssues > 0) {
  302. $this->stdout("请加 --dryRun=0 执行漏补,或运行 backfill-order-cg-clear --reset=1 全量漏补\n");
  303. }
  304. return $totalIssues > 0 ? ExitCode::UNSPECIFIED_ERROR : ExitCode::OK;
  305. }
  306. private function fetchClearSampleByStyle($clearStyle, $limit)
  307. {
  308. return Yii::$app->db->createCommand(
  309. 'SELECT id, orderSn, saleIds, purchaseIds, status, clearStyle FROM ' . Clear::tableName()
  310. . ' WHERE clearStyle = :style ORDER BY id DESC LIMIT ' . intval($limit)
  311. )->bindValue(':style', intval($clearStyle))->queryAll();
  312. }
  313. /**
  314. * @param int[] $clearStyles
  315. * @param string $progressKey 进度文件后缀
  316. * @param string $label 日志标签
  317. * @param bool $checkLegacyGys2KmGys 是否检测 gys2KmGys 旧格式行
  318. */
  319. private function runBackfillOrderCgClear(array $clearStyles, $progressKey, $label, $checkLegacyGys2KmGys)
  320. {
  321. ini_set('memory_limit', '512M');
  322. set_time_limit(0);
  323. if ($this->batchSize < 1) {
  324. $this->stderr("batchSize 必须大于 0\n");
  325. return ExitCode::UNSPECIFIED_ERROR;
  326. }
  327. if ($this->sleepMs < 0) {
  328. $this->stderr("sleepMs 不能小于 0\n");
  329. return ExitCode::UNSPECIFIED_ERROR;
  330. }
  331. $this->backfillProgressKey = $progressKey;
  332. if ($this->reset && $this->clearId <= 0) {
  333. $this->clearBackfillProgress();
  334. $this->stdout("已重置回填进度 [{$progressKey}]\n");
  335. }
  336. $cursorId = $this->clearId > 0 ? null : $this->loadBackfillProgress();
  337. $total = 0;
  338. $inserted = 0;
  339. $skipped = 0;
  340. $needBackfill = 0;
  341. $batchNo = 0;
  342. $styleStats = [];
  343. $this->stdout(sprintf(
  344. "开始漏补 xhOrderCgClear [%s] | id DESC 分批 | dryRun=%d batchSize=%d sleepMs=%d limit=%d | 续跑 id<%s | 进度=%s\n",
  345. $label,
  346. (int)$this->dryRun,
  347. $this->batchSize,
  348. $this->sleepMs,
  349. (int)$this->limit,
  350. $cursorId === null ? 'max' : (string)$cursorId,
  351. $this->getBackfillProgressFile()
  352. ));
  353. while (true) {
  354. if ($this->limit > 0 && $total >= $this->limit) {
  355. break;
  356. }
  357. $fetchLimit = $this->batchSize;
  358. if ($this->limit > 0) {
  359. $fetchLimit = min($fetchLimit, $this->limit - $total);
  360. if ($fetchLimit < 1) {
  361. break;
  362. }
  363. }
  364. $list = $this->fetchClearBatch($clearStyles, $cursorId, $fetchLimit);
  365. if (empty($list)) {
  366. break;
  367. }
  368. $batchNo++;
  369. $batchMinId = null;
  370. $clearIds = array_map('intval', array_column($list, 'id'));
  371. $emptyAmountClearIds = OrderCgClearClass::getClearIdsWithEmptyAmount($clearIds);
  372. $legacyGys2KmGysClearIds = $checkLegacyGys2KmGys
  373. ? OrderCgClearClass::getClearIdsWithLegacyGys2KmGysRows($clearIds)
  374. : [];
  375. $relationCountMap = OrderCgClearClass::getRelationCountMap($clearIds);
  376. foreach ($list as $clear) {
  377. $total++;
  378. $currentClearId = intval($clear['id'] ?? 0);
  379. $clearStyle = intval($clear['clearStyle'] ?? 0);
  380. $batchMinId = $currentClearId;
  381. $cursorId = $currentClearId;
  382. if (!isset($styleStats[$clearStyle])) {
  383. $styleStats[$clearStyle] = ['scan' => 0, 'need' => 0, 'skip' => 0];
  384. }
  385. $styleStats[$clearStyle]['scan']++;
  386. $saleIds = trim($clear['saleIds'] ?? '');
  387. $purchaseIds = trim($clear['purchaseIds'] ?? '');
  388. if ($saleIds === '' && $purchaseIds === '') {
  389. $skipped++;
  390. $styleStats[$clearStyle]['skip']++;
  391. continue;
  392. }
  393. $relationCount = $relationCountMap[$currentClearId] ?? 0;
  394. if (OrderCgClearClass::shouldSkipBackfill(
  395. $clear,
  396. $relationCount,
  397. $emptyAmountClearIds,
  398. $legacyGys2KmGysClearIds
  399. )) {
  400. $skipped++;
  401. $styleStats[$clearStyle]['skip']++;
  402. continue;
  403. }
  404. $needBackfill++;
  405. $styleStats[$clearStyle]['need']++;
  406. if ($this->dryRun) {
  407. $expected = OrderCgClearClass::expectedRelationCountFromClear($clear);
  408. $this->stdout(sprintf(
  409. "需漏补 clearId=%d clearStyle=%d 已有关系=%d 应有≈%d\n",
  410. $currentClearId,
  411. $clearStyle,
  412. $relationCount,
  413. $expected
  414. ));
  415. if ($this->logEvery > 0 && $needBackfill % $this->logEvery === 0) {
  416. $this->stdout("dryRun 需漏补累计 {$needBackfill} 条\n");
  417. }
  418. continue;
  419. }
  420. $count = OrderCgClearClass::backfillFromClear($clear, true);
  421. if ($count > 0) {
  422. $inserted += $count;
  423. } else {
  424. $skipped++;
  425. $styleStats[$clearStyle]['skip']++;
  426. $styleStats[$clearStyle]['need']--;
  427. $needBackfill--;
  428. }
  429. if ($this->logEvery > 0 && $total % $this->logEvery === 0) {
  430. $this->stdout("进度:扫描 {$total},需漏补 {$needBackfill},写入关系 {$inserted} 行,跳过 {$skipped},当前 clearId={$currentClearId}\n");
  431. }
  432. }
  433. if (!$this->dryRun && $this->clearId <= 0 && $batchMinId !== null) {
  434. $this->saveBackfillProgress($batchMinId);
  435. }
  436. $this->stdout(sprintf(
  437. "第 %d 批完成:本批 %d 条,累计扫描 %d,需漏补 %d,写入关系 %d 行,跳过 %d,checkpoint id=%d\n",
  438. $batchNo,
  439. count($list),
  440. $total,
  441. $needBackfill,
  442. $inserted,
  443. $skipped,
  444. (int)$batchMinId
  445. ));
  446. if ($this->clearId > 0 || count($list) < $fetchLimit) {
  447. break;
  448. }
  449. $this->sleepBetweenBatch();
  450. }
  451. if (!$this->dryRun && $this->clearId <= 0 && $cursorId !== null) {
  452. $this->stdout("进度已保存,下次从 id < {$cursorId} 继续\n");
  453. }
  454. $this->stdout(sprintf(
  455. "全部完成 [%s]:扫描 %d 条 xhClear,需漏补 %d 条,写入 %d 行关系,跳过 %d 条\n",
  456. $label,
  457. $total,
  458. $needBackfill,
  459. $inserted,
  460. $skipped
  461. ));
  462. if (!empty($styleStats)) {
  463. ksort($styleStats);
  464. foreach ($styleStats as $style => $stat) {
  465. $this->stdout(sprintf(
  466. " clearStyle=%d:扫描 %d,需漏补 %d,跳过 %d\n",
  467. $style,
  468. (int)$stat['scan'],
  469. (int)$stat['need'],
  470. (int)$stat['skip']
  471. ));
  472. }
  473. }
  474. return ExitCode::OK;
  475. }
  476. /**
  477. * 按主键倒序轻量拉取一批 xhClear(只查回填所需字段,LIMIT 走主键索引)
  478. */
  479. private function fetchClearBatch(array $clearStyles, $cursorId, $limit)
  480. {
  481. $clearStyles = array_values(array_unique(array_filter(array_map('intval', $clearStyles))));
  482. if (empty($clearStyles)) {
  483. return [];
  484. }
  485. $inParts = [];
  486. $params = [];
  487. foreach ($clearStyles as $idx => $style) {
  488. $key = ':s' . $idx;
  489. $inParts[] = $key;
  490. $params[$key] = $style;
  491. }
  492. $inSql = implode(',', $inParts);
  493. if ($this->clearId > 0) {
  494. $params[':id'] = intval($this->clearId);
  495. $row = Yii::$app->db->createCommand(
  496. 'SELECT id, orderSn, saleIds, purchaseIds, status, clearStyle FROM ' . Clear::tableName()
  497. . ' WHERE id = :id AND clearStyle IN (' . $inSql . ') LIMIT 1'
  498. )->bindValues($params)->queryOne();
  499. return $row ? [$row] : [];
  500. }
  501. $sql = 'SELECT id, orderSn, saleIds, purchaseIds, status, clearStyle FROM ' . Clear::tableName()
  502. . ' WHERE clearStyle IN (' . $inSql . ')';
  503. if ($cursorId !== null) {
  504. $sql .= ' AND id < :cursorId';
  505. $params[':cursorId'] = intval($cursorId);
  506. }
  507. $sql .= ' ORDER BY id DESC LIMIT ' . intval($limit);
  508. return Yii::$app->db->createCommand($sql)->bindValues($params)->queryAll();
  509. }
  510. private function sleepBetweenBatch()
  511. {
  512. if ($this->sleepMs > 0) {
  513. usleep($this->sleepMs * 1000);
  514. }
  515. }
  516. private function getBackfillProgressFile()
  517. {
  518. $key = $this->backfillProgressKey !== '' ? $this->backfillProgressKey : 'all';
  519. return Yii::getAlias('@console/runtime/order_cg_clear_backfill_' . $key . '.last_id');
  520. }
  521. private function loadBackfillProgress()
  522. {
  523. $file = $this->getBackfillProgressFile();
  524. if (!is_file($file)) {
  525. return null;
  526. }
  527. $id = intval(trim((string)file_get_contents($file)));
  528. return $id > 0 ? $id : null;
  529. }
  530. private function saveBackfillProgress($clearId)
  531. {
  532. if ($this->dryRun || $this->clearId > 0) {
  533. return;
  534. }
  535. $clearId = intval($clearId);
  536. if ($clearId <= 0) {
  537. return;
  538. }
  539. $file = $this->getBackfillProgressFile();
  540. $dir = dirname($file);
  541. if (!is_dir($dir)) {
  542. mkdir($dir, 0777, true);
  543. }
  544. file_put_contents($file, (string)$clearId);
  545. }
  546. private function clearBackfillProgress()
  547. {
  548. $file = $this->getBackfillProgressFile();
  549. if (is_file($file)) {
  550. unlink($file);
  551. }
  552. }
  553. private function isCliOptionPassed($name)
  554. {
  555. foreach ($_SERVER['argv'] ?? [] as $arg) {
  556. if ($arg === '--' . $name || strpos($arg, '--' . $name . '=') === 0) {
  557. return true;
  558. }
  559. }
  560. return false;
  561. }
  562. }