knowledge_approve_flow.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package knowledge_approve
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models/knowledge"
  5. knowledge_approve "eta_gn/eta_api/models/knowledge_approve"
  6. "eta_gn/eta_api/models/knowledge_approve/request"
  7. "eta_gn/eta_api/models/knowledge_approve/response"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "sort"
  11. "strings"
  12. "time"
  13. )
  14. // GetKnowledgeResourceApproveFlowList 获取知识资源审批流列表
  15. func GetKnowledgeResourceApproveFlowList(condition string, pars []interface{}, startSize, pageSize int) (items []*response.KnowledgeResourceApproveFlowItem, total int, msg string, err error) {
  16. total, err = knowledge_approve.GetKnowledgeResourceApproveFlowCountByCondition(condition, pars)
  17. if err != nil {
  18. msg = "获取审批流程列表失败"
  19. return
  20. }
  21. if total == 0 {
  22. items = make([]*response.KnowledgeResourceApproveFlowItem, 0)
  23. return
  24. }
  25. flowList, err := knowledge_approve.GetKnowledgeResourceApproveFlowByCondition(condition, pars, startSize, pageSize)
  26. if err != nil {
  27. msg = "获取审批流程列表失败"
  28. return
  29. }
  30. obj := new(knowledge.KnowledgeClassify)
  31. classifyList, err := obj.GetAllClassify(knowledge.KnowledgeResourceTypeOpinion)
  32. if err != nil {
  33. msg = "获取审批流程列表失败"
  34. err = fmt.Errorf("获取分类列表失败,%+v", err)
  35. return
  36. }
  37. sort.Slice(classifyList, func(i, j int) bool {
  38. if classifyList[i].Level != classifyList[j].Level {
  39. return classifyList[i].Level < classifyList[j].Level
  40. }
  41. return classifyList[i].ClassifyId < classifyList[j].ClassifyId
  42. })
  43. classifyMap := make(map[int]*knowledge.KnowledgeClassify)
  44. for _, classify := range classifyList {
  45. classifyMap[classify.ClassifyId] = classify
  46. }
  47. classifyPathMap := make(map[int]map[int]string)
  48. for _, classify := range classifyList {
  49. if _, ok := classifyPathMap[classify.ClassifyId]; !ok {
  50. classifyPathMap[classify.ClassifyId] = make(map[int]string)
  51. }
  52. currClassifyMap := classifyPathMap[classify.ClassifyId]
  53. currClassify := classify
  54. for i := classify.Level; i > 0; i-- {
  55. currClassifyMap[currClassify.Level] = currClassify.ClassifyName
  56. if currClassify.ParentId == 0 {
  57. break
  58. }
  59. currClassify = classifyMap[currClassify.ParentId]
  60. }
  61. }
  62. items = toKnowledgeResourceApproveFlowItem(flowList, classifyPathMap)
  63. return
  64. }
  65. // SaveKnowledgeResourceApproveFlow 保存审批流
  66. func SaveKnowledgeResourceApproveFlow(flow *request.KnowledgeResourceApproveFlowSaveReq) (ok bool, msg string, err error) {
  67. obj := new(knowledge.KnowledgeClassify)
  68. _, err = obj.GetClassifyById(flow.ClassifyId)
  69. if err != nil {
  70. if utils.IsErrNoRow(err) {
  71. msg = "保存审批流失败, 分类不存在"
  72. return
  73. }
  74. msg = "保存审批流失败"
  75. return
  76. }
  77. if flow.KnowledgeResourceApproveFlowId == 0 {
  78. tmp, er := knowledge_approve.GetKnowledgeResourceApproveFlowByClassifyId(flow.ClassifyId)
  79. if er != nil && !utils.IsErrNoRow(er) {
  80. msg = "保存审批流失败"
  81. err = er
  82. return
  83. }
  84. if tmp != nil && tmp.KnowledgeResourceApproveFlowId > 0 {
  85. msg = "保存审批流失败, 分类下已存在审批流"
  86. err = fmt.Errorf("分类下已存在审批流")
  87. return
  88. }
  89. t := &knowledge_approve.KnowledgeResourceApproveFlow{
  90. FlowName: flow.FlowName,
  91. ClassifyId: flow.ClassifyId,
  92. CurrVersion: 1,
  93. CreateTime: time.Now(),
  94. ModifyTime: time.Now(),
  95. }
  96. knowledgeFlowNodeItems := make([]*knowledge_approve.KnowledgeResourceApproveNode, 0)
  97. for _, node := range flow.Nodes {
  98. knowledgeFlowNode := new(knowledge_approve.KnowledgeResourceApproveNode)
  99. knowledgeFlowNode.ApproveType = node.ApproveType
  100. knowledgeFlowNode.CurrVersion = t.CurrVersion
  101. userBytes, er := json.Marshal(node.Users)
  102. if er != nil {
  103. err = er
  104. msg = "保存审批流失败"
  105. return
  106. }
  107. knowledgeFlowNode.Users = string(userBytes)
  108. knowledgeFlowNode.CreatedTime = time.Now()
  109. knowledgeFlowNodeItems = append(knowledgeFlowNodeItems, knowledgeFlowNode)
  110. }
  111. err = t.Add(knowledgeFlowNodeItems)
  112. if err != nil {
  113. msg = "保存审批流失败"
  114. return
  115. }
  116. ok = true
  117. } else {
  118. resFlow, er := knowledge_approve.GetKnowledgeResourceApproveFlowById(flow.KnowledgeResourceApproveFlowId)
  119. if er != nil {
  120. msg = "保存审批流失败"
  121. err = er
  122. return
  123. }
  124. ok, err = CheckDeleteKnowledgeResourceApproveFlow(resFlow.KnowledgeResourceApproveFlowId)
  125. if err != nil {
  126. msg = "保存审批流失败"
  127. return
  128. }
  129. if !ok {
  130. msg = "保存审批流失败, 存在还未审批的报告"
  131. return
  132. }
  133. var updateCols []string
  134. if resFlow.FlowName != flow.FlowName {
  135. resFlow.FlowName = flow.FlowName
  136. updateCols = append(updateCols, "flow_name")
  137. }
  138. resFlow.CurrVersion += 1
  139. resFlow.ModifyTime = time.Now()
  140. updateCols = append(updateCols, "modify_time", "curr_version")
  141. knowledgeFlowNodeItems := make([]*knowledge_approve.KnowledgeResourceApproveNode, 0)
  142. for _, node := range flow.Nodes {
  143. knowledgeFlowNode := new(knowledge_approve.KnowledgeResourceApproveNode)
  144. knowledgeFlowNode.ApproveType = node.ApproveType
  145. knowledgeFlowNode.CurrVersion = resFlow.CurrVersion
  146. userBytes, er := json.Marshal(node.Users)
  147. if er != nil {
  148. err = er
  149. msg = "保存审批流失败"
  150. return
  151. }
  152. knowledgeFlowNode.Users = string(userBytes)
  153. knowledgeFlowNode.CreatedTime = time.Now()
  154. knowledgeFlowNodeItems = append(knowledgeFlowNodeItems, knowledgeFlowNode)
  155. }
  156. err = resFlow.Update(updateCols, knowledgeFlowNodeItems)
  157. if err != nil {
  158. msg = "保存审批流失败"
  159. return
  160. }
  161. ok = true
  162. }
  163. return
  164. }
  165. // GetKnowledgeResourceApproveFlowDetail 获取审批流详情
  166. func GetKnowledgeResourceApproveFlowDetail(flowId int) (detail *response.KnowledgeResourceApproveFlowDetailResp, msg string, err error) {
  167. flowInfo, err := knowledge_approve.GetKnowledgeResourceApproveFlowById(flowId)
  168. if err != nil {
  169. msg = "获取审批流详情失败"
  170. return
  171. }
  172. flowNodes, err := knowledge_approve.GetKnowledgeResourceApproveNodeByFlowIdAndVersionId(flowId, flowInfo.CurrVersion)
  173. if err != nil {
  174. msg = "获取审批流详情失败"
  175. return
  176. }
  177. detail, err = FormatFlowAndNodesItem2Detail(flowInfo, flowNodes)
  178. if err != nil {
  179. msg = "获取审批流详情失败"
  180. return
  181. }
  182. return
  183. }
  184. // DeleteKnowledgeResourceApproveFlow 删除审批流
  185. func DeleteKnowledgeResourceApproveFlow(flowId int) (ok bool, msg string, err error) {
  186. ok, err = CheckDeleteKnowledgeResourceApproveFlow(flowId)
  187. if err != nil {
  188. if utils.IsErrNoRow(err) {
  189. msg = "删除审批流失败, 审批流不存在"
  190. return
  191. }
  192. msg = "删除审批流失败"
  193. return
  194. }
  195. if !ok {
  196. msg = "删除审批流失败, 存在还未审批的报告"
  197. return
  198. }
  199. t := &knowledge_approve.KnowledgeResourceApproveFlow{
  200. KnowledgeResourceApproveFlowId: flowId,
  201. }
  202. err = t.Delete()
  203. if err != nil {
  204. msg = "删除审批流失败"
  205. return
  206. }
  207. ok = true
  208. return
  209. }
  210. // CheckDeleteKnowledgeResourceApproveFlow 检查是否可以删除审批流
  211. func CheckDeleteKnowledgeResourceApproveFlow(flowId int) (ok bool, err error) {
  212. flowInfo, err := knowledge_approve.GetKnowledgeResourceApproveFlowById(flowId)
  213. if err != nil {
  214. return
  215. }
  216. // 检查是否存在还未审批的知识资源
  217. approveList, err := knowledge_approve.GetKnowledgeResourceApproveByFlowIdAndVersionId(flowInfo.KnowledgeResourceApproveFlowId, flowInfo.CurrVersion)
  218. if err != nil {
  219. return
  220. }
  221. for _, v := range approveList {
  222. if v.State == KnowledgeResourceApproveStateApproving {
  223. return false, nil
  224. }
  225. }
  226. ok = true
  227. return
  228. }
  229. func toKnowledgeResourceApproveFlowItem(src []*knowledge_approve.KnowledgeResourceApproveFlow, classifyPathMap map[int]map[int]string) (res []*response.KnowledgeResourceApproveFlowItem) {
  230. res = make([]*response.KnowledgeResourceApproveFlowItem, 0, len(src))
  231. for _, item := range src {
  232. classifyArr := make([]string, 0)
  233. for i := 1; i <= 3; i++ {
  234. if v, ok := classifyPathMap[item.ClassifyId][i]; ok {
  235. classifyArr = append(classifyArr, v)
  236. }
  237. }
  238. res = append(res, &response.KnowledgeResourceApproveFlowItem{
  239. KnowledgeResourceApproveFlowId: item.KnowledgeResourceApproveFlowId,
  240. FlowName: item.FlowName,
  241. ClassifyId: item.ClassifyId,
  242. ClassifyName: strings.Join(classifyArr, "/"),
  243. CurrVersion: item.CurrVersion,
  244. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  245. ModifyTime: item.ModifyTime.Format(utils.FormatDateTime),
  246. })
  247. }
  248. return
  249. }
  250. func FormatFlowAndNodesItem2Detail(flowItem *knowledge_approve.KnowledgeResourceApproveFlow, nodeItems []*knowledge_approve.KnowledgeResourceApproveNode) (detail *response.KnowledgeResourceApproveFlowDetailResp, err error) {
  251. if flowItem == nil {
  252. return
  253. }
  254. detail = new(response.KnowledgeResourceApproveFlowDetailResp)
  255. detail.KnowledgeResourceApproveFlowId = flowItem.KnowledgeResourceApproveFlowId
  256. detail.FlowName = flowItem.FlowName
  257. detail.ClassifyId = flowItem.ClassifyId
  258. detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime)
  259. detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime)
  260. detail.Nodes = make([]*response.KnowledgeResourceApproveNodeItem, 0)
  261. for _, v := range nodeItems {
  262. t, e := FormatKnowledgeResourceApproveNode2Item(v)
  263. if e != nil {
  264. err = fmt.Errorf("format node err: %s", e.Error())
  265. return
  266. }
  267. detail.Nodes = append(detail.Nodes, t)
  268. }
  269. return
  270. }
  271. // func ModifyKnowledgeResourceApproveFlowClassify(classifyId int, classifyName string) (err error) {
  272. // flow, err := knowledge_approve.GetKnowledgeResourceApproveFlowByClassifyId(classifyId)
  273. // if err != nil && !utils.IsErrNoRow(err) {
  274. // return
  275. // }
  276. // if utils.IsErrNoRow(err) {
  277. // err = nil
  278. // return
  279. // }
  280. // if classifyName != "" && flow.ClassifyName != classifyName {
  281. // err = knowledge_approve.UpdateFlowClassifyName(classifyId, classifyName)
  282. // }
  283. // return
  284. // }