ng_cws_client.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package ng_cws_client
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "git.sxidc.com/service-supports/cws-sdk/service"
  6. "git.sxidc.com/service-supports/cws-sdk/service/request"
  7. "git.sxidc.com/service-supports/cws-sdk/service/response"
  8. "git.sxidc.com/service-supports/cws-sdk/utils"
  9. "git.sxidc.com/service-supports/cws-sdk/utils/http_client"
  10. "time"
  11. )
  12. var NGCwsClient *http_client.Client
  13. var cwsUrl string
  14. var cwsTimeOut time.Duration
  15. func Init(url string, timeout time.Duration) error {
  16. if utils.IsStringEmpty(url) {
  17. return errors.New("未配置CWS地址")
  18. }
  19. NGCwsClient = http_client.New()
  20. cwsTimeOut = timeout
  21. cwsUrl = url
  22. return nil
  23. }
  24. func Destroy() {
  25. http_client.Destroy(NGCwsClient)
  26. }
  27. func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeRequest) (string, error) {
  28. //根据业务类型获取流程模型ID
  29. workflowTemplateId, err := getWorkTemplateInfoByBusinessType(reqParams.BusinessType, reqParams.TenantID)
  30. if err != nil {
  31. return "", err
  32. }
  33. if utils.IsStringEmpty(workflowTemplateId) {
  34. return "", errors.New("该业务类型未配置流程")
  35. }
  36. //发起流程前校验 获取人员组织信息
  37. customStructureInfo, err := LaunchWorkflowPrepare(&request.LaunchWorkflowPrepareParams{
  38. WorkflowTemplateID: workflowTemplateId,
  39. CreateUserID: reqParams.CreateUserID,
  40. TenantID: reqParams.TenantID,
  41. })
  42. if err != nil {
  43. return "", err
  44. }
  45. jsonBytes, err := json.Marshal(reqParams.Data)
  46. if err != nil {
  47. return "", err
  48. }
  49. businessObjectStr := string(jsonBytes)
  50. workflowId, err := StartWorkflow(&request.StartWorkflowRequest{
  51. WorkflowTemplateID: workflowTemplateId,
  52. LaunchUserName: reqParams.LaunchUserName,
  53. Matter: reqParams.Matter,
  54. BusinessObject: businessObjectStr,
  55. UserStructureID: customStructureInfo.ID,
  56. CreateUserID: reqParams.CreateUserID,
  57. TenantID: reqParams.TenantID,
  58. })
  59. if err != nil {
  60. return "", err
  61. }
  62. return workflowId, nil
  63. }
  64. func StartWorkflowTemplate(reqParams *request.StartWorkflowTemplateRequest) (string, error) {
  65. if utils.IsStringEmpty(cwsUrl) {
  66. return "", errors.New("未配置CWS地址")
  67. }
  68. //发起流程前校验 获取人员组织信息
  69. customStructureInfo, err := LaunchWorkflowTemplatePrepare(&request.LaunchWorkflowTemplatePrepareParams{
  70. StructureRootID: reqParams.StructureRootID,
  71. CreateUserID: reqParams.CreateUserID,
  72. TenantID: reqParams.TenantID,
  73. })
  74. if err != nil {
  75. return "", err
  76. }
  77. jsonBytes, err := json.Marshal(reqParams.Data)
  78. if err != nil {
  79. return "", err
  80. }
  81. businessObjectStr := string(jsonBytes)
  82. reqJsonParams := &request.StartWorkflowTemplateRequest{
  83. StructureRootID: reqParams.StructureRootID,
  84. WorkflowTemplateName: reqParams.WorkflowTemplateName,
  85. Process: reqParams.Process,
  86. LaunchUserName: reqParams.LaunchUserName,
  87. Matter: reqParams.Matter,
  88. BusinessObject: businessObjectStr,
  89. UserStructureID: customStructureInfo.ID,
  90. CreateUserID: reqParams.CreateUserID,
  91. TenantID: reqParams.TenantID,
  92. }
  93. if err != nil {
  94. return "", err
  95. }
  96. requestJson, err := json.Marshal(reqJsonParams)
  97. if err != nil {
  98. return "", err
  99. }
  100. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  101. Post(cwsUrl+service.StartWorkFlowTemplateMethodName, requestJson)
  102. if err != nil {
  103. return "", err
  104. }
  105. resp := new(response.InfoResponse[string])
  106. err = postResponse.Json(resp)
  107. if err != nil {
  108. return "", err
  109. }
  110. if !resp.Success {
  111. return "", errors.New(resp.Msg)
  112. }
  113. return resp.Info, nil
  114. }
  115. func StartWorkflow(reqParams *request.StartWorkflowRequest) (string, error) {
  116. if utils.IsStringEmpty(cwsUrl) {
  117. return "", errors.New("未配置CWS地址")
  118. }
  119. requestJson, err := json.Marshal(reqParams)
  120. if err != nil {
  121. return "", err
  122. }
  123. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  124. Post(cwsUrl+service.StartWorkFlowMethodName, requestJson)
  125. if err != nil {
  126. return "", err
  127. }
  128. resp := new(response.InfoResponse[string])
  129. err = postResponse.Json(resp)
  130. if err != nil {
  131. return "", err
  132. }
  133. if !resp.Success {
  134. return "", errors.New(resp.Msg)
  135. }
  136. return resp.Info, nil
  137. }
  138. func getWorkTemplateInfoByBusinessType(businessType string, tenantId string) (string, error) {
  139. //查询业务类型配置
  140. queryParams := map[string]string{
  141. "code": businessType,
  142. "tenantId": tenantId,
  143. }
  144. newRequest := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut))
  145. newRequest.SetQueryParams(queryParams)
  146. resp, err := newRequest.Get(cwsUrl + service.GetWorkTemplateByCodeMethodName)
  147. if err != nil {
  148. return "", err
  149. }
  150. respInfo := new(response.InfoResponse[response.BusinessCatalogsInfoWithWorkflowTemplate])
  151. err = resp.Json(respInfo)
  152. if err != nil {
  153. return "", err
  154. }
  155. if !respInfo.Success {
  156. return "", errors.New(respInfo.Msg)
  157. }
  158. return respInfo.Info.WorkTemplateId, nil
  159. }
  160. func LaunchWorkflowTemplatePrepare(reqParams *request.LaunchWorkflowTemplatePrepareParams) (*response.CustomStructureInfo, error) {
  161. requestJson, err := json.Marshal(reqParams)
  162. if err != nil {
  163. return nil, err
  164. }
  165. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  166. Post(cwsUrl+service.StartWorkFlowTemplatePrepareMethodName, requestJson)
  167. if err != nil {
  168. return nil, err
  169. }
  170. resp := new(response.InfosResponse[response.PrepareInfo])
  171. err = postResponse.Json(resp)
  172. if err != nil {
  173. return nil, err
  174. }
  175. if !resp.Success {
  176. return nil, errors.New(resp.Msg)
  177. }
  178. customStructureInfo := &response.CustomStructureInfo{}
  179. for _, info := range resp.Infos {
  180. if info.ID == "userStructureId" {
  181. customStructureInfo.ID = info.Options[0].Value
  182. break
  183. }
  184. }
  185. return customStructureInfo, nil
  186. }
  187. func LaunchWorkflowPrepare(reqParams *request.LaunchWorkflowPrepareParams) (*response.CustomStructureInfo, error) {
  188. requestJson, err := json.Marshal(reqParams)
  189. if err != nil {
  190. return nil, err
  191. }
  192. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  193. Post(cwsUrl+service.StartWorkFlowPrepareMethodName, requestJson)
  194. if err != nil {
  195. return nil, err
  196. }
  197. resp := new(response.InfosResponse[response.PrepareInfo])
  198. err = postResponse.Json(resp)
  199. if err != nil {
  200. return nil, err
  201. }
  202. if !resp.Success {
  203. return nil, errors.New(resp.Msg)
  204. }
  205. customStructureInfo := &response.CustomStructureInfo{}
  206. for _, info := range resp.Infos {
  207. if info.ID == "userStructureId" {
  208. customStructureInfo.ID = info.Options[0].Value
  209. break
  210. }
  211. }
  212. return customStructureInfo, nil
  213. }