补齐项目组负责人审批、费用信息、合作客户、生产出库菜单、申请单号 LCKY 前缀及配套 SQL 迁移,并同步相关前后端与单测。 Co-authored-by: Cursor <cursoragent@cursor.com>
188 lines
5.6 KiB
Python
188 lines
5.6 KiB
Python
"""合作客户 CRUD(biz_partner_customer)"""
|
||
from __future__ import annotations
|
||
|
||
import math
|
||
from datetime import datetime
|
||
from typing import Any, Dict, List, Optional, Tuple
|
||
|
||
from sqlalchemy import and_, func, or_
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.core.utils import generate_uuid
|
||
from app.models.models import BizPartnerCustomer
|
||
|
||
|
||
def _row_to_dict(row: BizPartnerCustomer) -> Dict[str, Any]:
|
||
return {
|
||
"id": row.id,
|
||
"customerName": row.customer_name or "",
|
||
"customerContact": row.customer_contact or "",
|
||
"customerPhone": row.customer_phone or "",
|
||
"tenantNo": row.tenant_no,
|
||
"orderNum": row.order_num if row.order_num is not None else 0,
|
||
"remark": row.remark or "",
|
||
"createTime": row.create_time.strftime("%Y-%m-%d %H:%M:%S") if row.create_time else "",
|
||
"updateTime": row.update_time.strftime("%Y-%m-%d %H:%M:%S") if row.update_time else "",
|
||
}
|
||
|
||
|
||
def list_partner_customers(
|
||
db: Session,
|
||
*,
|
||
page: int = 1,
|
||
page_size: int = 10,
|
||
keyword: Optional[str] = None,
|
||
tenant_no: Optional[str] = None,
|
||
) -> Tuple[List[Dict[str, Any]], int]:
|
||
def _filtered():
|
||
q0 = db.query(BizPartnerCustomer).filter(BizPartnerCustomer.del_flag == 0)
|
||
if tenant_no:
|
||
q0 = q0.filter(BizPartnerCustomer.tenant_no == tenant_no)
|
||
kw = (keyword or "").strip()
|
||
if kw:
|
||
like = f"%{kw}%"
|
||
q0 = q0.filter(
|
||
or_(
|
||
BizPartnerCustomer.customer_name.like(like),
|
||
BizPartnerCustomer.customer_contact.like(like),
|
||
BizPartnerCustomer.customer_phone.like(like),
|
||
)
|
||
)
|
||
return q0
|
||
|
||
total = _filtered().order_by(None).with_entities(func.count(BizPartnerCustomer.id)).scalar() or 0
|
||
page = max(1, page)
|
||
page_size = min(max(1, page_size), 200)
|
||
rows = (
|
||
_filtered()
|
||
.order_by(BizPartnerCustomer.create_time.desc())
|
||
.offset((page - 1) * page_size)
|
||
.limit(page_size)
|
||
.all()
|
||
)
|
||
return [_row_to_dict(x) for x in rows], int(total)
|
||
|
||
|
||
def get_partner_customer(
|
||
db: Session, customer_id: str, tenant_no: Optional[str] = None
|
||
) -> Optional[BizPartnerCustomer]:
|
||
q = db.query(BizPartnerCustomer).filter(
|
||
and_(BizPartnerCustomer.id == customer_id, BizPartnerCustomer.del_flag == 0)
|
||
)
|
||
if tenant_no:
|
||
q = q.filter(BizPartnerCustomer.tenant_no == tenant_no)
|
||
return q.first()
|
||
|
||
|
||
def create_partner_customer(
|
||
db: Session,
|
||
*,
|
||
customer_name: str,
|
||
customer_contact: str,
|
||
customer_phone: str,
|
||
tenant_no: str = "zkxw",
|
||
order_num: Optional[int] = None,
|
||
remark: Optional[str] = None,
|
||
user_id: Optional[str] = None,
|
||
create_dept_id: str = "",
|
||
commit: bool = True,
|
||
) -> BizPartnerCustomer:
|
||
name = (customer_name or "").strip()
|
||
contact = (customer_contact or "").strip()
|
||
phone = (customer_phone or "").strip()
|
||
if not name:
|
||
raise ValueError("客户名称不能为空")
|
||
if not contact:
|
||
raise ValueError("客户联系人不能为空")
|
||
if not phone:
|
||
raise ValueError("客户联系方式不能为空")
|
||
obj = BizPartnerCustomer(
|
||
id=generate_uuid(),
|
||
customer_name=name,
|
||
customer_contact=contact,
|
||
customer_phone=phone,
|
||
tenant_no=tenant_no,
|
||
order_num=order_num if order_num is not None else 0,
|
||
remark=(remark or "").strip() or None,
|
||
create_by=user_id,
|
||
create_dept_id=create_dept_id or "",
|
||
create_time=datetime.now(),
|
||
update_by=user_id,
|
||
update_time=datetime.now(),
|
||
del_flag=0,
|
||
)
|
||
db.add(obj)
|
||
if commit:
|
||
db.commit()
|
||
db.refresh(obj)
|
||
else:
|
||
db.flush()
|
||
return obj
|
||
|
||
|
||
def update_partner_customer(
|
||
db: Session,
|
||
customer_id: str,
|
||
*,
|
||
customer_name: Optional[str] = None,
|
||
customer_contact: Optional[str] = None,
|
||
customer_phone: Optional[str] = None,
|
||
order_num: Optional[int] = None,
|
||
remark: Optional[str] = None,
|
||
user_id: Optional[str] = None,
|
||
tenant_no: Optional[str] = None,
|
||
) -> BizPartnerCustomer:
|
||
obj = get_partner_customer(db, customer_id, tenant_no=tenant_no)
|
||
if not obj:
|
||
raise ValueError("合作客户不存在")
|
||
if customer_name is not None:
|
||
name = customer_name.strip()
|
||
if not name:
|
||
raise ValueError("客户名称不能为空")
|
||
obj.customer_name = name
|
||
if customer_contact is not None:
|
||
contact = customer_contact.strip()
|
||
if not contact:
|
||
raise ValueError("客户联系人不能为空")
|
||
obj.customer_contact = contact
|
||
if customer_phone is not None:
|
||
phone = customer_phone.strip()
|
||
if not phone:
|
||
raise ValueError("客户联系方式不能为空")
|
||
obj.customer_phone = phone
|
||
if order_num is not None:
|
||
obj.order_num = order_num
|
||
if remark is not None:
|
||
obj.remark = remark.strip() or None
|
||
obj.update_by = user_id
|
||
obj.update_time = datetime.now()
|
||
db.add(obj)
|
||
db.commit()
|
||
db.refresh(obj)
|
||
return obj
|
||
|
||
|
||
def soft_delete_partner_customer(
|
||
db: Session,
|
||
customer_id: str,
|
||
tenant_no: Optional[str] = None,
|
||
*,
|
||
commit: bool = True,
|
||
) -> None:
|
||
obj = get_partner_customer(db, customer_id, tenant_no=tenant_no)
|
||
if not obj:
|
||
raise ValueError("合作客户不存在")
|
||
obj.del_flag = 1
|
||
obj.update_time = datetime.now()
|
||
db.add(obj)
|
||
if commit:
|
||
db.commit()
|
||
else:
|
||
db.flush()
|
||
|
||
|
||
def total_pages(total: int, page_size: int) -> int:
|
||
if total <= 0:
|
||
return 0
|
||
return int(math.ceil(total / float(page_size)))
|