package data_approve import ( "encoding/json" "eta_gn/eta_api/models/data_manage" dataApproveModel "eta_gn/eta_api/models/data_manage/data_approve" "eta_gn/eta_api/models/data_manage/data_approve/response" "eta_gn/eta_api/services/data" "eta_gn/eta_api/utils" "fmt" "sort" "strings" "time" ) var ( timeField = map[int]string{1: fmt.Sprintf("a.%s", dataApproveModel.DataApproveRecordCols.CreateTime), 2: fmt.Sprintf("a.%s", dataApproveModel.DataApproveRecordCols.NodeApproveTime), 3: fmt.Sprintf("b.%s", dataApproveModel.DataApproveCols.ApproveTime)} myTimeField = map[int]string{1: dataApproveModel.DataApproveCols.CreateTime, 3: dataApproveModel.DataApproveCols.ApproveTime} orderRules = map[int]string{1: "ASC", 2: "DESC"} ) func PassDataApprove(approveId int, adminId int) (msg string, err error) { approveItem, e := dataApproveModel.GetDataApproveById(approveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批不存在, 请刷新页面" err = e return } msg = "操作失败" return } if approveItem.State != DataApproveStateApproving { msg = "审批状态有误, 请刷新页面" err = fmt.Errorf("审批状态有误, State: %d", approveItem.State) return } var ApprovePars []interface{} ApproveCond := ` AND data_approve_id =? AND approve_user_id =? AND state =? ` ApprovePars = append(ApprovePars, approveId, adminId, DataApproveStateApproving) recordItem, er := dataApproveModel.GetDataApproveRecordByCondition(ApproveCond, ApprovePars) if er != nil { if utils.IsErrNoRow(er) { msg = "无权审批" err = er return } msg = "操作失败" return } // 查询审批流和审批流节点 flowItem, e := dataApproveModel.GetDataApproveFlowById(approveItem.FlowId) if e != nil { err = fmt.Errorf("获取审批流失败, Err: %s", e.Error()) return } nodePars := make([]interface{}, 0) nodeCond := ` AND data_approve_flow_id =? AND curr_version =? ` nodePars = append(nodePars, flowItem.DataApproveFlowId, flowItem.CurrVersion) nodeItems, e := dataApproveModel.GetDataApproveNodeByCondition(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]*dataApproveModel.DataApproveNode) for _, v := range nodeItems { nodeMap[v.DataApproveNodeId] = v } // 取出审批记录的节点 currNodeItem := nodeMap[recordItem.NodeId] if currNodeItem == nil { err = fmt.Errorf("当前节点信息有误") return } currNode, e := FormatDataApproveNode2Item(currNodeItem) if e != nil { err = fmt.Errorf("当前节点信息有误, Err: %s", e.Error()) return } now := time.Now().Local() recordItem.State = DataApproveStatePass recordItem.ApproveTime = now recordItem.ModifyTime = now recordItem.NodeState = DataApproveStatePass 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.DataApproveNodeUser) // 下一个审批人, 为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(dataApproveModel.DataApproveRecord) newRecord.DataApproveId = recordItem.DataApproveId newRecord.State = DataApproveStateApproving newRecord.NodeId = currNode.DataApproveNodeId 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 = DataApproveStateApproving if e = newRecord.Create(); e != nil { err = fmt.Errorf("生成审批记录失败, Err: %s", e.Error()) return } // 推送审批消息 go func() { messageItem := new(dataApproveModel.DataApproveMessage) messageItem.SendUserId = approveItem.ApplyUserId messageItem.ReceiveUserId = nextUser.UserId messageItem.DataType = approveItem.DataType messageItem.Content = "您有新的待办任务" messageItem.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", approveItem.ApplyUserName, approveItem.Title) messageItem.DataApproveId = approveItem.DataApproveId messageItem.ApproveState = DataApproveStateApproving messageItem.CreateTime = now messageItem.ModifyTime = now if e = messageItem.Create(); e != nil { utils.FileLog.Info(fmt.Sprintf("PassDataApprove 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.DataType, approveItem.DataApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.Title) return } else { // 最后一个节点 lastApprove = true } } // 会签 if currNode.ApproveType == NodeApproveTypeAll { // 查询其他审批人是否已审批 otherCond := ` AND data_approve_id =? AND node_id =? AND approve_user_id <> ? ` otherPars := make([]interface{}, 0) otherPars = append(otherPars, approveItem.DataApproveId, recordItem.NodeId, adminId) otherRecords, e := dataApproveModel.GetDataApproveRecordItemsByCondition(otherCond, otherPars) if e != nil { err = fmt.Errorf("获取节点审批记录失败, Err: %s", e.Error()) return } otherPass := true for _, v := range otherRecords { if v.State != DataApproveStatePass { 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.DataType, approveItem.DataApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.Title) 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.DataApproveId, 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.DataType, approveItem.DataApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.Title) return } } // 最后一个审批, 更新审批记录、审批、资产状态、推送消息给申请人 if lastApprove { if e = recordItem.Update(recordCols); e != nil { err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error()) return } approveItem.State = DataApproveStatePass 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 = updateDataApproveState(approveItem, DataApproveStatePass); e != nil { err = fmt.Errorf("更新资产审批状态失败, Err: %s", e.Error()) return } go func() { messageItem := new(dataApproveModel.DataApproveMessage) messageItem.SendUserId = adminId messageItem.ReceiveUserId = approveItem.ApplyUserId messageItem.DataType = approveItem.DataType messageItem.Content = "您提交的审批已通过" messageItem.Remark = fmt.Sprintf("您提交的【%s】已通过", approveItem.Title) messageItem.DataApproveId = approveItem.DataApproveId messageItem.ApproveState = DataApproveStatePass messageItem.CreateTime = now messageItem.ModifyTime = now if e = messageItem.Create(); e != nil { utils.FileLog.Info(fmt.Sprintf("PassDataApprove message err: %s", e.Error())) return } }() } return } // BuildNextNodeRecordAndMsg 生成下一个节点的审批记录并推送消息 func BuildNextNodeRecordAndMsg(approveNodeItem *dataApproveModel.DataApproveNode, dataType, approveId, sysAdminId int, sysAdminName, title string) (err error) { if approveNodeItem == nil { err = fmt.Errorf("approve node nil") return } // 根据节点审批方式生成审批记录 now := time.Now().Local() approveNode, e := FormatDataApproveNode2Item(approveNodeItem) if e != nil { err = fmt.Errorf("FormatDataApproveNode2Item err: %s", e.Error()) return } if len(approveNode.Users) == 0 { err = fmt.Errorf("审批节点用户有误") return } newRecords := make([]*dataApproveModel.DataApproveRecord, 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(dataApproveModel.DataApproveRecord) r.DataApproveId = approveId r.State = DataApproveStateApproving r.NodeId = approveNode.DataApproveNodeId 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 = DataApproveStateApproving // 当前节点审批状态 newRecords = append(newRecords, r) // 依次审批仅生成一条记录 if approveNode.ApproveType == NodeApproveTypeRoll { break } } recordOb := new(dataApproveModel.DataApproveRecord) if e = recordOb.CreateMulti(newRecords); e != nil { err = fmt.Errorf("生成节点审批记录失败, Err: %s", e.Error()) return } // 推送审批消息 go func() { messageOb := new(dataApproveModel.DataApproveMessage) messages := make([]*dataApproveModel.DataApproveMessage, 0) for _, v := range newRecords { m := new(dataApproveModel.DataApproveMessage) m.SendUserId = sysAdminId m.DataType = dataType m.ReceiveUserId = v.ApproveUserId m.Content = "您有新的待办任务" m.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", sysAdminName, title) m.DataApproveId = approveId m.ApproveState = DataApproveStateApproving 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 } // updateDataApproveState 更新待审批资产的审批状态 func updateDataApproveState(dataApprove *dataApproveModel.DataApprove, state int) (err error) { // TODO 根据审批单去通过、驳回公开图库申请 obj := dataApproveModel.DataApproveRelation{} dataList, err := obj.GetListByDataApproveId(dataApprove.DataApproveId) if err != nil { return } dataItemList := make([]SetDataPublicItem, 0) for _, v := range dataList { dataItemList = append(dataItemList, SetDataPublicItem{ClassifyId: v.ClassifyId, DataId: v.DataId}) } err = UpdatePublicByDataList(dataApprove.DataType, state, dataItemList) return } // UpdatePublicByDataList // @Description: 更新公共资产权限 // @author: Roc // @datetime 2024-12-06 13:44:20 // @param dataType int // @param dataApproveState int // @param dataList []SetDataPublicItem // @return err error func UpdatePublicByDataList(dataType, dataApproveState int, dataList []SetDataPublicItem) (err error) { dataIdList := make([]int, 0) for _, v := range dataList { dataIdList = append(dataIdList, v.DataId) } switch dataType { case utils.DataApproveTypeEdb: switch dataApproveState { case DataApproveStateApproving: err = data_manage.UpdatePublicEdb(dataIdList, utils.DataPublicSuccess) case DataApproveStatePass: for _, dataItem := range dataList { err = data_manage.UpdatePublicEdbSuccess(dataItem.DataId, dataItem.ClassifyId) } case DataApproveStateRefuse: err = data_manage.UpdatePublicEdb(dataIdList, utils.DataPublicReject) case DataApproveStateCancel: err = data_manage.UpdatePublicEdb(dataIdList, utils.DataPublicDefault) } // 更新ES for _, dataId := range dataIdList { data.AddOrEditEdbInfoToEs(dataId) } // 指标库 case utils.DataApproveTypeChart: // 图库 switch dataApproveState { case DataApproveStateApproving: err = data_manage.UpdatePublicChart(dataIdList, utils.DataPublicSuccess) case DataApproveStatePass: for _, dataItem := range dataList { err = data_manage.UpdatePublicChartSuccess(dataItem.DataId, dataItem.ClassifyId) } case DataApproveStateRefuse: err = data_manage.UpdatePublicChart(dataIdList, utils.DataPublicReject) case DataApproveStateCancel: err = data_manage.UpdatePublicChart(dataIdList, utils.DataPublicDefault) } // 更新ES for _, dataId := range dataIdList { data.AddOrEditChartInfoToEs(dataId) } } return } func ProcessingDataApprove(adminId, dataType, timeType, sortField, sortRule, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.DataApproveItemOrmResp, respTotal int, msg string, err error) { cond := fmt.Sprintf(` AND a.%s = ? AND b.%s = ? AND a.%s = ?`, dataApproveModel.DataApproveRecordCols.State, dataApproveModel.DataApproveCols.State, dataApproveModel.DataApproveRecordCols.ApproveUserId) pars := make([]interface{}, 0) pars = append(pars, DataApproveStateApproving, DataApproveStateApproving, adminId) order := "" // 筛选条件 cond += fmt.Sprintf(` AND b.%s = ?`, dataApproveModel.DataApproveCols.DataType) pars = append(pars, dataType) 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 ?)`, dataApproveModel.DataApproveCols.CreateTime) pars = append(pars, startTime, tmpEndTime) } keyword = strings.TrimSpace(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") cond += fmt.Sprintf(` AND b.%s LIKE ?`, dataApproveModel.DataApproveCols.Title) 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 := dataApproveModel.GetApprovingDataApproveCount(cond, pars) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovingDataApproveCount err: %s", e.Error()) return } list, e := dataApproveModel.GetApprovingDataApprovePageList(cond, pars, order, startSize, pageSize) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovingDataApprovePageList err: %s", e.Error()) return } respList = toDataApproveItemOrmResp(list) respTotal = total return } // SolvedDataApprove 已处理的审批 func SolvedDataApprove(adminId, dataType, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.DataApproveItemOrmResp, respTotal int, msg string, err error) { cond := fmt.Sprintf(` AND a.%s = ? AND a.%s IN (?)`, dataApproveModel.DataApproveRecordCols.ApproveUserId, dataApproveModel.DataApproveRecordCols.NodeState) pars := make([]interface{}, 0) pars = append(pars, adminId, []int{DataApproveStatePass, DataApproveStateRefuse}) order := "" // 筛选条件 cond += fmt.Sprintf(` AND b.%s = ?`, dataApproveModel.DataApproveCols.DataType) pars = append(pars, dataType) 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 ?`, dataApproveModel.DataApproveCols.Title) 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 = ?`, dataApproveModel.DataApproveRecordCols.NodeState) pars = append(pars, approveState) } total, e := dataApproveModel.GetApprovedDataApproveCount(cond, pars) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovedDataApproveCount err: %s", e.Error()) return } list, e := dataApproveModel.GetApprovedDataApprovePageList(cond, pars, order, startSize, pageSize) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApprovedDataApprovePageList err: %s", e.Error()) return } for _, v := range list { // 这个时候的状态,用审批状态 v.RecordState = v.NodeState v.ApproveTime = v.NodeApproveTime } respList = toDataApproveItemOrmResp(list) respTotal = total return } func MyApplyDataApproves(adminId, dataType, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.DataApproveItemOrmResp, respTotal int, msg string, err error) { cond := fmt.Sprintf(` AND a.%s = ?`, dataApproveModel.DataApproveCols.ApplyUserId) pars := make([]interface{}, 0) pars = append(pars, adminId) order := "" // 筛选条件 cond += fmt.Sprintf(` AND a.%s = ?`, dataApproveModel.DataApproveCols.DataType) pars = append(pars, dataType) 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 ?`, dataApproveModel.DataApproveCols.Title) 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 = ?`, dataApproveModel.DataApproveRecordCols.State) pars = append(pars, approveState) } total, e := dataApproveModel.GetApplyDataApproveCount(cond, pars) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApplyDataApproveCount err: %s", e.Error()) return } respTotal = total list, e := dataApproveModel.GetApplyDataApprovePageList(cond, pars, order, startSize, pageSize) if e != nil { msg = "获取失败" err = fmt.Errorf("GetApplyDataApprovePageList err: %s", e.Error()) return } respList = toDataApproveItemOrmResp(list) return } func GetApproveDetail(approveId int) (resp *response.DataApproveDetail, msg string, err error) { approveItem, e := dataApproveModel.GetDataApproveById(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.DataApproveDetail) detail.Approve = new(response.DataApproveDetailItem) detail.Approve.DataApproveId = approveItem.DataApproveId 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.ApproveRemark = approveItem.ApproveRemark 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(dataApproveModel.DataApproveNode) nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, dataApproveModel.DataApproveNodeCols.DataApproveFlowId, dataApproveModel.DataApproveNodeCols.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(dataApproveModel.DataApproveRecord) recordCond := fmt.Sprintf(` AND %s = ?`, dataApproveModel.DataApproveRecordCols.DataApproveId) recordPars := make([]interface{}, 0) recordPars = append(recordPars, approveItem.DataApproveId) recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, fmt.Sprintf("%s DESC", dataApproveModel.DataApproveRecordCols.ApproveTime)) if e != nil { msg = "获取失败" err = fmt.Errorf("GetItemsByCondition err: %s", e.Error()) return } recordMap := make(map[string]*dataApproveModel.DataApproveRecord) for _, v := range recordItems { k := fmt.Sprintf("%d-%d", v.NodeId, v.ApproveUserId) recordMap[k] = v } // 审批流节点详情 detail.ApproveFlowNodes = make([]*response.DataApproveDetailNodes, 0) for _, v := range nodeItems { t := new(response.DataApproveDetailNodes) t.DataApproveNodeId = v.DataApproveNodeId t.DataApproveFlowId = v.DataApproveFlowId t.PrevNodeId = v.PrevNodeId t.NextNodeId = v.NextNodeId t.NodeType = v.NodeType t.ApproveType = v.ApproveType t.Users = make([]*response.DataApproveDetailNodeUser, 0) us := make([]*response.DataApproveNodeUserReq, 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.DataApproveDetailNodeUser) u.UserType = vu.UserType u.UserId = vu.UserId u.UserName = vu.UserName u.Sort = vu.Sort // 审批记录 k := fmt.Sprintf("%d-%d", v.DataApproveNodeId, vu.UserId) r := recordMap[k] if r != nil { u.ApproveRecord = new(response.DataApproveDetailNodeUserRecord) u.ApproveRecord.DataApproveRecordId = r.DataApproveRecordId 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) } // 返回审批数据明细 dataList := make([]response.DataApproveDetailData, 0) { obj := new(dataApproveModel.DataApproveRelation) dataItemList, tmpErr := obj.GetListByDataApproveId(approveItem.DataApproveId) if tmpErr != nil { err = tmpErr return } dataIdList := make([]int, 0) dataClassifyIdList := make([]int, 0) dataIdClassifyIdMap := make(map[int]int) for _, v := range dataItemList { dataIdList = append(dataIdList, v.DataId) dataClassifyIdList = append(dataClassifyIdList, v.ClassifyId) dataIdClassifyIdMap[v.DataId] = v.ClassifyId } switch approveItem.DataType { case utils.DataApproveTypeEdb: // 指标库 tmpList, tmpErr := data_manage.GetEdbInfoByIdList(dataIdList) if tmpErr != nil { err = tmpErr return } publicClassifyMap := make(map[int]string) // 获取分类信息 { publicClassifyObj := data_manage.EdbPublicClassify{} publicClassifyList, tmpErr := publicClassifyObj.GetEdbClassifyByIdList(dataClassifyIdList) if tmpErr != nil { err = tmpErr return } for _, v := range publicClassifyList { publicClassifyMap[v.EdbPublicClassifyId] = v.EdbPublicClassifyNamePath } } for _, v := range tmpList { classifyId := dataIdClassifyIdMap[v.EdbInfoId] item := response.DataApproveDetailData{ DataId: v.EdbInfoId, DataName: v.EdbName, DataCode: v.UniqueCode, DataClassifyId: classifyId, DataClassifyName: publicClassifyMap[classifyId], } dataList = append(dataList, item) } case utils.DataApproveTypeChart: // 图库 tmpList, tmpErr := data_manage.GetChartInfoByIdList(dataIdList) if tmpErr != nil { err = tmpErr return } publicClassifyMap := make(map[int]string) // 获取分类信息 { publicClassifyObj := data_manage.ChartPublicClassify{} publicClassifyList, tmpErr := publicClassifyObj.GetChartClassifyByIdList(dataClassifyIdList) if tmpErr != nil { err = tmpErr return } for _, v := range publicClassifyList { publicClassifyMap[v.ChartPublicClassifyId] = v.ChartPublicClassifyNamePath } } for _, v := range tmpList { classifyId := dataIdClassifyIdMap[v.ChartInfoId] item := response.DataApproveDetailData{ DataId: v.ChartInfoId, DataName: v.ChartName, DataCode: v.UniqueCode, DataClassifyId: classifyId, DataClassifyName: publicClassifyMap[classifyId], } dataList = append(dataList, item) } } } detail.DataList = dataList resp = detail return } func DataApproveRefuse(DataApproveId, adminId int, approveRemark string) (msg string, err error) { approveItem, e := dataApproveModel.GetDataApproveById(DataApproveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批不存在, 请刷新页面" err = e return } msg = "操作失败" err = fmt.Errorf("GetDataApproveById err: %s", e.Error()) return } if approveItem.State != DataApproveStateApproving { msg = "审批状态有误, 请刷新页面" err = fmt.Errorf("审批状态有误, State: %d", approveItem.State) return } // 校验审批记录和审批 recordOb := new(dataApproveModel.DataApproveRecord) recordCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, dataApproveModel.DataApproveRecordCols.DataApproveId, dataApproveModel.DataApproveRecordCols.ApproveUserId, dataApproveModel.DataApproveRecordCols.State) recordPars := make([]interface{}, 0) recordPars = append(recordPars, approveItem.DataApproveId, adminId, DataApproveStateApproving) 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 = refuseDataApprove(approveItem, recordItem, approveRemark, adminId); e != nil { msg = "操作失败" err = fmt.Errorf("RefuseDataApprove err: %s", e.Error()) return } return } // refuseDataApprove 驳回审批 func refuseDataApprove(approveItem *dataApproveModel.DataApprove, recordItem *dataApproveModel.DataApproveRecord, 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 = DataApproveStateRefuse recordItem.ApproveRemark = approveRemark recordItem.ApproveTime = now recordItem.ModifyTime = now recordItem.NodeState = DataApproveStateRefuse 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.DataApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil { err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error()) return } // 驳回-更新审批, 资产状态, 推送消息 approveItem.State = DataApproveStateRefuse 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 := updateDataApproveState(approveItem, DataApproveStateRefuse); e != nil { err = fmt.Errorf("更新资产状态失败, Err: %s", e.Error()) return } // 推送驳回消息给申请人 go func() { messageItem := new(dataApproveModel.DataApproveMessage) messageItem.SendUserId = sysAdminId messageItem.ReceiveUserId = approveItem.ApplyUserId messageItem.DataType = approveItem.DataType messageItem.Content = "您提交的审批被驳回" messageItem.Remark = fmt.Sprintf("您提交的【%s】已被驳回", approveItem.Title) messageItem.DataApproveId = approveItem.DataApproveId messageItem.ApproveState = DataApproveStateRefuse messageItem.CreateTime = now messageItem.ModifyTime = now if e := messageItem.Create(); e != nil { utils.FileLog.Info(fmt.Sprintf("ApproveData message err: %s", e.Error())) return } }() return } func DataApproveCancel(DataApproveId, adminId int, adminName string) (msg string, err error) { approveItem, e := dataApproveModel.GetDataApproveById(DataApproveId) if e != nil { if utils.IsErrNoRow(e) { msg = "审批已被删除, 请刷新页面" err = e return } msg = "操作失败" err = fmt.Errorf("GetDataApproveById err: %s", e.Error()) return } if approveItem.ApplyUserId != adminId { msg = "非申请人不可撤销" err = fmt.Errorf("非申请人不可撤销") return } // 撤销审批 e = cancelDataApprove(approveItem, approveItem.DataType, approveItem.DataApproveId, adminId, adminName) if e != nil { msg = "操作失败" err = fmt.Errorf("cancelDataApprove err: %s", e.Error()) return } return } // cancelDataApprove 撤回审批 func cancelDataApprove(item *dataApproveModel.DataApprove, dataType, approveId, sysAdminId int, sysAdminName string) (err error) { // todo //// 默认内部审批, 如果是走的第三方审批, 那么仅修改状态 //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.BusinessConfIsDataApprove]] //if !openApprove { // //err = fmt.Errorf("未开启审批") // return //} // 修改审批信息状态 approveItem, e := dataApproveModel.GetDataApproveById(approveId) if e != nil { if utils.IsErrNoRow(e) { err = e return } err = fmt.Errorf("approve GetItemById err: %s", e.Error()) return } if approveItem.State == DataApproveStateCancel { return } approveItem.State = DataApproveStateCancel 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 = updateDataApproveState(approveItem, DataApproveStateCancel) if e != nil { err = fmt.Errorf("更新资产审批撤回失败, Err: %s", e.Error()) return } // 推送撤回消息 go func() { recordOb := new(dataApproveModel.DataApproveRecord) recordCond := fmt.Sprintf(` AND %s = ?`, dataApproveModel.DataApproveRecordCols.DataApproveId) 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(dataApproveModel.DataApproveMessage) messages := make([]*dataApproveModel.DataApproveMessage, 0) for _, v := range recordItems { m := new(dataApproveModel.DataApproveMessage) m.SendUserId = sysAdminId m.ReceiveUserId = v.ApproveUserId m.DataType = dataType m.Content = fmt.Sprintf("%s提交的【%s】已撤回", sysAdminName, approveItem.Title) m.DataApproveId = approveId m.ApproveState = DataApproveStateCancel 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("CancelDataApprove messages err: %s", e.Error())) return } }() return } // CheckOpenApprove 校验资产是否开启了审批流 func CheckOpenApprove(dataType int) (opening bool, err error) { // 查询对应分类是否有审批流 flowOb := new(dataApproveModel.DataApproveFlow) flowCond := fmt.Sprintf(` AND %s = ?`, dataApproveModel.DataApproveFlowCols.DataType) flowPars := make([]interface{}, 0) flowPars = append(flowPars, dataType) flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "") if e != nil { if utils.IsErrNoRow(e) { return } err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error()) return } // 查看审批节点 if flowItem.DataApproveFlowId > 0 { nodeOb := new(dataApproveModel.DataApproveNode) nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ? `, dataApproveModel.DataApproveNodeCols.DataApproveFlowId, dataApproveModel.DataApproveNodeCols.CurrVersion) nodePars := make([]interface{}, 0) nodePars = append(nodePars, flowItem.DataApproveFlowId, flowItem.CurrVersion) nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "") if e != nil { err = e return } // 如果有审批节点,那么就确实有审批流,那么就返回true if len(nodeItems) > 0 { opening = true return } } return } type SetDataPublicItem struct { DataId int ClassifyId int } // SubmitDataApprove // @Description: 提交审批 // @author: Roc // @datetime 2024-12-06 10:13:40 // @param dataType int // @param dataIdList []int // @param title string // @param description string // @param sysAdminId int // @param sysAdminName string // @return approveId int // @return err error func SubmitDataApprove(dataType int, dataList []SetDataPublicItem, title, description string, sysAdminId int, sysAdminName string) (approveId int, err error) { // 查询审批流 flowOb := new(dataApproveModel.DataApproveFlow) flowCond := fmt.Sprintf(` AND %s = ?`, dataApproveModel.DataApproveFlowCols.DataType) flowPars := make([]interface{}, 0) flowPars = append(flowPars, dataType) flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "") if e != nil { err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error()) return } // 查询审批节点 nodeOb := new(dataApproveModel.DataApproveNode) nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, dataApproveModel.DataApproveNodeCols.DataApproveFlowId, dataApproveModel.DataApproveNodeCols.CurrVersion) nodePars := make([]interface{}, 0) nodePars = append(nodePars, flowItem.DataApproveFlowId, 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(dataApproveModel.DataApproveNode) for _, v := range nodeItems { if v.PrevNodeId == 0 { firstNodeItem = v continue } } if firstNodeItem == nil { err = fmt.Errorf("首个审批节点有误") return } // 审批信息 now := time.Now().Local() newApprove := new(dataApproveModel.DataApprove) newApprove.Title = title newApprove.ApproveRemark = description newApprove.DataType = dataType newApprove.State = DataApproveStateApproving newApprove.FlowId = flowItem.DataApproveFlowId newApprove.FlowVersion = flowItem.CurrVersion newApprove.StartNodeId = firstNodeItem.DataApproveNodeId newApprove.CurrNodeId = firstNodeItem.DataApproveNodeId newApprove.ApplyUserId = sysAdminId newApprove.ApplyUserName = sysAdminName newApprove.CreateTime = now newApprove.ModifyTime = now relationList := make([]*dataApproveModel.DataApproveRelation, 0) for _, dataItem := range dataList { relationList = append(relationList, &dataApproveModel.DataApproveRelation{ DataApproveRelationId: 0, DataApproveId: 0, DataId: dataItem.DataId, ClassifyId: dataItem.ClassifyId, CreateTime: now, }) } e = dataApproveModel.AddDataApprove(newApprove, relationList) if e != nil { err = fmt.Errorf("生成审批信息失败, Err: %s", e.Error()) return } approveId = newApprove.DataApproveId // 生成节点审批记录 err = BuildNextNodeRecordAndMsg(firstNodeItem, newApprove.DataType, newApprove.DataApproveId, sysAdminId, sysAdminName, newApprove.Title) return } func toDataApproveItemOrmResp(src []*dataApproveModel.DataApproveItemOrm) (res []*response.DataApproveItemOrmResp) { for _, v := range src { r := new(response.DataApproveItemOrmResp) r.DataApproveId = v.DataApproveId r.DataApproveRecordId = v.DataApproveRecordId r.Title = v.Title r.DataType = v.DataType 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 } // CheckHasDataApproving // @Description: 查询是否还存在带审批的审批单 // @author: Roc // @datetime 2024-12-06 10:43:00 // @return ok bool // @return err error func CheckHasDataApproving() (ok bool, err error) { count, err := dataApproveModel.GetDataApproveCountByState(DataApproveStateApproving) if err != nil { return } if count > 0 { ok = true } return }