report_approve.go 28 KB

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