[wip] negodata/backend: 견적 마감 스케줄러
- scheduler/(__init__·jobs): 마감 처리·낙찰자 선정 잡 - quotation_crud·service: 스케줄러 연동 조회/마감 로직 - web_main·requirements·docker-compose·config: 스케줄러 기동 설정 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
ae103fd0f1
commit
5406094ea1
9 files changed
+383
-22
No files matched your search
@@ -7,10 +7,10 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from common.database.db_session_manager import DB_SESSION_MNG
|
||||
from common.database.model.models import (
|
||||
quotations, sessions, chats, nego_cards, wild_cards, items, quotation_settings,
|
||||
quotations, sessions, chats, nego_cards, wild_cards, items, suppliers, quotation_settings,
|
||||
version_nego_cards, version_wild_cards,
|
||||
)
|
||||
from common.enums import ErrorType
|
||||
from common.enums import ErrorType, QuotationStatus, QuotationType, SessionStatus
|
||||
from common.logger import LOG
|
||||
from common.utils.gtime import GTime
|
||||
|
||||
@@ -59,6 +59,10 @@ class IQuotationCRUD(ABC):
|
||||
async def update_quotation(self, cdb: AsyncSession, qt_id, data: dict) -> ErrorType:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def update_sessions_status(self, cdb: AsyncSession, qt_id, from_statuses: list[int], to_status: int) -> ErrorType:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def soft_delete(self, cdb: AsyncSession, qt_id) -> ErrorType:
|
||||
pass
|
||||
@@ -83,6 +87,27 @@ class IQuotationCRUD(ABC):
|
||||
async def item_map(self, cdb: AsyncSession, qt_ids) -> Tuple[ErrorType, dict]:
|
||||
pass
|
||||
|
||||
# ----- 스케줄러(크론) 전용 -----
|
||||
@abstractmethod
|
||||
async def list_due_for_close(self, cdb: AsyncSession, now) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def list_requote_done(self, cdb: AsyncSession) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def list_done_sessions(self, cdb: AsyncSession, qt_id) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def bulk_update_quotation_status(self, cdb: AsyncSession, qt_ids, status: int) -> ErrorType:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def bulk_update_sessions_status(self, cdb: AsyncSession, qt_ids, from_statuses: list[int], to_status: int) -> ErrorType:
|
||||
pass
|
||||
|
||||
|
||||
class QuotationCRUD(IQuotationCRUD):
|
||||
async def search(
|
||||
@@ -311,6 +336,23 @@ class QuotationCRUD(IQuotationCRUD):
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED
|
||||
|
||||
async def update_sessions_status(self, cdb: AsyncSession, qt_id, from_statuses: list[int], to_status: int) -> ErrorType:
|
||||
# 견적에 딸린 세션 중 from_statuses 에 속한 것만 to_status 로 일괄 전이(삭제 제외). 다른 상태는 건드리지 않는다.
|
||||
try:
|
||||
query = (
|
||||
update(sessions)
|
||||
.where(
|
||||
sessions.quotation_id == qt_id,
|
||||
sessions.status.in_(from_statuses),
|
||||
sessions.deleted == False, # noqa: E712
|
||||
)
|
||||
.values(status=to_status, updated_at=GTime.UTC())
|
||||
)
|
||||
return await DB_SESSION_MNG.add(cdb, query)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED
|
||||
|
||||
async def soft_delete(self, cdb: AsyncSession, qt_id) -> ErrorType:
|
||||
try:
|
||||
query = update(quotations).where(quotations.qt_id == qt_id).values(deleted=True, updated_at=GTime.UTC())
|
||||
@@ -319,6 +361,106 @@ class QuotationCRUD(IQuotationCRUD):
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED
|
||||
|
||||
# ----- 스케줄러(크론) 전용 -----
|
||||
async def list_due_for_close(self, cdb: AsyncSession, now) -> Tuple[ErrorType, list]:
|
||||
"""[잡①] 마감시각이 지났는데 아직 안 닫힌 견적 qt_id 목록.
|
||||
조건: end_time < now AND status != 견적마감 AND not deleted."""
|
||||
try:
|
||||
query = select(quotations.qt_id).where(
|
||||
quotations.end_time < now,
|
||||
quotations.status != QuotationStatus.CLOSED.value,
|
||||
quotations.deleted == False, # noqa: E712
|
||||
)
|
||||
err_type, rows = await DB_SESSION_MNG.execute(cdb, query)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
return err_type, []
|
||||
return ErrorType.SUCCESS, list(rows)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def list_requote_done(self, cdb: AsyncSession) -> Tuple[ErrorType, list]:
|
||||
"""[잡②] 재견적(REQUOTE) 중 협상완료(DONE) 세션이 1건 이상이고 아직 안 닫힌 견적 qt_id 목록.
|
||||
재견적은 세션이 독립적이라 하나라도 완료되면 나머지를 기다리지 않고 마감 대상."""
|
||||
try:
|
||||
done_exists = (
|
||||
select(sessions.session_id)
|
||||
.where(
|
||||
sessions.quotation_id == quotations.qt_id,
|
||||
sessions.status == SessionStatus.DONE.value,
|
||||
sessions.deleted == False, # noqa: E712
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
query = select(quotations.qt_id).where(
|
||||
quotations.type == QuotationType.REQUOTE.value,
|
||||
quotations.status != QuotationStatus.CLOSED.value,
|
||||
quotations.deleted == False, # noqa: E712
|
||||
done_exists,
|
||||
)
|
||||
err_type, rows = await DB_SESSION_MNG.execute(cdb, query)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
return err_type, []
|
||||
return ErrorType.SUCCESS, list(rows)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def list_done_sessions(self, cdb: AsyncSession, qt_id) -> Tuple[ErrorType, list]:
|
||||
"""[잡②] 견적의 협상완료(DONE) 세션 → (supplier_id, bid_price, supplier_name) 목록. 낙찰자 판정 입력."""
|
||||
try:
|
||||
query = (
|
||||
select(sessions.supplier_id, sessions.bid_price, suppliers.name)
|
||||
.join(suppliers, suppliers.supplier_id == sessions.supplier_id)
|
||||
.where(
|
||||
sessions.quotation_id == qt_id,
|
||||
sessions.status == SessionStatus.DONE.value,
|
||||
sessions.deleted == False, # noqa: E712
|
||||
suppliers.deleted == False, # noqa: E712
|
||||
)
|
||||
)
|
||||
err_type, rows = await DB_SESSION_MNG.execute(cdb, query)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
return err_type, []
|
||||
return ErrorType.SUCCESS, list(rows)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def bulk_update_quotation_status(self, cdb: AsyncSession, qt_ids, status: int) -> ErrorType:
|
||||
"""[잡①] 여러 견적의 status 를 한 번에 전이."""
|
||||
try:
|
||||
if not qt_ids:
|
||||
return ErrorType.SUCCESS
|
||||
query = (
|
||||
update(quotations)
|
||||
.where(quotations.qt_id.in_(qt_ids))
|
||||
.values(status=status, updated_at=GTime.UTC())
|
||||
)
|
||||
return await DB_SESSION_MNG.add(cdb, query)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED
|
||||
|
||||
async def bulk_update_sessions_status(self, cdb: AsyncSession, qt_ids, from_statuses: list[int], to_status: int) -> ErrorType:
|
||||
"""[잡①] 여러 견적에 딸린 세션 중 from_statuses 에 속한 것만 to_status 로 일괄 전이(삭제 제외)."""
|
||||
try:
|
||||
if not qt_ids:
|
||||
return ErrorType.SUCCESS
|
||||
query = (
|
||||
update(sessions)
|
||||
.where(
|
||||
sessions.quotation_id.in_(qt_ids),
|
||||
sessions.status.in_(from_statuses),
|
||||
sessions.deleted == False, # noqa: E712
|
||||
)
|
||||
.values(status=to_status, updated_at=GTime.UTC())
|
||||
)
|
||||
return await DB_SESSION_MNG.add(cdb, query)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED
|
||||
|
||||
# ----- 견적 상세: 세션 / 채팅 / 사용카드 (읽기 전용) -----
|
||||
async def list_sessions(self, cdb: AsyncSession, qt_id) -> Tuple[ErrorType, list]:
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user