approve_flow.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package data_approve
  2. import (
  3. "encoding/json"
  4. DataApprove "eta_gn/eta_api/models/data_manage/data_approve"
  5. "eta_gn/eta_api/models/data_manage/data_approve/request"
  6. "eta_gn/eta_api/models/data_manage/data_approve/response"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "time"
  10. )
  11. // SaveDataApproveFlow 保存审批流
  12. func SaveDataApproveFlow(flow *request.DataApproveFlowSaveReq) (ok bool, msg string, err error) {
  13. flowInfo, err := DataApprove.GetDataApproveFlowByDataType(flow.DataType)
  14. if err != nil {
  15. if !utils.IsErrNoRow(err) {
  16. msg = "保存审批流失败, 查找审批流失败"
  17. return
  18. }
  19. }
  20. if flowInfo.DataApproveFlowId <= 0 {
  21. // 之前没有过记录,所以走新增
  22. var remark string
  23. switch flow.DataType {
  24. case utils.DataApproveTypeEdb:
  25. remark = `指标审批`
  26. case utils.DataApproveTypeChart:
  27. remark = `图表审批`
  28. }
  29. t := &DataApprove.DataApproveFlow{
  30. FlowName: flow.FlowName,
  31. DataType: flow.DataType,
  32. Remark: remark,
  33. CurrVersion: 1,
  34. CreateTime: time.Now(),
  35. ModifyTime: time.Now(),
  36. }
  37. flowNodeItems := make([]*DataApprove.DataApproveNode, 0)
  38. for _, node := range flow.Nodes {
  39. flowNode := new(DataApprove.DataApproveNode)
  40. flowNode.ApproveType = node.ApproveType
  41. flowNode.CurrVersion = t.CurrVersion
  42. userBytes, er := json.Marshal(node.Users)
  43. if er != nil {
  44. err = er
  45. msg = "保存审批流失败"
  46. return
  47. }
  48. flowNode.Users = string(userBytes)
  49. flowNode.CreatedTime = time.Now()
  50. flowNodeItems = append(flowNodeItems, flowNode)
  51. }
  52. err = t.Add(flowNodeItems)
  53. if err != nil {
  54. msg = "保存审批流失败"
  55. return
  56. }
  57. ok = true
  58. } else {
  59. ok, err = CheckDeleteDataApproveFlow(flowInfo.DataApproveFlowId)
  60. if err != nil {
  61. msg = "保存审批流失败"
  62. return
  63. }
  64. if !ok {
  65. if flowInfo.DataType == utils.DataApproveTypeEdb {
  66. msg = "保存审批流失败, 存在还未审批的指标"
  67. } else {
  68. msg = "保存审批流失败, 存在还未审批的图表"
  69. }
  70. return
  71. }
  72. var updateCols []string
  73. if flowInfo.FlowName != flow.FlowName {
  74. flowInfo.FlowName = flow.FlowName
  75. updateCols = append(updateCols, "flow_name")
  76. }
  77. flowInfo.CurrVersion += 1
  78. flowInfo.ModifyTime = time.Now()
  79. updateCols = append(updateCols, "modify_time", "curr_version")
  80. biFlowNodeItems := make([]*DataApprove.DataApproveNode, 0)
  81. for _, node := range flow.Nodes {
  82. if node.Users == nil || len(node.Users) == 0 {
  83. continue
  84. }
  85. flowNode := new(DataApprove.DataApproveNode)
  86. flowNode.ApproveType = node.ApproveType
  87. flowNode.CurrVersion = flowInfo.CurrVersion
  88. userBytes, er := json.Marshal(node.Users)
  89. if er != nil {
  90. err = er
  91. msg = "保存审批流失败"
  92. return
  93. }
  94. flowNode.Users = string(userBytes)
  95. flowNode.CreatedTime = time.Now()
  96. biFlowNodeItems = append(biFlowNodeItems, flowNode)
  97. }
  98. err = flowInfo.Update(updateCols, biFlowNodeItems)
  99. if err != nil {
  100. msg = "保存审批流失败"
  101. return
  102. }
  103. ok = true
  104. }
  105. return
  106. }
  107. // GetDataApproveFlowDetail 获取审批流详情
  108. func GetDataApproveFlowDetail(dataType int) (detail *response.DataApproveFlowDetailResp, msg string, err error) {
  109. flowInfo, err := DataApprove.GetDataApproveFlowByDataType(dataType)
  110. if err != nil && !utils.IsErrNoRow(err) {
  111. msg = "获取审批流详情失败"
  112. return
  113. }
  114. flowNodes := make([]*DataApprove.DataApproveNode, 0)
  115. // 如果有配置的话
  116. if flowInfo.DataApproveFlowId > 0 {
  117. flowNodes, err = DataApprove.GetDataApproveNodeByFlowIdAndVersionId(flowInfo.DataApproveFlowId, flowInfo.CurrVersion)
  118. if err != nil {
  119. msg = "获取审批流详情失败"
  120. return
  121. }
  122. }
  123. detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes)
  124. if err != nil {
  125. msg = "获取审批流详情失败"
  126. return
  127. }
  128. return
  129. }
  130. // CheckDeleteDataApproveFlow 检查是否可以删除审批流
  131. func CheckDeleteDataApproveFlow(flowId int) (ok bool, err error) {
  132. flowInfo, err := DataApprove.GetDataApproveFlowById(flowId)
  133. if err != nil {
  134. return
  135. }
  136. // 检查是否存在还未审批的Bi看报
  137. approveList, err := DataApprove.GetDataApproveByFlowIdAndVersionId(flowInfo.DataApproveFlowId, flowInfo.CurrVersion)
  138. if err != nil {
  139. return
  140. }
  141. for _, v := range approveList {
  142. if v.State == DataApproveStateApproving {
  143. return false, nil
  144. }
  145. }
  146. ok = true
  147. return
  148. }
  149. func FormatFlowAndNodesItem2Detail(flowItem *DataApprove.DataApproveFlow, nodeItems []*DataApprove.DataApproveNode) (detail *response.DataApproveFlowDetailResp, err error) {
  150. if flowItem == nil {
  151. return
  152. }
  153. detail = new(response.DataApproveFlowDetailResp)
  154. detail.DataApproveFlowId = flowItem.DataApproveFlowId
  155. detail.FlowName = flowItem.FlowName
  156. detail.DataType = flowItem.DataType
  157. detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime)
  158. detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime)
  159. detail.Nodes = make([]*response.DataApproveNodeItem, 0)
  160. for _, v := range nodeItems {
  161. t, e := FormatDataApproveNode2Item(v)
  162. if e != nil {
  163. err = fmt.Errorf("format node err: %s", e.Error())
  164. return
  165. }
  166. detail.Nodes = append(detail.Nodes, t)
  167. }
  168. return
  169. }