[refactor] 공급유형 기준을 상품-협력사 매핑으로 전환

This commit is contained in:
minaChoi committed 2026-07-07 17:05:07 +09:00
1 parent 9ac8fe6363
commit d21e7c10b0
35 files changed
+157 -333

No files matched your search

+11 -4
View File
@@ -24,7 +24,7 @@ from anchoring.constants import (
)
from anchoring.db import session_scope
from anchoring.log import LOG
from anchoring.models import Item, Quotation, Adjustment, Session
from anchoring.models import Item, Quotation, Adjustment, Session, SupplierItem
from anchoring.reader import get_latest_adjusted_value
from anchoring.redis_client import consume_failure_counts, ping, set_value
from anchoring.service import calc_anchoring_price, calc_price_range_index, evaluate_samples, judge_sample_type
@@ -76,9 +76,9 @@ async def _reconcile_cache() -> int:
async def _scan_pending(db, company_ids: list | None = None) -> list:
"""미처리 종료 재협상 세션 + 칸 해석 소스(supplier_type/company_id) 조인. §8 절차 1
"""미처리 종료 재협상 세션 + 칸 해석 소스(supply_type/company_id) 조인. §8 절차 1
조인 ON 절에 deleted 필터 — 소프트 삭제된 견적/상품의 세션은 칸 해석이 NULL 이 되어
조인 ON 절에 deleted 필터 — 소프트 삭제된 견적/상품/매핑의 세션은 칸 해석이 NULL 이 되어
제외 마킹(0)으로 정리된다(철회된 거래를 학습에 쓰지 않으면서 영구 재스캔도 방지).
company_ids: 대상 회사 한정(테스트·표적 수동 실행용). None = 전체.
"""
@@ -91,7 +91,7 @@ async def _scan_pending(db, company_ids: list | None = None) -> list:
Session.anchoring_price,
Session.anchoring_value,
Session.last_offer_price,
Quotation.supplier_type,
SupplierItem.supply_type.label("supplier_type"),
Item.company_id,
)
.join(
@@ -104,6 +104,13 @@ async def _scan_pending(db, company_ids: list | None = None) -> list:
(Item.item_id == Session.item_id) & Item.deleted.is_(False),
isouter=True,
)
.join(
SupplierItem,
(SupplierItem.item_id == Session.item_id)
& (SupplierItem.supplier_id == Session.supplier_id)
& SupplierItem.deleted.is_(False),
isouter=True,
)
.where(
Session.used_by_adjustment_id.is_(None),
Session.deleted.is_(False),
@@ -1,7 +1,7 @@
"""앵커링 도메인 상수 + 코드값(enum). 규범: docs/개발용.md §3.
backend 를 import 하지 않고 자체 보유한다(자립 모듈). 코드값은 프로젝트 컨벤션
(SMALLINT 1-based + 앱 enum 매핑)을 따르며 quotations.supplier_type 과 동일 코드다.
(SMALLINT 1-based + 앱 enum 매핑)을 따르며 supplier_items.supply_type 과 동일 코드다.
상수 변경은 정책 재확정 사안 — 코드에서 임의 조정 금지(§12).
"""
from enum import Enum
@@ -11,7 +11,7 @@ ANCHORING_VALUE_MIN = 10 # 하한 1%
ANCHORING_VALUE_MAX = 200 # 상한 20%
# 시작값은 상수가 아니라 정적 테이블(base_table)에서 로드 — 0.01/10 하드코딩 금지(§2)
# 유형별 조정폭 (올림·내림 대칭). 키 = quotations.supplier_type SMALLINT 코드
# 유형별 조정폭 (올림·내림 대칭). 키 = supplier_items.supply_type SMALLINT 코드
# ⚠️ 스왑 주의: 2=제조=±1%, 3=총판=±1.5% (v1.1 ENUM명 기준 표와 코드 순서가 다름)
ADJUSTMENT_STEP = {
1: 20, # 유통(DISTRIBUTION) ±2%
@@ -50,7 +50,7 @@ REDIS_SOCKET_TIMEOUT = 0.3 # 행(hang) 방지 — 초과 시 DB 폴백
class SupplierType(Enum):
"""협력사 유형 코드. quotation.quotations.supplier_type / anchoring.adjustments.supplier_type
"""협력사 유형 코드. partner.supplier_items.supply_type / anchoring.adjustments.supplier_type
(negodata SupplierType 과 동일 코드)"""
NONE = 0 # 미지정 — 앵커링 칸 구성 불가(집계 제외)
DISTRIBUTION = 1 # 유통
+13 -2
View File
@@ -2,7 +2,7 @@
- 소유(쓰기): anchoring.adjustments (append-only — UPDATE/DELETE 금지 §5)
- sessions 는 used_by_adjustment_id 마킹만 쓰기 가능(그 외 컬럼 수정 금지 §12).
quotations/items 는 읽기 전용 경량 매핑(집계에 필요한 컬럼만).
quotations/items/supplier_items 는 읽기 전용 경량 매핑(집계에 필요한 컬럼만).
"""
from sqlalchemy import BigInteger, Boolean, Column, DateTime, Integer, SmallInteger, text
from sqlalchemy.dialects.postgresql import JSONB, UUID
@@ -35,6 +35,7 @@ class Session(BASE):
session_id = Column(UUID(as_uuid=True), primary_key=True)
quotation_id = Column(UUID(as_uuid=True), nullable=False)
item_id = Column(UUID(as_uuid=True), nullable=False)
supplier_id = Column(UUID(as_uuid=True), nullable=False)
qt_type = Column(SmallInteger, nullable=False) # 1=재협상
target_price = Column(BigInteger, nullable=False)
anchoring_price = Column(BigInteger, nullable=True) # 박제 앵커가(판정 기준)
@@ -51,7 +52,6 @@ class Quotation(BASE):
__table_args__ = {"schema": "quotation"}
qt_id = Column(UUID(as_uuid=True), primary_key=True)
supplier_type = Column(SmallInteger, nullable=True) # NULL 이면 칸 구성 불가 → 제외
deleted = Column(Boolean, nullable=False)
@@ -62,3 +62,14 @@ class Item(BASE):
item_id = Column(UUID(as_uuid=True), primary_key=True)
company_id = Column(UUID(as_uuid=True), nullable=True) # 테넌트(갑) — NULL 이면 칸 구성 불가
deleted = Column(Boolean, nullable=False)
class SupplierItem(BASE):
__tablename__ = "supplier_items"
__table_args__ = {"schema": "partner"}
supplier_item_id = Column(UUID(as_uuid=True), primary_key=True)
supplier_id = Column(UUID(as_uuid=True), nullable=False)
item_id = Column(UUID(as_uuid=True), nullable=False)
supply_type = Column(SmallInteger, nullable=True) # 1유통/2제조/3총판. 0/NULL 이면 제외
deleted = Column(Boolean, nullable=False)
+9 -4
View File
@@ -100,13 +100,18 @@ class Seeder:
await db.execute(text(
"INSERT INTO quotation.quotations "
"(qt_id, user_id, qt_setting_id, version_id, name, number, type, round, status, "
" start_time, end_time, supplier_type) "
"VALUES (:qid, :uid, :sid, :vid, 'anchoring-it-test', :num, :qtype, 1, 3, now(), now(), :stype)"
" start_time, end_time) "
"VALUES (:qid, :uid, :sid, :vid, 'anchoring-it-test', :num, :qtype, 1, 3, now(), now())"
), {
"qid": qt_id, "uid": self.user_id, "sid": uuid.uuid4(), "vid": uuid.uuid4(),
"num": f"AT{uuid.uuid4().hex[:12]}", "qtype": qt_type, "stype": supplier_type,
"num": f"AT{uuid.uuid4().hex[:12]}", "qtype": qt_type,
})
session_id = uuid.uuid4()
supplier_id = uuid.uuid4()
await db.execute(text(
"INSERT INTO partner.supplier_items (supplier_item_id, supplier_id, item_id, supply_type) "
"VALUES (:siid, :supid, :iid, :stype)"
), {"siid": uuid.uuid4(), "supid": supplier_id, "iid": self.item_id, "stype": supplier_type or 0})
await db.execute(text(
"INSERT INTO negotiation.sessions "
"(session_id, quotation_id, item_id, supplier_id, qt_number, qt_round, qt_type, "
@@ -114,7 +119,7 @@ class Seeder:
" status, bid_price, end_time) "
"VALUES (:sid, :qid, :iid, :supid, 'AT-N', 1, :qtype, :tp, :ap, :value, :lop, :status, :bid, now())"
), {
"sid": session_id, "qid": qt_id, "iid": self.item_id, "supid": uuid.uuid4(),
"sid": session_id, "qid": qt_id, "iid": self.item_id, "supid": supplier_id,
"qtype": qt_type, "tp": target_price, "ap": anchoring_price, "value": anchoring_value,
"lop": last_offer_price, "status": status, "bid": bid_price,
})