83 lines
4.0 KiB
Python
83 lines
4.0 KiB
Python
"""部门匹配规则维护和只读试算。"""
|
|
import json
|
|
|
|
from flask import Blueprint, jsonify, request
|
|
from routes.auth import login_required, operation_required, role_required
|
|
from services.db import query, add_operation_log
|
|
from services.department_matching import records, validate_record, match_department, check_samples
|
|
|
|
department_matching_bp = Blueprint('department_matching', __name__)
|
|
|
|
|
|
@department_matching_bp.route('/api/department-matching/records')
|
|
@login_required
|
|
def list_records():
|
|
if not role_required('department_matching'):
|
|
return jsonify(code=403, msg='无部门匹配管理查看权限'), 403
|
|
return jsonify(code=0, data=records())
|
|
|
|
|
|
@department_matching_bp.route('/api/department-matching/save', methods=['POST'])
|
|
@login_required
|
|
def save_record():
|
|
data = request.get_json(silent=True)
|
|
if not isinstance(data, dict):
|
|
return jsonify(code=400, msg='请求格式不正确'), 400
|
|
if not operation_required('department_matching', 'edit' if data.get('id') else 'create'):
|
|
return jsonify(code=403, msg='无规则维护权限'), 403
|
|
try:
|
|
kind, payload, priority, enabled = validate_record(data)
|
|
row_id = int(data.get('id') or 0)
|
|
old = query('SELECT * FROM department_matching_records WHERE id=%s', (row_id,)) if row_id else []
|
|
if row_id and (not old or old[0]['kind'] != kind):
|
|
return jsonify(code=404, msg='记录不存在或类型不正确'), 404
|
|
if row_id:
|
|
query('UPDATE department_matching_records SET payload=%s,priority=%s,enabled=%s WHERE id=%s',
|
|
(json.dumps(payload, ensure_ascii=False), priority, enabled, row_id))
|
|
else:
|
|
row_id = query('INSERT INTO department_matching_records (kind,payload,priority,enabled) VALUES (%s,%s,%s,%s)',
|
|
(kind, json.dumps(payload, ensure_ascii=False), priority, enabled))
|
|
add_operation_log('编辑部门匹配规则' if old else '新增部门匹配规则', f'记录{row_id},类型{kind}',
|
|
page='department_matching', object_id=str(row_id),
|
|
old_values=old[0] if old else None, new_values=data)
|
|
return jsonify(code=0, msg='保存成功', id=row_id)
|
|
except (ValueError, TypeError) as exc:
|
|
return jsonify(code=400, msg=str(exc)), 400
|
|
|
|
|
|
@department_matching_bp.route('/api/department-matching/delete', methods=['POST'])
|
|
@login_required
|
|
def delete_record():
|
|
if not operation_required('department_matching', 'delete'):
|
|
return jsonify(code=403, msg='无删除权限'), 403
|
|
try:
|
|
row_id = int((request.get_json(silent=True) or {}).get('id', 0))
|
|
except (ValueError, TypeError):
|
|
return jsonify(code=400, msg='记录编号不正确'), 400
|
|
old = query('SELECT * FROM department_matching_records WHERE id=%s', (row_id,))
|
|
if not old:
|
|
return jsonify(code=404, msg='记录不存在'), 404
|
|
query('DELETE FROM department_matching_records WHERE id=%s', (row_id,))
|
|
add_operation_log('删除部门匹配规则', f'记录{row_id}', page='department_matching',
|
|
object_id=str(row_id), old_values=old[0])
|
|
return jsonify(code=0, msg='删除成功')
|
|
|
|
|
|
@department_matching_bp.route('/api/department-matching/test', methods=['POST'])
|
|
@login_required
|
|
def test_match():
|
|
if not role_required('department_matching'):
|
|
return jsonify(code=403, msg='无查看权限'), 403
|
|
data = request.get_json(silent=True) or {}
|
|
if not isinstance(data, dict) or any(not isinstance(data.get(k, ''), str) or len(data.get(k, '')) > 4000 for k in ('shop', 'source')):
|
|
return jsonify(code=400, msg='店铺和来源必须为4000字以内的文本'), 400
|
|
return jsonify(code=0, data=match_department(data.get('shop', ''), data.get('source', '')))
|
|
|
|
|
|
@department_matching_bp.route('/api/department-matching/check', methods=['POST'])
|
|
@login_required
|
|
def check_matching():
|
|
if not role_required('department_matching'):
|
|
return jsonify(code=403, msg='无查看权限'), 403
|
|
return jsonify(code=0, data=check_samples())
|