ticket_flow_history_service.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  2. from apps.ticket.models import TicketFlowHistory
  3. from service.account.account_user_service import account_user_service_ins
  4. from service.base_service import BaseService
  5. from service.workflow.workflow_transition_service import workflow_transition_service_ins
  6. class TicketFlowHistoryService(BaseService):
  7. """
  8. ticket flow history related service
  9. """
  10. @classmethod
  11. def add_ticket_flow_history(cls, tenant_id: int, operator_id: int, ticket_id: int, transition_id: int, comment: str,
  12. participant_type: str, participant: str, node_id: int, flow_type: str,
  13. ticket_data: dict) -> int:
  14. record = TicketFlowHistory(tenant_id=tenant_id, creator_id=operator_id, ticket_id=ticket_id,
  15. transition_id=transition_id, comment=comment, participant_type=participant_type,
  16. participant=participant, node_id=node_id, flow_type=flow_type, ticket_data=ticket_data)
  17. record.save()
  18. return record.id
  19. @classmethod
  20. def get_ticket_flow_history(cls, tenant_id: int, ticket_id: int, per_page: int=10, page: int=1, include_ticket_data: int=0, desc: int=1) -> dict:
  21. """
  22. get ticket flow history
  23. :param tenant_id:
  24. :param ticket_id:
  25. :param per_page:
  26. :param page:
  27. :param include_ticket_data:
  28. :param desc:
  29. :return:
  30. """
  31. if desc == 0:
  32. ticket_flow_history_queryset = TicketFlowHistory.objects.filter(tenant_id=tenant_id, ticket_id=ticket_id).order_by("id")
  33. else:
  34. ticket_flow_history_queryset = TicketFlowHistory.objects.filter(tenant_id=tenant_id, ticket_id=ticket_id).order_by("-id")
  35. paginator = Paginator(ticket_flow_history_queryset, per_page)
  36. try:
  37. ticket_history_result_paginator = paginator.page(page)
  38. except PageNotAnInteger:
  39. ticket_history_result_paginator = paginator.page(1)
  40. except EmptyPage:
  41. # If page is out of range (e.g. 9999), deliver last page of results
  42. ticket_history_result_paginator = paginator.page(paginator.num_pages)
  43. ticket_flow_history_object_format_list = []
  44. for ticket_flow_history in ticket_flow_history_queryset:
  45. node_info = dict(id=ticket_flow_history.node.id, name=ticket_flow_history.node.name)
  46. transition_id = ticket_flow_history.transition_id
  47. transition_info_dict = dict(id=transition_id)
  48. flow_type = ticket_flow_history.flow_type
  49. if transition_id == 0:
  50. transition_name = flow_type
  51. else:
  52. transition_obj = workflow_transition_service_ins.get_workflow_transition_by_id(transition_id)
  53. transition_name = transition_obj.name
  54. transition_info_dict["name"] = transition_name
  55. participant_info = dict(participant_type=ticket_flow_history.participant_type,
  56. participant=ticket_flow_history.participant,
  57. participant_alias=ticket_flow_history.participant,
  58. participant_email='', participant_phone=''
  59. )
  60. if ticket_flow_history.participant_type == "personal":
  61. user_info = account_user_service_ins.get_user_by_user_id(ticket_flow_history.participant)
  62. participant_info.update(participant_alias=user_info.alias, participant_email=user_info.email,
  63. participant_phone=user_info.phone)
  64. ticket_flow_history_restful = dict(id=ticket_flow_history.id, ticket_id=ticket_id,
  65. node_info=node_info, participant_info=participant_info,
  66. comment=ticket_flow_history.comment,
  67. create_at=ticket_flow_history.created_at.strftime('%Y-%m-%d %H:%M:%S %z'))
  68. if include_ticket_data:
  69. ticket_flow_history_restful.update(ticket_data=ticket_flow_history.ticket_data)
  70. ticket_flow_history_object_format_list.append(ticket_flow_history_restful)
  71. return dict(ticket_flow_history_object_format_list=ticket_flow_history_object_format_list,
  72. paginator_info=dict(per_page=per_page, page=page, total=paginator.count))
  73. ticket_flow_history_service_ins = TicketFlowHistoryService()