o2o-negosium-original/negodata/backend/services/quotation_setting_service.py
Mina Choi 103c80046e [feat] negodata: 신규 견적유형 + 목표가·앵커링 산정 + 협력사유형 + 협상카드 용도
- 견적유형 4종: QuotationType 에 신규협상(3)·신규견적(4) 추가(1·2 재 고정)
- 목표가 산정: items(매입가·판매가·인터넷최저가) + quotations.md_price +
  quotation_settings.internet_average_fee 로 _calc_target_price(KTC 이식, 신규/재 분리),
  세션 target_price·target_anchoring_price 저장
- 협력사유형: quotations.supplier_type(SupplierType) + 직전유형 조회 API(/supplier/{id}/last-type)
- 협상카드 용도: nego_cards/wild_cards.usage_type(CardUsageType: 공통/신규견적전용/재견적전용)
- 스키마(01-schema.sql)·ORM·protocol·service·crud·프론트(생성타입/폼) 일괄, /backend enums·models 미러

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 17:09:14 +09:00

122 lines
4.8 KiB
Python

import uuid
from fastapi import Depends
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import quotation_settings
from common.enums import DBWRType, ErrorType
from crud.quotation_setting_crud import IQuotationSettingCRUD, QuotationSettingCRUD
from router.v1.quotation_setting.protocol import (
QuotationSettingData,
Req_CreateQuotationSetting,
Req_UpdateQuotationSetting,
Res_DeleteQuotationSetting,
Res_QuotationSetting,
Res_QuotationSettingList,
)
class QuotationSettingService:
"""견적 설정 비즈니스 로직. user_id 로 소유권을 확인한다(유저별 설정)."""
def __init__(self, qs_crud: IQuotationSettingCRUD = Depends(QuotationSettingCRUD)):
self.qs_crud = qs_crud
async def _fetch_owned(self, user_uuid: uuid.UUID, qt_setting_id: uuid.UUID):
"""설정 조회 + 소유권 확인. (ErrorType, setting|None) 반환."""
err_type, setting = await DB_SESSION_MNG.execute_lambda(
quotation_settings.DBType(),
DBWRType.DB_READ.value,
lambda s: self.qs_crud.get_by_id(s, qt_setting_id),
)
if err_type != ErrorType.SUCCESS or setting is None:
return ErrorType.QUOTATION_SETTING_NOT_FOUND, None
if setting.user_id != user_uuid:
return ErrorType.QUOTATION_SETTING_NOT_FOUND, None
return ErrorType.SUCCESS, setting
async def list_settings(self, user_id: str) -> Res_QuotationSettingList:
res = Res_QuotationSettingList()
user_uuid = uuid.UUID(user_id)
err_type, rows, total = await DB_SESSION_MNG.execute_lambda(
quotation_settings.DBType(),
DBWRType.DB_READ.value,
lambda s: self.qs_crud.list_by_user(s, user_uuid),
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
res.settings = [QuotationSettingData.model_validate(r) for r in rows]
res.total = total
return res
async def get_setting(self, user_id: str, qt_setting_id: str) -> Res_QuotationSetting:
res = Res_QuotationSetting()
err_type, setting = await self._fetch_owned(uuid.UUID(user_id), uuid.UUID(qt_setting_id))
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
res.setting = QuotationSettingData.model_validate(setting)
return res
async def create_setting(self, user_id: str, req: Req_CreateQuotationSetting) -> Res_QuotationSetting:
res = Res_QuotationSetting()
setting = quotation_settings(
user_id=uuid.UUID(user_id),
target_margin_rate=req.target_margin_rate,
anchoring_value=req.anchoring_value,
internet_average_fee=req.internet_average_fee,
card_count=req.card_count,
)
err_type = await DB_SESSION_MNG.execute_lambda_run(
[quotation_settings.DBType()],
[lambda s: self.qs_crud.add_setting(s, setting)],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
# 서버 기본값(created_at/updated_at)은 insert 후 객체에 실리지 않으므로 재조회한다.
return await self.get_setting(user_id, str(setting.qt_setting_id))
async def update_setting(self, user_id: str, qt_setting_id: str, req: Req_UpdateQuotationSetting) -> Res_QuotationSetting:
res = Res_QuotationSetting()
user_uuid = uuid.UUID(user_id)
setting_uuid = uuid.UUID(qt_setting_id)
data = req.model_dump(exclude_unset=True)
# 소유권 확인
err_type, _ = await self._fetch_owned(user_uuid, setting_uuid)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
err_type = await DB_SESSION_MNG.execute_lambda_run(
[quotation_settings.DBType()],
[lambda s: self.qs_crud.update_setting(s, setting_uuid, data)],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
# 갱신 후 재조회
return await self.get_setting(user_id, qt_setting_id)
async def delete_setting(self, user_id: str, qt_setting_id: str) -> Res_DeleteQuotationSetting:
res = Res_DeleteQuotationSetting()
user_uuid = uuid.UUID(user_id)
setting_uuid = uuid.UUID(qt_setting_id)
err_type, _ = await self._fetch_owned(user_uuid, setting_uuid)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
err_type = await DB_SESSION_MNG.execute_lambda_run(
[quotation_settings.DBType()],
[lambda s: self.qs_crud.soft_delete(s, setting_uuid)],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res