package data_approve import ( "encoding/json" DataApprove "eta_gn/eta_api/models/data_manage/data_approve" "eta_gn/eta_api/models/data_manage/data_approve/request" "eta_gn/eta_api/models/data_manage/data_approve/response" "eta_gn/eta_api/utils" "fmt" "time" ) // SaveDataApproveFlow 保存审批流 func SaveDataApproveFlow(flow *request.DataApproveFlowSaveReq) (ok bool, msg string, err error) { flowInfo, err := DataApprove.GetDataApproveFlowByDataType(flow.DataType) if err != nil { if !utils.IsErrNoRow(err) { msg = "保存审批流失败, 查找审批流失败" return } } if flowInfo.DataApproveFlowId <= 0 { // 之前没有过记录,所以走新增 var remark string switch flow.DataType { case utils.DataApproveTypeEdb: remark = `指标审批` case utils.DataApproveTypeChart: remark = `图表审批` } t := &DataApprove.DataApproveFlow{ FlowName: flow.FlowName, DataType: flow.DataType, Remark: remark, CurrVersion: 1, CreateTime: time.Now(), ModifyTime: time.Now(), } flowNodeItems := make([]*DataApprove.DataApproveNode, 0) for _, node := range flow.Nodes { flowNode := new(DataApprove.DataApproveNode) flowNode.ApproveType = node.ApproveType flowNode.CurrVersion = t.CurrVersion userBytes, er := json.Marshal(node.Users) if er != nil { err = er msg = "保存审批流失败" return } flowNode.Users = string(userBytes) flowNode.CreatedTime = time.Now() flowNodeItems = append(flowNodeItems, flowNode) } err = t.Add(flowNodeItems) if err != nil { msg = "保存审批流失败" return } ok = true } else { ok, err = CheckDeleteDataApproveFlow(flowInfo.DataApproveFlowId) if err != nil { msg = "保存审批流失败" return } if !ok { if flowInfo.DataType == utils.DataApproveTypeEdb { msg = "保存审批流失败, 存在还未审批的指标" } else { msg = "保存审批流失败, 存在还未审批的图表" } return } var updateCols []string if flowInfo.FlowName != flow.FlowName { flowInfo.FlowName = flow.FlowName updateCols = append(updateCols, "flow_name") } flowInfo.CurrVersion += 1 flowInfo.ModifyTime = time.Now() updateCols = append(updateCols, "modify_time", "curr_version") biFlowNodeItems := make([]*DataApprove.DataApproveNode, 0) for _, node := range flow.Nodes { flowNode := new(DataApprove.DataApproveNode) flowNode.ApproveType = node.ApproveType flowNode.CurrVersion = flowInfo.CurrVersion userBytes, er := json.Marshal(node.Users) if er != nil { err = er msg = "保存审批流失败" return } flowNode.Users = string(userBytes) flowNode.CreatedTime = time.Now() biFlowNodeItems = append(biFlowNodeItems, flowNode) } err = flowInfo.Update(updateCols, biFlowNodeItems) if err != nil { msg = "保存审批流失败" return } ok = true } return } // GetDataApproveFlowDetail 获取审批流详情 func GetDataApproveFlowDetail(dataType int) (detail *response.DataApproveFlowDetailResp, msg string, err error) { flowInfo, err := DataApprove.GetDataApproveFlowByDataType(dataType) if err != nil && !utils.IsErrNoRow(err) { msg = "获取审批流详情失败" return } flowNodes := make([]*DataApprove.DataApproveNode, 0) // 如果有配置的话 if flowInfo.DataApproveFlowId > 0 { flowNodes, err = DataApprove.GetDataApproveNodeByFlowIdAndVersionId(flowInfo.DataApproveFlowId, flowInfo.CurrVersion) if err != nil { msg = "获取审批流详情失败" return } } detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes) if err != nil { msg = "获取审批流详情失败" return } return } // CheckDeleteDataApproveFlow 检查是否可以删除审批流 func CheckDeleteDataApproveFlow(flowId int) (ok bool, err error) { flowInfo, err := DataApprove.GetDataApproveFlowById(flowId) if err != nil { return } // 检查是否存在还未审批的Bi看报 approveList, err := DataApprove.GetDataApproveByFlowIdAndVersionId(flowInfo.DataApproveFlowId, flowInfo.CurrVersion) if err != nil { return } for _, v := range approveList { if v.State == DataApproveStateApproving { return false, nil } } ok = true return } func FormatFlowAndNodesItem2Detail(flowItem *DataApprove.DataApproveFlow, nodeItems []*DataApprove.DataApproveNode) (detail *response.DataApproveFlowDetailResp, err error) { if flowItem == nil { return } detail = new(response.DataApproveFlowDetailResp) detail.DataApproveFlowId = flowItem.DataApproveFlowId detail.FlowName = flowItem.FlowName detail.DataType = flowItem.DataType detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime) detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime) detail.Nodes = make([]*response.DataApproveNodeItem, 0) for _, v := range nodeItems { t, e := FormatDataApproveNode2Item(v) if e != nil { err = fmt.Errorf("format node err: %s", e.Error()) return } detail.Nodes = append(detail.Nodes, t) } return }