report_approve.go 30 KB

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