优化自动建案逻辑
This commit is contained in:
@@ -3676,13 +3676,16 @@ class TransferCase(APIView):
|
||||
|
||||
|
||||
class CaseChangeRequestCreate(APIView):
|
||||
"""创建案件变更申请"""
|
||||
"""创建案件变更申请(需传入审批人)"""
|
||||
def post(self, request, *args, **kwargs):
|
||||
case_id = request.data.get('case_id') # 案件ID(可选)
|
||||
contract_no = request.data.get('contract_no') # 合同编号(必填)
|
||||
change_item = request.data.get('change_item') # 变更事项
|
||||
change_reason = request.data.get('change_reason') # 变更原因
|
||||
change_agreement = request.FILES.getlist('change_agreement') or request.FILES.getlist('ChangeAgreement') # 变更协议文件
|
||||
approvers = request.data.get('approvers') # 审批人(必填):用户ID数组如[1,2,3],或用户名数组["张三","李四"],或逗号分隔字符串
|
||||
personincharge = request.data.get('personincharge') # 兼容旧接口,未传approvers时使用
|
||||
approvers = normalize_approvers_param(approvers, personincharge)
|
||||
|
||||
# 验证必填参数
|
||||
if not contract_no:
|
||||
@@ -3699,6 +3702,13 @@ class CaseChangeRequestCreate(APIView):
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
if not approvers:
|
||||
return Response({
|
||||
'status': 'error',
|
||||
'message': '缺少参数approvers(审批人),请传入审批人列表,如用户ID数组[1,2,3]或用户名数组["张三","李四"]',
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 如果提供了case_id,验证案件是否存在(可选)
|
||||
case = None
|
||||
if case_id:
|
||||
@@ -3730,23 +3740,7 @@ class CaseChangeRequestCreate(APIView):
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 是否需要审批(默认需要)
|
||||
need_approval = request.data.get('need_approval', True)
|
||||
if isinstance(need_approval, str):
|
||||
need_approval = need_approval not in ('false', '0', '否')
|
||||
|
||||
# 需要审批时获取审核人
|
||||
approvers_list = []
|
||||
if need_approval:
|
||||
approvers_list = get_change_approvers()
|
||||
if not approvers_list:
|
||||
return Response({
|
||||
'status': 'error',
|
||||
'message': '未找到管委会或行政部审批人,请先配置审批人',
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 创建变更申请
|
||||
# 创建变更申请(审批人由入参 approvers 传入,create_approval_with_team_logic 会解析并回写 approvers_order)
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
change_request = CaseChangeRequest.objects.create(
|
||||
case=case, # 案件关联(可选)
|
||||
@@ -3757,8 +3751,8 @@ class CaseChangeRequestCreate(APIView):
|
||||
times=today,
|
||||
applicant=applicant_name,
|
||||
state='审核中',
|
||||
approvers_order=json.dumps(approvers_list, ensure_ascii=False) if approvers_list else None,
|
||||
need_approval=need_approval
|
||||
approvers_order=None, # 由 create_approval_with_team_logic 根据 approvers 入参回写
|
||||
need_approval=True
|
||||
)
|
||||
|
||||
# 如果提供了case_id,更新案件的change_request字段
|
||||
@@ -3776,38 +3770,23 @@ class CaseChangeRequestCreate(APIView):
|
||||
|
||||
from User.utils import create_approval_with_team_logic
|
||||
|
||||
if need_approval:
|
||||
approval, approvers_order_json, needs_approval = create_approval_with_team_logic(
|
||||
team_name=None,
|
||||
approvers=approvers_list,
|
||||
title="案件变更申请",
|
||||
content=content,
|
||||
approval_type="案件变更",
|
||||
user_id=str(change_request.id),
|
||||
business_record=change_request,
|
||||
today=today,
|
||||
applicant=applicant_name
|
||||
)
|
||||
if approval is None and needs_approval:
|
||||
return Response({
|
||||
'status': 'error',
|
||||
'message': '未找到管委会或行政部审批人,请先配置审批人',
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
approval, _, _ = create_approval_with_team_logic(
|
||||
team_name=None,
|
||||
approvers=[],
|
||||
title="案件变更申请",
|
||||
content=content,
|
||||
approval_type="案件变更",
|
||||
user_id=str(change_request.id),
|
||||
business_record=change_request,
|
||||
today=today,
|
||||
applicant=applicant_name
|
||||
)
|
||||
change_request.state = "待查看"
|
||||
change_request.save(update_fields=['state'])
|
||||
approval, approvers_order_json, needs_approval = create_approval_with_team_logic(
|
||||
team_name=None,
|
||||
approvers=approvers,
|
||||
title="案件变更申请",
|
||||
content=content,
|
||||
approval_type="案件变更",
|
||||
user_id=str(change_request.id),
|
||||
business_record=change_request,
|
||||
today=today,
|
||||
applicant=applicant_name
|
||||
)
|
||||
if approval is None and needs_approval:
|
||||
return Response({
|
||||
'status': 'error',
|
||||
'message': '审批人无效或不存在,请检查approvers参数(需为系统中的用户ID或用户名)',
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return Response({
|
||||
'message': '变更申请提交成功',
|
||||
@@ -3819,11 +3798,17 @@ class CaseChangeRequestCreate(APIView):
|
||||
|
||||
|
||||
class CaseChangeRequestList(APIView):
|
||||
"""案件变更申请列表查询"""
|
||||
"""
|
||||
案件变更申请列表查询。
|
||||
变更申请表格:仅展示审批通过的数据,传 state=已通过 或 approved_only=true。
|
||||
我的申请/全部:不传 state 或传具体状态可查全部或按状态筛选。
|
||||
"""
|
||||
def post(self, request, *args, **kwargs):
|
||||
page = request.data.get('page')
|
||||
per_page = request.data.get('per_page')
|
||||
case_id = request.data.get('case_id') # 可选:按案件ID筛选
|
||||
state = request.data.get('state') # 可选:按状态筛选,如 已通过(变更申请表格仅看已通过)
|
||||
approved_only = request.data.get('approved_only') # 可选:为 true 时仅返回已通过,等同 state=已通过
|
||||
|
||||
# 参数验证和默认值处理
|
||||
try:
|
||||
@@ -3844,6 +3829,17 @@ class CaseChangeRequestList(APIView):
|
||||
Q_obj = Q(is_deleted=False)
|
||||
if case_id:
|
||||
Q_obj &= Q(case_id=case_id)
|
||||
# 变更申请表格:仅展示审批通过的数据
|
||||
if approved_only in (True, 'true', '1', 1) or state == '已通过':
|
||||
Q_obj &= Q(state='已通过')
|
||||
elif state:
|
||||
# 支持单状态或逗号分隔多状态
|
||||
if isinstance(state, str) and ',' in state:
|
||||
states = [s.strip() for s in state.split(',') if s.strip()]
|
||||
if states:
|
||||
Q_obj &= Q(state__in=states)
|
||||
else:
|
||||
Q_obj &= Q(state=state)
|
||||
|
||||
queryset = CaseChangeRequest.objects.filter(Q_obj).order_by('-id')
|
||||
total = queryset.count()
|
||||
|
||||
Reference in New Issue
Block a user