ClearController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace console\controllers;
  3. use bizGhs\clear\classes\OrderCgClearClass;
  4. use bizGhs\order\classes\OrderClearClass;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizHd\purchase\classes\PurchaseClass;
  7. use bizHd\purchase\classes\PurchaseClearClass;
  8. use common\components\dict;
  9. use yii\console\Controller;
  10. use yii\console\ExitCode;
  11. class ClearController extends Controller
  12. {
  13. /** @var int 是否仅预览,1=是 */
  14. public $dryRun = 0;
  15. /** @var int 每批处理条数 */
  16. public $batchSize = 200;
  17. /** @var int 指定结账单 ID,0=全部 */
  18. public $clearId = 0;
  19. public function options($actionID)
  20. {
  21. return array_merge(parent::options($actionID), [
  22. 'dryRun',
  23. 'batchSize',
  24. 'clearId',
  25. ]);
  26. }
  27. //更新 ssh 2023503
  28. public function actionUpdate()
  29. {
  30. ini_set('memory_limit', '2045M');
  31. set_time_limit(0);
  32. $list = OrderClearClass::getAllByCondition(['status' => 2], null, '*', null, true);
  33. if (!empty($list)) {
  34. foreach ($list as $clear) {
  35. $payTime = $clear->payTime ?? '';
  36. $saleIds = $clear->saleIds ?? '';
  37. $ids = explode(',', trim($saleIds));
  38. print_r($ids);
  39. if (!empty($ids)) {
  40. OrderClass::updateByIds($ids, ['clearTime' => $payTime]);
  41. }
  42. $purchaseIds = $clear->purchaseIds ?? '';
  43. $purchaseIds = explode(',', $purchaseIds);
  44. print_r($purchaseIds);
  45. if (!empty($purchaseIds)) {
  46. PurchaseClass::updateByIds($purchaseIds, ['clearTime' => $payTime]);
  47. }
  48. echo "-----------------\n";
  49. }
  50. }
  51. }
  52. /**
  53. * 历史 xhClear 回填 xhOrderCgClear
  54. *
  55. * 用法:
  56. * php yii clear/backfill-order-cg-clear
  57. * php yii clear/backfill-order-cg-clear --dryRun=1
  58. * php yii clear/backfill-order-cg-clear --clearId=123
  59. */
  60. public function actionBackfillOrderCgClear()
  61. {
  62. ini_set('memory_limit', '2045M');
  63. set_time_limit(0);
  64. if ($this->batchSize < 1) {
  65. $this->stderr("batchSize 必须大于 0\n");
  66. return ExitCode::UNSPECIFIED_ERROR;
  67. }
  68. $hd2Gys = dict::getDict('clearStyle', 'hd2Gys');
  69. $gys2Hd = dict::getDict('clearStyle', 'gys2Hd');
  70. $where = [
  71. 'clearStyle' => ['in', [intval($hd2Gys), intval($gys2Hd)]],
  72. ];
  73. if ($this->clearId > 0) {
  74. $where['id'] = intval($this->clearId);
  75. }
  76. $lastId = 0;
  77. $total = 0;
  78. $inserted = 0;
  79. $skipped = 0;
  80. $this->stdout(sprintf(
  81. "开始回填 xhOrderCgClear,dryRun=%d,batchSize=%d,clearId=%d\n",
  82. (int)$this->dryRun,
  83. $this->batchSize,
  84. (int)$this->clearId
  85. ));
  86. while (true) {
  87. $batchWhere = $where;
  88. if ($lastId > 0) {
  89. $batchWhere['id>'] = $lastId;
  90. }
  91. $list = PurchaseClearClass::getLimitList('*', $batchWhere, $this->batchSize, 'id asc');
  92. if (empty($list)) {
  93. break;
  94. }
  95. foreach ($list as $clear) {
  96. $total++;
  97. $lastId = intval($clear['id'] ?? 0);
  98. $saleIds = trim($clear['saleIds'] ?? '');
  99. $purchaseIds = trim($clear['purchaseIds'] ?? '');
  100. if ($saleIds === '' && $purchaseIds === '') {
  101. $skipped++;
  102. continue;
  103. }
  104. if ($this->dryRun) {
  105. $this->stdout("dryRun clearId={$lastId} saleIds={$saleIds} purchaseIds={$purchaseIds}\n");
  106. continue;
  107. }
  108. $count = OrderCgClearClass::backfillFromClear($clear);
  109. if ($count > 0) {
  110. $inserted += $count;
  111. $this->stdout("clearId={$lastId} 写入 {$count} 行\n");
  112. } else {
  113. $skipped++;
  114. }
  115. }
  116. if (count($list) < $this->batchSize) {
  117. break;
  118. }
  119. }
  120. $this->stdout("完成:扫描 {$total} 条,写入 {$inserted} 行关系,跳过 {$skipped} 条\n");
  121. return ExitCode::OK;
  122. }
  123. }