report_approve.go 31 KB

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