ng_cws_client.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. "git.sxidc.com/service-supports/cws-sdk/utils/request_util"
  11. "strings"
  12. "time"
  13. )
  14. var NGCwsClient *http_client.Client
  15. var cwsUrl string
  16. var cwsTimeOut time.Duration
  17. func Init(url string, timeout time.Duration) error {
  18. if utils.IsStringEmpty(url) {
  19. return errors.New("未配置CWS地址")
  20. }
  21. NGCwsClient = http_client.New()
  22. cwsTimeOut = timeout
  23. //判断是否有 /
  24. if !strings.HasSuffix(url, "/") {
  25. url = url + "/"
  26. }
  27. cwsUrl = url
  28. return nil
  29. }
  30. func Destroy() {
  31. http_client.Destroy(NGCwsClient)
  32. }
  33. func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeRequest) (string, error) {
  34. //根据业务类型获取流程模型ID
  35. workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(reqParams.BusinessType, reqParams.TenantID)
  36. if err != nil {
  37. return "", err
  38. }
  39. if utils.IsStringEmpty(workflowTemplateId) {
  40. return "", errors.New("该业务类型未配置流程")
  41. }
  42. //发起流程前校验 获取人员组织信息
  43. customStructureInfo, err := LaunchWorkflowPrepare(&request.LaunchWorkflowPrepareParams{
  44. WorkflowTemplateID: workflowTemplateId,
  45. CreateUserID: reqParams.CreateUserID,
  46. TenantID: reqParams.TenantID,
  47. })
  48. if err != nil {
  49. return "", err
  50. }
  51. jsonBytes, err := json.Marshal(reqParams.Data)
  52. if err != nil {
  53. return "", err
  54. }
  55. businessEntityObjectStr := string(jsonBytes)
  56. workflowId, err := StartWorkflow(&request.StartWorkflowRequest{
  57. ID: reqParams.ID,
  58. WorkflowTemplateID: workflowTemplateId,
  59. LaunchUserName: reqParams.LaunchUserName,
  60. Matter: reqParams.Matter,
  61. BusinessEntityObject: businessEntityObjectStr,
  62. BusinessObject: businessEntityObjectStr,
  63. UserStructureID: customStructureInfo.ID,
  64. CreateUserID: reqParams.CreateUserID,
  65. TenantID: reqParams.TenantID,
  66. })
  67. if err != nil {
  68. return "", err
  69. }
  70. return workflowId, nil
  71. }
  72. func StartWorkFlowWithoutPrepareByBusinessType(reqParams *request.StartWorkFlowWithoutPrepareByBusinessTypeRequest) (string, error) {
  73. //根据业务类型获取流程模型ID
  74. workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(reqParams.BusinessType, reqParams.TenantID)
  75. if err != nil {
  76. return "", err
  77. }
  78. if utils.IsStringEmpty(workflowTemplateId) {
  79. return "", errors.New("该业务类型未配置流程")
  80. }
  81. jsonBytes, err := json.Marshal(reqParams.Data)
  82. if err != nil {
  83. return "", err
  84. }
  85. businessEntityObjectStr := string(jsonBytes)
  86. workflowId, err := StartWorkflow(&request.StartWorkflowRequest{
  87. ID: reqParams.ID,
  88. WorkflowTemplateID: workflowTemplateId,
  89. LaunchUserName: reqParams.LaunchUserName,
  90. Matter: reqParams.Matter,
  91. BusinessEntityObject: businessEntityObjectStr,
  92. BusinessObject: businessEntityObjectStr,
  93. UserStructureID: reqParams.UserStructureID,
  94. CreateUserID: reqParams.CreateUserID,
  95. TenantID: reqParams.TenantID,
  96. ChoiceApproveInfos: reqParams.ChoiceApproveInfos,
  97. })
  98. if err != nil {
  99. return "", err
  100. }
  101. return workflowId, nil
  102. }
  103. func StartWorkflowTemplate(reqParams *request.StartWorkflowTemplateRequest) (string, error) {
  104. if utils.IsStringEmpty(cwsUrl) {
  105. return "", errors.New("未配置CWS地址")
  106. }
  107. //发起流程前校验 获取人员组织信息
  108. customStructureInfo, err := LaunchWorkflowTemplatePrepare(&request.LaunchWorkflowTemplatePrepareParams{
  109. StructureRootID: reqParams.StructureRootID,
  110. CreateUserID: reqParams.CreateUserID,
  111. TenantID: reqParams.TenantID,
  112. })
  113. if err != nil {
  114. return "", err
  115. }
  116. jsonBytes, err := json.Marshal(reqParams.Data)
  117. if err != nil {
  118. return "", err
  119. }
  120. businessEntityObjectStr := string(jsonBytes)
  121. reqJsonParams := &request.StartWorkflowTemplateRequest{
  122. StructureRootID: reqParams.StructureRootID,
  123. WorkflowTemplateName: reqParams.WorkflowTemplateName,
  124. Process: reqParams.Process,
  125. LaunchUserName: reqParams.LaunchUserName,
  126. Matter: reqParams.Matter,
  127. BusinessEntityObject: businessEntityObjectStr,
  128. BusinessObject: businessEntityObjectStr,
  129. UserStructureID: customStructureInfo.ID,
  130. CreateUserID: reqParams.CreateUserID,
  131. TenantID: reqParams.TenantID,
  132. }
  133. if err != nil {
  134. return "", err
  135. }
  136. requestJson, err := json.Marshal(reqJsonParams)
  137. if err != nil {
  138. return "", err
  139. }
  140. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  141. Post(cwsUrl+service.StartWorkFlowTemplateMethodName, requestJson)
  142. if err != nil {
  143. return "", err
  144. }
  145. resp := new(response.InfoResponse[string])
  146. err = postResponse.Json(resp)
  147. if err != nil {
  148. return "", err
  149. }
  150. if !resp.Success {
  151. return "", errors.New(resp.Msg)
  152. }
  153. return resp.Info, nil
  154. }
  155. func StartWorkflow(reqParams *request.StartWorkflowRequest) (string, error) {
  156. if utils.IsStringEmpty(cwsUrl) {
  157. return "", errors.New("未配置CWS地址")
  158. }
  159. reqMap := request_util.StructToMap(reqParams)
  160. for nodeID, userIDs := range reqParams.ChoiceApproveInfos {
  161. reqMap[nodeID] = userIDs
  162. }
  163. requestJson, err := json.Marshal(reqMap)
  164. if err != nil {
  165. return "", err
  166. }
  167. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  168. Post(cwsUrl+service.StartWorkFlowMethodName, requestJson)
  169. if err != nil {
  170. return "", err
  171. }
  172. resp := new(response.InfoResponse[string])
  173. err = postResponse.Json(resp)
  174. if err != nil {
  175. return "", err
  176. }
  177. if !resp.Success {
  178. return "", errors.New(resp.Msg)
  179. }
  180. return resp.Info, nil
  181. }
  182. func LaunchWorkflowTemplatePrepare(reqParams *request.LaunchWorkflowTemplatePrepareParams) (*response.CustomStructureInfo, error) {
  183. requestJson, err := json.Marshal(reqParams)
  184. if err != nil {
  185. return nil, err
  186. }
  187. postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)).
  188. Post(cwsUrl+service.StartWorkFlowTemplatePrepareMethodName, requestJson)
  189. if err != nil {
  190. return nil, err
  191. }
  192. resp := new(response.InfosResponse[response.PrepareInfo])
  193. err = postResponse.Json(resp)
  194. if err != nil {
  195. return nil, err
  196. }
  197. if !resp.Success {
  198. return nil, errors.New(resp.Msg)
  199. }
  200. customStructureInfo := &response.CustomStructureInfo{}
  201. for _, info := range resp.Infos {
  202. if info.ID == "userStructureId" {
  203. customStructureInfo.ID = info.Options[0].Value
  204. break
  205. }
  206. }
  207. return customStructureInfo, nil
  208. }
  209. func LaunchWorkflowPrepare(reqParams *request.LaunchWorkflowPrepareParams) (*response.CustomStructureInfo, error) {
  210. resp, err := launchWorkflowPrepareApi(reqParams)
  211. if err != nil {
  212. return nil, err
  213. }
  214. customStructureInfo := &response.CustomStructureInfo{}
  215. for _, info := range resp.Infos {
  216. if info.ID == "userStructureId" {
  217. customStructureInfo.ID = info.Options[0].Value
  218. break
  219. }
  220. }
  221. return customStructureInfo, nil
  222. }
  223. func LaunchWorkflowByBusinessTypePrepare(tenantID string, businessType string, createUserID string) ([]response.PrepareInfo, error) {
  224. //根据业务类型获取流程模型ID
  225. workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(businessType, tenantID)
  226. if err != nil {
  227. return nil, err
  228. }
  229. if utils.IsStringEmpty(workflowTemplateId) {
  230. return nil, errors.New("该业务类型未配置流程")
  231. }
  232. //发起流程前校验 获取人员组织信息
  233. resp, err := launchWorkflowPrepareApi(&request.LaunchWorkflowPrepareParams{
  234. WorkflowTemplateID: workflowTemplateId,
  235. CreateUserID: createUserID,
  236. TenantID: tenantID,
  237. })
  238. if err != nil {
  239. return nil, err
  240. }
  241. return resp.Infos, nil
  242. }
  243. // 获取流程列表,租户ID必传
  244. func GetWorkflowList(reqParams *request.QueryMyWorkflowParams) (*response.InfosResponse[response.WorkflowInfo], error) {
  245. if utils.IsStringEmpty(cwsUrl) {
  246. return nil, errors.New("未配置CWS地址")
  247. }
  248. //根据业务类型获取流程模型ID
  249. workflowList, err := getWorkflowListApi(reqParams)
  250. if err != nil {
  251. return nil, err
  252. }
  253. return workflowList, nil
  254. }
  255. // 通过流程ID批量获取流程列表,租户ID必传
  256. func GetWorkflowListByWorkflowIdList(reqParams *request.QueryWorkflowByIDs) (*response.InfosResponse[response.WorkflowInfo], error) {
  257. if utils.IsStringEmpty(cwsUrl) {
  258. return nil, errors.New("未配置CWS地址")
  259. }
  260. //根据业务类型获取流程模型ID
  261. workflowList, err := getWorkflowListByWorkflowIdListApi(reqParams)
  262. if err != nil {
  263. return nil, err
  264. }
  265. return workflowList, nil
  266. }
  267. // 通过业务类别获取流程列表,租户ID必传
  268. func GetWorkflowListByBusinessType(tenantID string, businessType string, reqParams *request.QueryMyWorkflowParams) (*response.InfosResponse[response.WorkflowInfo], error) {
  269. if utils.IsStringEmpty(cwsUrl) {
  270. return nil, errors.New("未配置CWS地址")
  271. }
  272. //根据业务类型获取流程模型ID
  273. workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(businessType, tenantID)
  274. if err != nil {
  275. return nil, err
  276. }
  277. if utils.IsStringEmpty(workflowTemplateId) {
  278. return nil, errors.New("该业务类型未配置流程")
  279. }
  280. reqParams.TemplateID = workflowTemplateId
  281. reqParams.TenantID = tenantID
  282. workflowList, err := GetWorkflowList(reqParams)
  283. if err != nil {
  284. return nil, err
  285. }
  286. return workflowList, nil
  287. }