123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- package biapprove
- import (
- "encoding/json"
- biapprove "eta_gn/eta_api/models/bi_approve"
- "eta_gn/eta_api/models/bi_approve/request"
- "eta_gn/eta_api/models/bi_approve/response"
- "eta_gn/eta_api/models/bi_dashboard"
- "eta_gn/eta_api/utils"
- "fmt"
- "time"
- )
- // GetBiApproveFlowList 获取BI审批流列表
- func GetBiApproveFlowList(condition string, pars []interface{}, startSize, pageSize int) (items []*response.BiApproveFlowItem, total int, msg string, err error) {
- total, err = biapprove.GetBiApproveFlowCountByCondition(condition, pars)
- if err != nil {
- msg = "获取审批流程列表失败"
- return
- }
- if total == 0 {
- items = make([]*response.BiApproveFlowItem, 0)
- return
- }
- flowList, err := biapprove.GetBiApproveFlowByCondition(condition, pars, startSize, pageSize)
- if err != nil {
- msg = "获取审批流程列表失败"
- return
- }
- items = toBiApproveFlowItem(flowList)
- return
- }
- // SaveBiApproveFlow 保存审批流
- func SaveBiApproveFlow(flow *request.BiApproveFlowSaveReq) (ok bool, msg string, err error) {
- classifyInfo, err := bi_dashboard.GetBiDashboardClassifyById(flow.ClassifyId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- msg = "保存审批流失败, 分类不存在"
- return
- }
- msg = "保存审批流失败"
- return
- }
- if flow.BiApproveFlowId == 0 {
- tmp, er := biapprove.GetBiApproveFlowByClassifyId(flow.ClassifyId)
- if er != nil && !utils.IsErrNoRow(er) {
- msg = "保存审批流失败"
- err = er
- return
- }
- if tmp != nil && tmp.BiApproveFlowId > 0 {
- msg = "保存审批流失败, 分类下已存在审批流"
- err = fmt.Errorf("分类下已存在审批流")
- return
- }
- t := &biapprove.BiApproveFlow{
- FlowName: flow.FlowName,
- ClassifyId: flow.ClassifyId,
- ClassifyName: classifyInfo.BiDashboardClassifyName,
- CurrVersion: 1,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- biFlowNodeItems := make([]*biapprove.BiApproveNode, 0)
- for _, node := range flow.Nodes {
- biFlowNode := new(biapprove.BiApproveNode)
- biFlowNode.ApproveType = node.ApproveType
- biFlowNode.CurrVersion = t.CurrVersion
- userBytes, er := json.Marshal(node.Users)
- if er != nil {
- err = er
- msg = "保存审批流失败"
- return
- }
- biFlowNode.Users = string(userBytes)
- biFlowNode.CreatedTime = time.Now()
- biFlowNodeItems = append(biFlowNodeItems, biFlowNode)
- }
- err = t.Add(biFlowNodeItems)
- if err != nil {
- msg = "保存审批流失败"
- return
- }
- ok = true
- } else {
- resFlow, er := biapprove.GetBiApproveFlowById(flow.BiApproveFlowId)
- if er != nil {
- msg = "保存审批流失败"
- err = er
- return
- }
- ok, err = CheckDeleteBiApproveFlow(resFlow.BiApproveFlowId)
- 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")
- biFlowNodeItems := make([]*biapprove.BiApproveNode, 0)
- for _, node := range flow.Nodes {
- biFlowNode := new(biapprove.BiApproveNode)
- biFlowNode.ApproveType = node.ApproveType
- biFlowNode.CurrVersion = resFlow.CurrVersion
- userBytes, er := json.Marshal(node.Users)
- if er != nil {
- err = er
- msg = "保存审批流失败"
- return
- }
- biFlowNode.Users = string(userBytes)
- biFlowNode.CreatedTime = time.Now()
- biFlowNodeItems = append(biFlowNodeItems, biFlowNode)
- }
- err = resFlow.Update(updateCols, biFlowNodeItems)
- if err != nil {
- msg = "保存审批流失败"
- return
- }
- ok = true
- }
- return
- }
- // GetBiApproveFlowDetail 获取审批流详情
- func GetBiApproveFlowDetail(flowId int) (detail *response.BiApproveFlowDetailResp, msg string, err error) {
- flowInfo, err := biapprove.GetBiApproveFlowById(flowId)
- if err != nil {
- msg = "获取审批流详情失败"
- return
- }
- flowNodes, err := biapprove.GetBiApproveNodeByFlowIdAndVersionId(flowId, flowInfo.CurrVersion)
- if err != nil {
- msg = "获取审批流详情失败"
- return
- }
- detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes)
- if err != nil {
- msg = "获取审批流详情失败"
- return
- }
- return
- }
- // DeleteBiApproveFlow 删除审批流
- func DeleteBiApproveFlow(flowId int) (ok bool, msg string, err error) {
- ok, err = CheckDeleteBiApproveFlow(flowId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- msg = "删除审批流失败, 审批流不存在"
- return
- }
- msg = "删除审批流失败"
- return
- }
- if !ok {
- msg = "删除审批流失败, 存在还未审批的报告"
- return
- }
- t := &biapprove.BiApproveFlow{
- BiApproveFlowId: flowId,
- }
- err = t.Delete()
- if err != nil {
- msg = "删除审批流失败"
- return
- }
- ok = true
- return
- }
- // CheckDeleteBiApproveFlow 检查是否可以删除审批流
- func CheckDeleteBiApproveFlow(flowId int) (ok bool, err error) {
- flowInfo, err := biapprove.GetBiApproveFlowById(flowId)
- if err != nil {
- return
- }
- // 检查是否存在还未审批的Bi看报
- approveList, err := biapprove.GetBiApproveByFlowIdAndVersionId(flowInfo.BiApproveFlowId, flowInfo.CurrVersion)
- if err != nil {
- return
- }
- for _, v := range approveList {
- if v.State == BiApproveStateApproving {
- return false, nil
- }
- }
- ok = true
- return
- }
- func toBiApproveFlowItem(src []*biapprove.BiApproveFlow) (res []*response.BiApproveFlowItem) {
- res = make([]*response.BiApproveFlowItem, 0, len(src))
- for _, item := range src {
- res = append(res, &response.BiApproveFlowItem{
- BiApproveFlowId: item.BiApproveFlowId,
- FlowName: item.FlowName,
- ClassifyId: item.ClassifyId,
- ClassifyName: item.ClassifyName,
- CurrVersion: item.CurrVersion,
- CreateTime: item.CreateTime.Format(utils.FormatDateTime),
- ModifyTime: item.ModifyTime.Format(utils.FormatDateTime),
- })
- }
- return
- }
- func FormatFlowAndNodesItem2Detail(flowItem *biapprove.BiApproveFlow, nodeItems []*biapprove.BiApproveNode) (detail *response.BiApproveFlowDetailResp, err error) {
- if flowItem == nil {
- return
- }
- detail = new(response.BiApproveFlowDetailResp)
- detail.BiApproveFlowId = flowItem.BiApproveFlowId
- 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.BiApproveNodeItem, 0)
- for _, v := range nodeItems {
- t, e := FormatBiApproveNode2Item(v)
- if e != nil {
- err = fmt.Errorf("format node err: %s", e.Error())
- return
- }
- detail.Nodes = append(detail.Nodes, t)
- }
- return
- }
- func ModifyBiApproveFlowClassify(classifyId int, classifyName string) (err error) {
- flow, err := biapprove.GetBiApproveFlowByClassifyId(classifyId)
- if err != nil && !utils.IsErrNoRow(err) {
- return
- }
- if utils.IsErrNoRow(err) {
- err = nil
- return
- }
- if classifyName != "" && flow.ClassifyName != classifyName {
- err = biapprove.UpdateFlowClassifyName(classifyId, classifyName)
- }
- return
- }
|