manage.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. case ServiceTypeActivity:
  15. return "/activity"
  16. case ServiceTypePlan:
  17. return "/plan"
  18. default:
  19. return "/person"
  20. }
  21. }
  22. func (s *ManageService) Save(params any) (string, error) {
  23. var resp IDResponse
  24. _, err := s.client.http.R().
  25. SetBody(params).
  26. SetResult(&resp).
  27. Post(s.client.apiV1Path(s.getServicePath() + "/save"))
  28. if err != nil {
  29. return "", fmt.Errorf("保存失败: %w", err)
  30. }
  31. if !resp.Success {
  32. return "", fmt.Errorf("保存失败: %s", resp.Msg)
  33. }
  34. return resp.Data, nil
  35. }
  36. func (s *ManageService) Query(params any) (*InfosData[map[string]any], error) {
  37. var resp InfosResponse[map[string]any]
  38. _, err := s.client.http.R().
  39. SetBody(params).
  40. SetResult(&resp).
  41. Post(s.client.apiV1Path(s.getServicePath() + "/query"))
  42. if err != nil {
  43. return nil, fmt.Errorf("查询列表失败: %w", err)
  44. }
  45. if !resp.Success {
  46. return nil, fmt.Errorf("查询列表失败: %s", resp.Msg)
  47. }
  48. return &resp.Data, nil
  49. }
  50. func (s *ManageService) Get(params GetPersonParams) (*map[string]any, error) {
  51. var resp InfoResponse[map[string]any]
  52. _, err := s.client.http.R().
  53. SetQueryParam("id", params.ID).
  54. SetResult(&resp).
  55. Get(s.client.apiV1Path(s.getServicePath() + "/get"))
  56. if err != nil {
  57. return nil, fmt.Errorf("查询详情失败: %w", err)
  58. }
  59. if !resp.Success {
  60. return nil, fmt.Errorf("查询详情失败: %s", resp.Msg)
  61. }
  62. return &resp.Data, nil
  63. }
  64. func (s *ManageService) Delete(params any) error {
  65. var resp MsgResponse
  66. _, err := s.client.http.R().
  67. SetBody(params).
  68. SetResult(&resp).
  69. Delete(s.client.apiV1Path(s.getServicePath() + "/delete"))
  70. if err != nil {
  71. return fmt.Errorf("删除失败: %w", err)
  72. }
  73. if !resp.Success {
  74. return fmt.Errorf("删除失败: %s", resp.Msg)
  75. }
  76. return nil
  77. }
  78. func (s *ManageService) Options() ([]map[string]any, error) {
  79. var resp InfosResponse[map[string]any]
  80. var path string
  81. switch s.client.config.Type {
  82. case ServiceTypePerson:
  83. path = "/person/genders"
  84. case ServiceTypeResource:
  85. path = "/resource/categories"
  86. case ServiceTypeActivity:
  87. // Activity 没有 options 接口,返回空
  88. return []map[string]any{}, nil
  89. case ServiceTypePlan:
  90. // Plan 没有 options 接口,返回空
  91. return []map[string]any{}, nil
  92. default:
  93. return nil, fmt.Errorf("不支持的服务类型")
  94. }
  95. _, err := s.client.http.R().
  96. SetResult(&resp).
  97. Get(s.client.apiV1Path(path))
  98. if err != nil {
  99. return nil, fmt.Errorf("查询选项列表失败: %w", err)
  100. }
  101. if !resp.Success {
  102. return nil, fmt.Errorf("查询选项列表失败: %s", resp.Msg)
  103. }
  104. return resp.Data.Infos, nil
  105. }
  106. func (s *ManageService) States() ([]map[string]any, error) {
  107. var resp InfosResponse[map[string]any]
  108. _, err := s.client.http.R().
  109. SetResult(&resp).
  110. Get(s.client.apiV1Path(s.getServicePath() + "/states"))
  111. if err != nil {
  112. return nil, fmt.Errorf("查询状态列表失败: %w", err)
  113. }
  114. if !resp.Success {
  115. return nil, fmt.Errorf("查询状态列表失败: %s", resp.Msg)
  116. }
  117. return resp.Data.Infos, nil
  118. }
  119. func (s *ManageService) PredefinedFields() ([]map[string]any, error) {
  120. var resp InfosResponse[map[string]any]
  121. _, err := s.client.http.R().
  122. SetResult(&resp).
  123. Get(s.client.apiV1Path(s.getServicePath() + "/predefined-fields"))
  124. if err != nil {
  125. return nil, fmt.Errorf("查询预定义字段失败: %w", err)
  126. }
  127. if !resp.Success {
  128. return nil, fmt.Errorf("查询预定义字段失败: %s", resp.Msg)
  129. }
  130. return resp.Data.Infos, nil
  131. }