完善生产异常维修与备件管理功能
This commit is contained in:
@@ -53,6 +53,11 @@ class RepairRecordService extends BaseService
|
||||
|
||||
private function prepareData(array $data): array
|
||||
{
|
||||
foreach (['category_ids', 'parts', 'images', 'operation_fault_steps', 'responsible_operators'] as $field) {
|
||||
if (isset($data[$field]) && is_string($data[$field])) {
|
||||
$data[$field] = $this->decodeArray($data[$field], $field);
|
||||
}
|
||||
}
|
||||
$hasCategoryIds = array_key_exists('category_ids', $data);
|
||||
$categoryIds = array_values(array_unique(array_filter(array_map(static fn ($id): string => trim((string) $id), (array) ($data['category_ids'] ?? [])), static fn (string $id): bool => $id !== '')));
|
||||
if ($hasCategoryIds) {
|
||||
@@ -100,12 +105,28 @@ class RepairRecordService extends BaseService
|
||||
$data['category_names'] = [$data['category_name']];
|
||||
$data['fault_description'] = $fault->description;
|
||||
}
|
||||
if (isset($data['parts']) && is_string($data['parts'])) {
|
||||
$data['parts'] = json_decode($data['parts'], true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 JSON 字符串转换为数组,避免前端表单提交字符串导致保存失败。
|
||||
*/
|
||||
private function decodeArray(string $value, string $field): ?array
|
||||
{
|
||||
if (trim($value) === '') {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
$decoded = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
||||
} catch (\JsonException $e) {
|
||||
throw new \InvalidArgumentException(sprintf('%s 格式不正确', $field), 0, $e);
|
||||
}
|
||||
if (!is_array($decoded)) {
|
||||
throw new \InvalidArgumentException(sprintf('%s 格式不正确', $field));
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
private function changeFaultUsage(int|string|null $faultId, int $delta): void
|
||||
{
|
||||
if (!$faultId) {
|
||||
|
||||
Reference in New Issue
Block a user