manage.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package managesdk
  2. import (
  3. "fmt"
  4. )
  5. type ManageService struct {
  6. client *Client
  7. }
  8. func (s *ManageService) getServicePath() string {
  9. switch s.client.config.Type {
  10. case ServiceTypePerson:
  11. return "/person"
  12. case ServiceTypeResource:
  13. return "/resource"
  14. default:
  15. return "/person"
  16. }
  17. }
  18. func (s *ManageService) Save(params any) (string, error) {
  19. var resp IDResponse
  20. _, err := s.client.http.R().
  21. SetBody(params).
  22. SetResult(&resp).
  23. Post(s.client.apiV1Path(s.getServicePath() + "/save"))
  24. if err != nil {
  25. return "", fmt.Errorf("保存失败: %w", err)
  26. }
  27. if !resp.Success {
  28. return "", fmt.Errorf("保存失败: %s", resp.Msg)
  29. }
  30. return resp.Data, nil
  31. }
  32. func (s *ManageService) Query(params any) (*InfosData[map[string]any], error) {
  33. var resp InfosResponse[map[string]any]
  34. _, err := s.client.http.R().
  35. SetBody(params).
  36. SetResult(&resp).
  37. Post(s.client.apiV1Path(s.getServicePath() + "/query"))
  38. if err != nil {
  39. return nil, fmt.Errorf("查询列表失败: %w", err)
  40. }
  41. if !resp.Success {
  42. return nil, fmt.Errorf("查询列表失败: %s", resp.Msg)
  43. }
  44. return &resp.Data, nil
  45. }
  46. func (s *ManageService) Get(params GetPersonParams) (*map[string]any, error) {
  47. var resp InfoResponse[map[string]any]
  48. _, err := s.client.http.R().
  49. SetQueryParam("id", params.ID).
  50. SetResult(&resp).
  51. Get(s.client.apiV1Path(s.getServicePath() + "/get"))
  52. if err != nil {
  53. return nil, fmt.Errorf("查询详情失败: %w", err)
  54. }
  55. if !resp.Success {
  56. return nil, fmt.Errorf("查询详情失败: %s", resp.Msg)
  57. }
  58. return &resp.Data, nil
  59. }
  60. func (s *ManageService) Delete(params any) error {
  61. var resp MsgResponse
  62. _, err := s.client.http.R().
  63. SetBody(params).
  64. SetResult(&resp).
  65. Delete(s.client.apiV1Path(s.getServicePath() + "/delete"))
  66. if err != nil {
  67. return fmt.Errorf("删除失败: %w", err)
  68. }
  69. if !resp.Success {
  70. return fmt.Errorf("删除失败: %s", resp.Msg)
  71. }
  72. return nil
  73. }
  74. func (s *ManageService) Options() ([]map[string]any, error) {
  75. var resp InfosResponse[map[string]any]
  76. var path string
  77. switch s.client.config.Type {
  78. case ServiceTypePerson:
  79. path = "/person/genders"
  80. case ServiceTypeResource:
  81. path = "/resource/categories"
  82. }
  83. _, err := s.client.http.R().
  84. SetResult(&resp).
  85. Get(s.client.apiV1Path(path))
  86. if err != nil {
  87. return nil, fmt.Errorf("查询选项列表失败: %w", err)
  88. }
  89. if !resp.Success {
  90. return nil, fmt.Errorf("查询选项列表失败: %s", resp.Msg)
  91. }
  92. return resp.Data.Infos, nil
  93. }
  94. func (s *ManageService) States() ([]map[string]any, error) {
  95. var resp InfosResponse[map[string]any]
  96. _, err := s.client.http.R().
  97. SetResult(&resp).
  98. Get(s.client.apiV1Path(s.getServicePath() + "/states"))
  99. if err != nil {
  100. return nil, fmt.Errorf("查询状态列表失败: %w", err)
  101. }
  102. if !resp.Success {
  103. return nil, fmt.Errorf("查询状态列表失败: %s", resp.Msg)
  104. }
  105. return resp.Data.Infos, nil
  106. }
  107. func (s *ManageService) PredefinedFields() ([]map[string]any, error) {
  108. var resp InfosResponse[map[string]any]
  109. _, err := s.client.http.R().
  110. SetResult(&resp).
  111. Get(s.client.apiV1Path(s.getServicePath() + "/predefined-fields"))
  112. if err != nil {
  113. return nil, fmt.Errorf("查询预定义字段失败: %w", err)
  114. }
  115. if !resp.Success {
  116. return nil, fmt.Errorf("查询预定义字段失败: %s", resp.Msg)
  117. }
  118. return resp.Data.Infos, nil
  119. }