ticket_node_service.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import time
  2. from apps.loon_base_model import SnowflakeIDGenerator
  3. from apps.ticket.models import TicketNode, TicketNodeParticipant
  4. from service.base_service import BaseService
  5. from service.util.archive_service import archive_service_ins
  6. class TicketNodeService(BaseService):
  7. @classmethod
  8. def update_batch_record(cls, tenant_id: int, ticket_node_participant_obj_list: list) -> bool:
  9. """
  10. update btch ticket record
  11. :param tenant_id:
  12. :param ticket_node_participant_obj_list:
  13. :return:
  14. """
  15. ticket_node_bulk_list = []
  16. ticket_node_participant_bulk_list = []
  17. for ticket_node_participant_obj in ticket_node_participant_obj_list:
  18. ticket_node_id = SnowflakeIDGenerator()()
  19. ticket_node_bulk = TicketNode(id=ticket_node_id, tenant_id=tenant_id,
  20. ticket_id=ticket_node_participant_obj.get("ticket_id"),
  21. node_id=ticket_node_participant_obj.get("node_id"),
  22. in_add_node=ticket_node_participant_obj.get("in_add_node"),
  23. add_node_target=ticket_node_participant_obj.get("add_node_target"),
  24. hook_state=ticket_node_participant_obj.get("hook_state"),
  25. all_participant_result=ticket_node_participant_obj.get("all_participant_result"))
  26. ticket_node_bulk_list.append(ticket_node_bulk)
  27. time.sleep(0.001) # SnowflakeIDGenerator has bug will, just workaround provisionally
  28. if ticket_node_participant_obj.get("destination_participant_type") in ("multi-person", "person"):
  29. real_destination_participant_type = "person"
  30. else:
  31. real_destination_participant_type = ticket_node_participant_obj.get("destination_participant_type")
  32. for destination_participant in ticket_node_participant_obj.get("destination_participant_list"):
  33. ticket_node_participant_bulk = TicketNodeParticipant(id=SnowflakeIDGenerator()(), tenant_id=tenant_id,
  34. ticket_id=ticket_node_participant_obj.get("ticket_id"),
  35. node_id=ticket_node_participant_obj.get("node_id"),
  36. participant_type=real_destination_participant_type,
  37. participant=destination_participant
  38. )
  39. time.sleep(0.001) # SnowflakeIDGenerator has bug, just workaround provisionally
  40. ticket_node_participant_bulk_list.append(ticket_node_participant_bulk)
  41. # del and add new record
  42. node_id_list = [ticket_node_participant_obj.get("id") for ticket_node_participant_obj in ticket_node_participant_obj_list]
  43. for_archive_ticket_node_queryset = TicketNode.objects.filter(tenant_id=tenant_id, ticket_id=ticket_node_participant_obj_list[0].get("ticket_id"), node_id__in=node_id_list)
  44. for_archive_ticket_node_participant_queryset = TicketNodeParticipant.objects.filter(tenant_id=tenant_id,
  45. ticket_id=ticket_node_participant_obj_list[0].get("ticket_id"), node_id__in=node_id_list)
  46. archive_service_ins.archive_record_list("TicketNode", for_archive_ticket_node_queryset, 0)
  47. archive_service_ins.archive_record_list("TicketNodeParticipant", for_archive_ticket_node_participant_queryset, 0)
  48. TicketNode.objects.bulk_create(ticket_node_bulk_list)
  49. TicketNodeParticipant.objects.bulk_create(ticket_node_participant_bulk_list)
  50. return True
  51. ticket_node_service_ins = TicketNodeService()