Files
sam-api/app/Services/Design/ModelVersionService.php
hskwon 17fa82c35b feat: Design BOM 템플릿 diff/clone API 및 모델버전 릴리즈 유효성 검사 도입
- Design BOM 템플릿 diff/clone 엔드포인트 추가
- 컨트롤러 검증 로직 FormRequest 분리(DiffRequest/CloneRequest/Upsert/ReplaceItems)
- BomTemplateService에 diffTemplates/cloneTemplate/replaceItems/쇼우 로직 정리
- ModelVersionController createDraft FormRequest 적용 및 서비스 호출 정리
- 모델버전 release 전 유효성 검사(존재/활성/테넌트 일치, qty>0, 중복 금지) 추가
- DB enum 미사용 방침 준수(status 문자열 유지)
- model_versions 인덱스 최적화(tenant_id, model_id, status / 기간 범위)
- Swagger 문서(Design BOM) 및 i18n 메시지 키 추가
2025-09-11 13:39:32 +09:00

105 lines
3.1 KiB
PHP

<?php
namespace App\Services\Design;
use App\Models\Design\DesignModel;
use App\Models\Design\ModelVersion;
use App\Services\Service;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ModelVersionService extends Service
{
/** 특정 모델의 버전 목록 */
public function listByModel(int $modelId)
{
$tenantId = $this->tenantId();
$model = DesignModel::query()
->where('tenant_id', $tenantId)
->find($modelId);
if (!$model) {
throw new NotFoundHttpException(__('error.not_found'));
}
return ModelVersion::query()
->where('tenant_id', $tenantId)
->where('model_id', $modelId)
->orderBy('version_no')
->get();
}
/** DRAFT 생성 (version_no 자동/수동) */
public function createDraft(int $modelId, array $extra = []): ModelVersion
{
$tenantId = $this->tenantId();
$model = DesignModel::query()
->where('tenant_id', $tenantId)
->find($modelId);
if (!$model) {
throw new NotFoundHttpException(__('error.not_found'));
}
return DB::transaction(function () use ($tenantId, $model, $extra) {
$versionNo = $extra['version_no'] ?? null;
if ($versionNo === null) {
$max = ModelVersion::query()
->where('model_id', $model->id)
->max('version_no');
$versionNo = (int)($max ?? 0) + 1;
} else {
$exists = ModelVersion::query()
->where('model_id', $model->id)
->where('version_no', $versionNo)
->exists();
if ($exists) {
throw ValidationException::withMessages(['version_no' => __('error.duplicate')]);
}
}
return ModelVersion::create([
'tenant_id' => $tenantId,
'model_id' => $model->id,
'version_no' => $versionNo,
'status' => 'DRAFT',
'is_active' => true,
'notes' => $extra['notes'] ?? null,
'effective_from' => $extra['effective_from'] ?? null,
'effective_to' => $extra['effective_to'] ?? null,
]);
});
}
/** RELEASED 전환 */
public function release(int $versionId): ModelVersion
{
$tenantId = $this->tenantId();
$mv = ModelVersion::query()
->where('tenant_id', $tenantId)
->find($versionId);
if (!$mv) {
throw new NotFoundHttpException(__('error.not_found'));
}
if ($mv->status === 'RELEASED') {
return $mv; // 멱등
}
// 릴리즈 전 유효성 검사
app(\App\Services\Design\BomTemplateService::class)->validateForRelease($mv->id);
$mv->status = 'RELEASED';
$mv->effective_from = $mv->effective_from ?? now();
$mv->save();
return $mv;
}
}