ticket_custom_field_service.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from apps.loon_base_model import SnowflakeIDGenerator
  2. from apps.ticket.models import TicketCustomField
  3. from service.base_service import BaseService
  4. from service.workflow.workflow_custom_field_service import workflow_custom_field_service_ins
  5. class TicketCustomFieldService(BaseService):
  6. """
  7. ticket custom field service
  8. """
  9. @classmethod
  10. def get_field_value_column(cls, field_type: str) -> str:
  11. """
  12. get what the column about field's value should save to
  13. :param field_type:
  14. :return:
  15. """
  16. if field_type in ["text", "select", "cascade", "user", "field"]:
  17. return "common_value"
  18. elif field_type == "number":
  19. return "number_value"
  20. elif field_type == "date":
  21. return "date_value"
  22. elif field_type == "datetime":
  23. return "datetime_value"
  24. elif field_type == "time":
  25. return "time_value"
  26. elif field_type == "rich_text":
  27. return "rich_text_value"
  28. @classmethod
  29. def add_record(cls, tenant_id: int, ticket_id:int, operator_id: int, workflow_id: int, field_info_dict: dict) -> bool:
  30. """
  31. add ticket custom field record
  32. :param tenant_id:
  33. :param ticket_id:
  34. :param operator_id:
  35. :param workflow_id:
  36. :param field_info_dict:
  37. :return:
  38. """
  39. workflow_custom_field_queryset = workflow_custom_field_service_ins.get_workflow_custom_field(workflow_id)
  40. custom_field_dict = {}
  41. for workflow_custom_field in workflow_custom_field_queryset:
  42. custom_field_dict[workflow_custom_field.field_key] = workflow_custom_field.field_type
  43. record_list = []
  44. for field_key, field_value in field_info_dict.items():
  45. field_value_column = cls.get_field_value_column(custom_field_dict.get(field_key))
  46. record_list.append(TicketCustomField(id=SnowflakeIDGenerator()(), tenant_id=tenant_id, ticket_id=ticket_id, creator_id=operator_id, field_key=field_key, field_type=custom_field_dict.get(field_key),
  47. **{field_value_column: field_value}))
  48. import time
  49. time.sleep(0.001) # SnowflakeIDGenerator has bug will, just workaround provisionally
  50. TicketCustomField.objects.bulk_create(record_list)
  51. return True
  52. ticket_custom_field_service_ins = TicketCustomFieldService()