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('--from 格式应为 YYYY-MM');
return 1;
}
if ($to !== '' && !preg_match('/^\d{4}-\d{2}$/', $to)) {
$output->writeln('--to 格式应为 YYYY-MM');
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('没有需要重算的工单');
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(" [跳过] {$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(" [写入失败] {$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(" [写入失败] {$orderNo}-{$yjno}");
}
} catch (\think\exception\HttpResponseException $e) {
// OneOrderSuperLoss 内部 error()/success() 会抛此异常(如未查到末版入仓),视为跳过
$stat['skip']++;
$output->writeln(" [跳过] {$orderNo}-{$yjno} 业务校验未通过");
} catch (\Throwable $e) {
$stat['fail']++;
Log::error('超节损重算失败: ' . $e->getMessage() . '; gdbh=' . $orderNo . ', yjno=' . $yjno);
$output->writeln(" [失败] {$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;
}
}