import datetime import random from typing import Union import pandas as pd from sqlalchemy import select, delete from tornado_swagger.model import register_swagger_model from wtforms import StringField, IntegerField, TextAreaField from wtforms.validators import Length import models from models.common_model import CommonModel from models.db_models import TD3iGovcTaskAttachment from paste.core.logging import echo_log from paste.rbac.rbac_user import RbacUser from paste.util.pagination import Pagination from paste.web.form import ModelForm class GovcTaskAttachmentForm(ModelForm): """ 工单附件表单验证类(完全映射 TD3iGovcTaskAttachment 字段)。 用于验证和处理市12345工单附件的创建/修改表单数据。 字段完全映射数据库表 t_d3i_govc_task_attachment 的字段结构。 """ # 基础信息 id = IntegerField('主键ID') task_id = IntegerField('关联工单主表ID', validators=[], # 外键非空由数据库约束,此处可补充自定义验证 description='关联工单主表ID(t_d3i_govc_task.id)') detail_id = IntegerField('关联工单详情ID', validators=[], description='关联工单详情ID(t_d3i_govc_task_detail.id)') name = StringField('附件名称', validators=[Length(max=500, message='附件名称长度不能超过500字符')]) attach_url = TextAreaField('附件地址', validators=[Length(max=65535, message='附件地址长度不能超过65535字符')]) type = StringField('附件类型', validators=[Length(max=64, message='附件类型长度不能超过64字符')]) created_at = StringField('创建时间', render_kw={'readonly': True}) created_by = StringField('创建者', validators=[Length(max=64, message='创建者长度不能超过64字符')]) updated_at = StringField('更新时间', render_kw={'readonly': True}) updated_by = StringField('更新者', validators=[Length(max=64, message='更新者长度不能超过64字符')]) def process(self, formdata=None, obj=None, **kwargs): """ 处理表单数据,在数据绑定前进行预处理。 主要功能: - 遍历所有表单字段 - 对字符串类型的值去除两端空白字符 - 调用父类的process方法继续处理 """ if formdata: for name, values in formdata.items(): if isinstance(values, list) and values: formdata[name] = [v.strip() if isinstance(v, str) else v for v in values] elif isinstance(values, str): formdata[name] = values.strip() super().process(formdata, obj, **kwargs) class GovcTaskAttachmentBase(TD3iGovcTaskAttachment, CommonModel): """ 工单附件基础类(完全映射 TD3iGovcTaskAttachment 字段)。 继承自数据库模型 TD3iGovcTaskAttachment 和通用模型 CommonModel。 封装所有与工单附件相关的通用操作方法。 """ FieldMapping = { 'id': 'id', 'task_id': 'task_id', 'detail_id': 'detail_id', 'name': 'name', 'attach_url': 'attach_url', 'type': 'type', 'created_at': 'created_at', 'created_by': 'created_by', 'updated_at': 'updated_at', 'updated_by': 'updated_by', } """ 工单附件数据映射 """ @classmethod async def is_exist(cls, task_id: int, detail_id: int, name: str): """ 检查工单附件记录是否已存在(根据工单ID+详情ID+附件名称)。 :param task_id: 关联工单主表ID :param detail_id: 关联工单详情ID :param name: 附件名称 :return: 存在返回对象,不存在返回None """ _query = select(cls).where( cls.task_id == task_id, cls.detail_id == detail_id, cls.name == name ) _attachment: cls = await cls.query_first(_query) return _attachment @classmethod async def search_base(cls, is_paging=True, **kwargs): """ 按参数搜索工单附件数据的基础方法。 支持字段: - 精确匹配:task_id, detail_id, type, created_by, updated_by - 模糊匹配:name, attach_url :param is_paging: 是否分页 :param kwargs: 查询参数 :key int page_number: 页码(缺省随机1~100) :key int page_size: 每页数量(缺省20) :key dict sort_clause: 排序配置,如 {'name': 'asc'} :key int task_id: 精确匹配关联工单主表ID :key int detail_id: 精确匹配关联工单详情ID :key str name: 模糊匹配附件名称 :key str attach_url: 模糊匹配附件地址 :key str type: 精确匹配附件类型 :key str created_by: 精确匹配创建者 :key str updated_by: 精确匹配更新者 :return: (DataFrame, Pagination) """ page_number = kwargs.get('page_number', random.randint(1, 100)) page_size = kwargs.get('page_size', 20) kwargs.update({'page_number': page_number, 'page_size': page_size}) # 模糊查询字段 _name_likes = { cls.name.key: '%{}%', cls.attach_url.key: '%{}%', } _query = select(cls).where( *cls.search_wheres(likes=_name_likes, **kwargs) ).group_by(cls.id) _paging = None if is_paging: _row_count = await cls.query_count(_query) _paging = Pagination(_row_count).paging(page_number, page_size) _data_query = _query.limit(page_size).offset(_paging.offset_size) else: _data_query = _query _sort_clause = cls.sort_clauses(kwargs.get('sort_clause', {})) if _sort_clause: _data_query = _data_query.order_by(*_sort_clause) else: _data_query = _data_query.order_by(cls.task_id, cls.detail_id, cls.name) _attachment_df = await cls.query_as_df(_data_query) if not _attachment_df.empty: _attachment_df.replace(models.EmptyInDF + models.EmptyDatetimeInDF, '', inplace=True) _attachment_df[cls.id.key] = _attachment_df[cls.id.key].astype(str) _attachment_df[cls.task_id.key] = _attachment_df[cls.task_id.key].astype(str) _attachment_df[cls.detail_id.key] = _attachment_df[cls.detail_id.key].astype(str) return _attachment_df, _paging @classmethod async def search(cls, **kwargs): """ 按参数搜索工单附件数据,返回分页格式数据。 """ _attachment_df, _paging = await cls.search_base(** kwargs) return { 'total': _paging.row_count, 'rows': _attachment_df.to_dict('records'), 'pagination': { 'page_number': _paging.page_number, 'page_count': _paging.page_count, 'page_size': _paging.page_size, }, } @classmethod async def exists_by_unique_key(cls, data_df: pd.DataFrame): """ 查找 data_df 中在数据库中已存在和不存在的记录。 根据 task_id + detail_id + name 组合判断唯一性。 :param data_df: 输入的数据框架,必须包含 task_id、detail_id、name 列 :return: (exists_df: pd.DataFrame, latest_df: pd.DataFrame) - exists_df: 在数据库中存在的记录(补充id字段) - latest_df: 在数据库中不存在的记录 """ if data_df.empty: return pd.DataFrame(), pd.DataFrame() # 校验必要列 required_cols = ['task_id', 'detail_id', 'name'] missing_cols = [col for col in required_cols if col not in data_df.columns] if missing_cols: echo_log(f"错误:exists_by_unique_key 要求输入数据必须包含 {missing_cols} 列") return pd.DataFrame(), data_df.copy() # 去重并构建查询条件 unique_keys = data_df[required_cols].drop_duplicates() if unique_keys.empty: return pd.DataFrame(), data_df.copy() # 构建批量查询条件 _query_conditions = [] for _, row in unique_keys.iterrows(): _query_conditions.append( (cls.task_id == row['task_id']) & (cls.detail_id == row['detail_id']) & (cls.name == row['name']) ) if not _query_conditions: return pd.DataFrame(), data_df.copy() # 查询数据库中已存在的记录 _query = select(cls.id, cls.task_id, cls.detail_id, cls.name).where( * _query_conditions ) exist_keys_df = await cls.query_as_df(_query) if exist_keys_df.empty: return pd.DataFrame(), data_df.copy() # 构建唯一键映射(task_id|detail_id|name -> id) exist_keys_df['unique_key'] = exist_keys_df.apply( lambda x: f"{x['task_id']}|{x['detail_id']}|{x['name']}", axis=1 ) data_df['unique_key'] = data_df.apply( lambda x: f"{x['task_id']}|{x['detail_id']}|{x['name']}", axis=1 ) key_to_id_map = dict(zip(exist_keys_df['unique_key'], exist_keys_df['id'])) # 划分存在/不存在的记录 mask_exists = data_df['unique_key'].isin(exist_keys_df['unique_key']) exists_df = data_df[mask_exists].copy() exists_df[cls.id.key] = exists_df['unique_key'].map(key_to_id_map) latest_df = data_df[~mask_exists].copy() # 清理临时列 for df in [exists_df, latest_df]: if 'unique_key' in df.columns: df.drop('unique_key', axis=1, inplace=True) return exists_df, latest_df @register_swagger_model class GovcTaskAttachment(GovcTaskAttachmentBase): """ 工单附件模型类(主业务类,完全继承 TD3iGovcTaskAttachment 字段)。 --- description: 市12345工单附件接口 type: object properties: id: description: 主键ID type: integer example: 1001 readOnly: true task_id: description: 关联工单主表ID type: integer example: 5001 required: true detail_id: description: 关联工单详情ID type: integer example: 6001 required: true name: description: 附件名称 type: string example: "现场照片.jpg" maxLength: 500 required: true attach_url: description: 附件地址 type: string example: "/uploads/2024/05/现场照片.jpg" maxLength: 65535 required: true type: description: 附件类型 type: string example: "image/jpeg" maxLength: 64 created_at: description: 创建时间,ISO格式的日期时间字符串 type: string format: date-time example: "2024-01-15 10:30:00" readOnly: true created_by: description: 创建者用户名 type: string example: "admin" maxLength: 64 readOnly: true updated_at: description: 修改时间,ISO格式的日期时间字符串 type: string format: date-time example: "2024-01-16 14:25:00" readOnly: true updated_by: description: 修改者用户名 type: string example: "editor" maxLength: 64 readOnly: true """ @classmethod async def create(cls, user: RbacUser = None, **kwargs): """ 创建新的工单附件记录。 业务流程: 1. 使用 GovcTaskAttachmentForm 验证表单数据完整性 2. 检查是否已存在相同 (task_id+detail_id+name) 的记录(避免重复提交) 3. 创建新附件对象 4. 设置创建者和更新者为当前用户 5. 保存到数据库 6. 返回创建的对象 :param RbacUser user: 操作用户对象 :param kwargs: 附件参数字典 :return: 新建附件对象 :rtype: GovcTaskAttachment :raises AssertionError: 当记录已存在时抛出 :raises ValidationError: 当表单验证失败时抛出 """ # 处理字符串字段去除空格 for _k, _v in kwargs.items(): if isinstance(_v, str): kwargs[_k] = _v.strip() _form = GovcTaskAttachmentForm(formdata=kwargs) _form.validate_form() # 检查是否已存在相同唯一键的记录 _existing = await cls.is_exist( task_id=_form.task_id.data, detail_id=_form.detail_id.data, name=_form.name.data ) assert _existing is None, "该工单下已存在同名附件,不能重复提交。" # 创建对象 _attachment = cls().copy_from_dict(_form.data, skip_none=True).before_save() if user: _attachment.created_by = user.username _attachment.updated_by = user.username await _attachment.async_save() return _attachment @classmethod async def delete(cls, attachment_id: Union[str, int]): """ 删除工单附件记录。 业务流程: 1. 根据ID查找记录 2. 验证存在性 3. 执行删除 :param attachment_id: 要删除的附件记录ID :return: 删除的记录对象 :rtype: GovcTaskAttachment :raises AssertionError: 当记录不存在时抛出 """ _attachment: cls = await cls.async_find_by_id(attachment_id) assert _attachment, f"根据 ID {attachment_id} 未找到工单附件记录。" _del_query = delete(cls).where(cls.id == _attachment.id) _del_count = (await cls.raw_execute(_del_query)).rowcount echo_log( f'已删除工单附件记录(工单ID:{_attachment.task_id},附件名称:{_attachment.name},ID:{_attachment.id}).') return _attachment @classmethod async def modify(cls, attachment_id: Union[str, int], user: RbacUser = None, **kwargs): """ 修改已有工单附件记录。 业务流程: 1. 处理字符串字段去除首尾空格 2. 使用 GovcTaskAttachmentForm 验证表单数据 3. 查询原记录 4. 验证存在性 5. 更新字段并设置更新者 6. 保存到数据库 7. 返回更新后的对象 :param attachment_id: 要修改的附件记录ID :param RbacUser user: 操作用户对象 :param kwargs: 需要更新的字段 :return: 修改后的附件对象 :rtype: GovcTaskAttachment :raises AssertionError: 当记录不存在时抛出 :raises ValidationError: 当表单验证失败时抛出 """ # 处理字符串字段去除空格 for _k, _v in kwargs.items(): if isinstance(_v, str): kwargs[_k] = _v.strip() # 表单验证 _form = GovcTaskAttachmentForm(formdata=kwargs) _form.validate_form() # 查询原记录 _attachment: cls = await cls.async_find_by_id(attachment_id) assert _attachment, f'查无此工单附件信息。' # 更新字段 _attachment.copy_from_dict(_form.data, skip_none=True).before_save() _attachment.updated_by = user.username if user else _attachment.updated_by await _attachment.async_save() return _attachment @classmethod async def create_batch(cls, data_df: pd.DataFrame, user: RbacUser = None): """ 批量创建工单附件记录(传入数据应为全新记录)。 :param data_df: 包含附件数据的 DataFrame :param user: 操作用户对象,用于设置 created_by / updated_by :return: 成功创建的数量 :rtype: int """ if data_df.empty: return 0 # 补充创建者/更新者信息 if user: data_df['created_by'] = user.username data_df['updated_by'] = user.username # 数据预处理(去空格) str_cols = ['name', 'attach_url', 'type', 'created_by', 'updated_by'] for col in str_cols: if col in data_df.columns: data_df[col] = data_df[col].apply(lambda x: x.strip() if isinstance(x, str) else x) records = data_df.to_dict('records') attachments = [cls().copy_from_dict(record, skip_none=True).before_save() for record in records] session = cls.get_aio_session() try: session.add_all(attachments) await session.commit() except Exception as e: await session.rollback() raise e finally: await session.close() echo_log(f"批量创建成功:创建 {len(attachments)} 条工单附件记录。") return len(attachments) @classmethod async def modify_batch(cls, data_df: pd.DataFrame, user: RbacUser = None): """ 批量修改已有工单附件记录。 :param data_df: 包含附件数据的 DataFrame(必须包含 id 列) :param user: 操作用户对象,用于设置 updated_by :return: 成功更新的数量 :rtype: int """ if data_df.empty: return 0 # 必须包含 id 列 if 'id' not in data_df.columns: echo_log(f"错误:modify_batch 要求输入数据必须包含 '{cls.id.key}' 列(主键)") return 0 # 数据预处理(去空格) str_cols = ['name', 'attach_url', 'type', 'updated_by'] for col in str_cols: if col in data_df.columns: data_df[col] = data_df[col].apply(lambda x: x.strip() if isinstance(x, str) else x) # 手动添加更新时间戳 data_df['updated_at'] = datetime.datetime.now() # 添加更新者信息 if user: data_df['updated_by'] = user.username # 转换为字典列表 update_data = data_df.to_dict('records') # 使用 bulk_update_mappings 批量更新 session = cls.get_aio_session() try: await session.run_sync( lambda sync_session: sync_session.bulk_update_mappings(cls, update_data) ) await session.commit() updated_count = len(update_data) except Exception as e: await session.rollback() raise e finally: await session.close() echo_log(f"批量修改成功:更新 {updated_count} 条工单附件记录。") return updated_count @classmethod async def save_batch(cls, data_df: pd.DataFrame, user: RbacUser = None): """ 批量保存工单附件数据,自动处理新建和更新。 :param data_df: 要保存的数据框架(需包含 task_id、detail_id、name 列) :param user: 操作用户对象 :return: (created_count, updated_count) 新建和更新的数量 """ # 筛选数据状态(按唯一键判断存在性) _exists_df, _latest_df = await cls.exists_by_unique_key(data_df) # 批量创建/更新 _created_count = await cls.create_batch(_latest_df, user) _updated_count = await cls.modify_batch(_exists_df, user) return _created_count, _updated_count