refactor: P3c 服务层迁入 src/modules/ → service.py(29 服务)
- 22 个单服务 + 3 组合并(config+config_item、dict+dict_item、login_log+operation_log) - 特殊命名:job_executor→executor、scheduler→scheduler_service、 mail_svc→mail_service、sms_svc→sms_service(含使用处标识符改写) - 重写引用:跨服务(module 别名/子模块)、models/forms→src.modules、 db/extensions→src.core、utils→src.common|core、R→src.core.response - application/services/ 全部替换为 shim 【验收】全部服务文件编译通过;应用启动;pytest 5 passed;核心服务导入正常 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
2586c0aace
commit
87aec4e26e
@@ -1,400 +1,8 @@
|
||||
"""
|
||||
参数管理服务模块
|
||||
兼容 shim(绞杀者迁移期间保留)
|
||||
=============================================================
|
||||
|
||||
该模块实现了参数管理相关的所有业务逻辑。
|
||||
包括参数的分页查询、详情查询、添加、更新、删除、状态设置、导入导出等功能。
|
||||
参数用于管理系统配置和业务配置,每个参数包含名称、编码、值、类型、排序、状态等属性。
|
||||
|
||||
命名规范:
|
||||
- 函数参数使用驼峰命名(如 paramId, pageNo)
|
||||
- 表单字段使用下划线命名(如 name, code, sort)- 与数据库字段保持一致
|
||||
- 返回数据使用驼峰命名(如 createUser, updateTime)- 与前端规范保持一致
|
||||
- 私有函数以下划线开头(如 _apply_filters, _build_page_data)
|
||||
|
||||
功能模块:
|
||||
1. 分页查询:支持按名称模糊搜索、编码模糊搜索、类型筛选、状态筛选,按sort字段排序
|
||||
2. 详情查询:根据ID获取单个参数的详细信息
|
||||
3. 数据维护:参数的添加、更新、删除(支持批量)
|
||||
4. 状态管理:单独更新参数的启用/禁用状态
|
||||
5. Excel导入:批量导入参数数据
|
||||
6. Excel导出:导出参数数据到Excel文件
|
||||
7. 数据列表:获取所有启用的参数列表(用于下拉选择等场景)
|
||||
8. 根据编码获取参数值:通过参数编码获取对应的参数值
|
||||
服务已迁移至 src.modules.param.service。
|
||||
本文件仅作兼容转发,最终在 P7(切尾)阶段删除。
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from flask import request
|
||||
from sqlalchemy import and_
|
||||
|
||||
from application.constants.common import PAGE_SIZE
|
||||
from application.forms.param import ParamForm, ParamStatusForm
|
||||
from application.models.param import Param
|
||||
from extensions import db
|
||||
from utils import R, regular
|
||||
from utils.model_utils import models_to_list, model_to_dict
|
||||
from utils.request import parse_request_body, parse_id_list, like_contains
|
||||
from utils.security import get_username
|
||||
|
||||
# 获取日志记录器
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数分页查询
|
||||
# ============================================================
|
||||
|
||||
def get_param_page():
|
||||
"""
|
||||
查询参数分页数据
|
||||
|
||||
Args:
|
||||
request: Flask request对象,包含查询参数
|
||||
- pageNo: 当前页码(可选,默认为1)
|
||||
- pageSize: 每页记录数(可选,默认为PAGE_SIZE)
|
||||
- name: 参数名称(可选,模糊查询)
|
||||
- code: 参数编码(可选,模糊查询)
|
||||
- type: 参数类型(可选,精确匹配)
|
||||
- status: 参数状态(可选,精确匹配)
|
||||
|
||||
Returns:
|
||||
R: 包含分页数据的JSON响应,格式为:
|
||||
{
|
||||
"records": 参数列表,
|
||||
"total": 总记录数,
|
||||
"size": 每页大小,
|
||||
"current": 当前页码,
|
||||
"pages": 总页数
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# 获取分页参数
|
||||
page_no = int(request.args.get('pageNo', 1))
|
||||
page_size = int(request.args.get('pageSize', PAGE_SIZE))
|
||||
|
||||
# 应用筛选条件
|
||||
query = _apply_filters()
|
||||
|
||||
# 记录总数
|
||||
total = query.count()
|
||||
# 计算总页数
|
||||
pages = (total + page_size - 1) // page_size if total > 0 else 0
|
||||
|
||||
# 分页查询
|
||||
param_list = query.limit(page_size).offset((page_no - 1) * page_size).all()
|
||||
# 构建返回数据
|
||||
page_data = _build_page_data(param_list, total, page_size, page_no, pages)
|
||||
return R.ok(data=page_data)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"查询参数分页数据异常: {str(e)}")
|
||||
return R.failed(msg="查询失败,请稍后重试")
|
||||
|
||||
|
||||
def _apply_filters():
|
||||
"""
|
||||
应用查询筛选条件
|
||||
|
||||
根据请求参数动态构建查询条件:
|
||||
- 自动过滤已删除记录(is_delete == 0)
|
||||
- 支持参数名称模糊查询
|
||||
- 支持参数编码模糊查询
|
||||
- 支持参数类型精确查询
|
||||
- 支持参数状态精确查询
|
||||
- 默认按排序值升序排列
|
||||
|
||||
Returns:
|
||||
Query: 应用筛选后的SQLAlchemy查询对象
|
||||
"""
|
||||
# 基础查询:过滤已删除记录
|
||||
query = Param.query.filter(Param.is_delete == 0)
|
||||
|
||||
# 参数名称模糊筛选
|
||||
name = request.args.get('name')
|
||||
if name:
|
||||
query = query.filter(Param.name.like(like_contains(name), escape='\\'))
|
||||
|
||||
# 参数编码模糊筛选
|
||||
code = request.args.get('code')
|
||||
if code:
|
||||
query = query.filter(Param.code.like(like_contains(code), escape='\\'))
|
||||
|
||||
# 参数类型精确筛选
|
||||
param_type = request.args.get('type')
|
||||
if param_type:
|
||||
query = query.filter(Param.type == param_type)
|
||||
|
||||
# 参数状态精确筛选
|
||||
status = request.args.get('status')
|
||||
if status:
|
||||
query = query.filter(Param.status == status)
|
||||
|
||||
# 按排序值升序排列
|
||||
return query.order_by(Param.sort.asc())
|
||||
|
||||
|
||||
def _build_page_data(param_list, total, page_size, page_no, pages):
|
||||
"""
|
||||
构建分页返回数据
|
||||
|
||||
Args:
|
||||
param_list: 参数对象列表
|
||||
total: 总记录数
|
||||
page_size: 每页大小
|
||||
page_no: 当前页码
|
||||
pages: 总页数
|
||||
|
||||
Returns:
|
||||
dict: 分页数据,包含 records、total、size、current、pages
|
||||
"""
|
||||
# 将模型对象列表转换为字典列表
|
||||
records = models_to_list(param_list)
|
||||
|
||||
return {
|
||||
'records': records,
|
||||
'total': total,
|
||||
'size': page_size,
|
||||
'current': page_no,
|
||||
'pages': pages
|
||||
}
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数详情查询
|
||||
# ============================================================
|
||||
|
||||
def get_param_detail(param_id):
|
||||
"""
|
||||
根据ID查询参数详情(返回字典格式)
|
||||
|
||||
此方法调用 get_info 获取模型对象,然后转换为字典格式返回。
|
||||
适用于前端接口调用。
|
||||
|
||||
Args:
|
||||
param_id: 参数ID
|
||||
|
||||
Returns:
|
||||
dict/None: 参数详情字典,不存在时返回 None
|
||||
"""
|
||||
try:
|
||||
# 复用 get_info 方法查询参数
|
||||
param = get_info(param_id)
|
||||
if not param:
|
||||
return None
|
||||
# 转换为字典格式
|
||||
return model_to_dict(param)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"查询参数详情异常,ID: {param_id}, 错误: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数信息查询(根据ID)- 核心查询方法
|
||||
# ============================================================
|
||||
|
||||
def get_info(param_id: int):
|
||||
"""
|
||||
根据ID查询参数信息(返回模型对象)
|
||||
|
||||
这是一个通用的参数查询方法,返回参数模型对象而非字典。
|
||||
与 get_param_detail 的区别:
|
||||
- get_param_detail: 返回字典格式,用于前端接口
|
||||
- get_info: 返回模型对象,用于内部服务调用
|
||||
|
||||
此方法被以下方法复用:
|
||||
- get_param_detail: 获取详情字典
|
||||
- update_param: 验证参数是否存在
|
||||
- update_param_status: 验证参数是否存在
|
||||
|
||||
Args:
|
||||
param_id: 参数ID
|
||||
|
||||
Returns:
|
||||
Param/None: 参数模型对象,不存在或已删除时返回 None
|
||||
|
||||
Example:
|
||||
param = get_info(param_id)
|
||||
if param:
|
||||
print(param.name)
|
||||
"""
|
||||
# 参数有效性验证
|
||||
if not param_id or param_id <= 0:
|
||||
return None
|
||||
|
||||
try:
|
||||
# 查询未删除的参数
|
||||
param = Param.query.filter(
|
||||
and_(Param.id == param_id, Param.is_delete == 0)
|
||||
).first()
|
||||
return param
|
||||
except Exception as e:
|
||||
logger.error(f"查询参数信息异常,ID: {param_id}, 错误: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数添加
|
||||
# ============================================================
|
||||
|
||||
def add_param():
|
||||
"""
|
||||
添加参数
|
||||
|
||||
流程:
|
||||
1. 解析请求体JSON数据
|
||||
2. 表单验证(名称唯一性、编码唯一性、字段格式等)
|
||||
3. 创建参数对象并保存到数据库
|
||||
|
||||
Returns:
|
||||
R: 操作结果
|
||||
"""
|
||||
try:
|
||||
# 获取参数
|
||||
json_data, error = parse_request_body()
|
||||
if error:
|
||||
return R.failed(msg=error)
|
||||
|
||||
# 表单验证
|
||||
form = ParamForm(data=json_data)
|
||||
if not form.validate():
|
||||
err_msg = regular.get_err(form)
|
||||
return R.failed(msg=err_msg)
|
||||
|
||||
# 创建参数
|
||||
param = Param(**form.data)
|
||||
param.create_user = get_username()
|
||||
param.save()
|
||||
|
||||
return R.ok(msg="添加成功")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"添加参数异常: {str(e)}")
|
||||
return R.failed(msg="添加参数失败,请稍后重试")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数更新
|
||||
# ============================================================
|
||||
|
||||
def update_param():
|
||||
"""
|
||||
更新参数
|
||||
|
||||
流程:
|
||||
1. 解析请求体JSON数据
|
||||
2. 表单验证
|
||||
3. 验证参数是否存在(复用 get_info)
|
||||
4. 更新参数信息
|
||||
|
||||
Returns:
|
||||
R: 操作结果
|
||||
"""
|
||||
try:
|
||||
# 获取参数
|
||||
json_data, error = parse_request_body()
|
||||
if error:
|
||||
return R.failed(msg=error)
|
||||
|
||||
# 表单验证
|
||||
form = ParamForm(data=json_data)
|
||||
if not form.validate():
|
||||
return R.failed(msg=regular.get_err(form))
|
||||
|
||||
# 获取参数ID
|
||||
param_id = form.data.get('id')
|
||||
if not param_id or int(param_id) <= 0:
|
||||
return R.failed("参数ID无效")
|
||||
param_id = int(param_id)
|
||||
|
||||
# 复用 get_info 方法验证参数是否存在
|
||||
param = get_info(param_id)
|
||||
if not param:
|
||||
return R.failed("参数不存在")
|
||||
|
||||
# 更新参数信息(移除ID,避免更新主键)
|
||||
form_data = form.data.copy()
|
||||
form_data.pop('id', None)
|
||||
|
||||
form_data['update_time'] = datetime.now(tz=timezone.utc)
|
||||
form_data['update_user'] = get_username()
|
||||
|
||||
result = Param.query.filter_by(id=param_id).update(form_data)
|
||||
|
||||
if not result:
|
||||
return R.failed("更新失败")
|
||||
|
||||
return R.ok(msg="更新成功")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"更新参数异常: {str(e)}")
|
||||
return R.failed(msg="更新参数失败,请稍后重试")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数删除
|
||||
# ============================================================
|
||||
|
||||
def delete_params(param_ids):
|
||||
"""
|
||||
删除参数(批量删除)
|
||||
|
||||
删除
|
||||
支持逗号分隔的多个ID批量删除。
|
||||
|
||||
Args:
|
||||
param_ids: 参数ID字符串,多个ID用逗号分隔,如 "1,2,3"
|
||||
|
||||
Returns:
|
||||
R: 操作结果
|
||||
"""
|
||||
return Param.batch_delete_r(param_ids)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 参数状态设置
|
||||
# ============================================================
|
||||
|
||||
def update_param_status():
|
||||
"""
|
||||
设置参数状态
|
||||
|
||||
单独更新参数的启用/禁用状态,不涉及其他字段。
|
||||
|
||||
Returns:
|
||||
R: 操作结果
|
||||
"""
|
||||
try:
|
||||
# 获取参数
|
||||
json_data, error = parse_request_body()
|
||||
if error:
|
||||
return R.failed(msg=error)
|
||||
|
||||
# 表单验证
|
||||
form = ParamStatusForm(data=json_data)
|
||||
if not form.validate():
|
||||
err_msg = regular.get_err(form)
|
||||
return R.failed(msg=err_msg)
|
||||
|
||||
param_id = int(form.data['id'])
|
||||
status = int(form.data['status'])
|
||||
|
||||
# 复用 get_info 方法验证参数是否存在
|
||||
param = get_info(param_id)
|
||||
if not param:
|
||||
return R.failed("参数不存在")
|
||||
|
||||
# 更新状态
|
||||
update_data = {"status": status, "update_time": datetime.now(tz=timezone.utc), "update_user": get_username()}
|
||||
result = Param.query.filter_by(id=param_id).update(update_data)
|
||||
|
||||
if not result:
|
||||
return R.failed("设置失败")
|
||||
|
||||
return R.ok(msg="设置成功")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"设置参数状态异常: {str(e)}")
|
||||
return R.failed(msg="设置参数状态失败,请稍后重试")
|
||||
from src.modules.param.service import * # noqa: F401,F403
|
||||
|
||||
Reference in New Issue
Block a user