bi_approve_flow.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/utils"
  8. "fmt"
  9. "time"
  10. )
  11. // GetBiApproveFlowList 获取BI审批流列表
  12. func GetBiApproveFlowList(condition string, pars []interface{}, startSize, pageSize int) (items []*response.BiApproveFlowItem, total int, msg string, err error) {
  13. total, err = biapprove.GetBiApproveFlowCountByCondition(condition, pars)
  14. if err != nil {
  15. msg = "获取审批流程列表失败"
  16. return
  17. }
  18. if total == 0 {
  19. items = make([]*response.BiApproveFlowItem, 0)
  20. return
  21. }
  22. flowList, err := biapprove.GetBiApproveFlowByCondition(condition, pars, startSize, pageSize)
  23. if err != nil {
  24. msg = "获取审批流程列表失败"
  25. return
  26. }
  27. items = toBiApproveFlowItem(flowList)
  28. return
  29. }
  30. // SaveBiApproveFlow 保存审批流
  31. func SaveBiApproveFlow(flow *request.BiApproveFlowSaveReq) (ok bool, msg string, err error) {
  32. if flow.BiApproveFlowId == 0 {
  33. t := &biapprove.BiApproveFlow{
  34. FlowName: flow.FlowName,
  35. ClassifyId: flow.ClassifyId,
  36. CurrVersion: 1,
  37. CreateTime: time.Now(),
  38. ModifyTime: time.Now(),
  39. }
  40. biFlowNodeItems := make([]*biapprove.BiApproveNode, 0)
  41. for _, node := range flow.Nodes {
  42. biFlowNode := new(biapprove.BiApproveNode)
  43. biFlowNode.ApproveType = node.ApproveType
  44. biFlowNode.CurrVersion = t.CurrVersion
  45. userBytes, er := json.Marshal(node.Users)
  46. if er != nil {
  47. err = er
  48. msg = "保存审批流失败"
  49. return
  50. }
  51. biFlowNode.Users = string(userBytes)
  52. biFlowNode.CreatedTime = time.Now()
  53. biFlowNodeItems = append(biFlowNodeItems, biFlowNode)
  54. }
  55. err = t.Add(biFlowNodeItems)
  56. if err != nil {
  57. msg = "保存审批流失败"
  58. return
  59. }
  60. ok = true
  61. } else {
  62. resFlow, er := biapprove.GetBiApproveFlowById(flow.BiApproveFlowId)
  63. if er != nil {
  64. msg = "保存审批流失败"
  65. err = er
  66. return
  67. }
  68. ok, err = CheckDeleteBiApproveFlow(resFlow.BiApproveFlowId)
  69. if err != nil {
  70. msg = "保存审批流失败"
  71. return
  72. }
  73. if !ok {
  74. msg = "保存审批流失败, 存在还未审批的报告"
  75. return
  76. }
  77. var updateCols []string
  78. if resFlow.FlowName != flow.FlowName {
  79. resFlow.FlowName = flow.FlowName
  80. updateCols = append(updateCols, "flow_name")
  81. }
  82. resFlow.CurrVersion += 1
  83. resFlow.ModifyTime = time.Now()
  84. updateCols = append(updateCols, "modify_time", "curr_version")
  85. biFlowNodeItems := make([]*biapprove.BiApproveNode, 0)
  86. for _, node := range flow.Nodes {
  87. biFlowNode := new(biapprove.BiApproveNode)
  88. biFlowNode.ApproveType = node.ApproveType
  89. biFlowNode.CurrVersion = resFlow.CurrVersion
  90. userBytes, er := json.Marshal(node.Users)
  91. if er != nil {
  92. err = er
  93. msg = "保存审批流失败"
  94. return
  95. }
  96. biFlowNode.Users = string(userBytes)
  97. biFlowNode.CreatedTime = time.Now()
  98. biFlowNodeItems = append(biFlowNodeItems, biFlowNode)
  99. }
  100. err = resFlow.Update(updateCols, biFlowNodeItems)
  101. if err != nil {
  102. msg = "保存审批流失败"
  103. return
  104. }
  105. ok = true
  106. }
  107. return
  108. }
  109. func GetBiApproveFlowDetail(flowId int) (detail *response.BiApproveFlowDetailResp, msg string, err error) {
  110. flowInfo, err := biapprove.GetBiApproveFlowById(flowId)
  111. if err != nil {
  112. msg = "获取审批流详情失败"
  113. return
  114. }
  115. flowNodes, err := biapprove.GetBiApproveNodeByFlowIdAndVersionId(flowId, flowInfo.CurrVersion)
  116. if err != nil {
  117. msg = "获取审批流详情失败"
  118. return
  119. }
  120. detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes)
  121. if err != nil {
  122. msg = "获取审批流详情失败"
  123. return
  124. }
  125. return
  126. }
  127. func DeleteBiApproveFlow(flowId int) (ok bool, msg string, err error) {
  128. ok, err = CheckDeleteBiApproveFlow(flowId)
  129. if err != nil {
  130. msg = "删除审批流失败"
  131. return
  132. }
  133. if !ok {
  134. msg = "删除审批流失败, 存在还未审批的报告"
  135. return
  136. }
  137. t := &biapprove.BiApproveFlow{
  138. BiApproveFlowId: flowId,
  139. }
  140. err = t.Delete()
  141. if err != nil {
  142. msg = "删除审批流失败"
  143. return
  144. }
  145. ok = true
  146. return
  147. }
  148. func CheckDeleteBiApproveFlow(flowId int) (ok bool, err error) {
  149. _, err = biapprove.GetBiApproveFlowById(flowId)
  150. if err != nil {
  151. return
  152. }
  153. // TODO: 检查是否存在还未审批的报告
  154. ok = true
  155. return
  156. }
  157. func toBiApproveFlowItem(src []*biapprove.BiApproveFlow) (res []*response.BiApproveFlowItem) {
  158. res = make([]*response.BiApproveFlowItem, 0, len(src))
  159. for _, item := range src {
  160. res = append(res, &response.BiApproveFlowItem{
  161. BiApproveFlowId: item.BiApproveFlowId,
  162. FlowName: item.FlowName,
  163. ClassifyId: item.ClassifyId,
  164. ClassifyName: item.ClassifyName,
  165. CurrVersion: item.CurrVersion,
  166. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  167. ModifyTime: item.ModifyTime.Format(utils.FormatDateTime),
  168. })
  169. }
  170. return
  171. }
  172. func FormatFlowAndNodesItem2Detail(flowItem *biapprove.BiApproveFlow, nodeItems []*biapprove.BiApproveNode) (detail *response.BiApproveFlowDetailResp, err error) {
  173. if flowItem == nil {
  174. return
  175. }
  176. detail = new(response.BiApproveFlowDetailResp)
  177. detail.BiApproveFlowId = flowItem.BiApproveFlowId
  178. detail.FlowName = flowItem.FlowName
  179. detail.ClassifyId = flowItem.ClassifyId
  180. detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime)
  181. detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime)
  182. detail.Nodes = make([]*response.BiApproveNodeItem, 0)
  183. for _, v := range nodeItems {
  184. t, e := FormatReportApproveNode2Item(v)
  185. if e != nil {
  186. err = fmt.Errorf("format node err: %s", e.Error())
  187. return
  188. }
  189. detail.Nodes = append(detail.Nodes, t)
  190. }
  191. return
  192. }