feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则
This commit is contained in:
1 parent
88408490d7
commit
34d489a2d1
167 files changed
+10365
-7978
No files matched your search
@@ -30,14 +30,21 @@ from app.services.shipping_notice.query_service import (
|
||||
get_focus_task_for_batch,
|
||||
get_task_for_user,
|
||||
list_batches_for_user,
|
||||
list_tasks_page_for_user,
|
||||
list_tasks_for_batch,
|
||||
list_tasks_for_user,
|
||||
resolve_task_export_download,
|
||||
serialize_batch_summary,
|
||||
serialize_task_detail,
|
||||
serialize_task_summary,
|
||||
task_export_error_message,
|
||||
)
|
||||
from app.services.shipping_notice.task_disposition_service import (
|
||||
archive_task,
|
||||
load_task_for_disposition,
|
||||
move_task_to_recycle_bin,
|
||||
purge_task_permanently,
|
||||
restore_task_from_recycle_bin,
|
||||
)
|
||||
from app.services.shipping_notice.ui_state import build_batch_ui_state, build_task_ui_state
|
||||
from app.services.shipping_notice.customer_identity_gate import (
|
||||
confirm_task_customer,
|
||||
@@ -69,12 +76,130 @@ def _ok(payload: dict):
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks', methods=['GET'])
|
||||
def workspace_shipping_tasks():
|
||||
"""列出当前用户可见的发货任务,可按批次与处理状态过滤。"""
|
||||
"""分页列出当前用户可见的发货任务,可按批次与处理状态过滤。"""
|
||||
user = _require_user()
|
||||
batch_no = str(request.args.get('batch_no') or '').strip() or None
|
||||
process_status = str(request.args.get('process_status') or '').strip() or None
|
||||
limit = int(request.args.get('limit') or 100)
|
||||
return jsonify(list_tasks_for_user(user.id, batch_no=batch_no, process_status=process_status, limit=limit))
|
||||
lifecycle_status = str(request.args.get('lifecycle_status') or '').strip() or None
|
||||
try:
|
||||
page = int(request.args.get('page') or 1)
|
||||
page_size = int(request.args.get('page_size') or 20)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValidationError('page and page_size must be integers') from exc
|
||||
if page < 1 or not 1 <= page_size <= 100:
|
||||
raise ValidationError('page must be positive and page_size must be between 1 and 100')
|
||||
return jsonify(list_tasks_page_for_user(
|
||||
user.id,
|
||||
batch_no=batch_no,
|
||||
process_status=process_status,
|
||||
lifecycle_status=lifecycle_status,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
))
|
||||
|
||||
|
||||
def _batch_task_nos(data: dict) -> list[str]:
|
||||
"""校验批量任务号,统一限制 100 条并去重,避免前端循环绕过服务端门禁。"""
|
||||
raw_task_nos = data.get('task_nos')
|
||||
if not isinstance(raw_task_nos, list) or not raw_task_nos:
|
||||
raise ValidationError('task_nos must be a non-empty list', code='SHIPPING.TASK_SELECTION_INVALID')
|
||||
normalized = list(dict.fromkeys(str(value or '').strip().upper() for value in raw_task_nos if str(value or '').strip()))
|
||||
if not normalized or len(normalized) > 100:
|
||||
raise ValidationError('task_nos must contain 1 to 100 task numbers', code='SHIPPING.TASK_SELECTION_INVALID')
|
||||
return normalized
|
||||
|
||||
|
||||
def _can_dispose_task(user, task: ShippingTask) -> bool:
|
||||
"""管理员可跨用户处置;普通用户仅可处置自己名下或未归属任务。"""
|
||||
return str(getattr(user, 'role', '')).lower() == 'admin' or task.user_id in {None, user.id}
|
||||
|
||||
|
||||
def _batch_dispose(action: str):
|
||||
"""在每条任务行锁下重算权限与资格,返回部分成功明细而不让一条失败回滚整批。"""
|
||||
user = _require_user()
|
||||
data = request.get_json(silent=True) or {}
|
||||
task_nos = _batch_task_nos(data)
|
||||
reason = str(data.get('reason') or '').strip()
|
||||
if action == 'delete' and not reason:
|
||||
raise ValidationError('reason is required', code='SHIPPING.TASK_DELETE_REASON_REQUIRED')
|
||||
request_id = getattr(g, 'request_id', None)
|
||||
results = []
|
||||
for task_no in task_nos:
|
||||
try:
|
||||
task = load_task_for_disposition(task_no)
|
||||
if not _can_dispose_task(user, task):
|
||||
results.append({'task_no': task_no, 'status': 'blocked', 'code': 'AUTH.FORBIDDEN'})
|
||||
db.session.rollback()
|
||||
continue
|
||||
if action == 'delete':
|
||||
outcome = move_task_to_recycle_bin(task, actor_id=user.id, reason=reason, request_id=request_id)
|
||||
elif action == 'archive':
|
||||
archive_task(task, actor_id=user.id, request_id=request_id)
|
||||
outcome = 'archived'
|
||||
else:
|
||||
restore_task_from_recycle_bin(task, actor_id=user.id, request_id=request_id)
|
||||
outcome = 'restored'
|
||||
db.session.commit()
|
||||
results.append({'task_no': task_no, 'status': outcome})
|
||||
except AppError as exc:
|
||||
db.session.rollback()
|
||||
results.append({'task_no': task_no, 'status': 'blocked', 'code': exc.code, 'message': exc.message})
|
||||
except Exception:
|
||||
db.session.rollback()
|
||||
raise
|
||||
return _ok({'results': results, 'requested_count': len(task_nos)})
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks/batch-purge', methods=['POST'])
|
||||
def workspace_batch_purge_shipping_tasks():
|
||||
"""仅管理员可永久清除回收站中的纯误建任务;每条均在行锁下重查全部引用。"""
|
||||
user = _require_user()
|
||||
if str(getattr(user, 'role', '')).lower() != 'admin':
|
||||
from app.core.errors import ForbiddenError
|
||||
|
||||
raise ForbiddenError('Admin privilege required')
|
||||
data = request.get_json(silent=True) or {}
|
||||
task_nos = _batch_task_nos(data)
|
||||
reason = str(data.get('reason') or '').strip()
|
||||
if not reason:
|
||||
raise ValidationError('reason is required', code='SHIPPING.TASK_PURGE_REASON_REQUIRED')
|
||||
results = []
|
||||
for task_no in task_nos:
|
||||
try:
|
||||
task = load_task_for_disposition(task_no)
|
||||
purge_task_permanently(
|
||||
task,
|
||||
actor_id=user.id,
|
||||
reason=reason,
|
||||
request_id=getattr(g, 'request_id', None),
|
||||
)
|
||||
db.session.commit()
|
||||
results.append({'task_no': task_no, 'status': 'purged'})
|
||||
except AppError as exc:
|
||||
db.session.rollback()
|
||||
results.append({'task_no': task_no, 'status': 'blocked', 'code': exc.code, 'message': exc.message})
|
||||
except Exception:
|
||||
db.session.rollback()
|
||||
raise
|
||||
return _ok({'results': results, 'requested_count': len(task_nos)})
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks/batch-delete', methods=['POST'])
|
||||
def workspace_batch_delete_shipping_tasks():
|
||||
"""批量删除任务到回收站;服务端按行锁判定是否需要同时作废。"""
|
||||
return _batch_dispose('delete')
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks/batch-archive', methods=['POST'])
|
||||
def workspace_batch_archive_shipping_tasks():
|
||||
"""批量归档任务,适用于已上传或外部处理中而不能作废的任务。"""
|
||||
return _batch_dispose('archive')
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks/batch-restore', methods=['POST'])
|
||||
def workspace_batch_restore_shipping_tasks():
|
||||
"""批量恢复回收站内仍为 active 的纯误建任务。"""
|
||||
return _batch_dispose('restore')
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-batches', methods=['GET'])
|
||||
def workspace_shipping_batches():
|
||||
|
||||
Reference in new issue
Block a user