fix : 자재관리 API (리스트,조회,등록,수정,삭제)
This commit is contained in:
297
app/Services/MaterialService.php
Normal file
297
app/Services/MaterialService.php
Normal file
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Models\Materials\Material;
|
||||
|
||||
class MaterialService extends Service
|
||||
{
|
||||
/** 공통 검증 헬퍼 */
|
||||
protected function v(array $input, array $rules)
|
||||
{
|
||||
$v = Validator::make($input, $rules);
|
||||
if ($v->fails()) {
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
return $v->validated();
|
||||
}
|
||||
|
||||
/** 목록 */
|
||||
public function getMaterials(array $params)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$p = $this->v($params, [
|
||||
'q' => 'nullable|string|max:100',
|
||||
'category' => 'nullable|integer|min:1',
|
||||
'page' => 'nullable|integer|min:1',
|
||||
'per_page' => 'nullable|integer|min:1|max:200',
|
||||
]);
|
||||
if (isset($p['error'])) return $p;
|
||||
|
||||
$q = Material::query()
|
||||
->where('tenant_id', $tenantId); // SoftDeletes가 있으면 기본적으로 deleted_at IS NULL
|
||||
|
||||
if (!empty($p['category'])) {
|
||||
$q->where('category_id', (int)$p['category']);
|
||||
}
|
||||
if (!empty($p['q'])) {
|
||||
$kw = '%' . $p['q'] . '%';
|
||||
$q->where(function ($w) use ($kw) {
|
||||
$w->where('item_name', 'like', $kw)
|
||||
->orWhere('name', 'like', $kw)
|
||||
->orWhere('material_code', 'like', $kw)
|
||||
->orWhere('search_tag', 'like', $kw);
|
||||
});
|
||||
}
|
||||
|
||||
$q->orderByDesc('id');
|
||||
|
||||
$perPage = $p['per_page'] ?? 20;
|
||||
$page = $p['page'] ?? null;
|
||||
|
||||
return $q->paginate($perPage, ['*'], 'page', $page);
|
||||
}
|
||||
|
||||
/** 단건 조회 */
|
||||
public function getMaterial(int $id)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
/** @var Material|null $row */
|
||||
$row = Material::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->find($id);
|
||||
|
||||
if (!$row) return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
|
||||
// 모델에서 casts가 없을 수 있으니 안전하게 배열화
|
||||
$row->attributes = is_array($row->attributes) ? $row->attributes : ($row->attributes ? json_decode($row->attributes, true) : null);
|
||||
$row->options = is_array($row->options) ? $row->options : ($row->options ? json_decode($row->options, true) : null);
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/** 등록 */
|
||||
public function setMaterial(array $params)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$p = $this->v($params, [
|
||||
'category_id' => 'nullable|integer|min:1',
|
||||
'name' => 'required|string|max:100',
|
||||
'unit' => 'required|string|max:10',
|
||||
'is_inspection' => 'nullable|in:Y,N',
|
||||
'search_tag' => 'nullable|string',
|
||||
'remarks' => 'nullable|string',
|
||||
'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'options' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'material_code' => 'nullable|string|max:50',
|
||||
'specification' => 'nullable|string|max:100',
|
||||
]);
|
||||
if (isset($p['error'])) return $p;
|
||||
|
||||
// 기존 normalizeAttributes 사용(그대로), options는 새 normalizeOptions 사용
|
||||
$attributes = $this->normalizeAttributes($p['attributes'] ?? null);
|
||||
$options = $this->normalizeOptions($p['options'] ?? null);
|
||||
|
||||
$itemName = $this->buildItemName($p['name'], $attributes);
|
||||
$specText = $p['specification'] ?? $this->buildSpecText($attributes);
|
||||
|
||||
$m = new Material();
|
||||
$m->tenant_id = $tenantId;
|
||||
$m->category_id = $p['category_id'] ?? null;
|
||||
$m->name = $p['name'];
|
||||
$m->item_name = $itemName;
|
||||
$m->specification = $specText;
|
||||
$m->material_code = $p['material_code'] ?? null;
|
||||
$m->unit = $p['unit'];
|
||||
$m->is_inspection = $p['is_inspection'] ?? 'N';
|
||||
$m->search_tag = $p['search_tag'] ?? null;
|
||||
$m->remarks = $p['remarks'] ?? null;
|
||||
$m->attributes = $attributes ?? null;
|
||||
$m->options = $options ?? null;
|
||||
$m->created_by = $userId ?? 0;
|
||||
$m->updated_by = $userId ?? null;
|
||||
$m->save();
|
||||
|
||||
return $this->getMaterial($m->id);
|
||||
}
|
||||
|
||||
/** 수정 */
|
||||
public function updateMaterial(int $id, array $params = [])
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
/** @var Material|null $exists */
|
||||
$exists = Material::query()->where('tenant_id', $tenantId)->find($id);
|
||||
if (!$exists) return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
|
||||
$p = $this->v($params, [
|
||||
'category_id' => 'nullable|integer|min:1',
|
||||
'name' => 'nullable|string|max:100',
|
||||
'unit' => 'nullable|string|max:10',
|
||||
'is_inspection' => 'nullable|in:Y,N',
|
||||
'search_tag' => 'nullable|string',
|
||||
'remarks' => 'nullable|string',
|
||||
'attributes' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'options' => 'nullable|array', // [{label,value,unit}] 또는 map
|
||||
'material_code' => 'nullable|string|max:50',
|
||||
'specification' => 'nullable|string|max:100',
|
||||
]);
|
||||
if (isset($p['error'])) return $p;
|
||||
|
||||
$currentAttrs = is_array($exists->attributes) ? $exists->attributes
|
||||
: ($exists->attributes ? json_decode($exists->attributes, true) : null);
|
||||
$currentOpts = is_array($exists->options) ? $exists->options
|
||||
: ($exists->options ? json_decode($exists->options, true) : null);
|
||||
|
||||
// 변경 점만 정규화
|
||||
$attrs = array_key_exists('attributes', $p)
|
||||
? $this->normalizeAttributes($p['attributes'])
|
||||
: $currentAttrs;
|
||||
$opts = array_key_exists('options', $p)
|
||||
? $this->normalizeOptions($p['options'])
|
||||
: $currentOpts;
|
||||
|
||||
$baseName = array_key_exists('name', $p) ? ($p['name'] ?? $exists->name) : $exists->name;
|
||||
|
||||
$exists->category_id = $p['category_id'] ?? $exists->category_id;
|
||||
$exists->name = $baseName;
|
||||
$exists->item_name = $this->buildItemName($baseName, $attrs);
|
||||
$exists->specification = array_key_exists('specification', $p)
|
||||
? ($p['specification'] ?? null)
|
||||
: ($exists->specification ?: $this->buildSpecText($attrs));
|
||||
$exists->material_code = $p['material_code'] ?? $exists->material_code;
|
||||
$exists->unit = $p['unit'] ?? $exists->unit;
|
||||
$exists->is_inspection = $p['is_inspection'] ?? $exists->is_inspection;
|
||||
$exists->search_tag = $p['search_tag'] ?? $exists->search_tag;
|
||||
$exists->remarks = $p['remarks'] ?? $exists->remarks;
|
||||
|
||||
if (array_key_exists('attributes', $p)) $exists->attributes = $attrs;
|
||||
if (array_key_exists('options', $p)) $exists->options = $opts;
|
||||
|
||||
$exists->updated_by = $userId ?? $exists->updated_by;
|
||||
$exists->save();
|
||||
|
||||
return $this->getMaterial($exists->id);
|
||||
}
|
||||
|
||||
/** 삭제(소프트) */
|
||||
public function destroyMaterial(int $id)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
/** @var Material|null $row */
|
||||
$row = Material::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->find($id);
|
||||
|
||||
if (!$row) return ['error' => '자재를 찾을 수 없습니다.', 'code' => 404];
|
||||
|
||||
$row->delete();
|
||||
|
||||
return ['id' => $id, 'deleted_at' => now()->toDateTimeString()];
|
||||
}
|
||||
|
||||
/* -------------------------
|
||||
헬퍼: 규격/품목명 빌더
|
||||
attributes 예시:
|
||||
[
|
||||
{"label":"두께","value":"10","unit":"T"},
|
||||
{"label":"길이","value":"150","unit":"CM"}
|
||||
]
|
||||
→ item_name: "철판 10T 150CM"
|
||||
→ specification: "두께 10T, 길이 150CM"
|
||||
------------------------- */
|
||||
|
||||
private function normalizeAttributes(null|array $attrs): ?array
|
||||
{
|
||||
if (!$attrs) return null;
|
||||
|
||||
$out = [];
|
||||
foreach ($attrs as $a) {
|
||||
if (!is_array($a)) continue;
|
||||
$label = trim((string)($a['label'] ?? ''));
|
||||
$value = trim((string)($a['value'] ?? ''));
|
||||
$unit = trim((string)($a['unit'] ?? ''));
|
||||
if ($label === '' && $value === '' && $unit === '') continue;
|
||||
|
||||
$out[] = ['label' => $label, 'value' => $value, 'unit' => $unit];
|
||||
}
|
||||
return $out ?: null;
|
||||
}
|
||||
|
||||
private function buildItemName(string $name, ?array $attrs): string
|
||||
{
|
||||
if (!$attrs || count($attrs) === 0) return $name;
|
||||
|
||||
$parts = [];
|
||||
foreach ($attrs as $a) {
|
||||
$value = (string)($a['value'] ?? '');
|
||||
$unit = (string)($a['unit'] ?? '');
|
||||
$chunk = trim($value . $unit);
|
||||
if ($chunk !== '') $parts[] = $chunk;
|
||||
}
|
||||
return trim($name . ' ' . implode(' ', $parts));
|
||||
}
|
||||
|
||||
private function buildSpecText(?array $attrs): ?string
|
||||
{
|
||||
if (!$attrs || count($attrs) === 0) return null;
|
||||
|
||||
$parts = [];
|
||||
foreach ($attrs as $a) {
|
||||
$label = (string)($a['label'] ?? '');
|
||||
$value = (string)($a['value'] ?? '');
|
||||
$unit = (string)($a['unit'] ?? '');
|
||||
$valueWithUnit = trim($value . $unit);
|
||||
|
||||
if ($label !== '' && $valueWithUnit !== '') {
|
||||
$parts[] = "{$label} {$valueWithUnit}";
|
||||
} elseif ($valueWithUnit !== '') {
|
||||
$parts[] = $valueWithUnit;
|
||||
}
|
||||
}
|
||||
return $parts ? implode(', ', $parts) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* options 입력을 [{label,value,unit}] 형태로 정규화.
|
||||
* - 이미 리스트(triple)면 그대로 정규화
|
||||
* - 맵({"키":"값"})이면 [{label:키, value:값, unit:""}...]로 변환
|
||||
*/
|
||||
private function normalizeOptions(null|array $in): ?array
|
||||
{
|
||||
if (!$in) return null;
|
||||
|
||||
// 연관 맵 형태인지 간단 판별
|
||||
$isAssoc = array_keys($in) !== range(0, count($in) - 1);
|
||||
|
||||
if ($isAssoc) {
|
||||
$out = [];
|
||||
foreach ($in as $k => $v) {
|
||||
$label = trim((string)$k);
|
||||
$value = is_scalar($v) ? trim((string)$v) : json_encode($v, JSON_UNESCAPED_UNICODE);
|
||||
if ($label === '' && $value === '') continue;
|
||||
$out[] = ['label' => $label, 'value' => $value, 'unit' => ''];
|
||||
}
|
||||
return $out ?: null;
|
||||
}
|
||||
|
||||
// 리스트(triple) 정규화
|
||||
$out = [];
|
||||
foreach ($in as $a) {
|
||||
if (!is_array($a)) continue;
|
||||
$label = trim((string)($a['label'] ?? ''));
|
||||
$value = trim((string)($a['value'] ?? ''));
|
||||
$unit = trim((string)($a['unit'] ?? ''));
|
||||
if ($label === '' && $value === '' && $unit === '') continue;
|
||||
$out[] = ['label' => $label, 'value' => $value, 'unit' => $unit];
|
||||
}
|
||||
return $out ?: null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user