bi_approve_flow.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package biapprove
  2. import (
  3. "encoding/json"
  4. biapprove "eta_gn/eta_api/models/bi_approve"
  5. "eta_gn/eta_api/models/bi_approve/request"
  6. "eta_gn/eta_api/models/bi_approve/response"
  7. "eta_gn/eta_api/models/bi_dashboard"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "time"
  11. )
  12. // GetBiApproveFlowList 获取BI审批流列表
  13. func GetBiApproveFlowList(condition string, pars []interface{}, startSize, pageSize int) (items []*response.BiApproveFlowItem, total int, msg string, err error) {
  14. total, err = biapprove.GetBiApproveFlowCountByCondition(condition, pars)
  15. if err != nil {
  16. msg = "获取审批流程列表失败"
  17. return
  18. }
  19. if total == 0 {
  20. items = make([]*response.BiApproveFlowItem, 0)
  21. return
  22. }
  23. flowList, err := biapprove.GetBiApproveFlowByCondition(condition, pars, startSize, pageSize)
  24. if err != nil {
  25. msg = "获取审批流程列表失败"
  26. return
  27. }
  28. items = toBiApproveFlowItem(flowList)
  29. return
  30. }
  31. // SaveBiApproveFlow 保存审批流
  32. func SaveBiApproveFlow(flow *request.BiApproveFlowSaveReq) (ok bool, msg string, err error) {
  33. classifyInfo, err := bi_dashboard.GetBiDashboardClassifyById(flow.ClassifyId)
  34. if err != nil {
  35. if utils.IsErrNoRow(err) {
  36. msg = "保存审批流失败, 分类不存在"
  37. return
  38. }
  39. msg = "保存审批流失败"
  40. return
  41. }
  42. if flow.BiApproveFlowId == 0 {
  43. tmp, er := biapprove.GetBiApproveFlowByClassifyId(flow.ClassifyId)
  44. if er != nil && !utils.IsErrNoRow(er) {
  45. msg = "保存审批流失败"
  46. err = er
  47. return
  48. }
  49. if tmp != nil && tmp.BiApproveFlowId > 0 {
  50. msg = "保存审批流失败, 分类下已存在审批流"
  51. err = fmt.Errorf("分类下已存在审批流")
  52. return
  53. }
  54. t := &biapprove.BiApproveFlow{
  55. FlowName: flow.FlowName,
  56. ClassifyId: flow.ClassifyId,
  57. ClassifyName: classifyInfo.BiDashboardClassifyName,
  58. CurrVersion: 1,
  59. CreateTime: time.Now(),
  60. ModifyTime: time.Now(),
  61. }
  62. biFlowNodeItems := make([]*biapprove.BiApproveNode, 0)
  63. for _, node := range flow.Nodes {
  64. biFlowNode := new(biapprove.BiApproveNode)
  65. biFlowNode.ApproveType = node.ApproveType
  66. biFlowNode.CurrVersion = t.CurrVersion
  67. userBytes, er := json.Marshal(node.Users)
  68. if er != nil {
  69. err = er
  70. msg = "保存审批流失败"
  71. return
  72. }
  73. biFlowNode.Users = string(userBytes)
  74. biFlowNode.CreatedTime = time.Now()
  75. biFlowNodeItems = append(biFlowNodeItems, biFlowNode)
  76. }
  77. err = t.Add(biFlowNodeItems)
  78. if err != nil {
  79. msg = "保存审批流失败"
  80. return
  81. }
  82. ok = true
  83. } else {
  84. resFlow, er := biapprove.GetBiApproveFlowById(flow.BiApproveFlowId)
  85. if er != nil {
  86. msg = "保存审批流失败"
  87. err = er
  88. return
  89. }
  90. ok, err = CheckDeleteBiApproveFlow(resFlow.BiApproveFlowId)
  91. if err != nil {
  92. msg = "保存审批流失败"
  93. return
  94. }
  95. if !ok {
  96. msg = "保存审批流失败, 存在还未审批的报告"
  97. return
  98. }
  99. var updateCols []string
  100. if resFlow.FlowName != flow.FlowName {
  101. resFlow.FlowName = flow.FlowName
  102. updateCols = append(updateCols, "flow_name")
  103. }
  104. resFlow.CurrVersion += 1
  105. resFlow.ModifyTime = time.Now()
  106. updateCols = append(updateCols, "modify_time", "curr_version")
  107. biFlowNodeItems := make([]*biapprove.BiApproveNode, 0)
  108. for _, node := range flow.Nodes {
  109. biFlowNode := new(biapprove.BiApproveNode)
  110. biFlowNode.ApproveType = node.ApproveType
  111. biFlowNode.CurrVersion = resFlow.CurrVersion
  112. userBytes, er := json.Marshal(node.Users)
  113. if er != nil {
  114. err = er
  115. msg = "保存审批流失败"
  116. return
  117. }
  118. biFlowNode.Users = string(userBytes)
  119. biFlowNode.CreatedTime = time.Now()
  120. biFlowNodeItems = append(biFlowNodeItems, biFlowNode)
  121. }
  122. err = resFlow.Update(updateCols, biFlowNodeItems)
  123. if err != nil {
  124. msg = "保存审批流失败"
  125. return
  126. }
  127. ok = true
  128. }
  129. return
  130. }
  131. // GetBiApproveFlowDetail 获取审批流详情
  132. func GetBiApproveFlowDetail(flowId int) (detail *response.BiApproveFlowDetailResp, msg string, err error) {
  133. flowInfo, err := biapprove.GetBiApproveFlowById(flowId)
  134. if err != nil {
  135. msg = "获取审批流详情失败"
  136. return
  137. }
  138. flowNodes, err := biapprove.GetBiApproveNodeByFlowIdAndVersionId(flowId, flowInfo.CurrVersion)
  139. if err != nil {
  140. msg = "获取审批流详情失败"
  141. return
  142. }
  143. detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes)
  144. if err != nil {
  145. msg = "获取审批流详情失败"
  146. return
  147. }
  148. return
  149. }
  150. // DeleteBiApproveFlow 删除审批流
  151. func DeleteBiApproveFlow(flowId int) (ok bool, msg string, err error) {
  152. ok, err = CheckDeleteBiApproveFlow(flowId)
  153. if err != nil {
  154. if utils.IsErrNoRow(err) {
  155. msg = "删除审批流失败, 审批流不存在"
  156. return
  157. }
  158. msg = "删除审批流失败"
  159. return
  160. }
  161. if !ok {
  162. msg = "删除审批流失败, 存在还未审批的报告"
  163. return
  164. }
  165. t := &biapprove.BiApproveFlow{
  166. BiApproveFlowId: flowId,
  167. }
  168. err = t.Delete()
  169. if err != nil {
  170. msg = "删除审批流失败"
  171. return
  172. }
  173. ok = true
  174. return
  175. }
  176. // CheckDeleteBiApproveFlow 检查是否可以删除审批流
  177. func CheckDeleteBiApproveFlow(flowId int) (ok bool, err error) {
  178. flowInfo, err := biapprove.GetBiApproveFlowById(flowId)
  179. if err != nil {
  180. return
  181. }
  182. // 检查是否存在还未审批的Bi看报
  183. approveList, err := biapprove.GetBiApproveByFlowIdAndVersionId(flowInfo.BiApproveFlowId, flowInfo.CurrVersion)
  184. if err != nil {
  185. return
  186. }
  187. for _, v := range approveList {
  188. if v.State == BiApproveStateApproving {
  189. return false, nil
  190. }
  191. }
  192. ok = true
  193. return
  194. }
  195. func toBiApproveFlowItem(src []*biapprove.BiApproveFlow) (res []*response.BiApproveFlowItem) {
  196. res = make([]*response.BiApproveFlowItem, 0, len(src))
  197. for _, item := range src {
  198. res = append(res, &response.BiApproveFlowItem{
  199. BiApproveFlowId: item.BiApproveFlowId,
  200. FlowName: item.FlowName,
  201. ClassifyId: item.ClassifyId,
  202. ClassifyName: item.ClassifyName,
  203. CurrVersion: item.CurrVersion,
  204. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  205. ModifyTime: item.ModifyTime.Format(utils.FormatDateTime),
  206. })
  207. }
  208. return
  209. }
  210. func FormatFlowAndNodesItem2Detail(flowItem *biapprove.BiApproveFlow, nodeItems []*biapprove.BiApproveNode) (detail *response.BiApproveFlowDetailResp, err error) {
  211. if flowItem == nil {
  212. return
  213. }
  214. detail = new(response.BiApproveFlowDetailResp)
  215. detail.BiApproveFlowId = flowItem.BiApproveFlowId
  216. detail.FlowName = flowItem.FlowName
  217. detail.ClassifyId = flowItem.ClassifyId
  218. detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime)
  219. detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime)
  220. detail.Nodes = make([]*response.BiApproveNodeItem, 0)
  221. for _, v := range nodeItems {
  222. t, e := FormatBiApproveNode2Item(v)
  223. if e != nil {
  224. err = fmt.Errorf("format node err: %s", e.Error())
  225. return
  226. }
  227. detail.Nodes = append(detail.Nodes, t)
  228. }
  229. return
  230. }
  231. func ModifyBiApproveFlowClassify(classifyId int, classifyName string) (err error) {
  232. flow, err := biapprove.GetBiApproveFlowByClassifyId(classifyId)
  233. if err != nil && !utils.IsErrNoRow(err) {
  234. return
  235. }
  236. if utils.IsErrNoRow(err) {
  237. err = nil
  238. return
  239. }
  240. if classifyName != "" && flow.ClassifyName != classifyName {
  241. err = biapprove.UpdateFlowClassifyName(classifyId, classifyName)
  242. }
  243. return
  244. }