hberp/hb_server/apps/wf/services.py

55 lines
1.7 KiB
Python

from apps.wf.models import State, Ticket, Transition, Workflow
from rest_framework.exceptions import APIException
class WfService(object):
@staticmethod
def get_wf_states(wf:Workflow):
"""
获取工作流状态列表
"""
return State.objects.filter(workflow=wf, is_deleted=False).order_by('sort')
@staticmethod
def get_wf_transitions(wf:Workflow):
"""
获取工作流流转列表
"""
return Transition.objects.filter(workflow=wf, is_deleted=False)
@staticmethod
def get_wf_start_state(wf:Workflow):
"""
获取工作流初始状态
"""
try:
wf_state_obj = State.objects.get(workflow=wf, type=State.STATE_TYPE_START, is_deleted=False)
return wf_state_obj
except:
raise Exception('工作流初始状态配置错误')
@classmethod
def get_ticket_transitions(cls, ticket:Ticket):
"""
获取工单当前状态下可用的流转条件
"""
return cls.get_state_transitions(ticket.state)
@classmethod
def get_state_transitions(cls, state:State):
"""
获取状态可执行的操作
"""
return Transition.objects.filter(is_deleted=False, source_state=state).all()
@classmethod
def get_ticket_next_state(cls, ticket:Ticket)->object:
transitions = Transition.objects.filter(source_state=ticket.state, is_deleted=False)
count = transitions.count()
if count == 0:
raise Exception('未配置流转条件')
elif count == 1:
return transitions.first()
else:
for i in transitions:
pass