ng_cws_client.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. var workflowTemplateMap = map[string]string{
  28. "CGJH": "4e0f65ab8547436ba9f744c4dcaef14e",
  29. "CGDD": "5e9bb84779b34d31947107f2c4ead93d",
  30. "CGHT": "d0c7ac374d2a4ed58cd2ee5f1c065110",
  31. "RKZJ": "ffc0b6044e154707ad80d174c08236be",
  32. "RKYS": "bdf8eeaf78c146cd914aef8067dea8b2",
  33. "RK": "2b6338dcdddb4e51b992347e87a1044a",
  34. "XSHT": "2f1997724b4b49c99379a18bf7067cee",
  35. "XSDD": "e73bd99b5761464ea74a612d3eaf3ea7",
  36. "CK": "f82e46c9c4c04862a9babbf3734432e8",
  37. "SCJH": "762685ccfdc3499590d99d3a8d55b78f",
  38. "SCGD": "0d43eaff78bb4abf9d6c5634b74e212e",
  39. "SCZJ": "f5c091e62e6a4cef9a68fda99736fd2e",
  40. "BSD": "0973c19af66546899638d816cf5ee5c6",
  41. "BYD": "9d9762864db14271ad9ada7acc73bacd",
  42. "DBD": "c8aff838c20741758de134903d24934a",
  43. "KHTH": "1e12eb97c9db4e53bb0704bb1c3d93f7",
  44. "GYSTH": "459542e8b07a4cda93d98ba0234c079f",
  45. }
  46. func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeRequest) error {
  47. //根据业务类型获取流程模型ID
  48. workflowTemplateId, err := getWorkTemplateInfoByBusinessType(reqParams.BusinessType, reqParams.TenantID)
  49. if err != nil {
  50. return err
  51. }
  52. if utils.IsStringEmpty(workflowTemplateId) {
  53. return errors.New("该业务类型未配置流程")
  54. }
  55. businessObject := &request.WorkflowBusinessObject{
  56. BusinessType: reqParams.BusinessType,
  57. BusinessObject: reqParams.Data,
  58. }
  59. jsonBytes, err := json.Marshal(businessObject)
  60. if err != nil {
  61. return err
  62. }
  63. businessObjectStr := string(jsonBytes)
  64. err = StartWorkflow(&request.StartWorkflowRequest{
  65. LaunchUserName: reqParams.LaunchUserName,
  66. WorkflowTemplateID: workflowTemplateId,
  67. Matter: reqParams.Matter,
  68. BusinessObject: businessObjectStr,
  69. CreateUserID: reqParams.CreateUserID,
  70. TenantID: reqParams.TenantID,
  71. })
  72. if err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. func StartWorkflow(reqParams *request.StartWorkflowRequest) error {
  78. if utils.IsStringEmpty(cwsUrl) {
  79. return errors.New("未配置CWS地址")
  80. }
  81. requestJson, err := json.Marshal(reqParams)
  82. if err != nil {
  83. return err
  84. }
  85. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  86. Post(cwsUrl+service.StartWorkFlowMethodName, requestJson)
  87. if err != nil {
  88. return err
  89. }
  90. resp := new(response.MsgResponse)
  91. err = postResponse.Json(resp)
  92. if err != nil {
  93. return err
  94. }
  95. if !resp.Success {
  96. return errors.New(resp.Msg)
  97. }
  98. return nil
  99. }
  100. func getWorkTemplateInfoByBusinessType(businessType string, tenantId string) (string, error) {
  101. //查询业务类型配置
  102. queryParams := map[string]string{
  103. "code": businessType,
  104. "tenantId": tenantId,
  105. }
  106. newRequest := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut))
  107. newRequest.SetQueryParams(queryParams)
  108. resp, err := newRequest.Get(cwsUrl + service.GetWorkTemplateByCodeMethodName)
  109. if err != nil {
  110. return "", err
  111. }
  112. respInfo := new(response.InfoResponse[response.BusinessCatalogsInfoWithWorkflowTemplate])
  113. err = resp.Json(respInfo)
  114. if err != nil {
  115. return "", err
  116. }
  117. if !respInfo.Success {
  118. return "", errors.New(respInfo.Msg)
  119. }
  120. return respInfo.Info.WorkTemplateId, nil
  121. }