package knowledge_approve import ( "encoding/json" "eta_gn/eta_api/models/knowledge" knowledge_approve "eta_gn/eta_api/models/knowledge_approve" "eta_gn/eta_api/models/knowledge_approve/request" "eta_gn/eta_api/models/knowledge_approve/response" "eta_gn/eta_api/utils" "fmt" "sort" "strings" "time" ) // GetKnowledgeResourceApproveFlowList 获取知识资源审批流列表 func GetKnowledgeResourceApproveFlowList(condition string, pars []interface{}, startSize, pageSize int) (items []*response.KnowledgeResourceApproveFlowItem, total int, msg string, err error) { total, err = knowledge_approve.GetKnowledgeResourceApproveFlowCountByCondition(condition, pars) if err != nil { msg = "获取审批流程列表失败" return } if total == 0 { items = make([]*response.KnowledgeResourceApproveFlowItem, 0) return } flowList, err := knowledge_approve.GetKnowledgeResourceApproveFlowByCondition(condition, pars, startSize, pageSize) if err != nil { msg = "获取审批流程列表失败" return } obj := new(knowledge.KnowledgeClassify) classifyList, err := obj.GetAllClassify(knowledge.KnowledgeResourceTypeOpinion) if err != nil { msg = "获取审批流程列表失败" err = fmt.Errorf("获取分类列表失败,%+v", err) return } sort.Slice(classifyList, func(i, j int) bool { if classifyList[i].Level != classifyList[j].Level { return classifyList[i].Level < classifyList[j].Level } return classifyList[i].ClassifyId < classifyList[j].ClassifyId }) classifyMap := make(map[int]*knowledge.KnowledgeClassify) for _, classify := range classifyList { classifyMap[classify.ClassifyId] = classify } classifyPathMap := make(map[int]map[int]string) for _, classify := range classifyList { if _, ok := classifyPathMap[classify.ClassifyId]; !ok { classifyPathMap[classify.ClassifyId] = make(map[int]string) } currClassifyMap := classifyPathMap[classify.ClassifyId] currClassify := classify for i := classify.Level; i > 0; i-- { currClassifyMap[currClassify.Level] = currClassify.ClassifyName if currClassify.ParentId == 0 { break } currClassify = classifyMap[currClassify.ParentId] } } items = toKnowledgeResourceApproveFlowItem(flowList, classifyPathMap) return } // SaveKnowledgeResourceApproveFlow 保存审批流 func SaveKnowledgeResourceApproveFlow(flow *request.KnowledgeResourceApproveFlowSaveReq) (ok bool, msg string, err error) { obj := new(knowledge.KnowledgeClassify) _, err = obj.GetClassifyById(flow.ClassifyId) if err != nil { if utils.IsErrNoRow(err) { msg = "保存审批流失败, 分类不存在" return } msg = "保存审批流失败" return } if flow.KnowledgeResourceApproveFlowId == 0 { tmp, er := knowledge_approve.GetKnowledgeResourceApproveFlowByClassifyId(flow.ClassifyId) if er != nil && !utils.IsErrNoRow(er) { msg = "保存审批流失败" err = er return } if tmp != nil && tmp.KnowledgeResourceApproveFlowId > 0 { msg = "保存审批流失败, 分类下已存在审批流" err = fmt.Errorf("分类下已存在审批流") return } t := &knowledge_approve.KnowledgeResourceApproveFlow{ FlowName: flow.FlowName, ClassifyId: flow.ClassifyId, CurrVersion: 1, CreateTime: time.Now(), ModifyTime: time.Now(), } knowledgeFlowNodeItems := make([]*knowledge_approve.KnowledgeResourceApproveNode, 0) for _, node := range flow.Nodes { knowledgeFlowNode := new(knowledge_approve.KnowledgeResourceApproveNode) knowledgeFlowNode.ApproveType = node.ApproveType knowledgeFlowNode.CurrVersion = t.CurrVersion userBytes, er := json.Marshal(node.Users) if er != nil { err = er msg = "保存审批流失败" return } knowledgeFlowNode.Users = string(userBytes) knowledgeFlowNode.CreatedTime = time.Now() knowledgeFlowNodeItems = append(knowledgeFlowNodeItems, knowledgeFlowNode) } err = t.Add(knowledgeFlowNodeItems) if err != nil { msg = "保存审批流失败" return } ok = true } else { resFlow, er := knowledge_approve.GetKnowledgeResourceApproveFlowById(flow.KnowledgeResourceApproveFlowId) if er != nil { msg = "保存审批流失败" err = er return } ok, err = CheckDeleteKnowledgeResourceApproveFlow(resFlow.KnowledgeResourceApproveFlowId) if err != nil { msg = "保存审批流失败" return } if !ok { msg = "保存审批流失败, 存在还未审批的报告" return } var updateCols []string if resFlow.FlowName != flow.FlowName { resFlow.FlowName = flow.FlowName updateCols = append(updateCols, "flow_name") } resFlow.CurrVersion += 1 resFlow.ModifyTime = time.Now() updateCols = append(updateCols, "modify_time", "curr_version") knowledgeFlowNodeItems := make([]*knowledge_approve.KnowledgeResourceApproveNode, 0) for _, node := range flow.Nodes { knowledgeFlowNode := new(knowledge_approve.KnowledgeResourceApproveNode) knowledgeFlowNode.ApproveType = node.ApproveType knowledgeFlowNode.CurrVersion = resFlow.CurrVersion userBytes, er := json.Marshal(node.Users) if er != nil { err = er msg = "保存审批流失败" return } knowledgeFlowNode.Users = string(userBytes) knowledgeFlowNode.CreatedTime = time.Now() knowledgeFlowNodeItems = append(knowledgeFlowNodeItems, knowledgeFlowNode) } err = resFlow.Update(updateCols, knowledgeFlowNodeItems) if err != nil { msg = "保存审批流失败" return } ok = true } return } // GetKnowledgeResourceApproveFlowDetail 获取审批流详情 func GetKnowledgeResourceApproveFlowDetail(flowId int) (detail *response.KnowledgeResourceApproveFlowDetailResp, msg string, err error) { flowInfo, err := knowledge_approve.GetKnowledgeResourceApproveFlowById(flowId) if err != nil { msg = "获取审批流详情失败" return } flowNodes, err := knowledge_approve.GetKnowledgeResourceApproveNodeByFlowIdAndVersionId(flowId, flowInfo.CurrVersion) if err != nil { msg = "获取审批流详情失败" return } detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes) if err != nil { msg = "获取审批流详情失败" return } return } // DeleteKnowledgeResourceApproveFlow 删除审批流 func DeleteKnowledgeResourceApproveFlow(flowId int) (ok bool, msg string, err error) { ok, err = CheckDeleteKnowledgeResourceApproveFlow(flowId) if err != nil { if utils.IsErrNoRow(err) { msg = "删除审批流失败, 审批流不存在" return } msg = "删除审批流失败" return } if !ok { msg = "删除审批流失败, 存在还未审批的报告" return } t := &knowledge_approve.KnowledgeResourceApproveFlow{ KnowledgeResourceApproveFlowId: flowId, } err = t.Delete() if err != nil { msg = "删除审批流失败" return } ok = true return } // CheckDeleteKnowledgeResourceApproveFlow 检查是否可以删除审批流 func CheckDeleteKnowledgeResourceApproveFlow(flowId int) (ok bool, err error) { flowInfo, err := knowledge_approve.GetKnowledgeResourceApproveFlowById(flowId) if err != nil { return } // 检查是否存在还未审批的知识资源 approveList, err := knowledge_approve.GetKnowledgeResourceApproveByFlowIdAndVersionId(flowInfo.KnowledgeResourceApproveFlowId, flowInfo.CurrVersion) if err != nil { return } for _, v := range approveList { if v.State == KnowledgeResourceApproveStateApproving { return false, nil } } ok = true return } func toKnowledgeResourceApproveFlowItem(src []*knowledge_approve.KnowledgeResourceApproveFlow, classifyPathMap map[int]map[int]string) (res []*response.KnowledgeResourceApproveFlowItem) { res = make([]*response.KnowledgeResourceApproveFlowItem, 0, len(src)) for _, item := range src { classifyArr := make([]string, 0) for i := 1; i <= 3; i++ { if v, ok := classifyPathMap[item.ClassifyId][i]; ok { classifyArr = append(classifyArr, v) } } res = append(res, &response.KnowledgeResourceApproveFlowItem{ KnowledgeResourceApproveFlowId: item.KnowledgeResourceApproveFlowId, FlowName: item.FlowName, ClassifyId: item.ClassifyId, ClassifyName: strings.Join(classifyArr, "/"), CurrVersion: item.CurrVersion, CreateTime: item.CreateTime.Format(utils.FormatDateTime), ModifyTime: item.ModifyTime.Format(utils.FormatDateTime), }) } return } func FormatFlowAndNodesItem2Detail(flowItem *knowledge_approve.KnowledgeResourceApproveFlow, nodeItems []*knowledge_approve.KnowledgeResourceApproveNode) (detail *response.KnowledgeResourceApproveFlowDetailResp, err error) { if flowItem == nil { return } detail = new(response.KnowledgeResourceApproveFlowDetailResp) detail.KnowledgeResourceApproveFlowId = flowItem.KnowledgeResourceApproveFlowId detail.FlowName = flowItem.FlowName detail.ClassifyId = flowItem.ClassifyId detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime) detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime) detail.Nodes = make([]*response.KnowledgeResourceApproveNodeItem, 0) for _, v := range nodeItems { t, e := FormatKnowledgeResourceApproveNode2Item(v) if e != nil { err = fmt.Errorf("format node err: %s", e.Error()) return } detail.Nodes = append(detail.Nodes, t) } return } // func ModifyKnowledgeResourceApproveFlowClassify(classifyId int, classifyName string) (err error) { // flow, err := knowledge_approve.GetKnowledgeResourceApproveFlowByClassifyId(classifyId) // if err != nil && !utils.IsErrNoRow(err) { // return // } // if utils.IsErrNoRow(err) { // err = nil // return // } // if classifyName != "" && flow.ClassifyName != classifyName { // err = knowledge_approve.UpdateFlowClassifyName(classifyId, classifyName) // } // return // }