package ng_cws_client import ( "encoding/json" "errors" "git.sxidc.com/go-tools/utils/slice" "git.sxidc.com/service-supports/cws-sdk/service" "git.sxidc.com/service-supports/cws-sdk/service/request" "git.sxidc.com/service-supports/cws-sdk/service/response" "git.sxidc.com/service-supports/cws-sdk/utils" "git.sxidc.com/service-supports/cws-sdk/utils/http_client" "git.sxidc.com/service-supports/cws-sdk/utils/request_util" "strings" "time" ) var NGCwsClient *http_client.Client var cwsUrl string var cwsTimeOut time.Duration func Init(url string, timeout time.Duration) error { if utils.IsStringEmpty(url) { return errors.New("未配置CWS地址") } NGCwsClient = http_client.New() cwsTimeOut = timeout //判断是否有 / if !strings.HasSuffix(url, "/") { url = url + "/" } cwsUrl = url return nil } func Destroy() { http_client.Destroy(NGCwsClient) } func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeRequest) (string, error) { //根据业务类型获取流程模型ID workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(reqParams.BusinessType, reqParams.TenantID) if err != nil { return "", err } if utils.IsStringEmpty(workflowTemplateId) { return "", errors.New("该业务类型未配置流程") } //发起流程前校验 获取人员组织信息 customStructureInfo, err := LaunchWorkflowPrepare(&request.LaunchWorkflowPrepareParams{ WorkflowTemplateID: workflowTemplateId, CreateUserID: reqParams.CreateUserID, TenantID: reqParams.TenantID, }) if err != nil { return "", err } jsonBytes, err := json.Marshal(reqParams.Data) if err != nil { return "", err } businessEntityObjectStr := string(jsonBytes) workflowId, err := StartWorkflow(&request.StartWorkflowRequest{ ID: reqParams.ID, WorkflowTemplateID: workflowTemplateId, LaunchUserName: reqParams.LaunchUserName, Matter: reqParams.Matter, BusinessEntityObject: businessEntityObjectStr, BusinessObject: businessEntityObjectStr, UserStructureID: customStructureInfo.ID, CreateUserID: reqParams.CreateUserID, TenantID: reqParams.TenantID, }) if err != nil { return "", err } return workflowId, nil } func StartWorkFlowWithoutPrepareByBusinessType(reqParams *request.StartWorkFlowWithoutPrepareByBusinessTypeRequest) (string, error) { //根据业务类型获取流程模型ID workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(reqParams.BusinessType, reqParams.TenantID) if err != nil { return "", err } if utils.IsStringEmpty(workflowTemplateId) { return "", errors.New("该业务类型未配置流程") } jsonBytes, err := json.Marshal(reqParams.Data) if err != nil { return "", err } businessEntityObjectStr := string(jsonBytes) workflowId, err := StartWorkflow(&request.StartWorkflowRequest{ ID: reqParams.ID, WorkflowTemplateID: workflowTemplateId, LaunchUserName: reqParams.LaunchUserName, Matter: reqParams.Matter, BusinessEntityObject: businessEntityObjectStr, BusinessObject: businessEntityObjectStr, UserStructureID: reqParams.UserStructureID, CreateUserID: reqParams.CreateUserID, TenantID: reqParams.TenantID, ChoiceApproveInfos: reqParams.ChoiceApproveInfos, }) if err != nil { return "", err } return workflowId, nil } func StartWorkflowTemplate(reqParams *request.StartWorkflowTemplateRequest) (string, error) { if utils.IsStringEmpty(cwsUrl) { return "", errors.New("未配置CWS地址") } //发起流程前校验 获取人员组织信息 customStructureInfo, err := LaunchWorkflowTemplatePrepare(&request.LaunchWorkflowTemplatePrepareParams{ StructureRootID: reqParams.StructureRootID, CreateUserID: reqParams.CreateUserID, TenantID: reqParams.TenantID, }) if err != nil { return "", err } jsonBytes, err := json.Marshal(reqParams.Data) if err != nil { return "", err } businessEntityObjectStr := string(jsonBytes) reqJsonParams := &request.StartWorkflowTemplateRequest{ StructureRootID: reqParams.StructureRootID, WorkflowTemplateName: reqParams.WorkflowTemplateName, Process: reqParams.Process, LaunchUserName: reqParams.LaunchUserName, Matter: reqParams.Matter, BusinessEntityObject: businessEntityObjectStr, BusinessObject: businessEntityObjectStr, UserStructureID: customStructureInfo.ID, CreateUserID: reqParams.CreateUserID, TenantID: reqParams.TenantID, } if err != nil { return "", err } requestJson, err := json.Marshal(reqJsonParams) if err != nil { return "", err } postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)). Post(cwsUrl+service.StartWorkFlowTemplateMethodName, requestJson) if err != nil { return "", err } resp := new(response.InfoResponse[string]) err = postResponse.Json(resp) if err != nil { return "", err } if !resp.Success { return "", errors.New(resp.Msg) } return resp.Info, nil } func StartWorkflow(reqParams *request.StartWorkflowRequest) (string, error) { if utils.IsStringEmpty(cwsUrl) { return "", errors.New("未配置CWS地址") } reqMap := request_util.StructToMap(reqParams) for nodeID, userIDs := range reqParams.ChoiceApproveInfos { reqMap[nodeID] = userIDs } requestJson, err := json.Marshal(reqMap) if err != nil { return "", err } postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)). Post(cwsUrl+service.StartWorkFlowMethodName, requestJson) if err != nil { return "", err } resp := new(response.InfoResponse[string]) err = postResponse.Json(resp) if err != nil { return "", err } if !resp.Success { return "", errors.New(resp.Msg) } return resp.Info, nil } func LaunchWorkflowTemplatePrepare(reqParams *request.LaunchWorkflowTemplatePrepareParams) (*response.CustomStructureInfo, error) { requestJson, err := json.Marshal(reqParams) if err != nil { return nil, err } postResponse, err := NGCwsClient.NewRequest(http_client.WithNewRequestTimeout(cwsTimeOut)). Post(cwsUrl+service.StartWorkFlowTemplatePrepareMethodName, requestJson) if err != nil { return nil, err } resp := new(response.InfosResponse[response.PrepareInfo]) err = postResponse.Json(resp) if err != nil { return nil, err } if !resp.Success { return nil, errors.New(resp.Msg) } customStructureInfo := &response.CustomStructureInfo{} for _, info := range resp.Infos { if info.ID == "userStructureId" { customStructureInfo.ID = info.Options[0].Value break } } return customStructureInfo, nil } func LaunchWorkflowPrepare(reqParams *request.LaunchWorkflowPrepareParams) (*response.CustomStructureInfo, error) { resp, err := launchWorkflowPrepareApi(reqParams) if err != nil { return nil, err } customStructureInfo := &response.CustomStructureInfo{} for _, info := range resp.Infos { if info.ID == "userStructureId" { customStructureInfo.ID = info.Options[0].Value break } } return customStructureInfo, nil } func LaunchWorkflowByBusinessTypePrepare(tenantID string, businessType string, createUserID string) ([]response.PrepareInfo, error) { //根据业务类型获取流程模型ID workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(businessType, tenantID) if err != nil { return nil, err } if utils.IsStringEmpty(workflowTemplateId) { return nil, errors.New("该业务类型未配置流程") } //发起流程前校验 获取人员组织信息 resp, err := launchWorkflowPrepareApi(&request.LaunchWorkflowPrepareParams{ WorkflowTemplateID: workflowTemplateId, CreateUserID: createUserID, TenantID: tenantID, }) if err != nil { return nil, err } return resp.Infos, nil } // 获取流程列表,租户ID必传 func GetWorkflowList(reqParams *request.QueryMyWorkflowParams) (*response.InfosResponse[response.WorkflowInfo], error) { if utils.IsStringEmpty(cwsUrl) { return nil, errors.New("未配置CWS地址") } //根据业务类型获取流程模型ID workflowList, err := getWorkflowListApi(reqParams) if err != nil { return nil, err } return workflowList, nil } // 通过流程ID批量获取流程列表,租户ID必传 func GetWorkflowListByWorkflowIdList(reqParams *request.QueryWorkflowByIDs) (*response.InfosResponse[response.WorkflowInfo], error) { if utils.IsStringEmpty(cwsUrl) { return nil, errors.New("未配置CWS地址") } //根据业务类型获取流程模型ID workflowList, err := getWorkflowListByWorkflowIdListApi(reqParams) if err != nil { return nil, err } return workflowList, nil } // 通过业务类别获取流程列表,租户ID必传 func GetWorkflowListByBusinessType(tenantID string, businessType string, reqParams *request.QueryMyWorkflowParams) (*response.InfosResponse[response.WorkflowInfo], error) { if utils.IsStringEmpty(cwsUrl) { return nil, errors.New("未配置CWS地址") } //根据业务类型获取流程模型ID workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(businessType, tenantID) if err != nil { return nil, err } if utils.IsStringEmpty(workflowTemplateId) { return nil, errors.New("该业务类型未配置流程") } reqParams.TemplateID = workflowTemplateId reqParams.TenantID = tenantID workflowList, err := GetWorkflowList(reqParams) if err != nil { return nil, err } return workflowList, nil } func GetUserStructureID(prepareInfos []response.PrepareInfo) (string, error) { for _, info := range prepareInfos { if info.ID == "userStructureId" { return info.Options[0].Value, nil } } return "", errors.New("数据异常未找到userStructureId") } func ExtractDynamicValueMap(inputModelMap map[string]any, inputParamStruct any) (map[string]any, error) { jsonBytes, err := json.Marshal(inputParamStruct) if err != nil { return nil, err } paramMap := make(map[string]any) err = json.Unmarshal(jsonBytes, ¶mMap) if err != nil { return nil, err } allValues := make([]string, 0) paramValues := make([]string, 0) for k := range inputModelMap { allValues = append(allValues, k) } for k := range paramMap { paramValues = append(paramValues, k) } extraKeys := slice.ExtractASubBSetValue(allValues, paramValues) dynamicValueMap := make(map[string]any) for _, key := range extraKeys { if inputModelAny, ok := inputModelMap[key]; ok { dynamicValueMap[key] = inputModelAny } } return dynamicValueMap, nil } // CheckWorkFlowByBusinessType 根据业务类型判断是否关联流程 func CheckWorkFlowByBusinessType(reqParams *request.CheckWorkFlowByBusinessTypeRequest) (bool, error) { //根据业务类型获取流程模型ID workflowTemplateId, err := getWorkTemplateInfoByBusinessTypeApi(reqParams.BusinessType, reqParams.TenantID) if err != nil { if errors.Is(err, errors.New("查询业务类型流程配置异常")) { return false, nil } return false, err } if utils.IsStringEmpty(workflowTemplateId) { return false, nil } return true, nil }