2
0

ticket_user_service.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import time
  2. from apps.loon_base_model import SnowflakeIDGenerator
  3. from apps.ticket.models import TicketUser
  4. from service.account.account_user_service import account_user_service_ins
  5. from service.base_service import BaseService
  6. class TicketUserService(BaseService):
  7. """
  8. ticket user related service
  9. """
  10. @classmethod
  11. def add_record(cls, tenant_id: int, user_id: int, ticket_id: int, as_creator: bool, as_participant: bool,
  12. as_processor: bool, as_cc_recipient: bool):
  13. ticket_user_queryset = TicketUser.objects.filter(tenant_id=tenant_id, ticket_id=ticket_id, user_id=user_id)
  14. update_dict = dict()
  15. if as_creator:
  16. update_dict["as_creator"] = True
  17. if as_participant:
  18. update_dict["as_participant"] = True
  19. if as_processor:
  20. update_dict["as_processor"] = True
  21. if as_cc_recipient:
  22. update_dict["as_participant"] = True
  23. if ticket_user_queryset:
  24. ticket_user_queryset.update(**update_dict)
  25. else:
  26. ticket_user = TicketUser(id=SnowflakeIDGenerator()(), tenant_id=tenant_id, ticket_id=ticket_id, user_id=user_id)
  27. ticket_user.save()
  28. TicketUser.objects.filter(tenant_id=tenant_id, ticket_id=ticket_id, user_id=user_id).update(**update_dict)
  29. @classmethod
  30. def add_participant_record_list(cls, tenant_id: int, ticket_id: int, user_id_list: list) -> bool:
  31. """
  32. add participant record list
  33. :param tenant_id:
  34. :param ticket_id:
  35. :param user_id_list:
  36. :return:
  37. """
  38. exist_queryset = TicketUser.objects.filter(tenant_id=tenant_id, ticket_id=ticket_id, user_id__in=user_id_list)
  39. exist_queryset.update(as_participant=True)
  40. exist_user_id_list = [exist_record.user_id for exist_record in exist_queryset]
  41. not_exist_user_id_list = [user_id for user_id in user_id_list if user_id not in exist_user_id_list]
  42. ticket_user_batch_list = []
  43. for not_exist_user_id in not_exist_user_id_list:
  44. ticket_user_batch_list.append(TicketUser(id=SnowflakeIDGenerator()(), tenant_id=tenant_id, ticket_id=ticket_id, user_id=not_exist_user_id, as_participant=True))
  45. time.sleep(0.001) # workaround for SnowflakeIDGenerator
  46. TicketUser.objects.bulk_create(ticket_user_batch_list)
  47. @classmethod
  48. def get_ticket_current_participant_list(cls, tenant_id: int, ticket_id: int):
  49. """
  50. get ticket current participant list
  51. :param tenant_id:
  52. :param ticket_id:
  53. :return:
  54. """
  55. ticket_participant_queryset = TicketUser.objects.filter(tenant_id=tenant_id, ticket_id=ticket_id, as_participant=True)
  56. ticket_participant_id_list = [ticket_participant.user_id for ticket_participant in ticket_participant_queryset]
  57. return account_user_service_ins.get_user_list_by_id_list(tenant_id, ticket_participant_id_list)
  58. ticket_user_service_ins = TicketUserService()