feat(produce): 完善统计、菜单与扫码搜索

This commit is contained in:
2026-09-01 20:45:40 +08:00
parent 32f1486f5e
commit 793f140a45
26 changed files with 591 additions and 41 deletions
@@ -5,11 +5,18 @@ declare(strict_types=1);
namespace plugin\produce\app\service\admin;
use Illuminate\Database\Query\Builder;
use plugin\produce\app\model\ExceptionRecord;
use plugin\produce\app\model\Process;
use plugin\produce\app\model\ProcessStep;
use plugin\produce\app\model\ProductRecord;
use plugin\produce\app\model\ProduceConfig;
use plugin\produce\app\model\RepairRecord;
use plugin\produce\app\model\ScanData;
class WorkStatisticsService
{
private ?array $repairFlow = null;
public function monthly(array $params): array
{
$month = $this->normalizeMonth($params['month'] ?? null);
@@ -44,9 +51,70 @@ class WorkStatisticsService
$rows = array_merge($rows, $this->queryProductRecord($params, $start, $end, $periodExpression)->get()->all());
}
if ($dataType === 'all' || $dataType === 'exception') {
$rows = array_merge($rows, $this->queryExceptionRecord($params, $start, $end, $periodExpression)->get()->all());
}
if ($dataType === 'all' || $dataType === 'repair') {
$rows = array_merge($rows, $this->queryRepairRecord($params, $start, $end, $periodExpression)->get()->all());
}
return $rows;
}
private function queryExceptionRecord(array $params, int $start, int $end, callable $periodExpression): Builder
{
$model = new ExceptionRecord();
$connection = $model->getConnection();
$table = $model->getTable();
$processTable = 'produce_process';
$stepTable = 'produce_process_step';
$query = $connection->table($table)
->leftJoin($processTable, $processTable . '.id', '=', $table . '.process_id')
->leftJoin($stepTable, $stepTable . '.id', '=', $table . '.step_id')
->whereBetween($table . '.reported_at', [$start, $end])
->whereNull($table . '.deleted_at');
$this->applyStatisticsFilters($query, $params, $table, $processTable, $stepTable, 'reporter');
return $query->select([$table . '.user_code', $table . '.reporter as operator'])
->selectRaw($periodExpression($connection->getTablePrefix() . $table . '.reported_at') . ' as period')
->selectRaw($connection->getTablePrefix() . $processTable . '.process_name as process_name')
->selectRaw($connection->getTablePrefix() . $stepTable . '.step_name as step_name')
->selectRaw('COUNT(*) as amount')
->groupBy('user_code', 'operator', 'process_name', 'step_name', 'period');
}
private function queryRepairRecord(array $params, int $start, int $end, callable $periodExpression): Builder
{
$model = new RepairRecord();
$exceptionModel = new ExceptionRecord();
$connection = $model->getConnection();
$table = $model->getTable();
$exceptionTable = $exceptionModel->getTable();
$flow = $this->getRepairFlow();
$query = $connection->table($table)
->leftJoin($exceptionTable, $exceptionTable . '.id', '=', $table . '.exception_id')
->whereBetween($table . '.repaired_at', [$start, $end])
->whereNull($table . '.deleted_at');
if (!$this->matchesRepairFlow($params, $flow)) {
$query->whereRaw('1 = 0');
}
if (($params['user_code'] ?? '') !== '') {
$query->where($exceptionTable . '.user_code', 'like', '%' . $params['user_code'] . '%');
}
if (($params['operator'] ?? '') !== '') {
$query->where($table . '.operator', 'like', '%' . $params['operator'] . '%');
}
return $query->select([$exceptionTable . '.user_code', $table . '.operator'])
->selectRaw($periodExpression($connection->getTablePrefix() . $table . '.repaired_at') . ' as period')
->selectRaw('? as process_name', [$flow['process_name']])
->selectRaw('? as step_name', [$flow['step_name']])
->selectRaw('COUNT(*) as amount')
->groupBy('user_code', 'operator', 'process_name', 'step_name', 'period');
}
private function queryScanData(array $params, int $start, int $end, callable $periodExpression): Builder
{
$scanDataModel = new ScanData();
@@ -61,7 +129,7 @@ class WorkStatisticsService
->whereBetween($scanDataTable . '.scan_time', [$start, $end])
->whereNull($scanDataTable . '.deleted_at');
foreach (['process_id', 'step_id', 'scan_result'] as $field) {
foreach (['process_id', 'step_id'] as $field) {
if (($params[$field] ?? '') !== '') {
$query->where($scanDataTable . '.' . $field, $params[$field]);
}
@@ -139,6 +207,65 @@ class WorkStatisticsService
->orderBy('step_name');
}
private function applyStatisticsFilters(Builder $query, array $params, string $table, string $processTable, string $stepTable, string $operatorField): void
{
foreach (['process_id', 'step_id', 'scan_result'] as $field) {
if (($params[$field] ?? '') !== '') {
$query->where($table . '.' . $field, $params[$field]);
}
}
if (($params['user_code'] ?? '') !== '') {
$query->where($table . '.user_code', 'like', '%' . $params['user_code'] . '%');
}
if (($params['operator'] ?? '') !== '') {
$query->where($table . '.' . $operatorField, 'like', '%' . $params['operator'] . '%');
}
foreach (['process_name' => $processTable . '.process_name', 'step_name' => $stepTable . '.step_name'] as $field => $column) {
if (($params[$field] ?? '') !== '') {
$query->where($column, 'like', '%' . $params[$field] . '%');
}
}
}
private function matchesRepairFlow(array $params, array $flow): bool
{
if (($params['process_id'] ?? '') !== '' && (string) $params['process_id'] !== (string) $flow['process_id']) {
return false;
}
if (($params['step_id'] ?? '') !== '' && (string) $params['step_id'] !== (string) $flow['step_id']) {
return false;
}
if (($params['process_name'] ?? '') !== '' && !str_contains($flow['process_name'], (string) $params['process_name'])) {
return false;
}
return ($params['step_name'] ?? '') === '' || str_contains($flow['step_name'], (string) $params['step_name']);
}
private function getRepairFlow(): array
{
if ($this->repairFlow !== null) {
return $this->repairFlow;
}
$configs = ProduceConfig::query()
->whereIn('group_code', ['basic_setting', 'repair_flow'])
->whereIn('code', ['repair_process_id', 'repair_step_id'])
->get(['group_code', 'code', 'content'])
->groupBy('group_code');
$basic = $configs->get('basic_setting', collect())->pluck('content', 'code');
$flow = $configs->get('repair_flow', collect())->pluck('content', 'code');
$processId = (int) (($basic['repair_process_id'] ?? '') ?: ($flow['repair_process_id'] ?? 0));
$stepId = (int) (($basic['repair_step_id'] ?? '') ?: ($flow['repair_step_id'] ?? 0));
$process = $processId > 0 ? Process::query()->withTrashed()->find($processId) : null;
$step = $stepId > 0 ? ProcessStep::query()->withTrashed()->find($stepId) : null;
return $this->repairFlow = [
'process_id' => $processId,
'step_id' => $stepId,
'process_name' => (string) ($process?->process_name ?? '维修'),
'step_name' => (string) ($step?->step_name ?? '维修'),
];
}
private function formatRows(iterable $rows, int $periodCount, callable $periodLabel): array
{
$grouped = [];