SuperlossRecalc.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\api\command;
  3. use app\api\controller\OrderSuperLoss;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\console\input\Option;
  8. use think\Db;
  9. use think\Log;
  10. /**
  11. * 超节损存量重算
  12. *
  13. * 用途:规则调整(application/extra/superloss.php)后,按新规则重算历史工单的超节损奖罚倍率,
  14. * 并把新的 CjsJe 及倍率追溯字段回写到 工单_质量考核汇总。
  15. *
  16. * 用法:
  17. * php think superloss:recalc --from=2026-01 --to=2026-10 --dry-run
  18. * php think superloss:recalc --from=2026-01 --to=2026-10
  19. * php think superloss:recalc --gdbh=Y2608003
  20. */
  21. class SuperlossRecalc extends Command
  22. {
  23. protected function configure()
  24. {
  25. $this->setName('superloss:recalc')
  26. ->addOption('from', null, Option::VALUE_REQUIRED, '起始月份,格式 YYYY-MM')
  27. ->addOption('to', null, Option::VALUE_REQUIRED, '结束月份,格式 YYYY-MM')
  28. ->addOption('gdbh', null, Option::VALUE_REQUIRED, '仅重算指定工单')
  29. ->addOption('dry-run', null, Option::VALUE_NONE, '只比对差异,不写数据库')
  30. ->setDescription('按最新规则重算超节损奖罚倍率并回写工单_质量考核汇总');
  31. }
  32. protected function execute(Input $input, Output $output)
  33. {
  34. $from = trim((string)$input->getOption('from'));
  35. $to = trim((string)$input->getOption('to'));
  36. $gdbh = trim((string)$input->getOption('gdbh'));
  37. $dryRun = (bool)$input->getOption('dry-run');
  38. if ($from !== '' && !preg_match('/^\d{4}-\d{2}$/', $from)) {
  39. $output->writeln('<error>--from 格式应为 YYYY-MM</error>');
  40. return 1;
  41. }
  42. if ($to !== '' && !preg_match('/^\d{4}-\d{2}$/', $to)) {
  43. $output->writeln('<error>--to 格式应为 YYYY-MM</error>');
  44. return 1;
  45. }
  46. $query = Db::name('工单_质量考核汇总')
  47. ->field('Gy0_gdbh, 印件及工序')
  48. ->group('Gy0_gdbh, 印件及工序');
  49. if ($gdbh !== '') {
  50. $query->where('Gy0_gdbh', $gdbh);
  51. }
  52. if ($from !== '') {
  53. $query->whereRaw("DATE_FORMAT(`sys_ny`, '%Y-%m') >= '{$from}'");
  54. }
  55. if ($to !== '') {
  56. $query->whereRaw("DATE_FORMAT(`sys_ny`, '%Y-%m') <= '{$to}'");
  57. }
  58. $orders = $query->select();
  59. if (empty($orders)) {
  60. $output->writeln('<comment>没有需要重算的工单</comment>');
  61. return 0;
  62. }
  63. $output->writeln(sprintf(
  64. '待重算 %d 个工单%s,规则版本 %s',
  65. count($orders),
  66. $dryRun ? '(dry-run)' : '',
  67. json_encode(config('superloss.rule_version'), JSON_UNESCAPED_UNICODE)
  68. ));
  69. $api = new OrderSuperLoss();
  70. $stat = ['ok' => 0, 'changed' => 0, 'skip' => 0, 'fail' => 0, 'old' => 0.0, 'new' => 0.0];
  71. foreach ($orders as $item) {
  72. $orderNo = trim((string)$item['Gy0_gdbh']);
  73. $yjno = trim((string)$item['印件及工序']);
  74. try {
  75. $oldSum = (float)Db::name('工单_质量考核汇总')
  76. ->where('Gy0_gdbh', $orderNo)
  77. ->where('印件及工序', $yjno)
  78. ->sum('CjsJe');
  79. $rows = $api->OneOrderSuperLoss($orderNo, $yjno);
  80. if (empty($rows) || !is_array($rows)) {
  81. $stat['skip']++;
  82. $output->writeln(" <comment>[跳过]</comment> {$orderNo}-{$yjno} 无可写数据");
  83. continue;
  84. }
  85. $newSum = 0.0;
  86. foreach ($rows as $row) {
  87. $newSum += (float)str_replace(',', '', (string)($row['CjsJe'] ?? 0));
  88. }
  89. $stat['old'] += $oldSum;
  90. $stat['new'] += $newSum;
  91. if (abs($newSum - $oldSum) < 0.005) {
  92. $stat['ok']++;
  93. if ($dryRun) {
  94. $output->writeln(" [一致] {$orderNo}-{$yjno} {$oldSum}");
  95. }
  96. if (!$dryRun && !$api->saveSuperLossResult($orderNo, $yjno, $rows)) {
  97. $stat['fail']++;
  98. $output->writeln(" <error>[写入失败]</error> {$orderNo}-{$yjno}");
  99. }
  100. continue;
  101. }
  102. $stat['changed']++;
  103. $output->writeln(sprintf(
  104. " %s %s-%s 旧=%.2f 新=%.2f 差额=%.2f",
  105. $dryRun ? '[差异]' : '[重算]',
  106. $orderNo,
  107. $yjno,
  108. $oldSum,
  109. $newSum,
  110. $newSum - $oldSum
  111. ));
  112. if (!$dryRun && !$api->saveSuperLossResult($orderNo, $yjno, $rows)) {
  113. $stat['fail']++;
  114. $output->writeln(" <error>[写入失败]</error> {$orderNo}-{$yjno}");
  115. }
  116. } catch (\think\exception\HttpResponseException $e) {
  117. // OneOrderSuperLoss 内部 error()/success() 会抛此异常(如未查到末版入仓),视为跳过
  118. $stat['skip']++;
  119. $output->writeln(" <comment>[跳过]</comment> {$orderNo}-{$yjno} 业务校验未通过");
  120. } catch (\Throwable $e) {
  121. $stat['fail']++;
  122. Log::error('超节损重算失败: ' . $e->getMessage() . '; gdbh=' . $orderNo . ', yjno=' . $yjno);
  123. $output->writeln(" <error>[失败]</error> {$orderNo}-{$yjno} " . $e->getMessage());
  124. }
  125. }
  126. $output->writeln(sprintf(
  127. '完成:一致/写入 %d,差异 %d,跳过 %d,失败 %d;金额合计 旧=%.2f 新=%.2f',
  128. $stat['ok'],
  129. $stat['changed'],
  130. $stat['skip'],
  131. $stat['fail'],
  132. $stat['old'],
  133. $stat['new']
  134. ));
  135. return $stat['fail'] > 0 ? 1 : 0;
  136. }
  137. }