[feat] negodata: 통계 인상억제율(재협상)·월별 차트 — 직전 라운드 투찰가 대비 인하율 파생 집계
This commit is contained in:
1 parent
e7a51b1b75
commit
e8d2acaade
10 files changed
+172
-4
No files matched your search
@@ -2,6 +2,7 @@ from abc import ABC, abstractmethod
|
||||
from typing import Tuple
|
||||
|
||||
from sqlalchemy import select, func, and_, case
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from common.database.db_session_manager import DB_SESSION_MNG
|
||||
@@ -54,6 +55,14 @@ class IStatisticsCRUD(ABC):
|
||||
async def regen_avg_round(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, float]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def markup_suppression(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, float]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def markup_suppression_monthly(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def card_usage(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
pass
|
||||
@@ -187,6 +196,87 @@ class StatisticsCRUD(IStatisticsCRUD):
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, 0.0
|
||||
|
||||
async def markup_suppression(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, float]:
|
||||
# 인상억제율(재협상 전용): 같은 견적번호(qt_number)의 직전 라운드 투찰가 대비 이번 라운드 투찰가가
|
||||
# 얼마나 안 올랐나 = avg((직전투찰 − 이번투찰) / 직전투찰). 양수=인하(억제 성공), 음수=인상 허용.
|
||||
# 직전·이번 둘 다 유효 투찰(bid_price)이 있는 재협상 쌍만 대상(직전이 개찰/거부면 비교 불가 → 제외).
|
||||
# 새 컬럼 없이 sessions.qt_number+qt_round+bid_price 로만 파생.
|
||||
try:
|
||||
prev = aliased(sessions)
|
||||
stmt = (
|
||||
select(func.avg((prev.bid_price - sessions.bid_price) * 1.0 / prev.bid_price))
|
||||
.select_from(sessions)
|
||||
.join(
|
||||
prev,
|
||||
and_(
|
||||
prev.qt_number == sessions.qt_number,
|
||||
prev.item_id == sessions.item_id,
|
||||
prev.supplier_id == sessions.supplier_id,
|
||||
prev.qt_round == sessions.qt_round - 1,
|
||||
prev.bid_price.isnot(None),
|
||||
prev.bid_price > 0,
|
||||
prev.deleted == False, # noqa: E712
|
||||
),
|
||||
)
|
||||
.join(quotations, quotations.qt_id == sessions.quotation_id)
|
||||
.where(
|
||||
and_(
|
||||
*_company_scope(company_id, owner),
|
||||
sessions.bid_price.isnot(None),
|
||||
sessions.qt_round >= 2,
|
||||
sessions.deleted == False, # noqa: E712
|
||||
quotations.updated_at >= since,
|
||||
)
|
||||
)
|
||||
)
|
||||
err, rows = await DB_SESSION_MNG.execute(cdb, stmt)
|
||||
if err != ErrorType.SUCCESS:
|
||||
return err, 0.0
|
||||
val = rows[0] if rows else None
|
||||
return ErrorType.SUCCESS, float(val) if val is not None else 0.0
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, 0.0
|
||||
|
||||
async def markup_suppression_monthly(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
# 월별 인상억제율: 이번 라운드 마감월(quotations.updated_at)별 avg((직전투찰 − 이번투찰)/직전투찰).
|
||||
try:
|
||||
prev = aliased(sessions)
|
||||
month = func.to_char(quotations.updated_at, "YYYY-MM")
|
||||
stmt = (
|
||||
select(month.label("m"), func.avg((prev.bid_price - sessions.bid_price) * 1.0 / prev.bid_price))
|
||||
.select_from(sessions)
|
||||
.join(
|
||||
prev,
|
||||
and_(
|
||||
prev.qt_number == sessions.qt_number,
|
||||
prev.item_id == sessions.item_id,
|
||||
prev.supplier_id == sessions.supplier_id,
|
||||
prev.qt_round == sessions.qt_round - 1,
|
||||
prev.bid_price.isnot(None),
|
||||
prev.bid_price > 0,
|
||||
prev.deleted == False, # noqa: E712
|
||||
),
|
||||
)
|
||||
.join(quotations, quotations.qt_id == sessions.quotation_id)
|
||||
.where(
|
||||
and_(
|
||||
*_company_scope(company_id, owner),
|
||||
sessions.bid_price.isnot(None),
|
||||
sessions.qt_round >= 2,
|
||||
sessions.deleted == False, # noqa: E712
|
||||
quotations.updated_at >= since,
|
||||
)
|
||||
)
|
||||
.group_by(month)
|
||||
.order_by(month)
|
||||
)
|
||||
err, rows = await DB_SESSION_MNG.execute(cdb, stmt)
|
||||
return (err, list(rows) if err == ErrorType.SUCCESS else [])
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def card_usage(self, cdb: AsyncSession, company_id, owner, since) -> Tuple[ErrorType, list]:
|
||||
# 카드 유형별 사용 빈도: card_used_yn=True 채팅을 card_type 별 집계(협상형 견적에서만 채팅 생성).
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user