提交
This commit is contained in:
1 parent
e6072457fd
commit
be4fab78e1
578 files changed
+174415
-214
No files matched your search
@@ -13,6 +13,8 @@ from app.api.qc import router as qc_router
|
||||
from app.api.research_donation import router as research_donation_router
|
||||
from app.api.production import router as production_router
|
||||
from app.api.pilot_service import router as pilot_service_router
|
||||
from app.api.insurance_company import router as insurance_company_router
|
||||
from app.api.material import router as material_router
|
||||
|
||||
__all__ = [
|
||||
"public_router",
|
||||
@@ -29,4 +31,6 @@ __all__ = [
|
||||
"research_donation_router",
|
||||
"production_router",
|
||||
"pilot_service_router",
|
||||
"insurance_company_router",
|
||||
"material_router",
|
||||
]
|
||||
@@ -1,11 +1,12 @@
|
||||
"""细胞存储 — 采集协议单 / 审核 / 财务确认 / 分期预警 API"""
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from sqlalchemy import and_ as sa_and
|
||||
from sqlalchemy import and_ as sa_and, func as sa_func, or_ as sa_or
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.menu_guard import require_admin_or_super_admin
|
||||
@@ -13,7 +14,14 @@ from app.core.response import error_response, success_response
|
||||
from app.core.utils import parse_datetime
|
||||
from app.crud import collection_protocol as cp_svc
|
||||
from app.crud import customer as customer_crud
|
||||
from app.models.models import BizCollectionProtocol, BizCustomer, SysUser
|
||||
from app.models.models import (
|
||||
BizCollectionProtocol,
|
||||
BizCustomer,
|
||||
BizProtocolPartyCustomer,
|
||||
BizProtocolSample,
|
||||
BizProtocolStorageCert,
|
||||
SysUser,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/web/admin", tags=["细胞存储"])
|
||||
|
||||
@@ -657,3 +665,253 @@ async def post_refund_confirm(
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 储存证明管理 / 满月待办
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/storage-certs")
|
||||
async def list_storage_certs(
|
||||
page: int = Query(1, ge=1),
|
||||
pageSize: int = Query(10, ge=1, le=100, alias="pageSize"),
|
||||
keyword: Optional[str] = None,
|
||||
makeStatus: Optional[str] = Query(None, alias="makeStatus"),
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
q = (
|
||||
db.query(BizCollectionProtocol)
|
||||
.filter(
|
||||
BizCollectionProtocol.del_flag == 0,
|
||||
BizCollectionProtocol.tenant_no == tenant_no,
|
||||
BizCollectionProtocol.protocol_status == "sxz",
|
||||
)
|
||||
)
|
||||
kw = (keyword or "").strip()
|
||||
if kw:
|
||||
like = f"%{kw}%"
|
||||
q = q.filter(BizCollectionProtocol.protocol_no.like(like))
|
||||
|
||||
protocol_ids_sub = q.with_entities(BizCollectionProtocol.id).subquery()
|
||||
|
||||
sample_exists = (
|
||||
db.query(BizProtocolSample)
|
||||
.filter(
|
||||
BizProtocolSample.collection_protocol_id == protocol_ids_sub.c.id,
|
||||
BizProtocolSample.del_flag == 0,
|
||||
BizProtocolSample.sample_status == "yrk",
|
||||
)
|
||||
.limit(1)
|
||||
.exists()
|
||||
)
|
||||
|
||||
q = q.filter(sample_exists)
|
||||
rows_all = q.order_by(BizCollectionProtocol.create_time.desc()).all()
|
||||
|
||||
protocol_ids = [r.id for r in rows_all]
|
||||
pc_map: Dict[str, Any] = {}
|
||||
sc_map: Dict[str, Any] = {}
|
||||
sample_map: Dict[str, List[Dict[str, Any]]] = {}
|
||||
if protocol_ids:
|
||||
pc_rows = (
|
||||
db.query(BizProtocolPartyCustomer)
|
||||
.filter(
|
||||
BizProtocolPartyCustomer.collection_protocol_id.in_(protocol_ids),
|
||||
BizProtocolPartyCustomer.del_flag == 0,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for pc in pc_rows:
|
||||
pc_map[pc.collection_protocol_id] = pc
|
||||
|
||||
sc_rows = (
|
||||
db.query(BizProtocolStorageCert)
|
||||
.filter(
|
||||
BizProtocolStorageCert.collection_protocol_id.in_(protocol_ids),
|
||||
BizProtocolStorageCert.del_flag == 0,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for sc in sc_rows:
|
||||
sc_map[sc.collection_protocol_id] = sc
|
||||
|
||||
s_rows = (
|
||||
db.query(BizProtocolSample)
|
||||
.filter(
|
||||
BizProtocolSample.collection_protocol_id.in_(protocol_ids),
|
||||
BizProtocolSample.del_flag == 0,
|
||||
BizProtocolSample.sample_status == "yrk",
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for s in s_rows:
|
||||
sample_map.setdefault(s.collection_protocol_id, []).append(
|
||||
cp_svc._sample_to_dict(s)
|
||||
)
|
||||
|
||||
items: List[Dict[str, Any]] = []
|
||||
for r in rows_all:
|
||||
sc = sc_map.get(r.id)
|
||||
ms = (sc.make_status or "0") if sc else "0"
|
||||
if makeStatus and makeStatus.strip() and ms != makeStatus.strip():
|
||||
continue
|
||||
pc = pc_map.get(r.id)
|
||||
d: Dict[str, Any] = {
|
||||
"id": r.id,
|
||||
"protocolNo": r.protocol_no or "",
|
||||
"protocolStatus": r.protocol_status or "",
|
||||
"motherName": (pc.mother_name or "") if pc else "",
|
||||
"newbornDeliveryTime": cp_svc._fmt_dt(pc.newborn_delivery_time) if pc else "",
|
||||
"samples": sample_map.get(r.id, []),
|
||||
"makeStatus": ms,
|
||||
"certImageUrls": (sc.cert_image_urls or "") if sc else "",
|
||||
"makeDate": ((sc.make_date or "").strip() if sc.make_date else "") if sc else "",
|
||||
}
|
||||
items.append(d)
|
||||
|
||||
total = len(items)
|
||||
page_safe = max(1, page)
|
||||
page_size_safe = min(max(1, pageSize), 100)
|
||||
start = (page_safe - 1) * page_size_safe
|
||||
page_items = items[start : start + page_size_safe]
|
||||
tp = cp_svc.total_pages(total, page_size_safe)
|
||||
return success_response(
|
||||
{"list": page_items, "total": total, "page": page_safe, "pageSize": page_size_safe, "totalPages": tp}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/fullmoon-todos")
|
||||
async def list_fullmoon_todos(
|
||||
page: int = Query(1, ge=1),
|
||||
pageSize: int = Query(10, ge=1, le=100, alias="pageSize"),
|
||||
keyword: Optional[str] = None,
|
||||
todoStatus: Optional[str] = Query(None, alias="todoStatus"),
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
q = (
|
||||
db.query(BizCollectionProtocol, BizProtocolPartyCustomer)
|
||||
.join(
|
||||
BizProtocolPartyCustomer,
|
||||
sa_and(
|
||||
BizProtocolPartyCustomer.collection_protocol_id == BizCollectionProtocol.id,
|
||||
BizProtocolPartyCustomer.del_flag == 0,
|
||||
),
|
||||
)
|
||||
.filter(
|
||||
BizCollectionProtocol.del_flag == 0,
|
||||
BizCollectionProtocol.tenant_no == tenant_no,
|
||||
BizCollectionProtocol.protocol_status == "sxz",
|
||||
BizProtocolPartyCustomer.tenant_no == tenant_no,
|
||||
)
|
||||
)
|
||||
kw = (keyword or "").strip()
|
||||
if kw:
|
||||
like = f"%{kw}%"
|
||||
q = q.filter(
|
||||
sa_or(
|
||||
BizCollectionProtocol.protocol_no.like(like),
|
||||
BizProtocolPartyCustomer.mother_name.like(like),
|
||||
)
|
||||
)
|
||||
|
||||
rows = q.order_by(BizCollectionProtocol.create_time.desc()).all()
|
||||
|
||||
protocol_ids = [r.id for r, _ in rows]
|
||||
ft_map: Dict[str, Any] = {}
|
||||
if protocol_ids:
|
||||
from app.models.models import BizProtocolFullmoonTodo
|
||||
ft_rows = (
|
||||
db.query(BizProtocolFullmoonTodo)
|
||||
.filter(
|
||||
BizProtocolFullmoonTodo.collection_protocol_id.in_(protocol_ids),
|
||||
BizProtocolFullmoonTodo.tenant_no == tenant_no,
|
||||
BizProtocolFullmoonTodo.del_flag == 0,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for ft in ft_rows:
|
||||
ft_map[ft.collection_protocol_id] = ft
|
||||
|
||||
now = datetime.now()
|
||||
items: List[Dict[str, Any]] = []
|
||||
for proto, pc in rows:
|
||||
dt = pc.newborn_delivery_time
|
||||
if not dt:
|
||||
continue
|
||||
fullmoon_date = dt + timedelta(days=30)
|
||||
if fullmoon_date > now:
|
||||
continue
|
||||
ft = ft_map.get(proto.id)
|
||||
ts = (ft.todo_status or "0") if ft else "0"
|
||||
if todoStatus and todoStatus.strip() and ts != todoStatus.strip():
|
||||
continue
|
||||
d: Dict[str, Any] = {
|
||||
"id": proto.id,
|
||||
"protocolNo": proto.protocol_no or "",
|
||||
"protocolStatus": proto.protocol_status or "",
|
||||
"motherName": pc.mother_name or "",
|
||||
"motherPhone": pc.mother_phone or "",
|
||||
"newbornName": pc.newborn_name or "",
|
||||
"newbornDeliveryTime": cp_svc._fmt_dt(dt),
|
||||
"fullmoonDate": fullmoon_date.strftime("%Y-%m-%d"),
|
||||
}
|
||||
if ft:
|
||||
d["fullmoonTodo"] = cp_svc._fullmoon_todo_to_dict(ft)
|
||||
else:
|
||||
d["fullmoonTodo"] = None
|
||||
items.append(d)
|
||||
|
||||
total = len(items)
|
||||
page_safe = max(1, page)
|
||||
page_size_safe = min(max(1, pageSize), 100)
|
||||
start = (page_safe - 1) * page_size_safe
|
||||
page_items = items[start : start + page_size_safe]
|
||||
tp = cp_svc.total_pages(total, page_size_safe)
|
||||
return success_response(
|
||||
{"list": page_items, "total": total, "page": page_safe, "pageSize": page_size_safe, "totalPages": tp}
|
||||
)
|
||||
|
||||
|
||||
class FullmoonTodoInsuranceBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
insurance_company_id: Optional[str] = Field(None, alias="insuranceCompanyId")
|
||||
insurance_company_name: Optional[str] = Field(None, alias="insuranceCompanyName")
|
||||
insurance_type: Optional[str] = Field(None, alias="insuranceType")
|
||||
insurance_policy_no: Optional[str] = Field(None, alias="insurancePolicyNo")
|
||||
insurance_effective_date: Optional[str] = Field(None, alias="insuranceEffectiveDate")
|
||||
insurance_expiry_date: Optional[str] = Field(None, alias="insuranceExpiryDate")
|
||||
insurance_subscribed: Optional[str] = Field(None, alias="insuranceSubscribed")
|
||||
insurance_fee: Optional[float] = Field(None, alias="insuranceFee")
|
||||
insurance_attachments: Optional[str] = Field(None, alias="insuranceAttachments")
|
||||
todo_status: Optional[str] = Field(None, alias="todoStatus")
|
||||
handle_date: Optional[str] = Field(None, alias="handleDate")
|
||||
handle_remark: Optional[str] = Field(None, alias="handleRemark")
|
||||
|
||||
|
||||
@router.put("/fullmoon-todos/{protocol_id}/insurance")
|
||||
async def update_fullmoon_todo_insurance(
|
||||
protocol_id: str,
|
||||
body: FullmoonTodoInsuranceBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
user_id = _get_current_user_id(user)
|
||||
try:
|
||||
payload = body.model_dump(exclude_unset=True, by_alias=False)
|
||||
ft = cp_svc.upsert_fullmoon_todo(
|
||||
db,
|
||||
protocol_id,
|
||||
tenant_no,
|
||||
user_id,
|
||||
**payload,
|
||||
)
|
||||
return success_response(cp_svc._fullmoon_todo_to_dict(ft), message="更新成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
@@ -0,0 +1,832 @@
|
||||
"""保险公司管理 API(保险公司 + 开票信息 + 项目价格)"""
|
||||
import math
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.menu_guard import require_admin_or_super_admin
|
||||
from app.core.response import error_response, success_response
|
||||
from app.models.models import (
|
||||
BizInsuranceCompany,
|
||||
BizInsuranceCompanyInvoice,
|
||||
BizInsuranceProjectPrice,
|
||||
SysUser,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/web/admin", tags=["保险公司管理"])
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _get_tenant_no(db: Session, user: dict) -> str:
|
||||
user_id = user.get("user_id")
|
||||
if not user_id:
|
||||
raise PermissionError("用户身份信息缺失,无法确定租户")
|
||||
sys_user = db.query(SysUser).filter(SysUser.id == user_id).first()
|
||||
if not sys_user or not sys_user.tenant_no:
|
||||
raise PermissionError("用户租户信息缺失")
|
||||
return sys_user.tenant_no
|
||||
|
||||
|
||||
def _generate_uuid() -> str:
|
||||
return str(uuid.uuid4()).replace("-", "")
|
||||
|
||||
|
||||
def _fmt_dt(v) -> Optional[str]:
|
||||
if v is None:
|
||||
return None
|
||||
if isinstance(v, datetime):
|
||||
return v.strftime("%Y-%m-%d %H:%M:%S")
|
||||
return str(v)
|
||||
|
||||
|
||||
def _total_pages(total: int, page_size: int) -> int:
|
||||
return math.ceil(total / page_size) if page_size > 0 else 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Row-to-dict helpers (camelCase JSON)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _row_to_dict_company(row: BizInsuranceCompany) -> dict:
|
||||
return {
|
||||
"id": row.id,
|
||||
"companyName": row.company_name,
|
||||
"companyAddress": row.company_address,
|
||||
"contactName": row.contact_name,
|
||||
"contactPhone": row.contact_phone,
|
||||
"status": row.status,
|
||||
"tenantNo": row.tenant_no,
|
||||
"orderNum": row.order_num,
|
||||
"remark": row.remark,
|
||||
"createBy": row.create_by,
|
||||
"createDeptId": row.create_dept_id,
|
||||
"createTime": _fmt_dt(row.create_time),
|
||||
"updateBy": row.update_by,
|
||||
"updateTime": _fmt_dt(row.update_time),
|
||||
}
|
||||
|
||||
|
||||
def _row_to_dict_invoice(row: BizInsuranceCompanyInvoice) -> dict:
|
||||
return {
|
||||
"id": row.id,
|
||||
"insuranceCompanyId": row.insurance_company_id,
|
||||
"companyName": row.company_name,
|
||||
"taxNo": row.tax_no,
|
||||
"companyAddress": row.company_address,
|
||||
"companyPhone": row.company_phone,
|
||||
"bankName": row.bank_name,
|
||||
"bankAccount": row.bank_account,
|
||||
"tenantNo": row.tenant_no,
|
||||
"orderNum": row.order_num,
|
||||
"remark": row.remark,
|
||||
"createBy": row.create_by,
|
||||
"createDeptId": row.create_dept_id,
|
||||
"createTime": _fmt_dt(row.create_time),
|
||||
"updateBy": row.update_by,
|
||||
"updateTime": _fmt_dt(row.update_time),
|
||||
}
|
||||
|
||||
|
||||
def _row_to_dict_project_price(row: BizInsuranceProjectPrice) -> dict:
|
||||
return {
|
||||
"id": row.id,
|
||||
"insuranceCompanyId": row.insurance_company_id,
|
||||
"projectName": row.project_name,
|
||||
"projectFee": float(row.project_fee) if row.project_fee is not None else None,
|
||||
"status": row.status,
|
||||
"tenantNo": row.tenant_no,
|
||||
"orderNum": row.order_num,
|
||||
"remark": row.remark,
|
||||
"createBy": row.create_by,
|
||||
"createDeptId": row.create_dept_id,
|
||||
"createTime": _fmt_dt(row.create_time),
|
||||
"updateBy": row.update_by,
|
||||
"updateTime": _fmt_dt(row.update_time),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Inline CRUD — 保险公司主表
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _list_insurance_companies(
|
||||
db: Session, *, page: int, page_size: int, keyword: Optional[str], tenant_no: str
|
||||
):
|
||||
q = db.query(BizInsuranceCompany).filter(
|
||||
BizInsuranceCompany.tenant_no == tenant_no,
|
||||
BizInsuranceCompany.del_flag == 0,
|
||||
)
|
||||
if keyword:
|
||||
q = q.filter(BizInsuranceCompany.company_name.like(f"%{keyword}%"))
|
||||
total = q.count()
|
||||
rows = (
|
||||
q.order_by(BizInsuranceCompany.create_time.desc())
|
||||
.offset((page - 1) * page_size)
|
||||
.limit(page_size)
|
||||
.all()
|
||||
)
|
||||
return [_row_to_dict_company(r) for r in rows], total
|
||||
|
||||
|
||||
def _create_insurance_company(
|
||||
db: Session,
|
||||
*,
|
||||
company_name: str,
|
||||
company_address: Optional[str],
|
||||
contact_name: Optional[str],
|
||||
contact_phone: Optional[str],
|
||||
status: Optional[str],
|
||||
tenant_no: str,
|
||||
order_num: Optional[int],
|
||||
remark: Optional[str],
|
||||
user_id: str,
|
||||
) -> BizInsuranceCompany:
|
||||
obj = BizInsuranceCompany(
|
||||
id=_generate_uuid(),
|
||||
company_name=company_name,
|
||||
company_address=company_address,
|
||||
contact_name=contact_name,
|
||||
contact_phone=contact_phone,
|
||||
status=status or "1",
|
||||
tenant_no=tenant_no,
|
||||
order_num=order_num or 0,
|
||||
remark=remark,
|
||||
create_by=user_id,
|
||||
create_dept_id="",
|
||||
create_time=datetime.now(),
|
||||
update_by=user_id,
|
||||
update_time=datetime.now(),
|
||||
del_flag=0,
|
||||
)
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def _update_insurance_company(
|
||||
db: Session,
|
||||
row_id: str,
|
||||
*,
|
||||
company_name: Optional[str],
|
||||
company_address: Optional[str],
|
||||
contact_name: Optional[str],
|
||||
contact_phone: Optional[str],
|
||||
status: Optional[str],
|
||||
order_num: Optional[int],
|
||||
remark: Optional[str],
|
||||
user_id: str,
|
||||
tenant_no: str,
|
||||
) -> BizInsuranceCompany:
|
||||
obj = (
|
||||
db.query(BizInsuranceCompany)
|
||||
.filter(
|
||||
BizInsuranceCompany.id == row_id,
|
||||
BizInsuranceCompany.tenant_no == tenant_no,
|
||||
BizInsuranceCompany.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
raise ValueError("记录不存在")
|
||||
if company_name is not None:
|
||||
obj.company_name = company_name
|
||||
if company_address is not None:
|
||||
obj.company_address = company_address
|
||||
if contact_name is not None:
|
||||
obj.contact_name = contact_name
|
||||
if contact_phone is not None:
|
||||
obj.contact_phone = contact_phone
|
||||
if status is not None:
|
||||
obj.status = status
|
||||
if order_num is not None:
|
||||
obj.order_num = order_num
|
||||
if remark is not None:
|
||||
obj.remark = remark
|
||||
obj.update_by = user_id
|
||||
obj.update_time = datetime.now()
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def _soft_delete_insurance_company(db: Session, row_id: str, *, tenant_no: str):
|
||||
obj = (
|
||||
db.query(BizInsuranceCompany)
|
||||
.filter(
|
||||
BizInsuranceCompany.id == row_id,
|
||||
BizInsuranceCompany.tenant_no == tenant_no,
|
||||
BizInsuranceCompany.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
raise ValueError("记录不存在")
|
||||
obj.del_flag = 1
|
||||
db.commit()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Inline CRUD — 保险公司开票信息
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _list_insurance_company_invoices(
|
||||
db: Session, insurance_company_id: str, *, tenant_no: str
|
||||
):
|
||||
# Verify parent exists
|
||||
parent = (
|
||||
db.query(BizInsuranceCompany)
|
||||
.filter(
|
||||
BizInsuranceCompany.id == insurance_company_id,
|
||||
BizInsuranceCompany.tenant_no == tenant_no,
|
||||
BizInsuranceCompany.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not parent:
|
||||
raise ValueError("保险公司不存在")
|
||||
rows = (
|
||||
db.query(BizInsuranceCompanyInvoice)
|
||||
.filter(
|
||||
BizInsuranceCompanyInvoice.insurance_company_id == insurance_company_id,
|
||||
BizInsuranceCompanyInvoice.tenant_no == tenant_no,
|
||||
BizInsuranceCompanyInvoice.del_flag == 0,
|
||||
)
|
||||
.order_by(BizInsuranceCompanyInvoice.create_time.desc())
|
||||
.all()
|
||||
)
|
||||
return [_row_to_dict_invoice(r) for r in rows]
|
||||
|
||||
|
||||
def _create_insurance_company_invoice(
|
||||
db: Session,
|
||||
*,
|
||||
insurance_company_id: str,
|
||||
company_name: str,
|
||||
tax_no: Optional[str],
|
||||
company_address: Optional[str],
|
||||
company_phone: Optional[str],
|
||||
bank_name: Optional[str],
|
||||
bank_account: Optional[str],
|
||||
tenant_no: str,
|
||||
order_num: Optional[int],
|
||||
remark: Optional[str],
|
||||
user_id: str,
|
||||
) -> BizInsuranceCompanyInvoice:
|
||||
obj = BizInsuranceCompanyInvoice(
|
||||
id=_generate_uuid(),
|
||||
insurance_company_id=insurance_company_id,
|
||||
company_name=company_name,
|
||||
tax_no=tax_no,
|
||||
company_address=company_address,
|
||||
company_phone=company_phone,
|
||||
bank_name=bank_name,
|
||||
bank_account=bank_account,
|
||||
tenant_no=tenant_no,
|
||||
order_num=order_num or 0,
|
||||
remark=remark,
|
||||
create_by=user_id,
|
||||
create_dept_id="",
|
||||
create_time=datetime.now(),
|
||||
update_by=user_id,
|
||||
update_time=datetime.now(),
|
||||
del_flag=0,
|
||||
)
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def _update_insurance_company_invoice(
|
||||
db: Session,
|
||||
row_id: str,
|
||||
*,
|
||||
company_name: Optional[str],
|
||||
tax_no: Optional[str],
|
||||
company_address: Optional[str],
|
||||
company_phone: Optional[str],
|
||||
bank_name: Optional[str],
|
||||
bank_account: Optional[str],
|
||||
order_num: Optional[int],
|
||||
remark: Optional[str],
|
||||
user_id: str,
|
||||
tenant_no: str,
|
||||
) -> BizInsuranceCompanyInvoice:
|
||||
obj = (
|
||||
db.query(BizInsuranceCompanyInvoice)
|
||||
.filter(
|
||||
BizInsuranceCompanyInvoice.id == row_id,
|
||||
BizInsuranceCompanyInvoice.tenant_no == tenant_no,
|
||||
BizInsuranceCompanyInvoice.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
raise ValueError("记录不存在")
|
||||
if company_name is not None:
|
||||
obj.company_name = company_name
|
||||
if tax_no is not None:
|
||||
obj.tax_no = tax_no
|
||||
if company_address is not None:
|
||||
obj.company_address = company_address
|
||||
if company_phone is not None:
|
||||
obj.company_phone = company_phone
|
||||
if bank_name is not None:
|
||||
obj.bank_name = bank_name
|
||||
if bank_account is not None:
|
||||
obj.bank_account = bank_account
|
||||
if order_num is not None:
|
||||
obj.order_num = order_num
|
||||
if remark is not None:
|
||||
obj.remark = remark
|
||||
obj.update_by = user_id
|
||||
obj.update_time = datetime.now()
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def _soft_delete_insurance_company_invoice(db: Session, row_id: str, *, tenant_no: str):
|
||||
obj = (
|
||||
db.query(BizInsuranceCompanyInvoice)
|
||||
.filter(
|
||||
BizInsuranceCompanyInvoice.id == row_id,
|
||||
BizInsuranceCompanyInvoice.tenant_no == tenant_no,
|
||||
BizInsuranceCompanyInvoice.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
raise ValueError("记录不存在")
|
||||
obj.del_flag = 1
|
||||
db.commit()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Inline CRUD — 保险项目价格
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _list_insurance_project_prices(
|
||||
db: Session, insurance_company_id: str, *, tenant_no: str
|
||||
):
|
||||
# Verify parent exists
|
||||
parent = (
|
||||
db.query(BizInsuranceCompany)
|
||||
.filter(
|
||||
BizInsuranceCompany.id == insurance_company_id,
|
||||
BizInsuranceCompany.tenant_no == tenant_no,
|
||||
BizInsuranceCompany.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not parent:
|
||||
raise ValueError("保险公司不存在")
|
||||
rows = (
|
||||
db.query(BizInsuranceProjectPrice)
|
||||
.filter(
|
||||
BizInsuranceProjectPrice.insurance_company_id == insurance_company_id,
|
||||
BizInsuranceProjectPrice.tenant_no == tenant_no,
|
||||
BizInsuranceProjectPrice.del_flag == 0,
|
||||
)
|
||||
.order_by(BizInsuranceProjectPrice.create_time.desc())
|
||||
.all()
|
||||
)
|
||||
return [_row_to_dict_project_price(r) for r in rows]
|
||||
|
||||
|
||||
def _create_insurance_project_price(
|
||||
db: Session,
|
||||
*,
|
||||
insurance_company_id: str,
|
||||
project_name: str,
|
||||
project_fee: Optional[float],
|
||||
status: Optional[str],
|
||||
tenant_no: str,
|
||||
order_num: Optional[int],
|
||||
remark: Optional[str],
|
||||
user_id: str,
|
||||
) -> BizInsuranceProjectPrice:
|
||||
obj = BizInsuranceProjectPrice(
|
||||
id=_generate_uuid(),
|
||||
insurance_company_id=insurance_company_id,
|
||||
project_name=project_name,
|
||||
project_fee=project_fee,
|
||||
status=status or "1",
|
||||
tenant_no=tenant_no,
|
||||
order_num=order_num or 0,
|
||||
remark=remark,
|
||||
create_by=user_id,
|
||||
create_dept_id="",
|
||||
create_time=datetime.now(),
|
||||
update_by=user_id,
|
||||
update_time=datetime.now(),
|
||||
del_flag=0,
|
||||
)
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def _update_insurance_project_price(
|
||||
db: Session,
|
||||
row_id: str,
|
||||
*,
|
||||
project_name: Optional[str],
|
||||
project_fee: Optional[float],
|
||||
status: Optional[str],
|
||||
order_num: Optional[int],
|
||||
remark: Optional[str],
|
||||
user_id: str,
|
||||
tenant_no: str,
|
||||
) -> BizInsuranceProjectPrice:
|
||||
obj = (
|
||||
db.query(BizInsuranceProjectPrice)
|
||||
.filter(
|
||||
BizInsuranceProjectPrice.id == row_id,
|
||||
BizInsuranceProjectPrice.tenant_no == tenant_no,
|
||||
BizInsuranceProjectPrice.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
raise ValueError("记录不存在")
|
||||
if project_name is not None:
|
||||
obj.project_name = project_name
|
||||
if project_fee is not None:
|
||||
obj.project_fee = project_fee
|
||||
if status is not None:
|
||||
obj.status = status
|
||||
if order_num is not None:
|
||||
obj.order_num = order_num
|
||||
if remark is not None:
|
||||
obj.remark = remark
|
||||
obj.update_by = user_id
|
||||
obj.update_time = datetime.now()
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def _soft_delete_insurance_project_price(db: Session, row_id: str, *, tenant_no: str):
|
||||
obj = (
|
||||
db.query(BizInsuranceProjectPrice)
|
||||
.filter(
|
||||
BizInsuranceProjectPrice.id == row_id,
|
||||
BizInsuranceProjectPrice.tenant_no == tenant_no,
|
||||
BizInsuranceProjectPrice.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not obj:
|
||||
raise ValueError("记录不存在")
|
||||
obj.del_flag = 1
|
||||
db.commit()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1. 保险公司(主表)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class InsuranceCompanyCreateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
company_name: str = Field(..., alias="companyName")
|
||||
company_address: Optional[str] = Field(None, alias="companyAddress")
|
||||
contact_name: Optional[str] = Field(None, alias="contactName")
|
||||
contact_phone: Optional[str] = Field(None, alias="contactPhone")
|
||||
status: Optional[str] = Field("1", alias="status")
|
||||
order_num: Optional[int] = Field(0, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
class InsuranceCompanyUpdateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
company_name: Optional[str] = Field(None, alias="companyName")
|
||||
company_address: Optional[str] = Field(None, alias="companyAddress")
|
||||
contact_name: Optional[str] = Field(None, alias="contactName")
|
||||
contact_phone: Optional[str] = Field(None, alias="contactPhone")
|
||||
status: Optional[str] = Field(None, alias="status")
|
||||
order_num: Optional[int] = Field(None, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/insurance-companies")
|
||||
async def list_insurance_companies(
|
||||
page: int = Query(1, ge=1),
|
||||
pageSize: int = Query(10, ge=1, le=100, alias="pageSize"),
|
||||
keyword: Optional[str] = None,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
items, total = _list_insurance_companies(
|
||||
db, page=page, page_size=pageSize, keyword=keyword, tenant_no=tenant_no
|
||||
)
|
||||
tp = _total_pages(total, pageSize)
|
||||
return success_response(
|
||||
{"list": items, "total": total, "page": page, "pageSize": pageSize, "totalPages": tp}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/insurance-companies")
|
||||
async def create_insurance_company(
|
||||
body: InsuranceCompanyCreateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
obj = _create_insurance_company(
|
||||
db,
|
||||
company_name=body.company_name,
|
||||
company_address=body.company_address,
|
||||
contact_name=body.contact_name,
|
||||
contact_phone=body.contact_phone,
|
||||
status=body.status,
|
||||
tenant_no=tenant_no,
|
||||
order_num=body.order_num,
|
||||
remark=body.remark,
|
||||
user_id=user.get("user_id"),
|
||||
)
|
||||
return success_response(_row_to_dict_company(obj), message="创建成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.put("/insurance-companies/{row_id}")
|
||||
async def update_insurance_company(
|
||||
row_id: str,
|
||||
body: InsuranceCompanyUpdateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
payload: dict[str, Any] = body.model_dump(exclude_unset=True, by_alias=False)
|
||||
obj = _update_insurance_company(
|
||||
db,
|
||||
row_id,
|
||||
company_name=payload.get("company_name"),
|
||||
company_address=payload.get("company_address"),
|
||||
contact_name=payload.get("contact_name"),
|
||||
contact_phone=payload.get("contact_phone"),
|
||||
status=payload.get("status"),
|
||||
order_num=payload.get("order_num"),
|
||||
remark=payload.get("remark"),
|
||||
user_id=user.get("user_id"),
|
||||
tenant_no=tenant_no,
|
||||
)
|
||||
return success_response(_row_to_dict_company(obj), message="更新成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.delete("/insurance-companies/{row_id}")
|
||||
async def delete_insurance_company(
|
||||
row_id: str,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
_soft_delete_insurance_company(db, row_id, tenant_no=tenant_no)
|
||||
return success_response(None, message="删除成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2. 保险公司开票信息(子表)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class InsuranceCompanyInvoiceCreateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
company_name: str = Field(..., alias="companyName")
|
||||
tax_no: Optional[str] = Field(None, alias="taxNo")
|
||||
company_address: Optional[str] = Field(None, alias="companyAddress")
|
||||
company_phone: Optional[str] = Field(None, alias="companyPhone")
|
||||
bank_name: Optional[str] = Field(None, alias="bankName")
|
||||
bank_account: Optional[str] = Field(None, alias="bankAccount")
|
||||
order_num: Optional[int] = Field(0, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
class InsuranceCompanyInvoiceUpdateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
company_name: Optional[str] = Field(None, alias="companyName")
|
||||
tax_no: Optional[str] = Field(None, alias="taxNo")
|
||||
company_address: Optional[str] = Field(None, alias="companyAddress")
|
||||
company_phone: Optional[str] = Field(None, alias="companyPhone")
|
||||
bank_name: Optional[str] = Field(None, alias="bankName")
|
||||
bank_account: Optional[str] = Field(None, alias="bankAccount")
|
||||
order_num: Optional[int] = Field(None, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/insurance-companies/{company_id}/invoices")
|
||||
async def list_insurance_company_invoices(
|
||||
company_id: str,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
items = _list_insurance_company_invoices(db, company_id, tenant_no=tenant_no)
|
||||
return success_response(items)
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.post("/insurance-companies/{company_id}/invoices")
|
||||
async def create_insurance_company_invoice(
|
||||
company_id: str,
|
||||
body: InsuranceCompanyInvoiceCreateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
obj = _create_insurance_company_invoice(
|
||||
db,
|
||||
insurance_company_id=company_id,
|
||||
company_name=body.company_name,
|
||||
tax_no=body.tax_no,
|
||||
company_address=body.company_address,
|
||||
company_phone=body.company_phone,
|
||||
bank_name=body.bank_name,
|
||||
bank_account=body.bank_account,
|
||||
tenant_no=tenant_no,
|
||||
order_num=body.order_num,
|
||||
remark=body.remark,
|
||||
user_id=user.get("user_id"),
|
||||
)
|
||||
return success_response(_row_to_dict_invoice(obj), message="创建成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.put("/insurance-company-invoices/{invoice_id}")
|
||||
async def update_insurance_company_invoice(
|
||||
invoice_id: str,
|
||||
body: InsuranceCompanyInvoiceUpdateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
payload: dict[str, Any] = body.model_dump(exclude_unset=True, by_alias=False)
|
||||
obj = _update_insurance_company_invoice(
|
||||
db,
|
||||
invoice_id,
|
||||
company_name=payload.get("company_name"),
|
||||
tax_no=payload.get("tax_no"),
|
||||
company_address=payload.get("company_address"),
|
||||
company_phone=payload.get("company_phone"),
|
||||
bank_name=payload.get("bank_name"),
|
||||
bank_account=payload.get("bank_account"),
|
||||
order_num=payload.get("order_num"),
|
||||
remark=payload.get("remark"),
|
||||
user_id=user.get("user_id"),
|
||||
tenant_no=tenant_no,
|
||||
)
|
||||
return success_response(_row_to_dict_invoice(obj), message="更新成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.delete("/insurance-company-invoices/{invoice_id}")
|
||||
async def delete_insurance_company_invoice(
|
||||
invoice_id: str,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
_soft_delete_insurance_company_invoice(db, invoice_id, tenant_no=tenant_no)
|
||||
return success_response(None, message="删除成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3. 保险项目价格(子表)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class InsuranceProjectPriceCreateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
project_name: str = Field(..., alias="projectName")
|
||||
project_fee: Optional[float] = Field(None, alias="projectFee")
|
||||
status: Optional[str] = Field("1", alias="status")
|
||||
order_num: Optional[int] = Field(0, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
class InsuranceProjectPriceUpdateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
project_name: Optional[str] = Field(None, alias="projectName")
|
||||
project_fee: Optional[float] = Field(None, alias="projectFee")
|
||||
status: Optional[str] = Field(None, alias="status")
|
||||
order_num: Optional[int] = Field(None, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/insurance-companies/{company_id}/project-prices")
|
||||
async def list_insurance_project_prices(
|
||||
company_id: str,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
items = _list_insurance_project_prices(db, company_id, tenant_no=tenant_no)
|
||||
return success_response(items)
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.post("/insurance-companies/{company_id}/project-prices")
|
||||
async def create_insurance_project_price(
|
||||
company_id: str,
|
||||
body: InsuranceProjectPriceCreateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
obj = _create_insurance_project_price(
|
||||
db,
|
||||
insurance_company_id=company_id,
|
||||
project_name=body.project_name,
|
||||
project_fee=body.project_fee,
|
||||
status=body.status,
|
||||
tenant_no=tenant_no,
|
||||
order_num=body.order_num,
|
||||
remark=body.remark,
|
||||
user_id=user.get("user_id"),
|
||||
)
|
||||
return success_response(_row_to_dict_project_price(obj), message="创建成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.put("/insurance-project-prices/{price_id}")
|
||||
async def update_insurance_project_price(
|
||||
price_id: str,
|
||||
body: InsuranceProjectPriceUpdateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
payload: dict[str, Any] = body.model_dump(exclude_unset=True, by_alias=False)
|
||||
obj = _update_insurance_project_price(
|
||||
db,
|
||||
price_id,
|
||||
project_name=payload.get("project_name"),
|
||||
project_fee=payload.get("project_fee"),
|
||||
status=payload.get("status"),
|
||||
order_num=payload.get("order_num"),
|
||||
remark=payload.get("remark"),
|
||||
user_id=user.get("user_id"),
|
||||
tenant_no=tenant_no,
|
||||
)
|
||||
return success_response(_row_to_dict_project_price(obj), message="更新成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.delete("/insurance-project-prices/{price_id}")
|
||||
async def delete_insurance_project_price(
|
||||
price_id: str,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
_soft_delete_insurance_project_price(db, price_id, tenant_no=tenant_no)
|
||||
return success_response(None, message="删除成功")
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
@@ -0,0 +1,226 @@
|
||||
"""市场物料库 API"""
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.sample_reception import _get_current_user_id, _get_tenant_no, _page_payload
|
||||
from app.core.database import get_db
|
||||
from app.core.menu_guard import require_admin_or_super_admin, require_menu_code
|
||||
from app.core.response import error_response, success_response
|
||||
from app.crud import material as mat_svc
|
||||
from app.crud import material_stock_log as msl_svc
|
||||
|
||||
router = APIRouter(prefix="/web/admin", tags=["市场物料"])
|
||||
|
||||
|
||||
# ---------- Body models ----------
|
||||
|
||||
|
||||
class MaterialBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
material_code: str = Field(..., alias="materialCode")
|
||||
material_name: str = Field(..., alias="materialName")
|
||||
category: Optional[str] = None
|
||||
spec_model: Optional[str] = Field(None, alias="specModel")
|
||||
unit: Optional[str] = None
|
||||
status: str = "1"
|
||||
order_num: Optional[int] = Field(None, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
image_urls: Optional[str] = Field(None, alias="imageUrls")
|
||||
|
||||
|
||||
class MaterialUpdateBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
material_code: Optional[str] = Field(None, alias="materialCode")
|
||||
material_name: Optional[str] = Field(None, alias="materialName")
|
||||
category: Optional[str] = None
|
||||
spec_model: Optional[str] = Field(None, alias="specModel")
|
||||
unit: Optional[str] = None
|
||||
status: Optional[str] = None
|
||||
order_num: Optional[int] = Field(None, alias="orderNum")
|
||||
remark: Optional[str] = None
|
||||
image_urls: Optional[str] = Field(None, alias="imageUrls")
|
||||
|
||||
|
||||
class StockInBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
qty: float = Field(..., gt=0)
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
class StockOutBody(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
qty: float = Field(..., gt=0)
|
||||
receiver: Optional[str] = None
|
||||
purpose: Optional[str] = None
|
||||
remark: Optional[str] = None
|
||||
|
||||
|
||||
# ---------- 物料 CRUD ----------
|
||||
|
||||
|
||||
@router.get("/materials")
|
||||
async def list_materials(
|
||||
page: int = Query(1, ge=1),
|
||||
pageSize: int = Query(10, ge=1, le=100),
|
||||
keyword: Optional[str] = None,
|
||||
category: Optional[str] = None,
|
||||
status: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
items, total = mat_svc.list_materials(
|
||||
db, tenant_no, page=page, page_size=pageSize,
|
||||
keyword=keyword, category=category, status=status,
|
||||
)
|
||||
return success_response(_page_payload(items, total, page, pageSize))
|
||||
except PermissionError as e:
|
||||
return error_response(str(e), code=403)
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
@router.get("/materials/options")
|
||||
async def material_options(
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
return success_response(mat_svc.options_list(db, tenant_no=tenant_no))
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
@router.post("/materials")
|
||||
async def create_material(
|
||||
body: MaterialBody,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
user_id = _get_current_user_id(user)
|
||||
obj = mat_svc.create_material(
|
||||
db, body.model_dump(by_alias=True, exclude_none=True),
|
||||
tenant_no=tenant_no, user_id=user_id,
|
||||
)
|
||||
return success_response(obj, message="创建成功")
|
||||
except ValueError as e:
|
||||
return error_response(str(e))
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
@router.put("/materials/{material_id}")
|
||||
async def update_material(
|
||||
material_id: str,
|
||||
body: MaterialUpdateBody,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
user_id = _get_current_user_id(user)
|
||||
obj = mat_svc.update_material(
|
||||
db, material_id, body.model_dump(by_alias=True, exclude_none=True),
|
||||
tenant_no=tenant_no, user_id=user_id,
|
||||
)
|
||||
return success_response(obj, message="更新成功")
|
||||
except ValueError as e:
|
||||
return error_response(str(e))
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
@router.delete("/materials/{material_id}")
|
||||
async def delete_material(
|
||||
material_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
user_id = _get_current_user_id(user)
|
||||
mat_svc.soft_delete_material(db, material_id, tenant_no=tenant_no, user_id=user_id)
|
||||
return success_response(None, message="删除成功")
|
||||
except ValueError as e:
|
||||
return error_response(str(e))
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
@router.post("/materials/{material_id}/stock-in")
|
||||
async def stock_in_material(
|
||||
material_id: str,
|
||||
body: StockInBody,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
user_id = _get_current_user_id(user)
|
||||
obj = mat_svc.stock_in(
|
||||
db, material_id, qty=body.qty, remark=body.remark,
|
||||
tenant_no=tenant_no, user_id=user_id,
|
||||
)
|
||||
return success_response(obj, message="入库成功")
|
||||
except ValueError as e:
|
||||
return error_response(str(e))
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
@router.post("/materials/{material_id}/stock-out")
|
||||
async def stock_out_material(
|
||||
material_id: str,
|
||||
body: StockOutBody,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:material:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
user_id = _get_current_user_id(user)
|
||||
obj = mat_svc.stock_out(
|
||||
db, material_id, qty=body.qty, receiver=body.receiver,
|
||||
purpose=body.purpose, remark=body.remark,
|
||||
tenant_no=tenant_no, user_id=user_id,
|
||||
)
|
||||
return success_response(obj, message="出库成功")
|
||||
except ValueError as e:
|
||||
return error_response(str(e))
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
|
||||
|
||||
# ---------- 库存变动记录 ----------
|
||||
|
||||
|
||||
@router.get("/material-stock-logs")
|
||||
async def list_stock_logs(
|
||||
page: int = Query(1, ge=1),
|
||||
pageSize: int = Query(10, ge=1, le=100),
|
||||
materialId: Optional[str] = None,
|
||||
changeType: Optional[str] = None,
|
||||
startTime: Optional[str] = None,
|
||||
endTime: Optional[str] = None,
|
||||
keyword: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: dict = Depends(require_menu_code("biz:materialStockLog:list")),
|
||||
):
|
||||
try:
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
items, total = msl_svc.list_stock_logs(
|
||||
db, tenant_no, page=page, page_size=pageSize,
|
||||
material_id=materialId, change_type=changeType,
|
||||
start_time=startTime, end_time=endTime, keyword=keyword,
|
||||
)
|
||||
return success_response(_page_payload(items, total, page, pageSize))
|
||||
except PermissionError as e:
|
||||
return error_response(str(e), code=403)
|
||||
except Exception as e:
|
||||
return error_response(str(e))
|
||||
+1
-13
@@ -6,20 +6,8 @@ from app.core.response import success_response
|
||||
|
||||
router = APIRouter(prefix="/web/menu", tags=["菜单管理"])
|
||||
|
||||
# 与 web-admin/src/router/modules/sqxp.ts 一致:工作台、系统管理、基础信息管理 等
|
||||
# 与 web-admin/src/router/modules/sqxp.ts 一致:系统管理、基础信息管理 等
|
||||
STATIC_MENU_TREE = [
|
||||
{
|
||||
"id": "10000000000000000000000000000001_child",
|
||||
"name": "Console",
|
||||
"path": "/console",
|
||||
"component": "/dashboard/console/index",
|
||||
"meta": {
|
||||
"title": "工作台",
|
||||
"keepAlive": True,
|
||||
"fixedTab": True,
|
||||
"menuCode": "dashboard",
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "100",
|
||||
"name": "System",
|
||||
|
||||
Reference in new issue
Block a user