package knowledge_approve import ( "encoding/json" "eta_gn/eta_api/models/knowledge" "eta_gn/eta_api/models/knowledge_approve" "eta_gn/eta_api/models/knowledge_approve/response" knowledgeSrv "eta_gn/eta_api/services/knowledge" "eta_gn/eta_api/utils" "fmt" "sort" "strings" "time" ) var ( timeField = map[int]string{1: fmt.Sprintf("a.%s", knowledge_approve.KnowledgeResourceApproveRecordCols.CreateTime), 2: fmt.Sprintf("a.%s", knowledge_approve.KnowledgeResourceApproveRecordCols.NodeApproveTime), 3: fmt.Sprintf("b.%s", knowledge_approve.KnowledgeResourceApproveCols.ApproveTime)} myTimeField = map[int]string{1: knowledge_approve.KnowledgeResourceApproveCols.CreateTime, 3: knowledge_approve.KnowledgeResourceApproveCols.ApproveTime} orderRules = map[int]string{1: "ASC", 2: "DESC"} ) func GetPulicKnowledgeClassifyList(resourceType int) (res []*knowledge.KnowledgeClassifyItem, msg string, err error) { obj := new(knowledge.KnowledgeClassify) classifyList, err := obj.GetAllClassify(resourceType) if err != nil { msg = "获取公共分类列表失败" return } res = toClassifyTree(classifyList, 0) return } func PassKnowledgeResourceApprove(approveId int, adminId int) (msg string, err error) { approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(approveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批不存在, 请刷新页面" err = e return } msg = "操作失败" return } if approveItem.State != KnowledgeResourceApproveStateApproving { msg = "审批状态有误, 请刷新页面" err = fmt.Errorf("审批状态有误, State: %d", approveItem.State) return } var ApprovePars []interface{} Approveconds := ` AND knowledge_resource_approve_id =? AND approve_user_id =? AND state =? ` ApprovePars = append(ApprovePars, approveId, adminId, KnowledgeResourceApproveStateApproving) recordItem, er := knowledge_approve.GetKnowledgeResourceApproveRecordByCondition(Approveconds, ApprovePars) if er != nil { if utils.IsErrNoRow(er) { msg = "无权审批" err = er return } msg = "操作失败" return } // 查询审批流和审批流节点 flowItem, e := knowledge_approve.GetKnowledgeResourceApproveFlowById(approveItem.FlowId) if e != nil { err = fmt.Errorf("获取审批流失败, Err: %s", e.Error()) return } nodePars := make([]interface{}, 0) nodeCond := ` AND knowledge_resource_approve_flow_id =? AND curr_version =? ` nodePars = append(nodePars, flowItem.KnowledgeResourceApproveFlowId, flowItem.CurrVersion) nodeItems, e := knowledge_approve.GetKnowledgeResourceApproveNodeByCondition(nodeCond, nodePars) if e != nil { err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error()) return } if len(nodeItems) == 0 { err = fmt.Errorf("无审批节点") return } nodeMap := make(map[int]*knowledge_approve.KnowledgeResourceApproveNode) for _, v := range nodeItems { nodeMap[v.KnowledgeResourceApproveNodeId] = v } // 取出审批记录的节点 currNodeItem := nodeMap[recordItem.NodeId] if currNodeItem == nil { err = fmt.Errorf("当前节点信息有误") return } currNode, e := FormatKnowledgeResourceApproveNode2Item(currNodeItem) if e != nil { err = fmt.Errorf("当前节点信息有误, Err: %s", e.Error()) return } now := time.Now().Local() recordItem.State = KnowledgeResourceApproveStatePass recordItem.ApproveTime = now recordItem.ModifyTime = now recordItem.NodeState = KnowledgeResourceApproveStatePass recordItem.NodeApproveUserId = recordItem.ApproveUserId recordItem.NodeApproveUserName = recordItem.ApproveUserName recordItem.NodeApproveTime = now recordCols := []string{"State", "ApproveTime", "ModifyTime", "NodeState", "NodeApproveUserId", "NodeApproveUserName", "NodeApproveTime"} lastApprove := false // 依次审批 if currNode.ApproveType == NodeApproveTypeRoll { if e = recordItem.Update(recordCols); e != nil { err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error()) return } // 检查依次审批情况 sort.Slice(currNode.Users, func(k, j int) bool { return currNode.Users[k].Sort < currNode.Users[j].Sort }) userLen := len(currNode.Users) //lastRoll := false nextUser := new(response.KnowledgeResourceApproveNodeUser) // 下一个审批人, 为nil则表示当前审批人即为最后 for k, v := range currNode.Users { // 当前审批人 if v.UserId == adminId && recordItem.ApproveUserSort == v.Sort { if (k + 1) < userLen { nextUser = currNode.Users[k+1] } } } // 当前节点下一个审批人, 生成下一个审批记录且return if nextUser.UserId > 0 { newRecord := new(knowledge_approve.KnowledgeResourceApproveRecord) newRecord.KnowledgeResourceApproveId = recordItem.KnowledgeResourceApproveId newRecord.State = KnowledgeResourceApproveStateApproving newRecord.NodeId = currNode.KnowledgeResourceApproveNodeId newRecord.PrevNodeId = currNode.PrevNodeId newRecord.NextNodeId = currNode.NextNodeId newRecord.ApproveType = currNode.ApproveType newRecord.ApproveUserId = nextUser.UserId newRecord.ApproveUserName = nextUser.UserName newRecord.ApproveUserSort = nextUser.Sort newRecord.CreateTime = now newRecord.ModifyTime = now newRecord.NodeState = KnowledgeResourceApproveStateApproving if e = newRecord.Create(); e != nil { err = fmt.Errorf("生成审批记录失败, Err: %s", e.Error()) return } // 推送审批消息 go func() { messageItem := new(knowledge_approve.KnowledgeResourceApproveMessage) messageItem.SendUserId = approveItem.ApplyUserId messageItem.ReceiveUserId = nextUser.UserId messageItem.Content = "您有新的待办任务" messageItem.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle) messageItem.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId messageItem.ApproveState = KnowledgeResourceApproveStateApproving messageItem.CreateTime = now messageItem.ModifyTime = now if e = messageItem.Create(); e != nil { utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove message err: %s", e.Error())) return } }() return } // 更新审批当前节点并进入下一个节点 if currNode.NextNodeId > 0 { nextNode := nodeMap[currNode.NextNodeId] approveItem.CurrNodeId = currNode.NextNodeId approveItem.ModifyTime = now if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil { err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error()) return } err = BuildNextNodeRecordAndMsg(nextNode, approveItem.KnowledgeResourceApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle) return } else { // 最后一个节点 lastApprove = true } } // 会签 if currNode.ApproveType == NodeApproveTypeAll { // 查询其他审批人是否已审批 otherCond := ` AND knowledge_resource_approve_id =? AND node_id =? AND approve_user_id <> ? ` otherPars := make([]interface{}, 0) otherPars = append(otherPars, approveItem.KnowledgeResourceApproveId, recordItem.NodeId, adminId) otherRecords, e := knowledge_approve.GetKnowledgeResourceApproveRecordItemsByCondition(otherCond, otherPars) if e != nil { err = fmt.Errorf("获取节点审批记录失败, Err: %s", e.Error()) return } otherPass := true for _, v := range otherRecords { if v.State != KnowledgeResourceApproveStatePass { otherPass = false } } // 其他人未审批, 仅更新当前审批记录 if e = recordItem.Update(recordCols); e != nil { err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error()) return } // 其他人已审批且为最后节点 if otherPass && currNode.NextNodeId == 0 { lastApprove = true } // 其他人已审批且不为最后节点, 进入下一节点 if otherPass && currNode.NextNodeId > 0 { nextNode := nodeMap[currNode.NextNodeId] approveItem.CurrNodeId = currNode.NextNodeId approveItem.ModifyTime = now if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil { err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error()) return } err = BuildNextNodeRecordAndMsg(nextNode, approveItem.KnowledgeResourceApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle) return } } // 或签 if currNode.ApproveType == NodeApproveTypeAny { // 需检查一下审批的当前节点和记录的节点是否匹配, 不匹配可能是因为另外的审批人已通过, 所以此处应给提示 // 前端也有做相应的判断,但是两个人同时进入审批详情页时就可能出现这种情况 if approveItem.CurrNodeId != recordItem.NodeId { msg = "该节点已完成审批, 请刷新页面" return } if e = recordItem.Update(recordCols); e != nil { err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error()) return } // 将该审批的同一个节点的记录标记为已审批 if e = recordItem.UpdateNodeState(recordItem.KnowledgeResourceApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil { err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error()) return } if currNode.NextNodeId == 0 { lastApprove = true } if currNode.NextNodeId > 0 { nextNode := nodeMap[currNode.NextNodeId] approveItem.CurrNodeId = currNode.NextNodeId approveItem.ModifyTime = now if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil { err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error()) return } err = BuildNextNodeRecordAndMsg(nextNode, approveItem.KnowledgeResourceApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle) return } } // 最后一个审批, 更新审批记录、审批、报告状态、推送消息给申请人 if lastApprove { if e = recordItem.Update(recordCols); e != nil { err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error()) return } approveItem.State = KnowledgeResourceApproveStatePass approveItem.ApproveTime = now approveItem.ModifyTime = now approveCols := []string{"State", "ApproveTime", "ModifyTime"} if e = approveItem.Update(approveCols); e != nil { err = fmt.Errorf("更新审批信息失败, Err: %s", e.Error()) return } if e = updateKnowledgeResourceApproveState(approveItem.KnowledgeResourceId, KnowledgeStatePass); e != nil { err = fmt.Errorf("更新报告审批状态失败, Err: %s", e.Error()) return } go func() { obj := new(knowledge.KnowledgeResource) KnowledgeItem, e := obj.GetById(approveItem.KnowledgeResourceId) if e != nil && !utils.IsErrNoRow(e) { utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update knowledge resource state err: %s", e.Error())) return } e = knowledgeSrv.EsAddOrEditKnowledgeResource(KnowledgeItem) if e != nil { utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update es err: %s", e.Error())) return } }() go func() { messageItem := new(knowledge_approve.KnowledgeResourceApproveMessage) messageItem.SendUserId = adminId messageItem.ReceiveUserId = approveItem.ApplyUserId messageItem.Content = "您提交的审批已通过" messageItem.Remark = fmt.Sprintf("您提交的【%s】已通过", approveItem.KnowledgeResourceTitle) messageItem.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId messageItem.ApproveState = KnowledgeResourceApproveStatePass messageItem.CreateTime = now messageItem.ModifyTime = now if e = messageItem.Create(); e != nil { utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove message err: %s", e.Error())) return } }() } return } func toClassifyTree(src []*knowledge.KnowledgeClassify, parentId int) (dst []*knowledge.KnowledgeClassifyItem) { for _, item := range src { if item.ParentId == parentId { tmp := new(knowledge.KnowledgeClassifyItem) tmp.KnowledgeClassify = *item tmp.Child = toClassifyTree(src, item.ClassifyId) dst = append(dst, tmp) } } return } // BuildNextNodeRecordAndMsg 生成下一个节点的审批记录并推送消息 func BuildNextNodeRecordAndMsg(approveNodeItem *knowledge_approve.KnowledgeResourceApproveNode, approveId, sysAdminId int, sysAdminName, knowledgeTitle string) (err error) { if approveNodeItem == nil { err = fmt.Errorf("approve node nil") return } // 根据节点审批方式生成审批记录 now := time.Now().Local() approveNode, e := FormatKnowledgeResourceApproveNode2Item(approveNodeItem) if e != nil { err = fmt.Errorf("FormatKnowledgeResourceApproveNode2Item err: %s", e.Error()) return } if len(approveNode.Users) == 0 { err = fmt.Errorf("审批节点用户有误") return } newRecords := make([]*knowledge_approve.KnowledgeResourceApproveRecord, 0) sort.Slice(approveNode.Users, func(k, j int) bool { return approveNode.Users[k].Sort < approveNode.Users[j].Sort }) for _, u := range approveNode.Users { r := new(knowledge_approve.KnowledgeResourceApproveRecord) r.KnowledgeResourceApproveId = approveId r.State = KnowledgeResourceApproveStateApproving r.NodeId = approveNode.KnowledgeResourceApproveNodeId r.PrevNodeId = approveNode.PrevNodeId r.NextNodeId = approveNode.NextNodeId r.ApproveType = approveNode.ApproveType r.ApproveUserId = u.UserId r.ApproveUserName = u.UserName r.ApproveUserSort = u.Sort r.CreateTime = now r.ModifyTime = now r.NodeState = KnowledgeResourceApproveStateApproving // 当前节点审批状态 newRecords = append(newRecords, r) // 依次审批仅生成一条记录 if approveNode.ApproveType == NodeApproveTypeRoll { break } } recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord) if e = recordOb.CreateMulti(newRecords); e != nil { err = fmt.Errorf("生成节点审批记录失败, Err: %s", e.Error()) return } // 推送审批消息 go func() { messageOb := new(knowledge_approve.KnowledgeResourceApproveMessage) messages := make([]*knowledge_approve.KnowledgeResourceApproveMessage, 0) for _, v := range newRecords { m := new(knowledge_approve.KnowledgeResourceApproveMessage) m.SendUserId = sysAdminId m.ReceiveUserId = v.ApproveUserId m.Content = "您有新的待办任务" m.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", sysAdminName, knowledgeTitle) m.KnowledgeResourceApproveId = approveId m.ApproveState = KnowledgeResourceApproveStateApproving m.CreateTime = now m.ModifyTime = now messages = append(messages, m) } e = messageOb.CreateMulti(messages) if e != nil { utils.FileLog.Info(fmt.Sprintf("BuildNextNodeRecordAndMsg messages err: %s", e.Error())) return } }() return } // updateKnowledgeResourceApproveState 更新知识资源审批状态 func updateKnowledgeResourceApproveState(KnowledgeResourceId, state int) (err error) { // updateCols := []string{"ApproveId", "State", "ModifyTime"} updateCols := []string{"State", "ModifyTime"} obj := new(knowledge.KnowledgeResource) KnowledgeItem, e := obj.GetById(KnowledgeResourceId) if e != nil && !utils.IsErrNoRow(e) { err = fmt.Errorf("获取知识资源失败, Err: %s", e.Error()) return } if KnowledgeItem != nil && KnowledgeItem.KnowledgeResourceId > 0 { KnowledgeItem.State = state KnowledgeItem.ModifyTime = time.Now().Local() if e = KnowledgeItem.Update(updateCols); e != nil { err = fmt.Errorf("更新知识资源审批状态失败, Err: %s", e.Error()) return } } return } func ProcessingKnowledgeResourceApprove(adminId, classifyId, timeType, sortField, sortRule, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.KnowledgeResourceApproveItemOrmResp, respTotal int, msg string, err error) { cond := fmt.Sprintf(` AND a.%s = ? AND b.%s = ? AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.State, knowledge_approve.KnowledgeResourceApproveCols.State, knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveUserId) pars := make([]interface{}, 0) pars = append(pars, KnowledgeResourceApproveStateApproving, KnowledgeResourceApproveStateApproving, adminId) order := "" // 筛选条件 if classifyId > 0 { cond += fmt.Sprintf(` AND b.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ClassifyId) pars = append(pars, classifyId) } if timeType <= 0 { timeType = 1 } if timeType == 1 && startTime != "" && endTime != "" { _, e := time.Parse(utils.FormatDate, startTime) if e != nil { msg = "开始时间格式有误" err = e return } tmpEndTime, e := time.Parse(utils.FormatDate, endTime) if e != nil { msg = "结束时间格式有误" err = e return } tmpEndTime = tmpEndTime.AddDate(0, 0, 1) cond += fmt.Sprintf(` AND (b.%s BETWEEN ? AND ?)`, knowledge_approve.KnowledgeResourceApproveCols.CreateTime) pars = append(pars, startTime, tmpEndTime) } keyword = strings.TrimSpace(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") cond += fmt.Sprintf(` AND b.%s LIKE ?`, knowledge_approve.KnowledgeResourceApproveCols.KnowledgeResourceTitle) pars = append(pars, kw) } if sortField > 0 && sortRule > 0 { orderField := timeField[sortField] if orderField == "" { msg = "时间排序字段有误" return } orderRule := orderRules[sortRule] if orderRule == "" { msg = "时间排序方式有误" return } order = fmt.Sprintf("%s %s", orderField, orderRule) } total, e := knowledge_approve.GetApprovingKnowledgeResourceApproveCount(cond, pars) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovingKnowledgeResourceApproveCount err: %s", e.Error()) return } list, e := knowledge_approve.GetApprovingKnowledgeResourceApprovePageList(cond, pars, order, startSize, pageSize) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovingKnowledgeResourceApprovePageList err: %s", e.Error()) return } respList = toKnowledgeResourceApproveItemOrmResp(list) respTotal = total return } // SolvedKnowledgeResourceApprove 已处理的审批 func SolvedKnowledgeResourceApprove(adminId, classifyId, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.KnowledgeResourceApproveItemOrmResp, respTotal int, msg string, err error) { cond := fmt.Sprintf(` AND a.%s = ? AND a.%s IN (?)`, knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveUserId, knowledge_approve.KnowledgeResourceApproveRecordCols.NodeState) pars := make([]interface{}, 0) pars = append(pars, adminId, []int{KnowledgeResourceApproveStatePass, KnowledgeResourceApproveStateRefuse}) order := "" // 筛选条件 if classifyId > 0 { cond += fmt.Sprintf(` AND b.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ClassifyId) pars = append(pars, classifyId) } if timeType > 0 && startTime != "" && endTime != "" { _, e := time.Parse(utils.FormatDate, startTime) if e != nil { msg = "开始时间格式有误" err = e return } tmpEndTime, e := time.Parse(utils.FormatDate, endTime) if e != nil { msg = "结束时间格式有误" err = e return } tmpEndTime = tmpEndTime.AddDate(0, 0, 1) cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, timeField[timeType]) pars = append(pars, startTime, tmpEndTime) } keyword = strings.TrimSpace(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") cond += fmt.Sprintf(` AND b.%s LIKE ?`, knowledge_approve.KnowledgeResourceApproveCols.KnowledgeResourceTitle) pars = append(pars, kw) } if sortField > 0 && sortRule > 0 { orderField := timeField[sortField] if orderField == "" { msg = "时间排序字段有误" return } orderRule := orderRules[sortRule] if orderRule == "" { msg = "时间排序方式有误" return } order = fmt.Sprintf("%s %s", orderField, orderRule) } if approveState > 0 { cond += fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.NodeState) pars = append(pars, approveState) } total, e := knowledge_approve.GetApprovedKnowledgeResourceApproveCount(cond, pars) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovedKnowledgeResourceApproveCount err: %s", e.Error()) return } list, e := knowledge_approve.GetApprovedKnowledgeResourceApprovePageList(cond, pars, order, startSize, pageSize) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovedKnowledgeResourceApprovePageList err: %s", e.Error()) return } for _, v := range list { // 这个时候的状态,用审批状态 v.RecordState = v.NodeState v.ApproveTime = v.NodeApproveTime } respList = toKnowledgeResourceApproveItemOrmResp(list) respTotal = total return } func MyApplyKnowledgeResourceApproves(adminId, classifyId, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.KnowledgeResourceApproveItemOrmResp, respTotal int, msg string, err error) { cond := fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ApplyUserId) pars := make([]interface{}, 0) pars = append(pars, adminId) order := "" // 筛选条件 if classifyId > 0 { cond += fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ClassifyId) pars = append(pars, classifyId) } if timeType > 0 && startTime != "" && endTime != "" { _, e := time.Parse(utils.FormatDate, startTime) if e != nil { msg = "开始时间格式有误" err = e return } tmpEndTime, e := time.Parse(utils.FormatDate, endTime) if e != nil { msg = "结束时间格式有误" err = e return } tmpEndTimeStr := tmpEndTime.AddDate(0, 0, 1).Format(utils.FormatDate) cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, myTimeField[timeType]) pars = append(pars, startTime, tmpEndTimeStr) } keyword = strings.TrimSpace(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") cond += fmt.Sprintf(` AND a.%s LIKE ?`, knowledge_approve.KnowledgeResourceApproveCols.KnowledgeResourceTitle) pars = append(pars, kw) } if sortField > 0 && sortRule > 0 { orderField := myTimeField[sortField] if orderField == "" { msg = "时间排序字段有误" return } orderRule := orderRules[sortRule] if orderRule == "" { msg = "时间排序方式有误" return } order = fmt.Sprintf("%s %s", orderField, orderRule) } if approveState > 0 { cond += fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.State) pars = append(pars, approveState) } total, e := knowledge_approve.GetApplyKnowledgeResourceApproveCount(cond, pars) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApplyKnowledgeResourceApproveCount err: %s", e.Error()) return } respTotal = total list, e := knowledge_approve.GetApplyKnowledgeResourceApprovePageList(cond, pars, order, startSize, pageSize) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApplyKnowledgeResourceApprovePageList err: %s", e.Error()) return } respList = toKnowledgeResourceApproveItemOrmResp(list) return } func GetApproveDetail(approveId int) (resp *response.KnowledgeResourceApproveDetail, msg string, err error) { approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(approveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批已被删除, 请刷新页面" err = e return } msg = "获取失败" err = fmt.Errorf("GetItemById err: %s", e.Error()) return } // 审批信息 detail := new(response.KnowledgeResourceApproveDetail) detail.Approve = new(response.KnowledgeResourceApproveDetailItem) detail.Approve.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId detail.Approve.State = approveItem.State detail.Approve.FlowId = approveItem.FlowId detail.Approve.FlowVersion = approveItem.FlowVersion detail.Approve.StartNodeId = approveItem.StartNodeId detail.Approve.CurrNodeId = approveItem.CurrNodeId detail.Approve.ApplyUserId = approveItem.ApplyUserId detail.Approve.ApplyUserName = approveItem.ApplyUserName detail.Approve.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ApproveTime) detail.Approve.CreateTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.CreateTime) detail.Approve.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ModifyTime) // 审批节点 nodeOb := new(knowledge_approve.KnowledgeResourceApproveNode) nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, knowledge_approve.KnowledgeResourceApproveNodeCols.KnowledgeResourceApproveFlowId, knowledge_approve.KnowledgeResourceApproveNodeCols.CurrVersion) nodePars := make([]interface{}, 0) nodePars = append(nodePars, approveItem.FlowId, approveItem.FlowVersion) nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "") if e != nil { msg = "获取失败" err = fmt.Errorf("GetItemsByCondition err: %s", e.Error()) return } // 审批记录 recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord) recordCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.KnowledgeResourceApproveId) recordPars := make([]interface{}, 0) recordPars = append(recordPars, approveItem.KnowledgeResourceApproveId) recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, fmt.Sprintf("%s DESC", knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveTime)) if e != nil { msg = "获取失败" err = fmt.Errorf("GetItemsByCondition err: %s", e.Error()) return } recordMap := make(map[string]*knowledge_approve.KnowledgeResourceApproveRecord) for _, v := range recordItems { k := fmt.Sprintf("%d-%d", v.NodeId, v.ApproveUserId) recordMap[k] = v } // 审批流节点详情 detail.ApproveFlowNodes = make([]*response.KnowledgeResourceApproveDetailNodes, 0) for _, v := range nodeItems { t := new(response.KnowledgeResourceApproveDetailNodes) t.KnowledgeResourceApproveNodeId = v.KnowledgeResourceApproveNodeId t.KnowledgeResourceApproveFlowId = v.KnowledgeResourceApproveFlowId t.PrevNodeId = v.PrevNodeId t.NextNodeId = v.NextNodeId t.NodeType = v.NodeType t.ApproveType = v.ApproveType t.Users = make([]*response.KnowledgeResourceApproveDetailNodeUser, 0) us := make([]*response.KnowledgeResourceApproveNodeUserReq, 0) if v.Users != "" { e = json.Unmarshal([]byte(v.Users), &us) if e != nil { msg = "获取失败" err = fmt.Errorf("json.Unmarshal err: %s", e.Error()) return } } for _, vu := range us { u := new(response.KnowledgeResourceApproveDetailNodeUser) u.UserType = vu.UserType u.UserId = vu.UserId u.UserName = vu.UserName u.Sort = vu.Sort // 审批记录 k := fmt.Sprintf("%d-%d", v.KnowledgeResourceApproveNodeId, vu.UserId) r := recordMap[k] if r != nil { u.ApproveRecord = new(response.KnowledgeResourceApproveDetailNodeUserRecord) u.ApproveRecord.KnowledgeResourceApproveRecordId = r.KnowledgeResourceApproveRecordId u.ApproveRecord.State = r.State u.ApproveRecord.ApproveUserId = r.ApproveUserId u.ApproveRecord.ApproveUserName = r.ApproveUserName u.ApproveRecord.ApproveRemark = r.ApproveRemark u.ApproveRecord.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, r.ApproveTime) } t.Users = append(t.Users, u) } sort.Slice(t.Users, func(k, j int) bool { return t.Users[k].Sort < t.Users[j].Sort }) detail.ApproveFlowNodes = append(detail.ApproveFlowNodes, t) } // 看板信息 cnClassifyIdName := make(map[int]string) obj := new(knowledge.KnowledgeClassify) cnClassify, e := obj.GetAllClassify(knowledge.KnowledgeResourceTypeOpinion) if e != nil { msg = "获取失败" err = fmt.Errorf("GetKnowledgeDashboardClassifyAllList err: %s", e.Error()) return } for _, v := range cnClassify { cnClassifyIdName[v.ClassifyId] = v.ClassifyName } detail.Knowledge = new(response.KnowledgeResourceApproveDetailKnowledge) detail.Knowledge.KnowledgeResourceId = approveItem.KnowledgeResourceId detail.Knowledge.KnowledgeResourceTitle = approveItem.KnowledgeResourceTitle detail.Knowledge.KnowledgeClassify = cnClassifyIdName[approveItem.ClassifyId] resp = detail return } func KnowledgeResourceApproveRefuse(KnowledgeResourceApproveId, adminId int, approveRemark string) (msg string, err error) { approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(KnowledgeResourceApproveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批不存在, 请刷新页面" err = e return } msg = "操作失败" err = fmt.Errorf("GetKnowledgeResourceApproveById err: %s", e.Error()) return } if approveItem.State != KnowledgeResourceApproveStateApproving { msg = "审批状态有误, 请刷新页面" err = fmt.Errorf("审批状态有误, State: %d", approveItem.State) return } // 校验审批记录和审批 recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord) recordCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.KnowledgeResourceApproveId, knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveUserId, knowledge_approve.KnowledgeResourceApproveRecordCols.State) recordPars := make([]interface{}, 0) recordPars = append(recordPars, approveItem.KnowledgeResourceApproveId, adminId, KnowledgeResourceApproveStateApproving) recordItem, e := recordOb.GetItemByCondition(recordCond, recordPars, "") if e != nil { if utils.IsErrNoRow(e) { msg = "无权审批" err = e return } msg = "操作失败" err = fmt.Errorf("GetItemByCondition err: %s", e.Error()) return } // 驳回审批 if e = refuseKnowledgeResourceApprove(approveItem, recordItem, approveRemark, adminId); e != nil { msg = "操作失败" err = fmt.Errorf("RefuseKnowledgeResourceApprove err: %s", e.Error()) return } return } // refuseKnowledgeResourceApprove 驳回审批 func refuseKnowledgeResourceApprove(approveItem *knowledge_approve.KnowledgeResourceApprove, recordItem *knowledge_approve.KnowledgeResourceApproveRecord, approveRemark string, sysAdminId int) (err error) { if approveItem == nil { err = fmt.Errorf("审批信息有误") return } if recordItem == nil { err = fmt.Errorf("审批记录有误") return } // 更新审批记录 now := time.Now().Local() recordItem.State = KnowledgeResourceApproveStateRefuse recordItem.ApproveRemark = approveRemark recordItem.ApproveTime = now recordItem.ModifyTime = now recordItem.NodeState = KnowledgeResourceApproveStateRefuse recordItem.NodeApproveUserId = recordItem.ApproveUserId recordItem.NodeApproveUserName = recordItem.ApproveUserName recordItem.NodeApproveTime = now recordCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime", "NodeState", "NodeApproveUserId", "NodeApproveUserName", "NodeApproveTime"} if e := recordItem.Update(recordCols); e != nil { err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error()) return } // 将该审批的同一个节点的记录标记为已审批 if e := recordItem.UpdateNodeState(recordItem.KnowledgeResourceApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil { err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error()) return } // 驳回-更新审批, 报告状态, 推送消息 approveItem.State = KnowledgeResourceApproveStateRefuse approveItem.ApproveRemark = approveRemark approveItem.ApproveTime = now approveItem.ModifyTime = now approveCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime"} if e := approveItem.Update(approveCols); e != nil { err = fmt.Errorf("更新审批状态失败, Err: %s", e.Error()) return } if e := updateKnowledgeResourceApproveState(approveItem.KnowledgeResourceId, KnowledgeStateRefused); e != nil { err = fmt.Errorf("更新报告状态失败, Err: %s", e.Error()) return } // 推送驳回消息给申请人 go func() { messageItem := new(knowledge_approve.KnowledgeResourceApproveMessage) messageItem.SendUserId = sysAdminId messageItem.ReceiveUserId = approveItem.ApplyUserId messageItem.Content = "您提交的审批被驳回" messageItem.Remark = fmt.Sprintf("您提交的【%s】已被驳回", approveItem.KnowledgeResourceTitle) messageItem.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId messageItem.ApproveState = KnowledgeResourceApproveStateRefuse messageItem.CreateTime = now messageItem.ModifyTime = now if e := messageItem.Create(); e != nil { utils.FileLog.Info(fmt.Sprintf("ApproveKnowledge message err: %s", e.Error())) return } }() return } func KnowledgeResourceApproveCancel(KnowledgeResourceApproveId, adminId int, adminName string) (msg string, err error) { approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(KnowledgeResourceApproveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批已被删除, 请刷新页面" err = e return } msg = "操作失败" err = fmt.Errorf("GetKnowledgeResourceApproveById err: %s", e.Error()) return } if approveItem.ApplyUserId != adminId { msg = "非申请人不可撤销" err = fmt.Errorf("非申请人不可撤销") return } // 撤销审批 e = cancelKnowledgeResourceApprove(approveItem.KnowledgeResourceId, approveItem.KnowledgeResourceApproveId, adminId, adminName) if e != nil { msg = "操作失败" err = fmt.Errorf("cancelKnowledgeResourceApprove err: %s", e.Error()) return } return } // cancelKnowledgeResourceApprove 撤回审批 func cancelKnowledgeResourceApprove(knowledgeId, approveId, sysAdminId int, sysAdminName string) (err error) { // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态 // confMap, e := models.GetBusinessConf() // if e != nil { // err = fmt.Errorf("GetBusinessConf err: %s", e.Error()) // return // } // openApprove := t // if !openApprove { // //err = fmt.Errorf("未开启审批") // return // } // 修改审批信息状态 approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(approveId) if e != nil { if utils.IsErrNoRow(e) { err = e return } err = fmt.Errorf("approve GetItemById err: %s", e.Error()) return } if approveItem.State == KnowledgeResourceApproveStateCancel { return } approveItem.State = KnowledgeResourceApproveStateCancel approveItem.ModifyTime = time.Now() cols := []string{"State", "ModifyTime"} if e = approveItem.Update(cols); e != nil { err = fmt.Errorf("approve Update err: %s", e.Error()) return } // 修改报告状态 e = updateKnowledgeResourceApproveState(knowledgeId, KnowledgeStateUnpublished) if e != nil { err = fmt.Errorf("更新报告审批撤回失败, Err: %s", e.Error()) return } // 推送到Es go func() { obj := new(knowledge.KnowledgeResource) KnowledgeItem, e := obj.GetById(approveItem.KnowledgeResourceId) if e != nil && !utils.IsErrNoRow(e) { utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update knowledge resource state err: %s", e.Error())) return } e = knowledgeSrv.EsAddOrEditKnowledgeResource(KnowledgeItem) if e != nil { utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update es err: %s", e.Error())) return } }() // 推送撤回消息 go func() { recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord) recordCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.KnowledgeResourceApproveId) recordPars := make([]interface{}, 0) recordPars = append(recordPars, approveId) recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, "") if e != nil { utils.FileLog.Info(fmt.Sprintf("approve record GetItemsByCondition err: %s", e.Error())) return } messageOb := new(knowledge_approve.KnowledgeResourceApproveMessage) messages := make([]*knowledge_approve.KnowledgeResourceApproveMessage, 0) for _, v := range recordItems { m := new(knowledge_approve.KnowledgeResourceApproveMessage) m.SendUserId = sysAdminId m.ReceiveUserId = v.ApproveUserId m.Content = fmt.Sprintf("%s提交的【%s】已撤回", sysAdminName, approveItem.KnowledgeResourceTitle) m.KnowledgeResourceApproveId = approveId m.ApproveState = KnowledgeResourceApproveStateCancel m.CreateTime = time.Now().Local() m.ModifyTime = time.Now().Local() messages = append(messages, m) } e = messageOb.CreateMulti(messages) if e != nil { utils.FileLog.Info(fmt.Sprintf("CancelKnowledgeResourceApprove messages err: %s", e.Error())) return } }() return } // CheckKnowledgeOpenApprove 校验报告是否开启了审批流 func CheckKnowledgeOpenApprove(classifyId int) (opening bool, err error) { // 获取审批配置 // confMap, e := models.GetBusinessConf() // if e != nil { // err = fmt.Errorf("GetBusinessConf err: %s", e.Error()) // return // } // openMap := map[string]bool{"false": false, "true": true} // openApprove := openMap[confMap[models.BusinessConfIsBIApprove]] // 查询对应分类是否有审批流 flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow) flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId) flowPars := make([]interface{}, 0) flowPars = append(flowPars, classifyId) flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "") if e != nil { if utils.IsErrNoRow(e) { opening = false return } err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error()) return } // 开启审批/有审批流 if flowItem != nil { opening = true return } return } // SubmitKnowledgeResourceApprove 提交审批 func SubmitKnowledgeResourceApprove(KnowledgeResourceId int, KnowledgeResourceTitle string, classifyId int, sysAdminId int, sysAdminName string) (approveId int, err error) { // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态 // confMap, e := models.GetBusinessConf() // if e != nil { // err = fmt.Errorf("GetBusinessConf err: %s", e.Error()) // return // } // openMap := map[string]bool{"false": false, "true": true} // openApprove := openMap[confMap[models.BusinessConfIsBIApprove]] // if !openApprove { // err = fmt.Errorf("未开启审批") // return // } // 查询审批流 flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow) flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId) flowPars := make([]interface{}, 0) flowPars = append(flowPars, classifyId) flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "") if e != nil { err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error()) return } // 查询审批节点 nodeOb := new(knowledge_approve.KnowledgeResourceApproveNode) nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, knowledge_approve.KnowledgeResourceApproveNodeCols.KnowledgeResourceApproveFlowId, knowledge_approve.KnowledgeResourceApproveNodeCols.CurrVersion) nodePars := make([]interface{}, 0) nodePars = append(nodePars, flowItem.KnowledgeResourceApproveFlowId, flowItem.CurrVersion) nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "") if e != nil { err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error()) return } if len(nodeItems) == 0 { err = fmt.Errorf("无审批节点") return } // 取出首个节点 firstNodeItem := new(knowledge_approve.KnowledgeResourceApproveNode) for _, v := range nodeItems { if v.PrevNodeId == 0 { firstNodeItem = v continue } } if firstNodeItem == nil { err = fmt.Errorf("首个审批节点有误") return } // 审批信息 now := time.Now().Local() newApprove := new(knowledge_approve.KnowledgeResourceApprove) newApprove.KnowledgeResourceId = KnowledgeResourceId newApprove.KnowledgeResourceTitle = KnowledgeResourceTitle newApprove.ClassifyId = classifyId newApprove.State = KnowledgeResourceApproveStateApproving newApprove.FlowId = flowItem.KnowledgeResourceApproveFlowId newApprove.FlowVersion = flowItem.CurrVersion newApprove.StartNodeId = firstNodeItem.KnowledgeResourceApproveNodeId newApprove.CurrNodeId = firstNodeItem.KnowledgeResourceApproveNodeId newApprove.ApplyUserId = sysAdminId newApprove.ApplyUserName = sysAdminName newApprove.CreateTime = now newApprove.ModifyTime = now if e = newApprove.Create(); e != nil { err = fmt.Errorf("生成审批信息失败, Err: %s", e.Error()) return } approveId = newApprove.KnowledgeResourceApproveId // 生成节点审批记录 err = BuildNextNodeRecordAndMsg(firstNodeItem, newApprove.KnowledgeResourceApproveId, sysAdminId, sysAdminName, newApprove.KnowledgeResourceTitle) return } func toKnowledgeResourceApproveItemOrmResp(src []*knowledge_approve.KnowledgeResourceApproveItemOrm) (res []*response.KnowledgeResourceApproveItemOrmResp) { res = make([]*response.KnowledgeResourceApproveItemOrmResp, 0) for _, v := range src { r := new(response.KnowledgeResourceApproveItemOrmResp) r.KnowledgeResourceApproveId = v.KnowledgeResourceApproveId r.KnowledgeResourceApproveRecordId = v.KnowledgeResourceApproveRecordId r.KnowledgeResourceId = v.KnowledgeResourceId r.KnowledgeResourceTitle = v.KnowledgeResourceTitle r.ClassifyId = v.ClassifyId r.State = v.State r.RecordState = v.RecordState r.FlowId = v.FlowId r.FlowVersion = v.FlowVersion r.StartNodeId = v.StartNodeId r.CurrNodeId = v.CurrNodeId r.ApplyUserId = v.ApplyUserId r.ApplyUserName = v.ApplyUserName r.ApproveRemark = v.ApproveRemark r.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, v.ApproveTime) r.HandleTime = utils.TimeTransferString(utils.FormatDateTime, v.HandleTime) r.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime) r.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime) r.NodeState = v.NodeState res = append(res, r) } return } func GetKnowledgeClassifyAll() (list []*knowledge.KnowledgeClassify, msg string, err error) { obj := new(knowledge.KnowledgeClassify) ClassifyList, e := obj.GetAllClassify(knowledge.KnowledgeResourceTypeOpinion) if e != nil { msg = "获取失败" err = fmt.Errorf("GetKnowledgeDashboardClassifyAllList err: %s", e.Error()) return } list = ClassifyList return } func CheckHasApprovingKnowledge() (ok bool, err error) { count, err := knowledge_approve.GetKnowledgeResourceApproveCountByState(KnowledgeResourceApproveStateApproving) if err != nil { return } if count > 0 { ok = true } return }