2020-07-15 01:40:56 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
2020-07-17 01:20:25 +08:00
|
|
|
|
"ferry/global/orm"
|
2020-07-15 01:40:56 +08:00
|
|
|
|
"ferry/models/process"
|
2020-07-17 01:20:25 +08:00
|
|
|
|
"ferry/tools"
|
2020-07-15 01:40:56 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
@Author : lanyulei
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
type WorkOrderData struct {
|
2020-07-17 01:20:25 +08:00
|
|
|
|
process.WorkOrderInfo
|
2020-07-15 01:40:56 +08:00
|
|
|
|
CurrentState string `json:"current_state"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ProcessStructure(c *gin.Context, processId int, workOrderId int) (result map[string]interface{}, err error) {
|
|
|
|
|
var (
|
|
|
|
|
processValue process.Info
|
|
|
|
|
processStructureDetails map[string]interface{}
|
|
|
|
|
processNode []map[string]interface{}
|
2020-07-17 01:20:25 +08:00
|
|
|
|
tplDetails []*process.TplInfo
|
2020-07-15 01:40:56 +08:00
|
|
|
|
workOrderInfo WorkOrderData
|
2020-07-17 01:20:25 +08:00
|
|
|
|
workOrderTpls []*process.TplData
|
|
|
|
|
workOrderHistory []*process.CirculationHistory
|
2020-07-15 01:40:56 +08:00
|
|
|
|
stateList []map[string]interface{}
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-17 01:20:25 +08:00
|
|
|
|
err = orm.Eloquent.Model(&processValue).Where("id = ?", processId).Find(&processValue).Error
|
2021-04-19 16:05:54 +08:00
|
|
|
|
//if err != nil {
|
|
|
|
|
// err = fmt.Errorf("查询流程失败,%v", err.Error())
|
|
|
|
|
// return
|
|
|
|
|
//}
|
2020-07-15 01:40:56 +08:00
|
|
|
|
|
2021-04-19 16:05:54 +08:00
|
|
|
|
if processValue.Structure != nil && len(processValue.Structure) > 0 {
|
|
|
|
|
err = json.Unmarshal([]byte(processValue.Structure), &processStructureDetails)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = fmt.Errorf("json转map失败,%v", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-07-15 01:40:56 +08:00
|
|
|
|
|
2021-04-19 16:05:54 +08:00
|
|
|
|
// 排序,使用冒泡
|
|
|
|
|
p := processStructureDetails["nodes"].([]interface{})
|
|
|
|
|
if len(p) > 1 {
|
|
|
|
|
for i := 0; i < len(p); i++ {
|
|
|
|
|
for j := 1; j < len(p)-i; j++ {
|
|
|
|
|
if p[j].(map[string]interface{})["sort"] == nil || p[j-1].(map[string]interface{})["sort"] == nil {
|
|
|
|
|
return nil, errors.New("流程未定义顺序属性,请确认")
|
|
|
|
|
}
|
|
|
|
|
leftInt, _ := strconv.Atoi(p[j].(map[string]interface{})["sort"].(string))
|
|
|
|
|
rightInt, _ := strconv.Atoi(p[j-1].(map[string]interface{})["sort"].(string))
|
|
|
|
|
if leftInt < rightInt {
|
|
|
|
|
//交换
|
|
|
|
|
p[j], p[j-1] = p[j-1], p[j]
|
|
|
|
|
}
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-19 16:05:54 +08:00
|
|
|
|
for _, node := range processStructureDetails["nodes"].([]interface{}) {
|
|
|
|
|
processNode = append(processNode, node.(map[string]interface{}))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
processNode = processStructureDetails["nodes"].([]map[string]interface{})
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processValue.Structure = nil
|
|
|
|
|
result = map[string]interface{}{
|
|
|
|
|
"process": processValue,
|
|
|
|
|
"nodes": processNode,
|
|
|
|
|
"edges": processStructureDetails["edges"],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取历史记录
|
2020-07-17 01:20:25 +08:00
|
|
|
|
err = orm.Eloquent.Model(&process.CirculationHistory{}).
|
2020-07-15 01:40:56 +08:00
|
|
|
|
Where("work_order = ?", workOrderId).
|
|
|
|
|
Order("id desc").
|
|
|
|
|
Find(&workOrderHistory).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
result["circulationHistory"] = workOrderHistory
|
|
|
|
|
|
|
|
|
|
if workOrderId == 0 {
|
|
|
|
|
// 查询流程模版
|
|
|
|
|
var tplIdList []int
|
|
|
|
|
err = json.Unmarshal(processValue.Tpls, &tplIdList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = fmt.Errorf("json转map失败,%v", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-07-17 01:20:25 +08:00
|
|
|
|
err = orm.Eloquent.Model(&tplDetails).
|
2020-07-15 01:40:56 +08:00
|
|
|
|
Where("id in (?)", tplIdList).
|
|
|
|
|
Find(&tplDetails).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = fmt.Errorf("查询模版失败,%v", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
result["tpls"] = tplDetails
|
|
|
|
|
} else {
|
|
|
|
|
// 查询工单信息
|
2020-07-17 01:20:25 +08:00
|
|
|
|
err = orm.Eloquent.Model(&process.WorkOrderInfo{}).
|
2020-07-15 01:40:56 +08:00
|
|
|
|
Where("id = ?", workOrderId).
|
|
|
|
|
Scan(&workOrderInfo).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 获取当前节点
|
|
|
|
|
err = json.Unmarshal(workOrderInfo.State, &stateList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = fmt.Errorf("序列化节点列表失败,%v", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(stateList) == 0 {
|
|
|
|
|
err = errors.New("当前工单没有下一节点数据")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 整理需要并行处理的数据
|
|
|
|
|
if len(stateList) > 1 {
|
|
|
|
|
continueHistoryTag:
|
|
|
|
|
for _, v := range workOrderHistory {
|
|
|
|
|
status := false
|
|
|
|
|
for i, s := range stateList {
|
|
|
|
|
if v.Source == s["id"].(string) && v.Target != "" {
|
|
|
|
|
status = true
|
|
|
|
|
stateList = append(stateList[:i], stateList[i+1:]...)
|
|
|
|
|
continue continueHistoryTag
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !status {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(stateList) > 0 {
|
|
|
|
|
breakStateTag:
|
|
|
|
|
for _, stateValue := range stateList {
|
2021-04-19 16:05:54 +08:00
|
|
|
|
if processStructureDetails["nodes"] != nil {
|
|
|
|
|
for _, processNodeValue := range processStructureDetails["nodes"].([]interface{}) {
|
|
|
|
|
if stateValue["id"].(string) == processNodeValue.(map[string]interface{})["id"] {
|
|
|
|
|
if _, ok := stateValue["processor"]; ok {
|
|
|
|
|
for _, userId := range stateValue["processor"].([]interface{}) {
|
|
|
|
|
if int(userId.(float64)) == tools.GetUserId(c) {
|
|
|
|
|
workOrderInfo.CurrentState = stateValue["id"].(string)
|
|
|
|
|
break breakStateTag
|
|
|
|
|
}
|
2020-10-09 00:58:14 +08:00
|
|
|
|
}
|
2021-04-19 16:05:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
err = errors.New("未查询到对应的处理人字段,请确认。")
|
|
|
|
|
return
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if workOrderInfo.CurrentState == "" {
|
|
|
|
|
workOrderInfo.CurrentState = stateList[0]["id"].(string)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result["workOrder"] = workOrderInfo
|
|
|
|
|
|
|
|
|
|
// 查询工单表单数据
|
2020-07-17 01:20:25 +08:00
|
|
|
|
err = orm.Eloquent.Model(&workOrderTpls).
|
2020-07-15 01:40:56 +08:00
|
|
|
|
Where("work_order = ?", workOrderId).
|
|
|
|
|
Find(&workOrderTpls).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
result["tpls"] = workOrderTpls
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|