"""工艺工序分类 CRUD(biz_process_category)""" 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 BizProcessCategory def _row_to_dict(c: BizProcessCategory) -> Dict[str, Any]: return { "id": c.id, "categoryCode": c.category_code, "categoryName": c.category_name, "tenantNo": c.tenant_no, "orderNum": c.order_num if c.order_num is not None else 0, "remark": c.remark or "", "createTime": c.create_time.strftime("%Y-%m-%d %H:%M:%S") if c.create_time else "", "updateTime": c.update_time.strftime("%Y-%m-%d %H:%M:%S") if c.update_time else "", } def list_process_categories( db: Session, *, page: int = 1, page_size: int = 10, keyword: Optional[str] = None, tenant_no: Optional[str] = None, user_info=None, ) -> Tuple[List[Dict[str, Any]], int]: def _filtered(): q0 = db.query(BizProcessCategory).filter(BizProcessCategory.del_flag == 0) if tenant_no: q0 = q0.filter(BizProcessCategory.tenant_no == tenant_no) kw = (keyword or "").strip() if kw: like = f"%{kw}%" q0 = q0.filter( or_( BizProcessCategory.category_code.like(like), BizProcessCategory.category_name.like(like), ) ) if user_info: from app.core.data_scope import apply_data_scope q0 = apply_data_scope(q0, BizProcessCategory, user_info, db, tenant_no) return q0 total = _filtered().order_by(None).with_entities(func.count(BizProcessCategory.id)).scalar() or 0 page = max(1, page) page_size = min(max(1, page_size), 100) rows = ( _filtered() .order_by(BizProcessCategory.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_process_category(db: Session, category_id: str) -> Optional[BizProcessCategory]: return ( db.query(BizProcessCategory) .filter(and_(BizProcessCategory.id == category_id, BizProcessCategory.del_flag == 0)) .first() ) def category_code_exists(db: Session, code: str, tenant_no: str, exclude_id: Optional[str] = None) -> bool: q = db.query(BizProcessCategory).filter( and_(BizProcessCategory.category_code == code.strip(), BizProcessCategory.tenant_no == tenant_no, BizProcessCategory.del_flag == 0) ) if exclude_id: q = q.filter(BizProcessCategory.id != exclude_id) return q.first() is not None def create_process_category( db: Session, *, category_code: str, category_name: str, tenant_no: str = "zkxw", order_num: Optional[int] = None, remark: Optional[str] = None, user_id: Optional[str] = None, ) -> BizProcessCategory: cc = (category_code or "").strip() if not cc: raise ValueError("分类编码不能为空") if category_code_exists(db, cc, tenant_no): raise ValueError("分类编码已存在") obj = BizProcessCategory( id=generate_uuid(), category_code=cc, category_name=(category_name or "").strip() or cc, 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_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_process_category( db: Session, category_id: str, *, category_code: Optional[str] = None, category_name: Optional[str] = None, order_num: Optional[int] = None, remark: Optional[str] = None, user_id: Optional[str] = None, ) -> BizProcessCategory: obj = get_process_category(db, category_id) if not obj: raise ValueError("工艺工序分类不存在") if category_code is not None: new_code = category_code.strip() if not new_code: raise ValueError("分类编码不能为空") if new_code != obj.category_code and category_code_exists(db, new_code, obj.tenant_no, exclude_id=category_id): raise ValueError("分类编码已存在") obj.category_code = new_code if category_name is not None: obj.category_name = (category_name or "").strip() or obj.category_name if order_num is not None: obj.order_num = order_num if remark is not None: obj.remark = (remark or "").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_process_category(db: Session, category_id: str) -> None: obj = get_process_category(db, category_id) if not obj: raise ValueError("工艺工序分类不存在") obj.del_flag = 1 obj.update_time = datetime.now() db.add(obj) db.commit() def total_pages(total: int, page_size: int) -> int: if total <= 0: return 0 return int(math.ceil(total / float(page_size)))