package taskmanagement import ( "appPlatform/models/customerForm" "appPlatform/models/modelshr" "appPlatform/overall" "appPlatform/overall/publicmethod" "encoding/json" "fmt" "strconv" "strings" "time" "github.com/gin-gonic/gin" ) /* * @ 作者: 秦东 @ 时间: 2024-06-18 09:11:38 @ 功能: 执行工作流 @ 参数 # @ 返回值 # @ 方法原型 # */ func (a *ApiMethod) RunTaskFlow(c *gin.Context) { var requestData SubmitAppResults err := c.ShouldBindJSON(&requestData) if err != nil { publicmethod.Result(100, err, c) return } if requestData.Id == "" { publicmethod.Result(100, err, c) return } if requestData.AgreeOrRefuse == 0 { requestData.AgreeOrRefuse = 2 } where := publicmethod.MapOut[string]() where["`id`"] = requestData.Id var flowInfo customerForm.RunWorkflow err = flowInfo.GetCont(where) if err != nil { publicmethod.Result(107, err, c) return } if flowInfo.NextStep == 0 || flowInfo.Status != 3 { publicmethod.Result(1, err, c, "此流程再不可审批状态!您的提交无效") return } if len(requestData.FlowList) < 1 { if flowInfo.FlowCont != "" { err = json.Unmarshal([]byte(flowInfo.FlowCont), &requestData.FlowList) if err != nil { publicmethod.Result(1, err, c, "流程异常!您的提交无效") return } } else { publicmethod.Result(1, err, c, "流程异常!您的提交无效") return } } context, _ := c.Get(overall.MyContJwt) var userCont modelshr.ManCont userCont.GetLoginCont(context) //当前操作人 //执行流程 var runFlow RunWorkFlow runFlow.Step = flowInfo.NextStep runFlow.TotalSteps = len(requestData.FlowList) //流程总长度 runFlow.Uuid = flowInfo.FlowKey runFlow.RunUid = flowInfo.RunKey runFlow.Participant = strings.Split(flowInfo.Participants, ",") runFlow.FlowList = requestData.FlowList runFlow.RunNodeHandle(userCont.Key, requestData.AgreeOrRefuse, requestData.Suggest) if runFlow.IsRun { publicmethod.Result(1, err, c, runFlow.Msg) return } flowJsonCont, _ := json.Marshal(runFlow.FlowList) //将步进流转化成json流 saveFlowInfo := publicmethod.MapOut[string]() saveTaskInfo := publicmethod.MapOut[string]() saveFlowInfo["`flow_cont`"] = flowJsonCont saveFlowInfo["`current_step`"] = runFlow.Step saveFlowInfo["`next_step`"] = runFlow.NextStep if flowInfo.Participants != "" { oldUser := strings.Split(flowInfo.Participants, ",") runFlow.Participant = append(runFlow.Participant, oldUser...) } //参与人去重 var parUser []string for _, v := range runFlow.Participant { if !publicmethod.IsInTrue[string](v, parUser) { parUser = append(parUser, v) } } saveFlowInfo["`participants`"] = strings.Join(parUser, ",") var tsakInfos customerForm.TaskRecord tsakInfos.GetCont(map[string]interface{}{"`runFlowId`": requestData.Id}, "`participant`") if len(parUser) > 0 { oldTaskUser := strings.Split(tsakInfos.Participant, ",") for _, v := range parUser { if !publicmethod.IsInTrue[string](v, oldTaskUser) { oldTaskUser = append(oldTaskUser, v) } } saveTaskInfo["`participant`"] = strings.Join(parUser, ",") } //抄送人 if flowInfo.MakeCopy != "" { oldCopyUser := strings.Split(flowInfo.MakeCopy, ",") runFlow.MakeCopy = append(runFlow.MakeCopy, oldCopyUser...) } var copyMan []string for _, v := range runFlow.MakeCopy { if !publicmethod.IsInTrue[string](v, copyMan) { copyMan = append(copyMan, v) } } saveFlowInfo["`makeCopy`"] = strings.Join(copyMan, ",") //下一步执行人 nextRunUser := runFlow.NextRunNodeUser(requestData.AgreeOrRefuse) saveFlowInfo["`next_executor`"] = strings.Join(nextRunUser, ",") saveFlowInfo["`update_time`"] = time.Now().Unix() if requestData.AgreeOrRefuse == 1 { if runFlow.NextStep != 0 { saveFlowInfo["`status`"] = 3 saveTaskInfo["`status`"] = 3 } else { saveFlowInfo["`status`"] = 4 saveTaskInfo["`status`"] = 4 JudgeEditFlow(flowInfo.FlowKey) } } else { saveFlowInfo["`runKey`"] = publicmethod.GetUUid(6) if runFlow.Step != 1 { saveFlowInfo["`status`"] = 3 } else { saveFlowInfo["`status`"] = 2 saveTaskInfo["`status`"] = 1 } } saveTaskInfo["`edit_time`"] = time.Now().Unix() gordb := overall.CONSTANT_DB_CustomerForm.Begin() flowErr := gordb.Model(&customerForm.RunWorkflow{}).Where(where).Updates(saveFlowInfo).Error taskErr := gordb.Model(&customerForm.TaskRecord{}).Where("`runFlowId` = ?", requestData.Id).Updates(saveTaskInfo).Error if flowErr != nil || taskErr != nil { gordb.Rollback() publicmethod.Result(100, err, c, "数据提交失败!请重新提交!") return } gordb.Commit() publicmethod.Result(0, err, c) } /* * @ 作者: 秦东 @ 时间: 2024-06-18 09:18:27 @ 功能: 执行节点处理 @ 参数 #userKey 当前处理人 #AgreeToRefuse 1:同意,2:驳回 #Suggest 审批意见 @ 返回值 # @ 方法原型 # */ func (r *RunWorkFlow) RunNodeHandle(userKey int64, AgreeToRefuse int, Suggest string) { //判断流程是否存在 if len(r.FlowList) < 1 { return } userKeyStr := strconv.FormatInt(userKey, 10) //当前操作人识别符转字符类型 fmt.Printf("执行节点处理--->%v------>%v\n", r.Step, r.NextStep) //遍历流程判断当前要执行的步骤 for i := 0; i < r.TotalSteps; i++ { if r.FlowList[i].Step == r.Step { //获取当前操作节点 //Step1:判断当前节点是不是抄送节点 if r.FlowList[i].Types == 2 { currentStep, nextStep := PaceStep(r.RunUid, r.Step, r.TotalSteps, r.FlowList[i]) for _, op := range r.FlowList[i].Operator { r.MakeCopy = append(r.MakeCopy, op.Id) userkIntId, _ := strconv.ParseInt(op.Id, 10, 64) title := fmt.Sprintf("向%v发送抄送数据", op.Name) FlowRunLog(r.Uuid, userkIntId, AgreeToRefuse, r.FlowList[i].NodeKey, title, "") // fmt.Printf("抄送人---->%v---->%v\n", userkIntId, title) } r.Step = currentStep r.NextStep = nextStep if nextStep > 0 { if JudgeRunNode(nextStep, r.FlowList) { r.Step = nextStep r.RunNodeHandle(userKey, AgreeToRefuse, Suggest) } } } else { r.IsRun, r.Msg = JudgeOperUser(userKey, r.RunUid, r.FlowList[i].Operator) //判断操作人是有操作权限或是否已经操作过 if r.IsRun { return } else { if AgreeToRefuse != 1 { //驳回操作 r.FlowList[i].Status = 3 operatorAry, nodeUser := FindOperator(userKeyStr, r.RunUid, 3, r.FlowList[i].Operator, Suggest) r.FlowList[i].Operator = operatorAry r.Participant = append(r.Participant, nodeUser...) r.RejectNode(r.FlowList[i].GoBackNode) return } else { currentStep, nextStep := PaceStep(r.RunUid, r.Step, r.TotalSteps, r.FlowList[i]) fmt.Printf("同意操作---->%v---->%v---->%v\n", currentStep, nextStep, r.FlowList[i].Types) r.FlowList[i].Status = 2 operatorAry, nodeUser := FindOperator(userKeyStr, r.RunUid, 2, r.FlowList[i].Operator, Suggest) r.FlowList[i].Operator = operatorAry r.Participant = append(r.Participant, nodeUser...) switch r.FlowList[i].Types { case 0: r.Step = currentStep r.NextStep = nextStep FlowRunLog(r.Uuid, userKey, 2, r.FlowList[i].NodeKey, "发起流程", "") if JudgeRunNode(nextStep, r.FlowList) { r.Step = nextStep r.FlowStepRun(userKey, AgreeToRefuse, Suggest) } return case 1: r.Step = currentStep r.NextStep = nextStep // fmt.Printf("判断是否继续执行---->%v\n", JudgeRunNode(nextStep, r.FlowList)) FlowRunLog(r.Uuid, userKey, 2, r.FlowList[i].NodeKey, Suggest, "") if JudgeRunNode(nextStep, r.FlowList) { r.Step = nextStep r.RunNodeHandle(userKey, AgreeToRefuse, Suggest) } case 2: if currentStep+1 < r.TotalSteps { r.Step = currentStep + 1 if nextStep+1 <= r.TotalSteps { r.NextStep = nextStep + 1 } else { r.NextStep = 0 } } else { r.Step = r.TotalSteps r.NextStep = 0 } for _, op := range r.FlowList[i].Operator { r.MakeCopy = append(r.MakeCopy, op.Id) userkIntId, _ := strconv.ParseInt(op.Id, 10, 64) title := fmt.Sprintf("向%v发送抄送数据", op.Name) FlowRunLog(r.Uuid, userkIntId, AgreeToRefuse, r.FlowList[i].NodeKey, title, "") } case 3: r.Step = currentStep r.NextStep = nextStep FlowRunLog(r.Uuid, userKey, 2, r.FlowList[i].NodeKey, Suggest, "") if JudgeRunNode(nextStep, r.FlowList) { r.Step = nextStep r.RunNodeHandle(userKey, AgreeToRefuse, Suggest) } default: } } } } } } }