ng_cws_client.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. ////发起流程
  46. //businessObject := &request.WorkflowBusinessObject{
  47. // BusinessType: reqParams.BusinessType,
  48. // BusinessObject: reqParams.Data,
  49. //}
  50. jsonBytes, err := json.Marshal(reqParams.Data)
  51. if err != nil {
  52. return "", err
  53. }
  54. businessObjectStr := string(jsonBytes)
  55. workflowId, err := StartWorkflow(&request.StartWorkflowRequest{
  56. WorkflowTemplateID: workflowTemplateId,
  57. LaunchUserName: reqParams.LaunchUserName,
  58. Matter: reqParams.Matter,
  59. BusinessObject: businessObjectStr,
  60. UserStructureID: customStructureInfo.ID,
  61. CreateUserID: reqParams.CreateUserID,
  62. TenantID: reqParams.TenantID,
  63. })
  64. if err != nil {
  65. return "", err
  66. }
  67. return workflowId, nil
  68. }
  69. func StartWorkflow(reqParams *request.StartWorkflowRequest) (string, error) {
  70. if utils.IsStringEmpty(cwsUrl) {
  71. return "", errors.New("未配置CWS地址")
  72. }
  73. requestJson, err := json.Marshal(reqParams)
  74. if err != nil {
  75. return "", err
  76. }
  77. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  78. Post(cwsUrl+service.StartWorkFlowMethodName, requestJson)
  79. if err != nil {
  80. return "", err
  81. }
  82. resp := new(response.InfoResponse[string])
  83. err = postResponse.Json(resp)
  84. if err != nil {
  85. return "", err
  86. }
  87. if !resp.Success {
  88. return "", errors.New(resp.Msg)
  89. }
  90. return resp.Info, nil
  91. }
  92. func getWorkTemplateInfoByBusinessType(businessType string, tenantId string) (string, error) {
  93. //查询业务类型配置
  94. queryParams := map[string]string{
  95. "code": businessType,
  96. "tenantId": tenantId,
  97. }
  98. newRequest := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut))
  99. newRequest.SetQueryParams(queryParams)
  100. resp, err := newRequest.Get(cwsUrl + service.GetWorkTemplateByCodeMethodName)
  101. if err != nil {
  102. return "", err
  103. }
  104. respInfo := new(response.InfoResponse[response.BusinessCatalogsInfoWithWorkflowTemplate])
  105. err = resp.Json(respInfo)
  106. if err != nil {
  107. return "", err
  108. }
  109. if !respInfo.Success {
  110. return "", errors.New(respInfo.Msg)
  111. }
  112. return respInfo.Info.WorkTemplateId, nil
  113. }
  114. func LaunchWorkflowPrepare(reqParams *request.LaunchWorkflowPrepareParams) (*response.CustomStructureInfo, error) {
  115. requestJson, err := json.Marshal(reqParams)
  116. if err != nil {
  117. return nil, err
  118. }
  119. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  120. Post(cwsUrl+service.StartWorkFlowPrepareMethodName, requestJson)
  121. if err != nil {
  122. return nil, err
  123. }
  124. resp := new(response.InfosResponse[response.PrepareInfo])
  125. err = postResponse.Json(resp)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if !resp.Success {
  130. return nil, errors.New(resp.Msg)
  131. }
  132. customStructureInfo := &response.CustomStructureInfo{}
  133. for _, info := range resp.Infos {
  134. if info.ID == "userStructureId" {
  135. customStructureInfo.ID = info.Options[0].Value
  136. break
  137. }
  138. }
  139. return customStructureInfo, nil
  140. }