Files
MrleeAdmin/backend/plugin/produce/app/model/RepairRecord.php
T

161 lines
5.6 KiB
PHP
Raw Normal View History

2026-08-31 13:16:46 +08:00
<?php
declare(strict_types=1);
namespace plugin\produce\app\model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use plugin\produce\app\model\BaseModel;
use plugin\produce\app\model\MaterialCategory;
class RepairRecord extends BaseModel
{
protected $table = 'produce_repair_record';
protected $appends = [
'created_date', 'updated_date', 'fault_description_name', 'fault_description_ids', 'fault_descriptions', 'repaired_date', 'exception_type_name',
'part_category', 'part_code', 'part_name', 'part_fault', 'part_quantity', 'part_source',
'order_product', 'repair_result_name', 'responsible_operator_names',
];
protected $with = ['exception'];
protected $fillable = [
'id', 'repair_no', 'exception_id', 'exception_reason', 'exception_issue', 'order_no', 'product_code', 'product_name', 'category_id', 'category_ids', 'category_name', 'category_names',
'fault_description_id', 'fault_description', 'repair_result', 'parts', 'images', 'operation_fault_steps', 'responsible_operators', 'operator', 'repaired_at',
'remark', 'created_by', 'updated_by', 'created_at', 'updated_at', 'deleted_at',
];
protected $casts = [
'category_ids' => 'array',
'category_names' => 'array',
'parts' => 'array',
'images' => 'array',
'operation_fault_steps' => 'array',
'responsible_operators' => 'array',
'repaired_at' => 'integer',
'created_at' => 'integer',
'updated_at' => 'integer',
'deleted_at' => 'integer',
];
public function category(): BelongsTo
{
return $this->belongsTo(MaterialCategory::class, 'category_id', 'id')->withTrashed();
}
public function exception(): BelongsTo
{
return $this->belongsTo(ExceptionRecord::class, 'exception_id', 'id')->withTrashed();
}
public function getExceptionTypeNameAttribute(): string
{
return (string) ($this->exception?->type_name ?? '');
}
public function faultDescription(): BelongsTo
{
return $this->belongsTo(HardwareFaultDescription::class, 'fault_description_id', 'id')->withTrashed();
}
public function getFaultDescriptionNameAttribute(): string
{
return (string) $this->fault_description;
}
public function getOrderProductAttribute(): string
{
return implode('/', array_filter([(string) $this->order_no, (string) $this->product_code]));
}
public function getRepairResultNameAttribute(): string
{
return match ($this->repair_result) {
'material_defect' => '物料不良',
'operation_defect' => '作业不良',
'assembly_mismatch' => '搭配错误',
'process_defect' => '工艺不良',
'no_defect' => '不良不现',
default => (string) $this->repair_result,
};
}
public function getResponsibleOperatorNamesAttribute(): string
{
return implode("\n", array_filter(array_map(
static function (array $snapshot): string {
$step = (string) ($snapshot['step_name'] ?? '');
$operator = (string) ($snapshot['operator'] ?? $snapshot['user_code'] ?? '');
return implode('-', array_filter([$step, $operator]));
},
is_array($this->responsible_operators) ? $this->responsible_operators : [],
)));
}
public function getPartCategoryAttribute(): string
{
return $this->formatPartValues(static fn (array $part): string => (string) ($part['category_name'] ?? ''));
}
public function getPartCodeAttribute(): string
{
return $this->formatPartValues(static fn (array $part): string => (string) ($part['product_code'] ?? ''));
}
public function getPartNameAttribute(): string
{
return $this->formatPartValues(static fn (array $part): string => (string) ($part['product_name'] ?? ''));
}
public function getPartFaultAttribute(): string
{
return $this->formatPartValues(static function (array $part): string {
return implode('、', array_filter(array_map(
static fn (array $fault): string => (string) ($fault['description'] ?? ''),
is_array($part['fault_descriptions'] ?? null) ? $part['fault_descriptions'] : [],
)));
});
}
public function getPartQuantityAttribute(): string
{
return $this->formatPartValues(static fn (array $part): string => (string) ($part['quantity'] ?? 1));
}
public function getPartSourceAttribute(): string
{
return $this->formatPartValues(static function (array $part): string {
return match ($part['source'] ?? '') {
'new' => '新料',
'old', 'after_sale', 'after_sales', 'after-sale' => '售后',
'repair', 'refurbished' => '返修',
default => (string) ($part['source'] ?? '-'),
};
});
}
private function formatPartValues(callable $callback): string
{
return implode("\n", array_map(
static fn (array $part): string => $callback($part) ?: '-',
is_array($this->parts) ? $this->parts : [],
));
}
public function getFaultDescriptionIdsAttribute(): array
{
return $this->fault_description_id ? [$this->fault_description_id] : [];
}
public function getFaultDescriptionsAttribute(): array
{
return $this->fault_description ? [$this->fault_description] : [];
}
public function getRepairedDateAttribute(): ?string
{
return $this->repaired_at ? date('Y-m-d H:i:s', (int) $this->repaired_at) : null;
}
}