应用集成平台服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
8.4 KiB

2 years ago
package taskflow
import (
"appPlatform/models/flowlog"
"appPlatform/models/modelAppPlatform"
"appPlatform/models/modelshr"
"appPlatform/overall"
"appPlatform/overall/publicmethod"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
/*
*
@ 作者: 秦东
@ 时间: 2023-11-22 16:13:21
@ 功能: 发起工作流
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func (a *ApiMethod) StartRunFlow(c *gin.Context) {
var requestData StartFlow
err := c.ShouldBindJSON(&requestData)
if err != nil {
publicmethod.Result(10001, err, c)
return
}
if requestData.Id == "" {
publicmethod.Result(10001, err, c, "未知进程!不可执行")
return
}
if len(requestData.FlowList) < 1 {
publicmethod.Result(10001, err, c, "未知进程!不可执行")
return
}
context, _ := c.Get(overall.MyContJwt)
var userCont modelshr.ManCont
userCont.GetLoginCont(context) //当前操作人
creetTime := time.Now().Unix() //当前时间
var taskInfo modelAppPlatform.Task
taskInfo.GetCont(map[string]interface{}{"`masters_key`": requestData.Id}, "`flow_key`", "`flow_run_sing`")
var flowVersionInfo modelAppPlatform.WorkFlowVersion
flowVersionInfo.GetCont(map[string]interface{}{"`id`": taskInfo.FlowRunSing}, "`content`")
var runFlowInfo modelAppPlatform.RunFlow
uuid := publicmethod.GetUUid(1)
runUuId := publicmethod.GetUUid(6)
runFlowInfo.Id = uuid
runFlowInfo.FlowKey, _ = strconv.ParseInt(requestData.Id, 10, 64) //统一识别符
// runFlowInfo.FlowKey = taskInfo.FlowKey //工作流主体
runFlowInfo.Version = strconv.FormatInt(taskInfo.FlowRunSing, 10) //工作流版本
runFlowInfo.VersionCont = flowVersionInfo.Content //当前工作流内容
runFlowInfo.Creater = userCont.Key //流程发起人
runFlowInfo.Status = 3 //状态:1、草稿;2:驳回;3:审批中;4:归档;5:删除
runFlowInfo.StartTime = creetTime //开始时间
runFlowInfo.UpdateTime = creetTime //更新时间
runFlowInfo.RunKey = runUuId //当前执行识别符
startUs := strconv.FormatInt(userCont.Key, 10) //当前操作人
//执行流程
var runFlow RunWorkFlow
runFlow.Step = 1 //执行第几部
runFlow.FlowList = requestData.FlowList
runFlow.Participant = append(runFlow.Participant, startUs)
runFlow.TotalSteps = len(requestData.FlowList) //流程总长度
runFlow.Uuid = uuid
runFlow.RunUid = runUuId
runFlow.GainRunNode(startUs, 2)
// fmt.Printf("执行完毕--10->%v->%v\n", runFlow.Step, runFlow.NextStep)
flowJsonCont, _ := json.Marshal(runFlow.FlowList) //将步进流转化成json流
runFlowInfo.FlowCont = string(flowJsonCont) //流程执行体
runFlowInfo.CurrentStep = runFlow.Step
runFlowInfo.NextStep = runFlow.NextStep
//参与人去重
var parUser []string
for _, v := range runFlow.Participant {
if !publicmethod.IsInTrue[string](v, parUser) {
parUser = append(parUser, v)
}
}
runFlowInfo.Participants = strings.Join(parUser, ",")
nextRunUser := runFlow.NextRunNodeUser()
runFlowInfo.NextExecutor = strings.Join(nextRunUser, ",")
// xie, _ := json.Marshal(runFlowInfo)
// fmt.Printf("写入数据--10->%v\n", string(xie))
if runFlow.NextStep <= 0 {
runFlowInfo.Status = 4
}
err = runFlowInfo.WriteCont()
if err != nil {
publicmethod.Result(10001, err, c, "流程写入失败!请重新发起")
return
}
if runFlow.NextStep <= 0 {
var taskCont modelAppPlatform.Task
taskCont.EiteCont(map[string]interface{}{"`masters_key`": requestData.Id}, map[string]interface{}{"`status`": 4})
}
publicmethod.Result(0, err, c)
}
/*
*
@ 作者: 秦东
@ 时间: 2023-11-23 10:05:43
@ 功能: 流程执行记录
@ 参数
#uuid 流程uuid
#userKey 操作人
#AgreeToRefuse 同意或者驳回
#nodeKey 操作得节点
#idea 意见
#annex 附件
@ 返回值
#
@ 方法原型
#
*/
func FlowRunLog(uuid, userKey int64, AgreeToRefuse int, nodeKey, idea, annex string) {
creetTime := time.Now().Unix()
var flowRunLog flowlog.WorkFlowLog
flowRunLog.FlowId = uuid //执行的流程"`
flowRunLog.Creater = userKey //操作人"`
flowRunLog.Status = AgreeToRefuse //状态:1、草稿;2:驳回;3:通过;4:归档;5:删除"`
flowRunLog.Content = idea //审批意见"`
flowRunLog.Annex = annex //附件"`
flowRunLog.Time = creetTime //创建时间"`
flowRunLog.EditTime = creetTime //ault编辑时间"`
flowRunLog.NodeKey = nodeKey //操作得节点
flowRunLog.WriteCont()
}
/*
*
@ 作者: 秦东
@ 时间: 2023-11-23 08:59:16
@ 功能: 获取下一步执行人
@ 参数
#userAry 下一步执行人
@ 返回值
#
@ 方法原型
#
*/
func (r *RunWorkFlow) NextRunNodeUser() (userAry []string) {
if r.NextStep <= 0 {
return
}
for _, v := range r.FlowList {
if v.Step == r.Step {
for _, op := range v.Operator {
if !publicmethod.IsInTrue[string](op.Id, userAry) {
userAry = append(userAry, op.Id)
}
}
}
}
return
}
/*
*
@ 作者: 秦东
@ 时间: 2023-11-22 16:57:06
@ 功能: 执行工作流
@ 参数
#userKey //当前执行人
#AgreeToRefuse 同意或者驳回
@ 返回值
#
@ 方法原型
#
*/
func (r *RunWorkFlow) GainRunNode(userKey string, AgreeToRefuse int) {
userkInt, _ := strconv.ParseInt(userKey, 10, 64)
currentStep, nextStep := PaceStep(2, r.TotalSteps)
for i := 0; i < r.TotalSteps; i++ {
if r.FlowList[i].Step == r.Step {
operatorAry, nodeUser := FindOperator(userKey, r.RunUid, AgreeToRefuse, r.FlowList[i].Operator)
r.FlowList[i].Operator = operatorAry
r.FlowList[i].Status = AgreeToRefuse
r.Participant = append(r.Participant, nodeUser...)
switch r.FlowList[i].Types {
case 0:
r.Step = currentStep
r.NextStep = nextStep
FlowRunLog(r.Uuid, userkInt, AgreeToRefuse, r.FlowList[i].NodeKey, "发起流程", "")
if JudgeRunNode(currentStep, r.FlowList) {
r.GainRunNode(userKey, AgreeToRefuse)
}
return
case 1:
r.Step = currentStep
r.NextStep = nextStep
return
case 2:
r.Step = currentStep
r.NextStep = nextStep
// fmt.Printf("执行曹总--0->%v->%v\n", r.Step, r.NextStep)
for _, op := range r.FlowList[i].Operator {
userkIntId, _ := strconv.ParseInt(op.Id, 10, 64)
title := fmt.Sprintf("向%v发送抄送数据", op.Name)
FlowRunLog(r.Uuid, userkIntId, AgreeToRefuse, r.FlowList[i].NodeKey, title, "")
}
if r.NextStep > 0 {
r.GainRunNode(userKey, AgreeToRefuse)
}
return
case 3:
r.Step = currentStep
r.NextStep = nextStep
return
default:
}
}
}
}
/*
*
@ 作者: 秦东
@ 时间: 2023-11-23 08:46:38
@ 功能: 判断当前节点是否要继续执行
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func JudgeRunNode(step int, FlowList []RunFlow) (isRun bool) {
isRun = false
for _, v := range FlowList {
if v.Step == step {
if v.Types == 2 {
isRun = true
}
}
}
return
}
/*
*
@ 作者: 秦东
@ 时间: 2023-11-23 08:35:27
@ 功能: 计算步伐
@ 参数
#
@ 返回值
#
@ 方法原型
#
*/
func PaceStep(step, totalSteps int) (currentStep, nextStep int) {
// fmt.Printf("计算步伐--1-->%v-->%v\n", step, totalSteps)
if step >= totalSteps {
currentStep = totalSteps
nextStep = 0
return
}
// fmt.Printf("计算步伐--2-->%v-->%v\n", currentStep, nextStep)
currentStep = step
if currentStep >= totalSteps {
currentStep = totalSteps
nextStep = 0
return
}
// fmt.Printf("计算步伐--3-->%v-->%v\n", currentStep, nextStep)
nextStep = currentStep + 1
if nextStep >= totalSteps {
nextStep = totalSteps
}
// fmt.Printf("计算步伐--4-->%v-->%v\n", currentStep, nextStep)
return
}
/*
*
@ 作者: 秦东
@ 时间: 2023-11-23 08:21:52
@ 功能: 比对操作人
@ 参数
#userKey 操作人
#runUid 执行Uid
#AgreeToRefuse 同意或者驳回
#operator 当前节点操作人
@ 返回值
#
@ 方法原型
#
*/
func FindOperator(userKey string, runUid int64, AgreeToRefuse int, operator []OperatorList) (OperatorAry []OperatorList, nodeUser []string) {
// isOk := true
var logCont LogList
logCont.State = AgreeToRefuse //状态 1、未操作;2、通过;3、驳回
logCont.TimeVal = publicmethod.UnixTimeToDay(time.Now().Unix(), 1)
logCont.UID = strconv.FormatInt(runUid, 10)
for _, v := range operator {
nodeUser = append(nodeUser, v.Id)
if v.Id == userKey {
// isOk = false
v.LogList = append(v.LogList, logCont)
}
OperatorAry = append(OperatorAry, v)
}
return
}