| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace app\api\command;
- use app\api\controller\OrderSuperLoss;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\console\input\Option;
- use think\Db;
- use think\Log;
- /**
- * 超节损存量重算
- *
- * 用途:规则调整(application/extra/superloss.php)后,按新规则重算历史工单的超节损奖罚倍率,
- * 并把新的 CjsJe 及倍率追溯字段回写到 工单_质量考核汇总。
- *
- * 用法:
- * php think superloss:recalc --from=2026-01 --to=2026-10 --dry-run
- * php think superloss:recalc --from=2026-01 --to=2026-10
- * php think superloss:recalc --gdbh=Y2608003
- */
- class SuperlossRecalc extends Command
- {
- protected function configure()
- {
- $this->setName('superloss:recalc')
- ->addOption('from', null, Option::VALUE_REQUIRED, '起始月份,格式 YYYY-MM')
- ->addOption('to', null, Option::VALUE_REQUIRED, '结束月份,格式 YYYY-MM')
- ->addOption('gdbh', null, Option::VALUE_REQUIRED, '仅重算指定工单')
- ->addOption('dry-run', null, Option::VALUE_NONE, '只比对差异,不写数据库')
- ->setDescription('按最新规则重算超节损奖罚倍率并回写工单_质量考核汇总');
- }
- protected function execute(Input $input, Output $output)
- {
- $from = trim((string)$input->getOption('from'));
- $to = trim((string)$input->getOption('to'));
- $gdbh = trim((string)$input->getOption('gdbh'));
- $dryRun = (bool)$input->getOption('dry-run');
- if ($from !== '' && !preg_match('/^\d{4}-\d{2}$/', $from)) {
- $output->writeln('<error>--from 格式应为 YYYY-MM</error>');
- return 1;
- }
- if ($to !== '' && !preg_match('/^\d{4}-\d{2}$/', $to)) {
- $output->writeln('<error>--to 格式应为 YYYY-MM</error>');
- return 1;
- }
- $query = Db::name('工单_质量考核汇总')
- ->field('Gy0_gdbh, 印件及工序')
- ->group('Gy0_gdbh, 印件及工序');
- if ($gdbh !== '') {
- $query->where('Gy0_gdbh', $gdbh);
- }
- if ($from !== '') {
- $query->whereRaw("DATE_FORMAT(`sys_ny`, '%Y-%m') >= '{$from}'");
- }
- if ($to !== '') {
- $query->whereRaw("DATE_FORMAT(`sys_ny`, '%Y-%m') <= '{$to}'");
- }
- $orders = $query->select();
- if (empty($orders)) {
- $output->writeln('<comment>没有需要重算的工单</comment>');
- return 0;
- }
- $output->writeln(sprintf(
- '待重算 %d 个工单%s,规则版本 %s',
- count($orders),
- $dryRun ? '(dry-run)' : '',
- json_encode(config('superloss.rule_version'), JSON_UNESCAPED_UNICODE)
- ));
- $api = new OrderSuperLoss();
- $stat = ['ok' => 0, 'changed' => 0, 'skip' => 0, 'fail' => 0, 'old' => 0.0, 'new' => 0.0];
- foreach ($orders as $item) {
- $orderNo = trim((string)$item['Gy0_gdbh']);
- $yjno = trim((string)$item['印件及工序']);
- try {
- $oldSum = (float)Db::name('工单_质量考核汇总')
- ->where('Gy0_gdbh', $orderNo)
- ->where('印件及工序', $yjno)
- ->sum('CjsJe');
- $rows = $api->OneOrderSuperLoss($orderNo, $yjno);
- if (empty($rows) || !is_array($rows)) {
- $stat['skip']++;
- $output->writeln(" <comment>[跳过]</comment> {$orderNo}-{$yjno} 无可写数据");
- continue;
- }
- $newSum = 0.0;
- foreach ($rows as $row) {
- $newSum += (float)str_replace(',', '', (string)($row['CjsJe'] ?? 0));
- }
- $stat['old'] += $oldSum;
- $stat['new'] += $newSum;
- if (abs($newSum - $oldSum) < 0.005) {
- $stat['ok']++;
- if ($dryRun) {
- $output->writeln(" [一致] {$orderNo}-{$yjno} {$oldSum}");
- }
- if (!$dryRun && !$api->saveSuperLossResult($orderNo, $yjno, $rows)) {
- $stat['fail']++;
- $output->writeln(" <error>[写入失败]</error> {$orderNo}-{$yjno}");
- }
- continue;
- }
- $stat['changed']++;
- $output->writeln(sprintf(
- " %s %s-%s 旧=%.2f 新=%.2f 差额=%.2f",
- $dryRun ? '[差异]' : '[重算]',
- $orderNo,
- $yjno,
- $oldSum,
- $newSum,
- $newSum - $oldSum
- ));
- if (!$dryRun && !$api->saveSuperLossResult($orderNo, $yjno, $rows)) {
- $stat['fail']++;
- $output->writeln(" <error>[写入失败]</error> {$orderNo}-{$yjno}");
- }
- } catch (\think\exception\HttpResponseException $e) {
- // OneOrderSuperLoss 内部 error()/success() 会抛此异常(如未查到末版入仓),视为跳过
- $stat['skip']++;
- $output->writeln(" <comment>[跳过]</comment> {$orderNo}-{$yjno} 业务校验未通过");
- } catch (\Throwable $e) {
- $stat['fail']++;
- Log::error('超节损重算失败: ' . $e->getMessage() . '; gdbh=' . $orderNo . ', yjno=' . $yjno);
- $output->writeln(" <error>[失败]</error> {$orderNo}-{$yjno} " . $e->getMessage());
- }
- }
- $output->writeln(sprintf(
- '完成:一致/写入 %d,差异 %d,跳过 %d,失败 %d;金额合计 旧=%.2f 新=%.2f',
- $stat['ok'],
- $stat['changed'],
- $stat['skip'],
- $stat['fail'],
- $stat['old'],
- $stat['new']
- ));
- return $stat['fail'] > 0 ? 1 : 0;
- }
- }
|