knowledge_approve.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. package knowledge_approve
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models/knowledge"
  5. "eta_gn/eta_api/models/knowledge_approve"
  6. "eta_gn/eta_api/models/knowledge_approve/response"
  7. knowledgeSrv "eta_gn/eta_api/services/knowledge"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "sort"
  11. "strings"
  12. "time"
  13. )
  14. var (
  15. timeField = map[int]string{1: fmt.Sprintf("a.%s", knowledge_approve.KnowledgeResourceApproveRecordCols.CreateTime), 2: fmt.Sprintf("a.%s", knowledge_approve.KnowledgeResourceApproveRecordCols.NodeApproveTime), 3: fmt.Sprintf("b.%s", knowledge_approve.KnowledgeResourceApproveCols.ApproveTime)}
  16. myTimeField = map[int]string{1: knowledge_approve.KnowledgeResourceApproveCols.CreateTime, 3: knowledge_approve.KnowledgeResourceApproveCols.ApproveTime}
  17. orderRules = map[int]string{1: "ASC", 2: "DESC"}
  18. )
  19. func GetPulicKnowledgeClassifyList(resourceType int) (res []*knowledge.KnowledgeClassifyItem, msg string, err error) {
  20. obj := new(knowledge.KnowledgeClassify)
  21. classifyList, err := obj.GetAllClassify(resourceType)
  22. if err != nil {
  23. msg = "获取公共分类列表失败"
  24. return
  25. }
  26. res = toClassifyTree(classifyList, 0)
  27. return
  28. }
  29. func PassKnowledgeResourceApprove(approveId int, adminId int) (msg string, err error) {
  30. approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(approveId)
  31. if e != nil {
  32. if utils.IsErrNoRow(e) {
  33. msg = "审批不存在, 请刷新页面"
  34. err = e
  35. return
  36. }
  37. msg = "操作失败"
  38. return
  39. }
  40. if approveItem.State != KnowledgeResourceApproveStateApproving {
  41. msg = "审批状态有误, 请刷新页面"
  42. err = fmt.Errorf("审批状态有误, State: %d", approveItem.State)
  43. return
  44. }
  45. var ApprovePars []interface{}
  46. Approveconds := ` AND knowledge_resource_approve_id =? AND approve_user_id =? AND state =? `
  47. ApprovePars = append(ApprovePars, approveId, adminId, KnowledgeResourceApproveStateApproving)
  48. recordItem, er := knowledge_approve.GetKnowledgeResourceApproveRecordByCondition(Approveconds, ApprovePars)
  49. if er != nil {
  50. if utils.IsErrNoRow(er) {
  51. msg = "无权审批"
  52. err = er
  53. return
  54. }
  55. msg = "操作失败"
  56. return
  57. }
  58. // 查询审批流和审批流节点
  59. flowItem, e := knowledge_approve.GetKnowledgeResourceApproveFlowById(approveItem.FlowId)
  60. if e != nil {
  61. err = fmt.Errorf("获取审批流失败, Err: %s", e.Error())
  62. return
  63. }
  64. nodePars := make([]interface{}, 0)
  65. nodeCond := ` AND knowledge_resource_approve_flow_id =? AND curr_version =? `
  66. nodePars = append(nodePars, flowItem.KnowledgeResourceApproveFlowId, flowItem.CurrVersion)
  67. nodeItems, e := knowledge_approve.GetKnowledgeResourceApproveNodeByCondition(nodeCond, nodePars)
  68. if e != nil {
  69. err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error())
  70. return
  71. }
  72. if len(nodeItems) == 0 {
  73. err = fmt.Errorf("无审批节点")
  74. return
  75. }
  76. nodeMap := make(map[int]*knowledge_approve.KnowledgeResourceApproveNode)
  77. for _, v := range nodeItems {
  78. nodeMap[v.KnowledgeResourceApproveNodeId] = v
  79. }
  80. // 取出审批记录的节点
  81. currNodeItem := nodeMap[recordItem.NodeId]
  82. if currNodeItem == nil {
  83. err = fmt.Errorf("当前节点信息有误")
  84. return
  85. }
  86. currNode, e := FormatKnowledgeResourceApproveNode2Item(currNodeItem)
  87. if e != nil {
  88. err = fmt.Errorf("当前节点信息有误, Err: %s", e.Error())
  89. return
  90. }
  91. now := time.Now().Local()
  92. recordItem.State = KnowledgeResourceApproveStatePass
  93. recordItem.ApproveTime = now
  94. recordItem.ModifyTime = now
  95. recordItem.NodeState = KnowledgeResourceApproveStatePass
  96. recordItem.NodeApproveUserId = recordItem.ApproveUserId
  97. recordItem.NodeApproveUserName = recordItem.ApproveUserName
  98. recordItem.NodeApproveTime = now
  99. recordCols := []string{"State", "ApproveTime", "ModifyTime", "NodeState", "NodeApproveUserId", "NodeApproveUserName", "NodeApproveTime"}
  100. lastApprove := false
  101. // 依次审批
  102. if currNode.ApproveType == NodeApproveTypeRoll {
  103. if e = recordItem.Update(recordCols); e != nil {
  104. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  105. return
  106. }
  107. // 检查依次审批情况
  108. sort.Slice(currNode.Users, func(k, j int) bool {
  109. return currNode.Users[k].Sort < currNode.Users[j].Sort
  110. })
  111. userLen := len(currNode.Users)
  112. //lastRoll := false
  113. nextUser := new(response.KnowledgeResourceApproveNodeUser) // 下一个审批人, 为nil则表示当前审批人即为最后
  114. for k, v := range currNode.Users {
  115. // 当前审批人
  116. if v.UserId == adminId && recordItem.ApproveUserSort == v.Sort {
  117. if (k + 1) < userLen {
  118. nextUser = currNode.Users[k+1]
  119. }
  120. }
  121. }
  122. // 当前节点下一个审批人, 生成下一个审批记录且return
  123. if nextUser.UserId > 0 {
  124. newRecord := new(knowledge_approve.KnowledgeResourceApproveRecord)
  125. newRecord.KnowledgeResourceApproveId = recordItem.KnowledgeResourceApproveId
  126. newRecord.State = KnowledgeResourceApproveStateApproving
  127. newRecord.NodeId = currNode.KnowledgeResourceApproveNodeId
  128. newRecord.PrevNodeId = currNode.PrevNodeId
  129. newRecord.NextNodeId = currNode.NextNodeId
  130. newRecord.ApproveType = currNode.ApproveType
  131. newRecord.ApproveUserId = nextUser.UserId
  132. newRecord.ApproveUserName = nextUser.UserName
  133. newRecord.ApproveUserSort = nextUser.Sort
  134. newRecord.CreateTime = now
  135. newRecord.ModifyTime = now
  136. newRecord.NodeState = KnowledgeResourceApproveStateApproving
  137. if e = newRecord.Create(); e != nil {
  138. err = fmt.Errorf("生成审批记录失败, Err: %s", e.Error())
  139. return
  140. }
  141. // 推送审批消息
  142. go func() {
  143. messageItem := new(knowledge_approve.KnowledgeResourceApproveMessage)
  144. messageItem.SendUserId = approveItem.ApplyUserId
  145. messageItem.ReceiveUserId = nextUser.UserId
  146. messageItem.Content = "您有新的待办任务"
  147. messageItem.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle)
  148. messageItem.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId
  149. messageItem.ApproveState = KnowledgeResourceApproveStateApproving
  150. messageItem.CreateTime = now
  151. messageItem.ModifyTime = now
  152. if e = messageItem.Create(); e != nil {
  153. utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove message err: %s", e.Error()))
  154. return
  155. }
  156. }()
  157. return
  158. }
  159. // 更新审批当前节点并进入下一个节点
  160. if currNode.NextNodeId > 0 {
  161. nextNode := nodeMap[currNode.NextNodeId]
  162. approveItem.CurrNodeId = currNode.NextNodeId
  163. approveItem.ModifyTime = now
  164. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  165. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  166. return
  167. }
  168. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.KnowledgeResourceApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle)
  169. return
  170. } else {
  171. // 最后一个节点
  172. lastApprove = true
  173. }
  174. }
  175. // 会签
  176. if currNode.ApproveType == NodeApproveTypeAll {
  177. // 查询其他审批人是否已审批
  178. otherCond := ` AND knowledge_resource_approve_id =? AND node_id =? AND approve_user_id <> ? `
  179. otherPars := make([]interface{}, 0)
  180. otherPars = append(otherPars, approveItem.KnowledgeResourceApproveId, recordItem.NodeId, adminId)
  181. otherRecords, e := knowledge_approve.GetKnowledgeResourceApproveRecordItemsByCondition(otherCond, otherPars)
  182. if e != nil {
  183. err = fmt.Errorf("获取节点审批记录失败, Err: %s", e.Error())
  184. return
  185. }
  186. otherPass := true
  187. for _, v := range otherRecords {
  188. if v.State != KnowledgeResourceApproveStatePass {
  189. otherPass = false
  190. }
  191. }
  192. // 其他人未审批, 仅更新当前审批记录
  193. if e = recordItem.Update(recordCols); e != nil {
  194. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  195. return
  196. }
  197. // 其他人已审批且为最后节点
  198. if otherPass && currNode.NextNodeId == 0 {
  199. lastApprove = true
  200. }
  201. // 其他人已审批且不为最后节点, 进入下一节点
  202. if otherPass && currNode.NextNodeId > 0 {
  203. nextNode := nodeMap[currNode.NextNodeId]
  204. approveItem.CurrNodeId = currNode.NextNodeId
  205. approveItem.ModifyTime = now
  206. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  207. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  208. return
  209. }
  210. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.KnowledgeResourceApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle)
  211. return
  212. }
  213. }
  214. // 或签
  215. if currNode.ApproveType == NodeApproveTypeAny {
  216. // 需检查一下审批的当前节点和记录的节点是否匹配, 不匹配可能是因为另外的审批人已通过, 所以此处应给提示
  217. // 前端也有做相应的判断,但是两个人同时进入审批详情页时就可能出现这种情况
  218. if approveItem.CurrNodeId != recordItem.NodeId {
  219. msg = "该节点已完成审批, 请刷新页面"
  220. return
  221. }
  222. if e = recordItem.Update(recordCols); e != nil {
  223. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  224. return
  225. }
  226. // 将该审批的同一个节点的记录标记为已审批
  227. if e = recordItem.UpdateNodeState(recordItem.KnowledgeResourceApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil {
  228. err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error())
  229. return
  230. }
  231. if currNode.NextNodeId == 0 {
  232. lastApprove = true
  233. }
  234. if currNode.NextNodeId > 0 {
  235. nextNode := nodeMap[currNode.NextNodeId]
  236. approveItem.CurrNodeId = currNode.NextNodeId
  237. approveItem.ModifyTime = now
  238. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  239. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  240. return
  241. }
  242. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.KnowledgeResourceApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.KnowledgeResourceTitle)
  243. return
  244. }
  245. }
  246. // 最后一个审批, 更新审批记录、审批、报告状态、推送消息给申请人
  247. if lastApprove {
  248. if e = recordItem.Update(recordCols); e != nil {
  249. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  250. return
  251. }
  252. approveItem.State = KnowledgeResourceApproveStatePass
  253. approveItem.ApproveTime = now
  254. approveItem.ModifyTime = now
  255. approveCols := []string{"State", "ApproveTime", "ModifyTime"}
  256. if e = approveItem.Update(approveCols); e != nil {
  257. err = fmt.Errorf("更新审批信息失败, Err: %s", e.Error())
  258. return
  259. }
  260. if e = updateKnowledgeResourceApproveState(approveItem.KnowledgeResourceId, KnowledgeStatePass); e != nil {
  261. err = fmt.Errorf("更新报告审批状态失败, Err: %s", e.Error())
  262. return
  263. }
  264. go func() {
  265. obj := new(knowledge.KnowledgeResource)
  266. KnowledgeItem, e := obj.GetById(approveItem.KnowledgeResourceId)
  267. if e != nil && !utils.IsErrNoRow(e) {
  268. utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update knowledge resource state err: %s", e.Error()))
  269. return
  270. }
  271. e = knowledgeSrv.EsAddOrEditKnowledgeResource(KnowledgeItem)
  272. if e != nil {
  273. utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update es err: %s", e.Error()))
  274. return
  275. }
  276. }()
  277. go func() {
  278. messageItem := new(knowledge_approve.KnowledgeResourceApproveMessage)
  279. messageItem.SendUserId = adminId
  280. messageItem.ReceiveUserId = approveItem.ApplyUserId
  281. messageItem.Content = "您提交的审批已通过"
  282. messageItem.Remark = fmt.Sprintf("您提交的【%s】已通过", approveItem.KnowledgeResourceTitle)
  283. messageItem.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId
  284. messageItem.ApproveState = KnowledgeResourceApproveStatePass
  285. messageItem.CreateTime = now
  286. messageItem.ModifyTime = now
  287. if e = messageItem.Create(); e != nil {
  288. utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove message err: %s", e.Error()))
  289. return
  290. }
  291. }()
  292. }
  293. return
  294. }
  295. func toClassifyTree(src []*knowledge.KnowledgeClassify, parentId int) (dst []*knowledge.KnowledgeClassifyItem) {
  296. for _, item := range src {
  297. if item.ParentId == parentId {
  298. tmp := new(knowledge.KnowledgeClassifyItem)
  299. tmp.KnowledgeClassify = *item
  300. tmp.Child = toClassifyTree(src, item.ClassifyId)
  301. dst = append(dst, tmp)
  302. }
  303. }
  304. return
  305. }
  306. // BuildNextNodeRecordAndMsg 生成下一个节点的审批记录并推送消息
  307. func BuildNextNodeRecordAndMsg(approveNodeItem *knowledge_approve.KnowledgeResourceApproveNode, approveId, sysAdminId int, sysAdminName, knowledgeTitle string) (err error) {
  308. if approveNodeItem == nil {
  309. err = fmt.Errorf("approve node nil")
  310. return
  311. }
  312. // 根据节点审批方式生成审批记录
  313. now := time.Now().Local()
  314. approveNode, e := FormatKnowledgeResourceApproveNode2Item(approveNodeItem)
  315. if e != nil {
  316. err = fmt.Errorf("FormatKnowledgeResourceApproveNode2Item err: %s", e.Error())
  317. return
  318. }
  319. if len(approveNode.Users) == 0 {
  320. err = fmt.Errorf("审批节点用户有误")
  321. return
  322. }
  323. newRecords := make([]*knowledge_approve.KnowledgeResourceApproveRecord, 0)
  324. sort.Slice(approveNode.Users, func(k, j int) bool {
  325. return approveNode.Users[k].Sort < approveNode.Users[j].Sort
  326. })
  327. for _, u := range approveNode.Users {
  328. r := new(knowledge_approve.KnowledgeResourceApproveRecord)
  329. r.KnowledgeResourceApproveId = approveId
  330. r.State = KnowledgeResourceApproveStateApproving
  331. r.NodeId = approveNode.KnowledgeResourceApproveNodeId
  332. r.PrevNodeId = approveNode.PrevNodeId
  333. r.NextNodeId = approveNode.NextNodeId
  334. r.ApproveType = approveNode.ApproveType
  335. r.ApproveUserId = u.UserId
  336. r.ApproveUserName = u.UserName
  337. r.ApproveUserSort = u.Sort
  338. r.CreateTime = now
  339. r.ModifyTime = now
  340. r.NodeState = KnowledgeResourceApproveStateApproving // 当前节点审批状态
  341. newRecords = append(newRecords, r)
  342. // 依次审批仅生成一条记录
  343. if approveNode.ApproveType == NodeApproveTypeRoll {
  344. break
  345. }
  346. }
  347. recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord)
  348. if e = recordOb.CreateMulti(newRecords); e != nil {
  349. err = fmt.Errorf("生成节点审批记录失败, Err: %s", e.Error())
  350. return
  351. }
  352. // 推送审批消息
  353. go func() {
  354. messageOb := new(knowledge_approve.KnowledgeResourceApproveMessage)
  355. messages := make([]*knowledge_approve.KnowledgeResourceApproveMessage, 0)
  356. for _, v := range newRecords {
  357. m := new(knowledge_approve.KnowledgeResourceApproveMessage)
  358. m.SendUserId = sysAdminId
  359. m.ReceiveUserId = v.ApproveUserId
  360. m.Content = "您有新的待办任务"
  361. m.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", sysAdminName, knowledgeTitle)
  362. m.KnowledgeResourceApproveId = approveId
  363. m.ApproveState = KnowledgeResourceApproveStateApproving
  364. m.CreateTime = now
  365. m.ModifyTime = now
  366. messages = append(messages, m)
  367. }
  368. e = messageOb.CreateMulti(messages)
  369. if e != nil {
  370. utils.FileLog.Info(fmt.Sprintf("BuildNextNodeRecordAndMsg messages err: %s", e.Error()))
  371. return
  372. }
  373. }()
  374. return
  375. }
  376. // updateKnowledgeResourceApproveState 更新知识资源审批状态
  377. func updateKnowledgeResourceApproveState(KnowledgeResourceId, state int) (err error) {
  378. // updateCols := []string{"ApproveId", "State", "ModifyTime"}
  379. updateCols := []string{"State", "ModifyTime"}
  380. obj := new(knowledge.KnowledgeResource)
  381. KnowledgeItem, e := obj.GetById(KnowledgeResourceId)
  382. if e != nil && !utils.IsErrNoRow(e) {
  383. err = fmt.Errorf("获取知识资源失败, Err: %s", e.Error())
  384. return
  385. }
  386. if KnowledgeItem != nil && KnowledgeItem.KnowledgeResourceId > 0 {
  387. KnowledgeItem.State = state
  388. KnowledgeItem.ModifyTime = time.Now().Local()
  389. if e = KnowledgeItem.Update(updateCols); e != nil {
  390. err = fmt.Errorf("更新知识资源审批状态失败, Err: %s", e.Error())
  391. return
  392. }
  393. }
  394. return
  395. }
  396. func ProcessingKnowledgeResourceApprove(adminId, classifyId, timeType, sortField, sortRule, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.KnowledgeResourceApproveItemOrmResp, respTotal int, msg string, err error) {
  397. cond := fmt.Sprintf(` AND a.%s = ? AND b.%s = ? AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.State, knowledge_approve.KnowledgeResourceApproveCols.State, knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveUserId)
  398. pars := make([]interface{}, 0)
  399. pars = append(pars, KnowledgeResourceApproveStateApproving, KnowledgeResourceApproveStateApproving, adminId)
  400. order := ""
  401. // 筛选条件
  402. if classifyId > 0 {
  403. cond += fmt.Sprintf(` AND b.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ClassifyId)
  404. pars = append(pars, classifyId)
  405. }
  406. if timeType <= 0 {
  407. timeType = 1
  408. }
  409. if timeType == 1 && startTime != "" && endTime != "" {
  410. _, e := time.Parse(utils.FormatDate, startTime)
  411. if e != nil {
  412. msg = "开始时间格式有误"
  413. err = e
  414. return
  415. }
  416. tmpEndTime, e := time.Parse(utils.FormatDate, endTime)
  417. if e != nil {
  418. msg = "结束时间格式有误"
  419. err = e
  420. return
  421. }
  422. tmpEndTime = tmpEndTime.AddDate(0, 0, 1)
  423. cond += fmt.Sprintf(` AND (b.%s BETWEEN ? AND ?)`, knowledge_approve.KnowledgeResourceApproveCols.CreateTime)
  424. pars = append(pars, startTime, tmpEndTime)
  425. }
  426. keyword = strings.TrimSpace(keyword)
  427. if keyword != "" {
  428. kw := fmt.Sprint("%", keyword, "%")
  429. cond += fmt.Sprintf(` AND b.%s LIKE ?`, knowledge_approve.KnowledgeResourceApproveCols.KnowledgeResourceTitle)
  430. pars = append(pars, kw)
  431. }
  432. if sortField > 0 && sortRule > 0 {
  433. orderField := timeField[sortField]
  434. if orderField == "" {
  435. msg = "时间排序字段有误"
  436. return
  437. }
  438. orderRule := orderRules[sortRule]
  439. if orderRule == "" {
  440. msg = "时间排序方式有误"
  441. return
  442. }
  443. order = fmt.Sprintf("%s %s", orderField, orderRule)
  444. }
  445. total, e := knowledge_approve.GetApprovingKnowledgeResourceApproveCount(cond, pars)
  446. if e != nil {
  447. msg = "获取失败"
  448. err = fmt.Errorf("GetApprovingKnowledgeResourceApproveCount err: %s", e.Error())
  449. return
  450. }
  451. list, e := knowledge_approve.GetApprovingKnowledgeResourceApprovePageList(cond, pars, order, startSize, pageSize)
  452. if e != nil {
  453. msg = "获取失败"
  454. err = fmt.Errorf("GetApprovingKnowledgeResourceApprovePageList err: %s", e.Error())
  455. return
  456. }
  457. respList = toKnowledgeResourceApproveItemOrmResp(list)
  458. respTotal = total
  459. return
  460. }
  461. // SolvedKnowledgeResourceApprove 已处理的审批
  462. func SolvedKnowledgeResourceApprove(adminId, classifyId, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.KnowledgeResourceApproveItemOrmResp, respTotal int, msg string, err error) {
  463. cond := fmt.Sprintf(` AND a.%s = ? AND a.%s IN (?)`, knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveUserId, knowledge_approve.KnowledgeResourceApproveRecordCols.NodeState)
  464. pars := make([]interface{}, 0)
  465. pars = append(pars, adminId, []int{KnowledgeResourceApproveStatePass, KnowledgeResourceApproveStateRefuse})
  466. order := ""
  467. // 筛选条件
  468. if classifyId > 0 {
  469. cond += fmt.Sprintf(` AND b.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ClassifyId)
  470. pars = append(pars, classifyId)
  471. }
  472. if timeType > 0 && startTime != "" && endTime != "" {
  473. _, e := time.Parse(utils.FormatDate, startTime)
  474. if e != nil {
  475. msg = "开始时间格式有误"
  476. err = e
  477. return
  478. }
  479. tmpEndTime, e := time.Parse(utils.FormatDate, endTime)
  480. if e != nil {
  481. msg = "结束时间格式有误"
  482. err = e
  483. return
  484. }
  485. tmpEndTime = tmpEndTime.AddDate(0, 0, 1)
  486. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, timeField[timeType])
  487. pars = append(pars, startTime, tmpEndTime)
  488. }
  489. keyword = strings.TrimSpace(keyword)
  490. if keyword != "" {
  491. kw := fmt.Sprint("%", keyword, "%")
  492. cond += fmt.Sprintf(` AND b.%s LIKE ?`, knowledge_approve.KnowledgeResourceApproveCols.KnowledgeResourceTitle)
  493. pars = append(pars, kw)
  494. }
  495. if sortField > 0 && sortRule > 0 {
  496. orderField := timeField[sortField]
  497. if orderField == "" {
  498. msg = "时间排序字段有误"
  499. return
  500. }
  501. orderRule := orderRules[sortRule]
  502. if orderRule == "" {
  503. msg = "时间排序方式有误"
  504. return
  505. }
  506. order = fmt.Sprintf("%s %s", orderField, orderRule)
  507. }
  508. if approveState > 0 {
  509. cond += fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.NodeState)
  510. pars = append(pars, approveState)
  511. }
  512. total, e := knowledge_approve.GetApprovedKnowledgeResourceApproveCount(cond, pars)
  513. if e != nil {
  514. msg = "获取失败"
  515. err = fmt.Errorf("GetApprovedKnowledgeResourceApproveCount err: %s", e.Error())
  516. return
  517. }
  518. list, e := knowledge_approve.GetApprovedKnowledgeResourceApprovePageList(cond, pars, order, startSize, pageSize)
  519. if e != nil {
  520. msg = "获取失败"
  521. err = fmt.Errorf("GetApprovedKnowledgeResourceApprovePageList err: %s", e.Error())
  522. return
  523. }
  524. for _, v := range list {
  525. // 这个时候的状态,用审批状态
  526. v.RecordState = v.NodeState
  527. v.ApproveTime = v.NodeApproveTime
  528. }
  529. respList = toKnowledgeResourceApproveItemOrmResp(list)
  530. respTotal = total
  531. return
  532. }
  533. func MyApplyKnowledgeResourceApproves(adminId, classifyId, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.KnowledgeResourceApproveItemOrmResp, respTotal int, msg string, err error) {
  534. cond := fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ApplyUserId)
  535. pars := make([]interface{}, 0)
  536. pars = append(pars, adminId)
  537. order := ""
  538. // 筛选条件
  539. if classifyId > 0 {
  540. cond += fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveCols.ClassifyId)
  541. pars = append(pars, classifyId)
  542. }
  543. if timeType > 0 && startTime != "" && endTime != "" {
  544. _, e := time.Parse(utils.FormatDate, startTime)
  545. if e != nil {
  546. msg = "开始时间格式有误"
  547. err = e
  548. return
  549. }
  550. tmpEndTime, e := time.Parse(utils.FormatDate, endTime)
  551. if e != nil {
  552. msg = "结束时间格式有误"
  553. err = e
  554. return
  555. }
  556. tmpEndTimeStr := tmpEndTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  557. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, myTimeField[timeType])
  558. pars = append(pars, startTime, tmpEndTimeStr)
  559. }
  560. keyword = strings.TrimSpace(keyword)
  561. if keyword != "" {
  562. kw := fmt.Sprint("%", keyword, "%")
  563. cond += fmt.Sprintf(` AND a.%s LIKE ?`, knowledge_approve.KnowledgeResourceApproveCols.KnowledgeResourceTitle)
  564. pars = append(pars, kw)
  565. }
  566. if sortField > 0 && sortRule > 0 {
  567. orderField := myTimeField[sortField]
  568. if orderField == "" {
  569. msg = "时间排序字段有误"
  570. return
  571. }
  572. orderRule := orderRules[sortRule]
  573. if orderRule == "" {
  574. msg = "时间排序方式有误"
  575. return
  576. }
  577. order = fmt.Sprintf("%s %s", orderField, orderRule)
  578. }
  579. if approveState > 0 {
  580. cond += fmt.Sprintf(` AND a.%s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.State)
  581. pars = append(pars, approveState)
  582. }
  583. total, e := knowledge_approve.GetApplyKnowledgeResourceApproveCount(cond, pars)
  584. if e != nil {
  585. msg = "获取失败"
  586. err = fmt.Errorf("GetApplyKnowledgeResourceApproveCount err: %s", e.Error())
  587. return
  588. }
  589. respTotal = total
  590. list, e := knowledge_approve.GetApplyKnowledgeResourceApprovePageList(cond, pars, order, startSize, pageSize)
  591. if e != nil {
  592. msg = "获取失败"
  593. err = fmt.Errorf("GetApplyKnowledgeResourceApprovePageList err: %s", e.Error())
  594. return
  595. }
  596. respList = toKnowledgeResourceApproveItemOrmResp(list)
  597. return
  598. }
  599. func GetApproveDetail(approveId int) (resp *response.KnowledgeResourceApproveDetail, msg string, err error) {
  600. approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(approveId)
  601. if e != nil {
  602. if utils.IsErrNoRow(e) {
  603. msg = "审批已被删除, 请刷新页面"
  604. err = e
  605. return
  606. }
  607. msg = "获取失败"
  608. err = fmt.Errorf("GetItemById err: %s", e.Error())
  609. return
  610. }
  611. // 审批信息
  612. detail := new(response.KnowledgeResourceApproveDetail)
  613. detail.Approve = new(response.KnowledgeResourceApproveDetailItem)
  614. detail.Approve.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId
  615. detail.Approve.State = approveItem.State
  616. detail.Approve.FlowId = approveItem.FlowId
  617. detail.Approve.FlowVersion = approveItem.FlowVersion
  618. detail.Approve.StartNodeId = approveItem.StartNodeId
  619. detail.Approve.CurrNodeId = approveItem.CurrNodeId
  620. detail.Approve.ApplyUserId = approveItem.ApplyUserId
  621. detail.Approve.ApplyUserName = approveItem.ApplyUserName
  622. detail.Approve.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ApproveTime)
  623. detail.Approve.CreateTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.CreateTime)
  624. detail.Approve.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ModifyTime)
  625. // 审批节点
  626. nodeOb := new(knowledge_approve.KnowledgeResourceApproveNode)
  627. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, knowledge_approve.KnowledgeResourceApproveNodeCols.KnowledgeResourceApproveFlowId, knowledge_approve.KnowledgeResourceApproveNodeCols.CurrVersion)
  628. nodePars := make([]interface{}, 0)
  629. nodePars = append(nodePars, approveItem.FlowId, approveItem.FlowVersion)
  630. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  631. if e != nil {
  632. msg = "获取失败"
  633. err = fmt.Errorf("GetItemsByCondition err: %s", e.Error())
  634. return
  635. }
  636. // 审批记录
  637. recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord)
  638. recordCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.KnowledgeResourceApproveId)
  639. recordPars := make([]interface{}, 0)
  640. recordPars = append(recordPars, approveItem.KnowledgeResourceApproveId)
  641. recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, fmt.Sprintf("%s DESC", knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveTime))
  642. if e != nil {
  643. msg = "获取失败"
  644. err = fmt.Errorf("GetItemsByCondition err: %s", e.Error())
  645. return
  646. }
  647. recordMap := make(map[string]*knowledge_approve.KnowledgeResourceApproveRecord)
  648. for _, v := range recordItems {
  649. k := fmt.Sprintf("%d-%d", v.NodeId, v.ApproveUserId)
  650. recordMap[k] = v
  651. }
  652. // 审批流节点详情
  653. detail.ApproveFlowNodes = make([]*response.KnowledgeResourceApproveDetailNodes, 0)
  654. for _, v := range nodeItems {
  655. t := new(response.KnowledgeResourceApproveDetailNodes)
  656. t.KnowledgeResourceApproveNodeId = v.KnowledgeResourceApproveNodeId
  657. t.KnowledgeResourceApproveFlowId = v.KnowledgeResourceApproveFlowId
  658. t.PrevNodeId = v.PrevNodeId
  659. t.NextNodeId = v.NextNodeId
  660. t.NodeType = v.NodeType
  661. t.ApproveType = v.ApproveType
  662. t.Users = make([]*response.KnowledgeResourceApproveDetailNodeUser, 0)
  663. us := make([]*response.KnowledgeResourceApproveNodeUserReq, 0)
  664. if v.Users != "" {
  665. e = json.Unmarshal([]byte(v.Users), &us)
  666. if e != nil {
  667. msg = "获取失败"
  668. err = fmt.Errorf("json.Unmarshal err: %s", e.Error())
  669. return
  670. }
  671. }
  672. for _, vu := range us {
  673. u := new(response.KnowledgeResourceApproveDetailNodeUser)
  674. u.UserType = vu.UserType
  675. u.UserId = vu.UserId
  676. u.UserName = vu.UserName
  677. u.Sort = vu.Sort
  678. // 审批记录
  679. k := fmt.Sprintf("%d-%d", v.KnowledgeResourceApproveNodeId, vu.UserId)
  680. r := recordMap[k]
  681. if r != nil {
  682. u.ApproveRecord = new(response.KnowledgeResourceApproveDetailNodeUserRecord)
  683. u.ApproveRecord.KnowledgeResourceApproveRecordId = r.KnowledgeResourceApproveRecordId
  684. u.ApproveRecord.State = r.State
  685. u.ApproveRecord.ApproveUserId = r.ApproveUserId
  686. u.ApproveRecord.ApproveUserName = r.ApproveUserName
  687. u.ApproveRecord.ApproveRemark = r.ApproveRemark
  688. u.ApproveRecord.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, r.ApproveTime)
  689. }
  690. t.Users = append(t.Users, u)
  691. }
  692. sort.Slice(t.Users, func(k, j int) bool {
  693. return t.Users[k].Sort < t.Users[j].Sort
  694. })
  695. detail.ApproveFlowNodes = append(detail.ApproveFlowNodes, t)
  696. }
  697. // 看板信息
  698. cnClassifyIdName := make(map[int]string)
  699. obj := new(knowledge.KnowledgeClassify)
  700. cnClassify, e := obj.GetAllClassify(knowledge.KnowledgeResourceTypeOpinion)
  701. if e != nil {
  702. msg = "获取失败"
  703. err = fmt.Errorf("GetKnowledgeDashboardClassifyAllList err: %s", e.Error())
  704. return
  705. }
  706. for _, v := range cnClassify {
  707. cnClassifyIdName[v.ClassifyId] = v.ClassifyName
  708. }
  709. detail.Knowledge = new(response.KnowledgeResourceApproveDetailKnowledge)
  710. detail.Knowledge.KnowledgeResourceId = approveItem.KnowledgeResourceId
  711. detail.Knowledge.KnowledgeResourceTitle = approveItem.KnowledgeResourceTitle
  712. detail.Knowledge.KnowledgeClassify = cnClassifyIdName[approveItem.ClassifyId]
  713. resp = detail
  714. return
  715. }
  716. func KnowledgeResourceApproveRefuse(KnowledgeResourceApproveId, adminId int, approveRemark string) (msg string, err error) {
  717. approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(KnowledgeResourceApproveId)
  718. if e != nil {
  719. if utils.IsErrNoRow(e) {
  720. msg = "审批不存在, 请刷新页面"
  721. err = e
  722. return
  723. }
  724. msg = "操作失败"
  725. err = fmt.Errorf("GetKnowledgeResourceApproveById err: %s", e.Error())
  726. return
  727. }
  728. if approveItem.State != KnowledgeResourceApproveStateApproving {
  729. msg = "审批状态有误, 请刷新页面"
  730. err = fmt.Errorf("审批状态有误, State: %d", approveItem.State)
  731. return
  732. }
  733. // 校验审批记录和审批
  734. recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord)
  735. recordCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.KnowledgeResourceApproveId, knowledge_approve.KnowledgeResourceApproveRecordCols.ApproveUserId, knowledge_approve.KnowledgeResourceApproveRecordCols.State)
  736. recordPars := make([]interface{}, 0)
  737. recordPars = append(recordPars, approveItem.KnowledgeResourceApproveId, adminId, KnowledgeResourceApproveStateApproving)
  738. recordItem, e := recordOb.GetItemByCondition(recordCond, recordPars, "")
  739. if e != nil {
  740. if utils.IsErrNoRow(e) {
  741. msg = "无权审批"
  742. err = e
  743. return
  744. }
  745. msg = "操作失败"
  746. err = fmt.Errorf("GetItemByCondition err: %s", e.Error())
  747. return
  748. }
  749. // 驳回审批
  750. if e = refuseKnowledgeResourceApprove(approveItem, recordItem, approveRemark, adminId); e != nil {
  751. msg = "操作失败"
  752. err = fmt.Errorf("RefuseKnowledgeResourceApprove err: %s", e.Error())
  753. return
  754. }
  755. return
  756. }
  757. // refuseKnowledgeResourceApprove 驳回审批
  758. func refuseKnowledgeResourceApprove(approveItem *knowledge_approve.KnowledgeResourceApprove, recordItem *knowledge_approve.KnowledgeResourceApproveRecord, approveRemark string, sysAdminId int) (err error) {
  759. if approveItem == nil {
  760. err = fmt.Errorf("审批信息有误")
  761. return
  762. }
  763. if recordItem == nil {
  764. err = fmt.Errorf("审批记录有误")
  765. return
  766. }
  767. // 更新审批记录
  768. now := time.Now().Local()
  769. recordItem.State = KnowledgeResourceApproveStateRefuse
  770. recordItem.ApproveRemark = approveRemark
  771. recordItem.ApproveTime = now
  772. recordItem.ModifyTime = now
  773. recordItem.NodeState = KnowledgeResourceApproveStateRefuse
  774. recordItem.NodeApproveUserId = recordItem.ApproveUserId
  775. recordItem.NodeApproveUserName = recordItem.ApproveUserName
  776. recordItem.NodeApproveTime = now
  777. recordCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime", "NodeState", "NodeApproveUserId", "NodeApproveUserName", "NodeApproveTime"}
  778. if e := recordItem.Update(recordCols); e != nil {
  779. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  780. return
  781. }
  782. // 将该审批的同一个节点的记录标记为已审批
  783. if e := recordItem.UpdateNodeState(recordItem.KnowledgeResourceApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil {
  784. err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error())
  785. return
  786. }
  787. // 驳回-更新审批, 报告状态, 推送消息
  788. approveItem.State = KnowledgeResourceApproveStateRefuse
  789. approveItem.ApproveRemark = approveRemark
  790. approveItem.ApproveTime = now
  791. approveItem.ModifyTime = now
  792. approveCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime"}
  793. if e := approveItem.Update(approveCols); e != nil {
  794. err = fmt.Errorf("更新审批状态失败, Err: %s", e.Error())
  795. return
  796. }
  797. if e := updateKnowledgeResourceApproveState(approveItem.KnowledgeResourceId, KnowledgeStateRefused); e != nil {
  798. err = fmt.Errorf("更新报告状态失败, Err: %s", e.Error())
  799. return
  800. }
  801. // 推送驳回消息给申请人
  802. go func() {
  803. messageItem := new(knowledge_approve.KnowledgeResourceApproveMessage)
  804. messageItem.SendUserId = sysAdminId
  805. messageItem.ReceiveUserId = approveItem.ApplyUserId
  806. messageItem.Content = "您提交的审批被驳回"
  807. messageItem.Remark = fmt.Sprintf("您提交的【%s】已被驳回", approveItem.KnowledgeResourceTitle)
  808. messageItem.KnowledgeResourceApproveId = approveItem.KnowledgeResourceApproveId
  809. messageItem.ApproveState = KnowledgeResourceApproveStateRefuse
  810. messageItem.CreateTime = now
  811. messageItem.ModifyTime = now
  812. if e := messageItem.Create(); e != nil {
  813. utils.FileLog.Info(fmt.Sprintf("ApproveKnowledge message err: %s", e.Error()))
  814. return
  815. }
  816. }()
  817. return
  818. }
  819. func KnowledgeResourceApproveCancel(KnowledgeResourceApproveId, adminId int, adminName string) (msg string, err error) {
  820. approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(KnowledgeResourceApproveId)
  821. if e != nil {
  822. if utils.IsErrNoRow(e) {
  823. msg = "审批已被删除, 请刷新页面"
  824. err = e
  825. return
  826. }
  827. msg = "操作失败"
  828. err = fmt.Errorf("GetKnowledgeResourceApproveById err: %s", e.Error())
  829. return
  830. }
  831. if approveItem.ApplyUserId != adminId {
  832. msg = "非申请人不可撤销"
  833. err = fmt.Errorf("非申请人不可撤销")
  834. return
  835. }
  836. // 撤销审批
  837. e = cancelKnowledgeResourceApprove(approveItem.KnowledgeResourceId, approveItem.KnowledgeResourceApproveId, adminId, adminName)
  838. if e != nil {
  839. msg = "操作失败"
  840. err = fmt.Errorf("cancelKnowledgeResourceApprove err: %s", e.Error())
  841. return
  842. }
  843. return
  844. }
  845. // cancelKnowledgeResourceApprove 撤回审批
  846. func cancelKnowledgeResourceApprove(knowledgeId, approveId, sysAdminId int, sysAdminName string) (err error) {
  847. // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态
  848. // confMap, e := models.GetBusinessConf()
  849. // if e != nil {
  850. // err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  851. // return
  852. // }
  853. // openApprove := t
  854. // if !openApprove {
  855. // //err = fmt.Errorf("未开启审批")
  856. // return
  857. // }
  858. // 修改审批信息状态
  859. approveItem, e := knowledge_approve.GetKnowledgeResourceApproveById(approveId)
  860. if e != nil {
  861. if utils.IsErrNoRow(e) {
  862. err = e
  863. return
  864. }
  865. err = fmt.Errorf("approve GetItemById err: %s", e.Error())
  866. return
  867. }
  868. if approveItem.State == KnowledgeResourceApproveStateCancel {
  869. return
  870. }
  871. approveItem.State = KnowledgeResourceApproveStateCancel
  872. approveItem.ModifyTime = time.Now()
  873. cols := []string{"State", "ModifyTime"}
  874. if e = approveItem.Update(cols); e != nil {
  875. err = fmt.Errorf("approve Update err: %s", e.Error())
  876. return
  877. }
  878. // 修改报告状态
  879. e = updateKnowledgeResourceApproveState(knowledgeId, KnowledgeStateUnpublished)
  880. if e != nil {
  881. err = fmt.Errorf("更新报告审批撤回失败, Err: %s", e.Error())
  882. return
  883. }
  884. // 推送到Es
  885. go func() {
  886. obj := new(knowledge.KnowledgeResource)
  887. KnowledgeItem, e := obj.GetById(approveItem.KnowledgeResourceId)
  888. if e != nil && !utils.IsErrNoRow(e) {
  889. utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update knowledge resource state err: %s", e.Error()))
  890. return
  891. }
  892. e = knowledgeSrv.EsAddOrEditKnowledgeResource(KnowledgeItem)
  893. if e != nil {
  894. utils.FileLog.Info(fmt.Sprintf("PassKnowledgeResourceApprove update es err: %s", e.Error()))
  895. return
  896. }
  897. }()
  898. // 推送撤回消息
  899. go func() {
  900. recordOb := new(knowledge_approve.KnowledgeResourceApproveRecord)
  901. recordCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveRecordCols.KnowledgeResourceApproveId)
  902. recordPars := make([]interface{}, 0)
  903. recordPars = append(recordPars, approveId)
  904. recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, "")
  905. if e != nil {
  906. utils.FileLog.Info(fmt.Sprintf("approve record GetItemsByCondition err: %s", e.Error()))
  907. return
  908. }
  909. messageOb := new(knowledge_approve.KnowledgeResourceApproveMessage)
  910. messages := make([]*knowledge_approve.KnowledgeResourceApproveMessage, 0)
  911. for _, v := range recordItems {
  912. m := new(knowledge_approve.KnowledgeResourceApproveMessage)
  913. m.SendUserId = sysAdminId
  914. m.ReceiveUserId = v.ApproveUserId
  915. m.Content = fmt.Sprintf("%s提交的【%s】已撤回", sysAdminName, approveItem.KnowledgeResourceTitle)
  916. m.KnowledgeResourceApproveId = approveId
  917. m.ApproveState = KnowledgeResourceApproveStateCancel
  918. m.CreateTime = time.Now().Local()
  919. m.ModifyTime = time.Now().Local()
  920. messages = append(messages, m)
  921. }
  922. e = messageOb.CreateMulti(messages)
  923. if e != nil {
  924. utils.FileLog.Info(fmt.Sprintf("CancelKnowledgeResourceApprove messages err: %s", e.Error()))
  925. return
  926. }
  927. }()
  928. return
  929. }
  930. // CheckKnowledgeOpenApprove 校验报告是否开启了审批流
  931. func CheckKnowledgeOpenApprove(classifyId int) (opening bool, err error) {
  932. // 获取审批配置
  933. // confMap, e := models.GetBusinessConf()
  934. // if e != nil {
  935. // err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  936. // return
  937. // }
  938. // openMap := map[string]bool{"false": false, "true": true}
  939. // openApprove := openMap[confMap[models.BusinessConfIsBIApprove]]
  940. // 查询对应分类是否有审批流
  941. flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow)
  942. flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId)
  943. flowPars := make([]interface{}, 0)
  944. flowPars = append(flowPars, classifyId)
  945. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  946. if e != nil {
  947. if utils.IsErrNoRow(e) {
  948. opening = false
  949. return
  950. }
  951. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  952. return
  953. }
  954. // 开启审批/有审批流
  955. if flowItem != nil {
  956. opening = true
  957. return
  958. }
  959. return
  960. }
  961. // SubmitKnowledgeResourceApprove 提交审批
  962. func SubmitKnowledgeResourceApprove(KnowledgeResourceId int, KnowledgeResourceTitle string, classifyId int, sysAdminId int, sysAdminName string) (approveId int, err error) {
  963. // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态
  964. // confMap, e := models.GetBusinessConf()
  965. // if e != nil {
  966. // err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  967. // return
  968. // }
  969. // openMap := map[string]bool{"false": false, "true": true}
  970. // openApprove := openMap[confMap[models.BusinessConfIsBIApprove]]
  971. // if !openApprove {
  972. // err = fmt.Errorf("未开启审批")
  973. // return
  974. // }
  975. // 查询审批流
  976. flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow)
  977. flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId)
  978. flowPars := make([]interface{}, 0)
  979. flowPars = append(flowPars, classifyId)
  980. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  981. if e != nil {
  982. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  983. return
  984. }
  985. // 查询审批节点
  986. nodeOb := new(knowledge_approve.KnowledgeResourceApproveNode)
  987. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, knowledge_approve.KnowledgeResourceApproveNodeCols.KnowledgeResourceApproveFlowId, knowledge_approve.KnowledgeResourceApproveNodeCols.CurrVersion)
  988. nodePars := make([]interface{}, 0)
  989. nodePars = append(nodePars, flowItem.KnowledgeResourceApproveFlowId, flowItem.CurrVersion)
  990. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  991. if e != nil {
  992. err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error())
  993. return
  994. }
  995. if len(nodeItems) == 0 {
  996. err = fmt.Errorf("无审批节点")
  997. return
  998. }
  999. // 取出首个节点
  1000. firstNodeItem := new(knowledge_approve.KnowledgeResourceApproveNode)
  1001. for _, v := range nodeItems {
  1002. if v.PrevNodeId == 0 {
  1003. firstNodeItem = v
  1004. continue
  1005. }
  1006. }
  1007. if firstNodeItem == nil {
  1008. err = fmt.Errorf("首个审批节点有误")
  1009. return
  1010. }
  1011. // 审批信息
  1012. now := time.Now().Local()
  1013. newApprove := new(knowledge_approve.KnowledgeResourceApprove)
  1014. newApprove.KnowledgeResourceId = KnowledgeResourceId
  1015. newApprove.KnowledgeResourceTitle = KnowledgeResourceTitle
  1016. newApprove.ClassifyId = classifyId
  1017. newApprove.State = KnowledgeResourceApproveStateApproving
  1018. newApprove.FlowId = flowItem.KnowledgeResourceApproveFlowId
  1019. newApprove.FlowVersion = flowItem.CurrVersion
  1020. newApprove.StartNodeId = firstNodeItem.KnowledgeResourceApproveNodeId
  1021. newApprove.CurrNodeId = firstNodeItem.KnowledgeResourceApproveNodeId
  1022. newApprove.ApplyUserId = sysAdminId
  1023. newApprove.ApplyUserName = sysAdminName
  1024. newApprove.CreateTime = now
  1025. newApprove.ModifyTime = now
  1026. if e = newApprove.Create(); e != nil {
  1027. err = fmt.Errorf("生成审批信息失败, Err: %s", e.Error())
  1028. return
  1029. }
  1030. approveId = newApprove.KnowledgeResourceApproveId
  1031. // 生成节点审批记录
  1032. err = BuildNextNodeRecordAndMsg(firstNodeItem, newApprove.KnowledgeResourceApproveId, sysAdminId, sysAdminName, newApprove.KnowledgeResourceTitle)
  1033. return
  1034. }
  1035. func toKnowledgeResourceApproveItemOrmResp(src []*knowledge_approve.KnowledgeResourceApproveItemOrm) (res []*response.KnowledgeResourceApproveItemOrmResp) {
  1036. res = make([]*response.KnowledgeResourceApproveItemOrmResp, 0)
  1037. for _, v := range src {
  1038. r := new(response.KnowledgeResourceApproveItemOrmResp)
  1039. r.KnowledgeResourceApproveId = v.KnowledgeResourceApproveId
  1040. r.KnowledgeResourceApproveRecordId = v.KnowledgeResourceApproveRecordId
  1041. r.KnowledgeResourceId = v.KnowledgeResourceId
  1042. r.KnowledgeResourceTitle = v.KnowledgeResourceTitle
  1043. r.ClassifyId = v.ClassifyId
  1044. r.State = v.State
  1045. r.RecordState = v.RecordState
  1046. r.FlowId = v.FlowId
  1047. r.FlowVersion = v.FlowVersion
  1048. r.StartNodeId = v.StartNodeId
  1049. r.CurrNodeId = v.CurrNodeId
  1050. r.ApplyUserId = v.ApplyUserId
  1051. r.ApplyUserName = v.ApplyUserName
  1052. r.ApproveRemark = v.ApproveRemark
  1053. r.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, v.ApproveTime)
  1054. r.HandleTime = utils.TimeTransferString(utils.FormatDateTime, v.HandleTime)
  1055. r.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  1056. r.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
  1057. r.NodeState = v.NodeState
  1058. res = append(res, r)
  1059. }
  1060. return
  1061. }
  1062. func GetKnowledgeClassifyAll() (list []*knowledge.KnowledgeClassify, msg string, err error) {
  1063. obj := new(knowledge.KnowledgeClassify)
  1064. ClassifyList, e := obj.GetAllClassify(knowledge.KnowledgeResourceTypeOpinion)
  1065. if e != nil {
  1066. msg = "获取失败"
  1067. err = fmt.Errorf("GetKnowledgeDashboardClassifyAllList err: %s", e.Error())
  1068. return
  1069. }
  1070. list = ClassifyList
  1071. return
  1072. }
  1073. func CheckHasApprovingKnowledge() (ok bool, err error) {
  1074. count, err := knowledge_approve.GetKnowledgeResourceApproveCountByState(KnowledgeResourceApproveStateApproving)
  1075. if err != nil {
  1076. return
  1077. }
  1078. if count > 0 {
  1079. ok = true
  1080. }
  1081. return
  1082. }