全局审计日fix: 使用鸭子类型强制安全解包 SQLAlchemy Row 对象,彻底解决 to_dict 报错志
This commit is contained in:
@ -122,7 +122,8 @@ def get_list():
|
||||
'isEnabled': request.args.get('isEnabled', None),
|
||||
'orderByColumn': request.args.get('orderByColumn', ''),
|
||||
'isAsc': request.args.get('isAsc', None),
|
||||
'advancedFilters': advanced_filters_list
|
||||
'advancedFilters': advanced_filters_list,
|
||||
'enableWarningSort': request.args.get('enableWarningSort', 'false').lower() == 'true'
|
||||
}
|
||||
|
||||
user_permissions = get_current_user_permissions()
|
||||
@ -325,3 +326,76 @@ def delete(id):
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
# 2.5 批量设置预警 API (POST /api/v1/inbound/base/warning/batch-set)
|
||||
# ==============================================================================
|
||||
@inbound_base_bp.route('/warning/batch-set', methods=['POST'])
|
||||
@permission_required('material_list:edit_warning')
|
||||
def batch_set_warning():
|
||||
"""
|
||||
批量设置物料预警配置
|
||||
请求体格式: [
|
||||
{"baseId": 1, "isEnabled": true, "yellowThreshold": 10, "redThreshold": 5},
|
||||
{"baseId": 2, "isEnabled": false}
|
||||
]
|
||||
"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not isinstance(data, list):
|
||||
return jsonify({"code": 400, "msg": "请求体必须为数组"})
|
||||
|
||||
from app.models.base import MaterialWarningSetting
|
||||
|
||||
updated_count = 0
|
||||
created_count = 0
|
||||
|
||||
for item in data:
|
||||
base_id = item.get('baseId')
|
||||
if not base_id:
|
||||
continue
|
||||
|
||||
# 查找物料是否存在
|
||||
material = MaterialBase.query.get(base_id)
|
||||
if not material:
|
||||
current_app.logger.warning(f"物料ID {base_id} 不存在,跳过")
|
||||
continue
|
||||
|
||||
# 查找现有预警设置
|
||||
warning = MaterialWarningSetting.query.filter_by(base_id=base_id).first()
|
||||
|
||||
if warning:
|
||||
# 更新现有记录
|
||||
if 'isEnabled' in item:
|
||||
warning.is_enabled = bool(item['isEnabled'])
|
||||
if 'yellowThreshold' in item:
|
||||
warning.yellow_threshold = item['yellowThreshold']
|
||||
if 'redThreshold' in item:
|
||||
warning.red_threshold = item['redThreshold']
|
||||
updated_count += 1
|
||||
else:
|
||||
# 创建新记录
|
||||
warning = MaterialWarningSetting(
|
||||
base_id=base_id,
|
||||
is_enabled=item.get('isEnabled', False),
|
||||
yellow_threshold=item.get('yellowThreshold'),
|
||||
red_threshold=item.get('redThreshold')
|
||||
)
|
||||
db.session.add(warning)
|
||||
created_count += 1
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({
|
||||
"code": 200,
|
||||
"msg": "批量设置成功",
|
||||
"data": {
|
||||
"created": created_count,
|
||||
"updated": updated_count
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
current_app.logger.error(f"批量设置预警失败: {str(e)}")
|
||||
return jsonify({"code": 500, "msg": f"批量设置预警失败: {str(e)}"}), 500
|
||||
|
||||
Reference in New Issue
Block a user