Files
sam-api/app/Services/BendingItemService.php
강영보 57133541d0 feat: [bending] 절곡품 기초관리 API 구현
- BendingItemController: CRUD + filters 엔드포인트 (pagination 메타 보존)
- BendingItemService: items 테이블 item_category=BENDING 필터 기반
- BendingItemResource: options → 최상위 필드 노출 + 계산값(width_sum, bend_count)
- FormRequest: Index/Store/Update 유효성 검증 (unique:items,code 포함)
- BendingFillOptions: BD-* prefix/분류 속성 자동 보강 커맨드
- BendingImportLegacy: chandj 레거시 전개도(bendingData) 임포트 커맨드 (125/170건 매칭)
- ensureContext: Bearer 토큰 없이 X-TENANT-ID 헤더로 컨텍스트 설정
2026-03-16 20:49:20 +09:00

120 lines
4.0 KiB
PHP

<?php
namespace App\Services;
use App\Models\Items\Item;
use Illuminate\Pagination\LengthAwarePaginator;
class BendingItemService extends Service
{
public function list(array $params): LengthAwarePaginator
{
return Item::where('item_category', 'BENDING')
->when($params['item_sep'] ?? null, fn ($q, $v) => $q->where('options->item_sep', $v))
->when($params['item_bending'] ?? null, fn ($q, $v) => $q->where('options->item_bending', $v))
->when($params['material'] ?? null, fn ($q, $v) => $q->where('options->material', 'like', "%{$v}%"))
->when($params['model_UA'] ?? null, fn ($q, $v) => $q->where('options->model_UA', $v))
->when($params['model_name'] ?? null, fn ($q, $v) => $q->where('options->model_name', $v))
->when($params['search'] ?? null, fn ($q, $v) => $q->where(
fn ($q2) => $q2
->where('name', 'like', "%{$v}%")
->orWhere('code', 'like', "%{$v}%")
->orWhere('options->search_keyword', 'like', "%{$v}%")
->orWhere('options->item_spec', 'like', "%{$v}%")
))
->orderBy('code')
->paginate($params['size'] ?? 50);
}
public function filters(): array
{
$items = Item::where('item_category', 'BENDING')
->select('options')
->get();
return [
'item_sep' => $items->pluck('options.item_sep')->filter()->unique()->sort()->values(),
'item_bending' => $items->pluck('options.item_bending')->filter()->unique()->sort()->values(),
'material' => $items->pluck('options.material')->filter()->unique()->sort()->values(),
'model_UA' => $items->pluck('options.model_UA')->filter()->unique()->sort()->values(),
'model_name' => $items->pluck('options.model_name')->filter()->unique()->sort()->values(),
];
}
public function find(int $id): Item
{
return Item::where('item_category', 'BENDING')->findOrFail($id);
}
public function create(array $data): Item
{
$options = $this->buildOptions($data);
return Item::create([
'tenant_id' => $this->tenantId(),
'item_type' => 'PT',
'item_category' => 'BENDING',
'code' => $data['code'],
'name' => $data['name'],
'unit' => $data['unit'] ?? 'EA',
'options' => $options,
'is_active' => true,
'created_by' => $this->apiUserId(),
]);
}
public function update(int $id, array $data): Item
{
$item = Item::where('item_category', 'BENDING')->findOrFail($id);
if (isset($data['code'])) {
$item->code = $data['code'];
}
if (isset($data['name'])) {
$item->name = $data['name'];
}
$optionKeys = self::OPTION_KEYS;
foreach ($optionKeys as $key) {
if (array_key_exists($key, $data)) {
$item->setOption($key, $data[$key]);
}
}
$item->updated_by = $this->apiUserId();
$item->save();
return $item;
}
public function delete(int $id): bool
{
$item = Item::where('item_category', 'BENDING')->findOrFail($id);
$item->deleted_by = $this->apiUserId();
$item->save();
return $item->delete();
}
private function buildOptions(array $data): array
{
$options = [];
foreach (self::OPTION_KEYS as $key) {
if (isset($data[$key])) {
$options[$key] = $data[$key];
}
}
return $options;
}
private const OPTION_KEYS = [
'item_name', 'item_sep', 'item_bending', 'item_spec',
'material', 'model_name', 'model_UA', 'search_keyword',
'rail_width', 'registration_date', 'author', 'memo',
'parent_num', 'exit_direction', 'front_bottom_width',
'box_width', 'box_height', 'bendingData',
'prefix', 'length_code', 'length_mm',
];
}