report_approve.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. package services
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/report_approve"
  5. "eta/eta_api/models/smart_report"
  6. "eta/eta_api/services/alarm_msg"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "sort"
  10. "time"
  11. )
  12. // GetReportClassifyTreeRecursive 递归获取报告分类树
  13. func GetReportClassifyTreeRecursive(list []*models.Classify, parentId int) []*report_approve.ReportClassifyTreeItem {
  14. res := make([]*report_approve.ReportClassifyTreeItem, 0)
  15. for _, v := range list {
  16. if v.ParentId == parentId {
  17. t := new(report_approve.ReportClassifyTreeItem)
  18. t.ClassifyId = v.Id
  19. t.ClassifyName = v.ClassifyName
  20. t.ParentId = v.ParentId
  21. t.Children = GetReportClassifyTreeRecursive(list, v.Id)
  22. res = append(res, t)
  23. }
  24. }
  25. return res
  26. }
  27. // CheckReportApproveFlowChange 校验是否可变更分类
  28. func CheckReportApproveFlowChange(reportType, classifyFirstId, classifySecondId int) (ok bool, err error) {
  29. var count int
  30. cond := ` AND classify_id_first = ? AND classify_id_second = ? AND state = ?`
  31. pars := make([]interface{}, 0)
  32. pars = append(pars, classifyFirstId, classifySecondId, models.ReportStateWaitApprove)
  33. switch reportType {
  34. case report_approve.FlowReportTypeChinese:
  35. ct, e := models.GetReportListCount(cond, pars, "")
  36. if e != nil {
  37. err = fmt.Errorf("GetReportListCount err: %s", e.Error())
  38. return
  39. }
  40. count = ct
  41. case report_approve.FlowReportTypeEnglish:
  42. ct, e := models.GetEnglishReportListCount(cond, pars, "")
  43. if e != nil {
  44. err = fmt.Errorf("GetEnglishReportListCount err: %s", e.Error())
  45. return
  46. }
  47. count = ct
  48. case report_approve.FlowReportTypeSmart:
  49. ob := new(smart_report.SmartReport)
  50. ct, e := ob.GetCountByCondition(cond, pars)
  51. if e != nil {
  52. err = fmt.Errorf("SmartReport GetCountByCondition err: %s", e.Error())
  53. return
  54. }
  55. count = ct
  56. default:
  57. err = fmt.Errorf("报告类型有误")
  58. return
  59. }
  60. if count <= 0 {
  61. ok = true
  62. }
  63. return
  64. }
  65. // CheckReportOpenApprove 校验报告是否开启了审批流
  66. func CheckReportOpenApprove(reportType, firstId, secondId int) (opening bool, err error) {
  67. // 获取审批配置
  68. confMap, e := models.GetBusinessConf()
  69. if e != nil {
  70. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  71. return
  72. }
  73. openMap := map[string]bool{"false": false, "true": true}
  74. openApprove := openMap[confMap[models.BusinessConfIsReportApprove]]
  75. // 查询对应分类是否有审批流
  76. flowOb := new(report_approve.ReportApproveFlow)
  77. flowCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId)
  78. flowPars := make([]interface{}, 0)
  79. flowPars = append(flowPars, reportType, firstId, secondId)
  80. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  81. if e != nil && e.Error() != utils.ErrNoRow() {
  82. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  83. return
  84. }
  85. // 开启审批/有审批流
  86. if openApprove && (flowItem != nil || confMap[models.BusinessConfReportApproveType] == models.BusinessConfReportApproveTypeOther) {
  87. opening = true
  88. return
  89. }
  90. return
  91. }
  92. // CheckReportCurrState 校验报告当前应有的状态
  93. func CheckReportCurrState(reportType, firstId, secondId, operate int) (state int, err error) {
  94. // 获取审批配置
  95. confMap, e := models.GetBusinessConf()
  96. if e != nil {
  97. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  98. return
  99. }
  100. openMap := map[string]bool{"false": false, "true": true}
  101. openApprove := openMap[confMap[models.BusinessConfIsReportApprove]]
  102. // 查询对应分类是否有审批流
  103. flowOb := new(report_approve.ReportApproveFlow)
  104. flowCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId)
  105. flowPars := make([]interface{}, 0)
  106. flowPars = append(flowPars, reportType, firstId, secondId)
  107. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  108. if e != nil && e.Error() != utils.ErrNoRow() {
  109. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  110. return
  111. }
  112. // 开启审批/有审批流
  113. if openApprove && (flowItem != nil || confMap[models.BusinessConfReportApproveType] == models.BusinessConfReportApproveTypeOther) {
  114. // 操作为无审批的操作时, 会转为有审批的初始状态-待提交
  115. stateMap := map[int]int{
  116. models.ReportOperateAdd: models.ReportStateWaitSubmit, // 新增
  117. models.ReportOperateEdit: models.ReportStateWaitSubmit, // 编辑
  118. models.ReportOperatePublish: models.ReportStateWaitSubmit, // 发布
  119. models.ReportOperateCancelPublish: models.ReportStateWaitSubmit, // 取消发布
  120. models.ReportOperateSubmitApprove: models.ReportStateWaitApprove, // 提审
  121. models.ReportOperateCancelApprove: models.ReportStateWaitSubmit, // 撤回
  122. }
  123. state = stateMap[operate]
  124. return
  125. }
  126. // 关闭审批/分类无审批
  127. // 操作为有审批的操作时, 会转为无审批的初始状态-未发布
  128. stateMap := map[int]int{
  129. models.ReportOperateAdd: models.ReportStateUnpublished, // 新增
  130. models.ReportOperateEdit: models.ReportStateUnpublished, // 编辑
  131. models.ReportOperatePublish: models.ReportStatePublished, // 发布
  132. models.ReportOperateCancelPublish: models.ReportStateUnpublished, // 取消发布
  133. models.ReportOperateSubmitApprove: models.ReportStateUnpublished, // 提审
  134. models.ReportOperateCancelApprove: models.ReportStateUnpublished, // 撤回
  135. }
  136. state = stateMap[operate]
  137. return
  138. }
  139. // SubmitReportApprove 提交审批
  140. func SubmitReportApprove(reportType, reportId int, reportTitle string, firstId, secondId int, sysAdminId int, sysAdminName string) (approveId int, err error) {
  141. // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态
  142. confMap, e := models.GetBusinessConf()
  143. if e != nil {
  144. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  145. return
  146. }
  147. openMap := map[string]bool{"false": false, "true": true}
  148. openApprove := openMap[confMap[models.BusinessConfIsReportApprove]]
  149. if !openApprove {
  150. err = fmt.Errorf("未开启审批")
  151. return
  152. }
  153. confApproveType := confMap[models.BusinessConfReportApproveType]
  154. if confApproveType == "" {
  155. confApproveType = models.BusinessConfReportApproveTypeEta
  156. }
  157. if confApproveType == models.BusinessConfReportApproveTypeOther {
  158. return
  159. }
  160. // 查询审批流
  161. flowOb := new(report_approve.ReportApproveFlow)
  162. flowCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId)
  163. flowPars := make([]interface{}, 0)
  164. flowPars = append(flowPars, reportType, firstId, secondId)
  165. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  166. if e != nil {
  167. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  168. return
  169. }
  170. // 查询审批节点
  171. nodeOb := new(report_approve.ReportApproveNode)
  172. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  173. nodePars := make([]interface{}, 0)
  174. nodePars = append(nodePars, flowItem.ReportApproveFlowId, flowItem.CurrVersion)
  175. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  176. if e != nil {
  177. err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error())
  178. return
  179. }
  180. if len(nodeItems) == 0 {
  181. err = fmt.Errorf("无审批节点")
  182. return
  183. }
  184. // 取出首个节点
  185. firstNodeItem := new(report_approve.ReportApproveNode)
  186. for _, v := range nodeItems {
  187. if v.PrevNodeId == 0 {
  188. firstNodeItem = v
  189. continue
  190. }
  191. }
  192. if firstNodeItem == nil {
  193. err = fmt.Errorf("首个审批节点有误")
  194. return
  195. }
  196. // 审批信息
  197. now := time.Now().Local()
  198. newApprove := new(report_approve.ReportApprove)
  199. newApprove.ReportType = reportType
  200. newApprove.ReportId = reportId
  201. newApprove.ReportTitle = reportTitle
  202. newApprove.ClassifyFirstId = firstId
  203. newApprove.ClassifySecondId = secondId
  204. newApprove.State = report_approve.ReportApproveStateApproving
  205. newApprove.FlowId = flowItem.ReportApproveFlowId
  206. newApprove.FlowVersion = flowItem.CurrVersion
  207. newApprove.StartNodeId = firstNodeItem.ReportApproveNodeId
  208. newApprove.CurrNodeId = firstNodeItem.ReportApproveNodeId
  209. newApprove.ApplyUserId = sysAdminId
  210. newApprove.ApplyUserName = sysAdminName
  211. newApprove.CreateTime = now
  212. newApprove.ModifyTime = now
  213. if e = newApprove.Create(); e != nil {
  214. err = fmt.Errorf("生成审批信息失败, Err: %s", e.Error())
  215. return
  216. }
  217. approveId = newApprove.ReportApproveId
  218. // 生成节点审批记录
  219. err = BuildNextNodeRecordAndMsg(firstNodeItem, newApprove.ReportApproveId, sysAdminId, sysAdminName)
  220. return
  221. }
  222. // CancelReportApprove 撤回审批
  223. func CancelReportApprove(reportType, reportId, approveId, sysAdminId int, sysAdminName string) (err error) {
  224. // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态
  225. confMap, e := models.GetBusinessConf()
  226. if e != nil {
  227. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  228. return
  229. }
  230. openMap := map[string]bool{"false": false, "true": true}
  231. openApprove := openMap[confMap[models.BusinessConfIsReportApprove]]
  232. if !openApprove {
  233. //err = fmt.Errorf("未开启审批")
  234. return
  235. }
  236. confApproveType := confMap[models.BusinessConfReportApproveType]
  237. if confApproveType == "" {
  238. confApproveType = models.BusinessConfReportApproveTypeEta
  239. }
  240. // 第三方审批仅修改报告状态
  241. if confApproveType == models.BusinessConfReportApproveTypeOther {
  242. e = updateReportApproveState(reportType, reportId, 0, models.ReportStateWaitSubmit)
  243. if e != nil {
  244. err = fmt.Errorf("更新报告审批撤回失败, Err: %s", e.Error())
  245. return
  246. }
  247. return
  248. }
  249. // 修改审批信息状态
  250. approveOb := new(report_approve.ReportApprove)
  251. approveItem, e := approveOb.GetItemById(approveId)
  252. if e != nil {
  253. if e.Error() == utils.ErrNoRow() {
  254. return
  255. }
  256. err = fmt.Errorf("approve GetItemById err: %s", e.Error())
  257. return
  258. }
  259. if approveItem.State == report_approve.ReportApproveStateCancel {
  260. return
  261. }
  262. approveItem.State = report_approve.ReportApproveStateCancel
  263. cols := []string{"State", "ModifyTime"}
  264. if e = approveItem.Update(cols); e != nil {
  265. err = fmt.Errorf("approve Update err: %s", e.Error())
  266. return
  267. }
  268. // 修改报告状态
  269. e = updateReportApproveState(reportType, reportId, 0, models.ReportStateWaitSubmit)
  270. if e != nil {
  271. err = fmt.Errorf("更新报告审批撤回失败, Err: %s", e.Error())
  272. return
  273. }
  274. // 推送撤回消息
  275. go func() {
  276. recordOb := new(report_approve.ReportApproveRecord)
  277. recordCond := fmt.Sprintf(` AND %s = ?`, report_approve.ReportApproveRecordCols.ReportApproveId)
  278. recordPars := make([]interface{}, 0)
  279. recordPars = append(recordPars, approveId)
  280. recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, "")
  281. if e != nil {
  282. utils.FileLog.Info(fmt.Sprintf("approve record GetItemsByCondition err: %s", e.Error()))
  283. return
  284. }
  285. messageOb := new(report_approve.ReportApproveMessage)
  286. messages := make([]*report_approve.ReportApproveMessage, 0)
  287. for _, v := range recordItems {
  288. m := new(report_approve.ReportApproveMessage)
  289. m.SendUserId = sysAdminId
  290. m.ReceiveUserId = v.ApproveUserId
  291. m.Content = fmt.Sprintf("%s提交的【研报审批】已撤回", sysAdminName)
  292. m.ReportApproveId = approveId
  293. m.ApproveState = report_approve.ReportApproveStateCancel
  294. m.CreateTime = time.Now().Local()
  295. m.ModifyTime = time.Now().Local()
  296. messages = append(messages, m)
  297. }
  298. e = messageOb.CreateMulti(messages)
  299. if e != nil {
  300. utils.FileLog.Info(fmt.Sprintf("CancelReportApprove messages err: %s", e.Error()))
  301. return
  302. }
  303. }()
  304. return
  305. }
  306. // updateReportApproveState 更新报告审批状态
  307. func updateReportApproveState(reportType, reportId, approveId, state int) (err error) {
  308. updateCols := []string{"ApproveId", "State", "ModifyTime"}
  309. if reportType == report_approve.FlowReportTypeChinese {
  310. reportOb := new(models.Report)
  311. reportItem, e := reportOb.GetItemById(reportId)
  312. // 报告可能会被删除, 这里查不到就忽略掉
  313. if e != nil && e.Error() != utils.ErrNoRow() {
  314. err = fmt.Errorf("获取中文研报失败, Err: %s", e.Error())
  315. return
  316. }
  317. if reportItem != nil {
  318. reportItem.ApproveId = approveId
  319. reportItem.State = state
  320. reportItem.ModifyTime = time.Now().Local()
  321. if state == models.ReportStatePass || state == models.ReportStateRefused {
  322. updateCols = append(updateCols, "ApproveTime")
  323. reportItem.ApproveTime = time.Now().Local()
  324. }
  325. if state == models.ReportStatePass {
  326. updateCols = append(updateCols, "PublishTime")
  327. reportItem.PublishTime = time.Now().Local()
  328. }
  329. if e = reportItem.UpdateReport(updateCols); e != nil {
  330. err = fmt.Errorf("更新中文研报审批状态失败, Err: %s", e.Error())
  331. return
  332. }
  333. }
  334. }
  335. if reportType == report_approve.FlowReportTypeEnglish {
  336. reportItem, e := models.GetEnglishReportItemById(reportId)
  337. if e != nil && e.Error() != utils.ErrNoRow() {
  338. err = fmt.Errorf("获取英文研报失败, Err: %s", e.Error())
  339. return
  340. }
  341. if reportItem != nil {
  342. reportItem.ApproveId = approveId
  343. reportItem.State = state
  344. reportItem.ModifyTime = time.Now().Local()
  345. if state == models.ReportStatePass || state == models.ReportStateRefused {
  346. updateCols = append(updateCols, "ApproveTime")
  347. reportItem.ApproveTime = time.Now().Local()
  348. }
  349. if state == models.ReportStatePass {
  350. updateCols = append(updateCols, "PublishTime")
  351. reportItem.PublishTime = time.Now().Local()
  352. }
  353. if e = reportItem.UpdateReport(updateCols); e != nil {
  354. err = fmt.Errorf("更新英文研报审批状态失败, Err: %s", e.Error())
  355. return
  356. }
  357. }
  358. }
  359. if reportType == report_approve.FlowReportTypeSmart {
  360. reportOb := new(smart_report.SmartReport)
  361. reportItem, e := reportOb.GetItemById(reportId)
  362. if e != nil && e.Error() != utils.ErrNoRow() {
  363. err = fmt.Errorf("获取智能研报失败, Err: %s", e.Error())
  364. return
  365. }
  366. if reportItem != nil {
  367. reportItem.ApproveId = approveId
  368. reportItem.State = state
  369. reportItem.ModifyTime = time.Now().Local()
  370. if state == models.ReportStatePass || state == models.ReportStateRefused {
  371. updateCols = append(updateCols, "ApproveTime")
  372. reportItem.ApproveTime = time.Now().Local()
  373. }
  374. if state == models.ReportStatePass {
  375. updateCols = append(updateCols, "PublishTime")
  376. reportItem.PublishTime = time.Now().Local()
  377. }
  378. if e = reportItem.Update(updateCols); e != nil {
  379. err = fmt.Errorf("更新智能研报审批状态失败, Err: %s", e.Error())
  380. return
  381. }
  382. }
  383. }
  384. return
  385. }
  386. // PassReportApprove 通过审批
  387. func PassReportApprove(approveItem *report_approve.ReportApprove, recordItem *report_approve.ReportApproveRecord, sysAdminId int) (tips string, err error) {
  388. if approveItem == nil {
  389. err = fmt.Errorf("审批信息有误")
  390. return
  391. }
  392. if recordItem == nil {
  393. err = fmt.Errorf("审批记录有误")
  394. return
  395. }
  396. // 查询审批流和审批流节点
  397. flowOb := new(report_approve.ReportApproveFlow)
  398. flowItem, e := flowOb.GetItemById(approveItem.FlowId)
  399. if e != nil {
  400. err = fmt.Errorf("获取审批流失败, Err: %s", e.Error())
  401. return
  402. }
  403. nodeOb := new(report_approve.ReportApproveNode)
  404. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  405. nodePars := make([]interface{}, 0)
  406. nodePars = append(nodePars, flowItem.ReportApproveFlowId, flowItem.CurrVersion)
  407. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  408. if e != nil {
  409. err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error())
  410. return
  411. }
  412. if len(nodeItems) == 0 {
  413. err = fmt.Errorf("无审批节点")
  414. return
  415. }
  416. nodeMap := make(map[int]*report_approve.ReportApproveNode)
  417. for _, v := range nodeItems {
  418. nodeMap[v.ReportApproveNodeId] = v
  419. }
  420. // 取出审批记录的节点
  421. currNodeItem := nodeMap[recordItem.NodeId]
  422. if currNodeItem == nil {
  423. err = fmt.Errorf("当前节点信息有误")
  424. return
  425. }
  426. currNode, e := report_approve.FormatReportApproveNode2Item(currNodeItem)
  427. if e != nil {
  428. err = fmt.Errorf("当前节点信息有误, Err: %s", e.Error())
  429. return
  430. }
  431. now := time.Now().Local()
  432. recordItem.State = report_approve.ReportApproveStatePass
  433. recordItem.ApproveTime = now
  434. recordItem.ModifyTime = now
  435. recordCols := []string{"State", "ApproveTime", "ModifyTime"}
  436. lastApprove := false
  437. // 依次审批
  438. if currNode.ApproveType == report_approve.NodeApproveTypeRoll {
  439. if e = recordItem.Update(recordCols); e != nil {
  440. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  441. return
  442. }
  443. // 检查依次审批情况
  444. sort.Slice(currNode.Users, func(k, j int) bool {
  445. return currNode.Users[k].Sort < currNode.Users[j].Sort
  446. })
  447. userLen := len(currNode.Users)
  448. //lastRoll := false
  449. nextUser := new(report_approve.ReportApproveNodeUserReq) // 下一个审批人, 为nil则表示当前审批人即为最后
  450. for k, v := range currNode.Users {
  451. // 当前审批人
  452. if v.UserId == sysAdminId && recordItem.ApproveUserSort == v.Sort {
  453. //if k == (userLen - 1) {
  454. // lastRoll = true
  455. // break
  456. //}
  457. if (k + 1) < userLen {
  458. nextUser = currNode.Users[k+1]
  459. }
  460. }
  461. }
  462. //if lastRoll && currNode.NextNodeId == 0 {
  463. // lastApprove = true
  464. //}
  465. // 当前节点下一个审批人, 生成下一个审批记录且return
  466. if nextUser.UserId > 0 {
  467. newRecord := new(report_approve.ReportApproveRecord)
  468. newRecord.ReportApproveId = recordItem.ReportApproveId
  469. newRecord.State = report_approve.ReportApproveStateApproving
  470. newRecord.NodeId = currNode.ReportApproveNodeId
  471. newRecord.PrevNodeId = currNode.PrevNodeId
  472. newRecord.NextNodeId = currNode.NextNodeId
  473. newRecord.ApproveType = currNode.ApproveType
  474. newRecord.ApproveUserId = nextUser.UserId
  475. newRecord.ApproveUserName = nextUser.UserName
  476. newRecord.ApproveUserSort = nextUser.Sort
  477. newRecord.CreateTime = now
  478. newRecord.ModifyTime = now
  479. if e = newRecord.Create(); e != nil {
  480. err = fmt.Errorf("生成审批记录失败, Err: %s", e.Error())
  481. return
  482. }
  483. // 推送审批消息
  484. go func() {
  485. messageItem := new(report_approve.ReportApproveMessage)
  486. messageItem.SendUserId = approveItem.ApplyUserId
  487. messageItem.ReceiveUserId = nextUser.UserId
  488. messageItem.Content = "您有新的待办任务"
  489. messageItem.Remark = fmt.Sprintf("%s提交的【研报审批】需要您审批,请及时处理", approveItem.ApplyUserName)
  490. messageItem.ReportApproveId = approveItem.ReportApproveId
  491. messageItem.ApproveState = report_approve.ReportApproveStateApproving
  492. messageItem.CreateTime = now
  493. messageItem.ModifyTime = now
  494. if e = messageItem.Create(); e != nil {
  495. utils.FileLog.Info(fmt.Sprintf("PassReportApprove message err: %s", e.Error()))
  496. return
  497. }
  498. }()
  499. return
  500. }
  501. // 更新审批当前节点并进入下一个节点
  502. if currNode.NextNodeId > 0 {
  503. nextNode := nodeMap[currNode.NextNodeId]
  504. approveItem.CurrNodeId = currNode.NextNodeId
  505. approveItem.ModifyTime = now
  506. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  507. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  508. return
  509. }
  510. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.ReportApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName)
  511. return
  512. } else {
  513. // 最后一个节点
  514. lastApprove = true
  515. }
  516. }
  517. // 会签
  518. if currNode.ApproveType == report_approve.NodeApproveTypeAll {
  519. // 查询其他审批人是否已审批
  520. otherOb := new(report_approve.ReportApproveRecord)
  521. otherCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s <> ?`, report_approve.ReportApproveRecordCols.ReportApproveId, report_approve.ReportApproveRecordCols.NodeId, report_approve.ReportApproveRecordCols.ApproveUserId)
  522. otherPars := make([]interface{}, 0)
  523. otherPars = append(otherPars, approveItem.ReportApproveId, recordItem.NodeId, sysAdminId)
  524. otherRecords, e := otherOb.GetItemsByCondition(otherCond, otherPars, []string{}, "")
  525. if e != nil {
  526. err = fmt.Errorf("获取节点审批记录失败, Err: %s", e.Error())
  527. return
  528. }
  529. otherPass := true
  530. for _, v := range otherRecords {
  531. if v.State != report_approve.ReportApproveStatePass {
  532. otherPass = false
  533. }
  534. }
  535. // 其他人未审批, 仅更新当前审批记录
  536. if e = recordItem.Update(recordCols); e != nil {
  537. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  538. return
  539. }
  540. // 其他人已审批且为最后节点
  541. if otherPass && currNode.NextNodeId == 0 {
  542. lastApprove = true
  543. }
  544. // 其他人已审批且不为最后节点, 进入下一节点
  545. if otherPass && currNode.NextNodeId > 0 {
  546. nextNode := nodeMap[currNode.NextNodeId]
  547. approveItem.CurrNodeId = currNode.NextNodeId
  548. approveItem.ModifyTime = now
  549. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  550. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  551. return
  552. }
  553. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.ReportApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName)
  554. return
  555. }
  556. }
  557. // 或签
  558. if currNode.ApproveType == report_approve.NodeApproveTypeAny {
  559. // 需检查一下审批的当前节点和记录的节点是否匹配, 不匹配可能是因为另外的审批人已通过, 所以此处应给提示
  560. // 前端也有做相应的判断,但是两个人同时进入审批详情页时就可能出现这种情况
  561. if approveItem.CurrNodeId != recordItem.NodeId {
  562. tips = "该节点已完成审批, 请刷新页面"
  563. return
  564. }
  565. if e = recordItem.Update(recordCols); e != nil {
  566. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  567. return
  568. }
  569. if currNode.NextNodeId == 0 {
  570. lastApprove = true
  571. }
  572. if currNode.NextNodeId > 0 {
  573. nextNode := nodeMap[currNode.NextNodeId]
  574. approveItem.CurrNodeId = currNode.NextNodeId
  575. approveItem.ModifyTime = now
  576. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  577. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  578. return
  579. }
  580. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.ReportApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName)
  581. return
  582. }
  583. }
  584. // 最后一个审批, 更新审批记录、审批、报告状态、推送消息给申请人
  585. if lastApprove {
  586. if e = recordItem.Update(recordCols); e != nil {
  587. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  588. return
  589. }
  590. approveItem.State = report_approve.ReportApproveStatePass
  591. approveItem.ApproveTime = now
  592. approveItem.ModifyTime = now
  593. approveCols := []string{"State", "ApproveTime", "ModifyTime"}
  594. if e = approveItem.Update(approveCols); e != nil {
  595. err = fmt.Errorf("更新审批信息失败, Err: %s", e.Error())
  596. return
  597. }
  598. if e = updateReportApproveState(approveItem.ReportType, approveItem.ReportId, approveItem.ReportApproveId, models.ReportStatePass); e != nil {
  599. err = fmt.Errorf("更新报告审批状态失败, Err: %s", e.Error())
  600. return
  601. }
  602. go func() {
  603. messageItem := new(report_approve.ReportApproveMessage)
  604. messageItem.SendUserId = sysAdminId
  605. messageItem.ReceiveUserId = approveItem.ApplyUserId
  606. messageItem.Content = "您提交的审批已通过"
  607. messageItem.Remark = "您提交的【研报审批】已通过"
  608. messageItem.ReportApproveId = approveItem.ReportApproveId
  609. messageItem.ApproveState = report_approve.ReportApproveStatePass
  610. messageItem.CreateTime = now
  611. messageItem.ModifyTime = now
  612. if e = messageItem.Create(); e != nil {
  613. utils.FileLog.Info(fmt.Sprintf("PassReportApprove message err: %s", e.Error()))
  614. return
  615. }
  616. }()
  617. // 审批通过之后的处理
  618. go func() {
  619. if e = AfterReportApprovePass(approveItem.ReportType, approveItem.ReportId); e != nil {
  620. utils.FileLog.Info(fmt.Sprintf("AfterReportApprovePass err: %s, ReportType: %d, ReportId: %d", e.Error(), approveItem.ReportType, approveItem.ReportId))
  621. return
  622. }
  623. }()
  624. }
  625. return
  626. }
  627. // RefuseReportApprove 驳回审批
  628. func RefuseReportApprove(approveItem *report_approve.ReportApprove, recordItem *report_approve.ReportApproveRecord, approveRemark string, sysAdminId int) (err error) {
  629. if approveItem == nil {
  630. err = fmt.Errorf("审批信息有误")
  631. return
  632. }
  633. if recordItem == nil {
  634. err = fmt.Errorf("审批记录有误")
  635. return
  636. }
  637. // 更新审批记录
  638. now := time.Now().Local()
  639. recordItem.State = report_approve.ReportApproveStateRefuse
  640. recordItem.ApproveRemark = approveRemark
  641. recordItem.ApproveTime = now
  642. recordItem.ModifyTime = now
  643. recordCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime"}
  644. if e := recordItem.Update(recordCols); e != nil {
  645. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  646. return
  647. }
  648. // 驳回-更新审批, 报告状态, 推送消息
  649. approveItem.State = report_approve.ReportApproveStateRefuse
  650. approveItem.ApproveRemark = approveRemark
  651. approveItem.ApproveTime = now
  652. approveItem.ModifyTime = now
  653. approveCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime"}
  654. if e := approveItem.Update(approveCols); e != nil {
  655. err = fmt.Errorf("更新审批状态失败, Err: %s", e.Error())
  656. return
  657. }
  658. if e := updateReportApproveState(approveItem.ReportType, approveItem.ReportId, approveItem.ReportApproveId, models.ReportStateRefused); e != nil {
  659. err = fmt.Errorf("更新报告状态失败, Err: %s", e.Error())
  660. return
  661. }
  662. // 推送驳回消息给申请人
  663. go func() {
  664. messageItem := new(report_approve.ReportApproveMessage)
  665. messageItem.SendUserId = sysAdminId
  666. messageItem.ReceiveUserId = approveItem.ApplyUserId
  667. messageItem.Content = "您提交的审批被驳回"
  668. messageItem.Remark = "您提交的【研报审批】已被驳回"
  669. messageItem.ReportApproveId = approveItem.ReportApproveId
  670. messageItem.ApproveState = report_approve.ReportApproveStateRefuse
  671. messageItem.CreateTime = now
  672. messageItem.ModifyTime = now
  673. if e := messageItem.Create(); e != nil {
  674. utils.FileLog.Info(fmt.Sprintf("ApproveReport message err: %s", e.Error()))
  675. return
  676. }
  677. }()
  678. return
  679. }
  680. // BuildNextNodeRecordAndMsg 生成下一个节点的审批记录并推送消息
  681. func BuildNextNodeRecordAndMsg(approveNodeItem *report_approve.ReportApproveNode, approveId, sysAdminId int, sysAdminName string) (err error) {
  682. if approveNodeItem == nil {
  683. err = fmt.Errorf("approve node nil")
  684. return
  685. }
  686. // 根据节点审批方式生成审批记录
  687. now := time.Now().Local()
  688. approveNode, e := report_approve.FormatReportApproveNode2Item(approveNodeItem)
  689. if e != nil {
  690. err = fmt.Errorf("FormatReportApproveNode2Item err: %s", e.Error())
  691. return
  692. }
  693. if len(approveNode.Users) == 0 {
  694. err = fmt.Errorf("审批节点用户有误")
  695. return
  696. }
  697. newRecords := make([]*report_approve.ReportApproveRecord, 0)
  698. sort.Slice(approveNode.Users, func(k, j int) bool {
  699. return approveNode.Users[k].Sort < approveNode.Users[j].Sort
  700. })
  701. for _, u := range approveNode.Users {
  702. r := new(report_approve.ReportApproveRecord)
  703. r.ReportApproveId = approveId
  704. r.State = report_approve.ReportApproveStateApproving
  705. r.NodeId = approveNode.ReportApproveNodeId
  706. r.PrevNodeId = approveNode.PrevNodeId
  707. r.NextNodeId = approveNode.NextNodeId
  708. r.ApproveType = approveNode.ApproveType
  709. r.ApproveUserId = u.UserId
  710. r.ApproveUserName = u.UserName
  711. r.ApproveUserSort = u.Sort
  712. r.CreateTime = now
  713. r.ModifyTime = now
  714. newRecords = append(newRecords, r)
  715. // 依次审批仅生成一条记录
  716. if approveNode.ApproveType == report_approve.NodeApproveTypeRoll {
  717. break
  718. }
  719. }
  720. recordOb := new(report_approve.ReportApproveRecord)
  721. if e = recordOb.CreateMulti(newRecords); e != nil {
  722. err = fmt.Errorf("生成节点审批记录失败, Err: %s", e.Error())
  723. return
  724. }
  725. // 推送审批消息
  726. go func() {
  727. messageOb := new(report_approve.ReportApproveMessage)
  728. messages := make([]*report_approve.ReportApproveMessage, 0)
  729. for _, v := range newRecords {
  730. m := new(report_approve.ReportApproveMessage)
  731. m.SendUserId = sysAdminId
  732. m.ReceiveUserId = v.ApproveUserId
  733. m.Content = "您有新的待办任务"
  734. m.Remark = fmt.Sprintf("%s提交的【研报审批】需要您审批,请及时处理", sysAdminName)
  735. m.ReportApproveId = approveId
  736. m.ApproveState = report_approve.ReportApproveStateApproving
  737. m.CreateTime = now
  738. m.ModifyTime = now
  739. messages = append(messages, m)
  740. }
  741. e = messageOb.CreateMulti(messages)
  742. if e != nil {
  743. utils.FileLog.Info(fmt.Sprintf("BuildNextNodeRecordAndMsg messages err: %s", e.Error()))
  744. return
  745. }
  746. }()
  747. return
  748. }
  749. // AfterReportApprovePass 报告审批通过后的处理
  750. func AfterReportApprovePass(reportType, reportId int) (err error) {
  751. // 中文研报
  752. if reportType == report_approve.FlowReportTypeChinese {
  753. report, e := models.GetReportById(reportId)
  754. if e != nil {
  755. if e.Error() == utils.ErrNoRow() {
  756. return
  757. }
  758. err = fmt.Errorf("获取研报信息失败, Err: %s", e.Error())
  759. return
  760. }
  761. _ = CreateVideo(report)
  762. _ = UpdateReportEs(report.Id, models.ReportStatePublished)
  763. return
  764. }
  765. // 英文研报
  766. if reportType == report_approve.FlowReportTypeEnglish {
  767. _ = UpdateEnglishReportEs(reportId, models.ReportStatePublished)
  768. return
  769. }
  770. // 智能研报
  771. if reportType == report_approve.FlowReportTypeSmart {
  772. ob := new(smart_report.SmartReport)
  773. item, e := ob.GetItemById(reportId)
  774. if e != nil {
  775. if e.Error() == utils.ErrNoRow() {
  776. return
  777. }
  778. err = fmt.Errorf("获取智能研报信息失败, Err: %s", e.Error())
  779. return
  780. }
  781. // 写入队列
  782. var queue smart_report.Report2ImgQueueReq
  783. queue.ReportType = 2
  784. queue.ReportCode = item.ReportCode
  785. _ = utils.Rc.LPush(utils.CACHE_CREATE_REPORT_IMGPDF_QUEUE, queue)
  786. // 生成音频
  787. if item.VideoUrl == "" {
  788. SmartReportBuildVideoAndUpdate(item)
  789. }
  790. // ES更新报告
  791. _ = SmartReportElasticUpsert(item.SmartReportId, models.ReportStatePublished)
  792. }
  793. return
  794. }
  795. // CheckCloseReportApproveConf 校验是否可以关闭报告审批
  796. func CheckCloseReportApproveConf() (yes bool, err error) {
  797. // 查询待审批中的报告数量
  798. count, e := models.GetReportStateCount(models.ReportStateWaitApprove)
  799. if e != nil {
  800. err = fmt.Errorf("查询审批中的报告数量失败, Err: %s", e.Error())
  801. return
  802. }
  803. if count > 0 {
  804. return
  805. }
  806. count, e = models.GetEnglishReportStateCount(models.ReportStateWaitApprove)
  807. if e != nil {
  808. err = fmt.Errorf("查询审批中的英文报告数量失败, Err: %s", e.Error())
  809. return
  810. }
  811. if count > 0 {
  812. return
  813. }
  814. smartOb := new(smart_report.SmartReport)
  815. cond := ` AND state = ?`
  816. pars := make([]interface{}, 0)
  817. pars = append(pars, models.ReportStateWaitApprove)
  818. count, e = smartOb.GetCountByCondition(cond, pars)
  819. if e != nil {
  820. err = fmt.Errorf("查询审批中的智能报告数量失败, Err: %s", e.Error())
  821. return
  822. }
  823. if count > 0 {
  824. return
  825. }
  826. yes = true
  827. return
  828. }
  829. // FlowOperateResetReportState 审批流变化-重置报告的初始状态
  830. func FlowOperateResetReportState(reportType, classifyFirstId, classifySecondId, oldState, State int) (err error) {
  831. defer func() {
  832. if err != nil {
  833. tips := fmt.Sprintf("审批流变化-重置报告初始状态失败, ErrMsg: %s", err.Error())
  834. utils.FileLog.Info(tips)
  835. go alarm_msg.SendAlarmMsg(tips, 2)
  836. }
  837. }()
  838. // 中文研报
  839. if reportType == report_approve.FlowReportTypeChinese {
  840. e := models.UpdateReportsStateByCond(classifyFirstId, classifySecondId, oldState, State)
  841. if e != nil {
  842. err = fmt.Errorf("UpdateReportsStateByCond err: %s", e.Error())
  843. }
  844. return
  845. }
  846. // 英文研报
  847. if reportType == report_approve.FlowReportTypeEnglish {
  848. e := models.UpdateEnglishReportsStateByCond(classifyFirstId, classifySecondId, oldState, State)
  849. if e != nil {
  850. err = fmt.Errorf("UpdateEnglishReportsStateByCond err: %s", e.Error())
  851. }
  852. return
  853. }
  854. // 智能研报
  855. if reportType == report_approve.FlowReportTypeSmart {
  856. e := smart_report.UpdateSmartReportsStateByCond(classifyFirstId, classifySecondId, oldState, State)
  857. if e != nil {
  858. err = fmt.Errorf("UpdateSmartReportsStateByCond err: %s", e.Error())
  859. }
  860. return
  861. }
  862. return
  863. }
  864. // ConfigChangeResetReportState 审批配置变化-重置报告的初始状态
  865. func ConfigChangeResetReportState(changeType string) (err error) {
  866. defer func() {
  867. if err != nil {
  868. tips := fmt.Sprintf("审批配置变化-重置报告初始状态失败, ErrMsg: %s", err.Error())
  869. utils.FileLog.Info(tips)
  870. go alarm_msg.SendAlarmMsg(tips, 2)
  871. }
  872. }()
  873. // 关闭审批-待提交->未发布
  874. if changeType == "" {
  875. e := models.UpdateReportsStateByCond(0, 0, models.ReportStateWaitSubmit, models.ReportStateUnpublished)
  876. if e != nil {
  877. err = fmt.Errorf("UpdateReportsStateByCond err: %s", e.Error())
  878. }
  879. e = models.UpdateEnglishReportsStateByCond(0, 0, models.ReportStateWaitSubmit, models.ReportStateUnpublished)
  880. if e != nil {
  881. err = fmt.Errorf("UpdateEnglishReportsStateByCond err: %s", e.Error())
  882. }
  883. e = smart_report.UpdateSmartReportsStateByCond(0, 0, models.ReportStateWaitSubmit, models.ReportStateUnpublished)
  884. if e != nil {
  885. err = fmt.Errorf("UpdateSmartReportsStateByCond err: %s", e.Error())
  886. }
  887. return
  888. }
  889. // 开启内部审批-未发布->部分待提交
  890. if changeType == models.BusinessConfReportApproveTypeEta {
  891. flowOb := new(report_approve.ReportApproveFlow)
  892. flows, e := flowOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  893. if e != nil {
  894. err = fmt.Errorf("GetApproveFlows err: %s", e.Error())
  895. return
  896. }
  897. reportSecondIds, englishSecondIds, smartSecondIds := make([]int, 0), make([]int, 0), make([]int, 0)
  898. for _, v := range flows {
  899. if v.ReportType == report_approve.FlowReportTypeChinese {
  900. reportSecondIds = append(reportSecondIds, v.ClassifySecondId)
  901. continue
  902. }
  903. if v.ReportType == report_approve.FlowReportTypeEnglish {
  904. englishSecondIds = append(englishSecondIds, v.ClassifySecondId)
  905. continue
  906. }
  907. if v.ReportType == report_approve.FlowReportTypeSmart {
  908. smartSecondIds = append(smartSecondIds, v.ClassifySecondId)
  909. }
  910. }
  911. if len(reportSecondIds) > 0 {
  912. e = models.UpdateReportsStateBySecondIds(models.ReportStateUnpublished, models.ReportStateWaitSubmit, reportSecondIds)
  913. if e != nil {
  914. err = fmt.Errorf("UpdateReportsStateBySecondIds err: %s", e.Error())
  915. return
  916. }
  917. }
  918. if len(englishSecondIds) > 0 {
  919. e = models.UpdateEnglishReportsStateBySecondIds(models.ReportStateUnpublished, models.ReportStateWaitSubmit, englishSecondIds)
  920. if e != nil {
  921. err = fmt.Errorf("UpdateEnglishReportsStateBySecondIds err: %s", e.Error())
  922. return
  923. }
  924. }
  925. if len(smartSecondIds) > 0 {
  926. e = smart_report.UpdateSmartReportsStateBySecondIds(models.ReportStateUnpublished, models.ReportStateWaitSubmit, smartSecondIds)
  927. if e != nil {
  928. err = fmt.Errorf("UpdateSmartReportsStateBySecondIds err: %s", e.Error())
  929. return
  930. }
  931. }
  932. return
  933. }
  934. // 开启第三方审批->未发布->待提交
  935. if changeType == models.BusinessConfReportApproveTypeOther {
  936. e := models.UpdateReportsStateByCond(0, 0, models.ReportStateUnpublished, models.ReportStateWaitSubmit)
  937. if e != nil {
  938. err = fmt.Errorf("UpdateReportsStateByCond err: %s", e.Error())
  939. }
  940. e = models.UpdateEnglishReportsStateByCond(0, 0, models.ReportStateUnpublished, models.ReportStateWaitSubmit)
  941. if e != nil {
  942. err = fmt.Errorf("UpdateEnglishReportsStateByCond err: %s", e.Error())
  943. }
  944. e = smart_report.UpdateSmartReportsStateByCond(0, 0, models.ReportStateUnpublished, models.ReportStateWaitSubmit)
  945. if e != nil {
  946. err = fmt.Errorf("UpdateSmartReportsStateByCond err: %s", e.Error())
  947. }
  948. return
  949. }
  950. return
  951. }