report_approve.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. package report_approve
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/report_approve"
  7. "eta/eta_api/services"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // ReportApproveController 报告审批
  17. type ReportApproveController struct {
  18. controllers.BaseAuthController
  19. }
  20. // List
  21. // @Title 审批列表
  22. // @Description 审批列表
  23. // @Param PageSize query int true "每页数据条数"
  24. // @Param CurrentIndex query int true "当前页页码"
  25. // @Param ListType query int true "列表类型:1-待处理;2-已处理;3-我发起的"
  26. // @Param ReportType query int false "报告类型:1-中文研报;2-英文研报;3-智能研报"
  27. // @Param ClassifyIdFirst query int false "一级分类ID"
  28. // @Param ClassifyIdSecond query int false "二级分类ID"
  29. // @Param Keyword query string false "搜索关键词"
  30. // @Param ApproveState query int false "审批状态:1-待审批;2-已审批;3-已驳回;4-已撤回"
  31. // @Param TimeType query int false "时间类型:1-提交时间;2-处理时间;3-审批时间"
  32. // @Param StartTime query string false "开始时间"
  33. // @Param EndTime query string false "结束时间"
  34. // @Param SortField query int false "排序字段:1-提交时间;2-处理时间;3-审批时间"
  35. // @Param SortRule query int false "排序方式: 1-正序; 2-倒序(默认)"
  36. // @Success 200 {object} report_approve.ReportApproveListResp
  37. // @router /list [get]
  38. func (this *ReportApproveController) List() {
  39. br := new(models.BaseResponse).Init()
  40. defer func() {
  41. if br.ErrMsg == "" {
  42. br.IsSendEmail = false
  43. }
  44. this.Data["json"] = br
  45. this.ServeJSON()
  46. }()
  47. sysUser := this.SysUser
  48. if sysUser == nil {
  49. br.Msg = "请登录"
  50. br.ErrMsg = "请登录,SysUser Is Empty"
  51. br.Ret = 408
  52. return
  53. }
  54. params := new(report_approve.ReportApproveListReq)
  55. if e := this.ParseForm(params); e != nil {
  56. br.Msg = "获取失败"
  57. br.ErrMsg = "入参解析失败, Err: " + e.Error()
  58. return
  59. }
  60. if params.ListType != 1 && params.ListType != 2 && params.ListType != 3 {
  61. br.Msg = "列表类型有误"
  62. return
  63. }
  64. // 报告分类
  65. cnClassifyIdName, enClassifyIdName := make(map[int]string), make(map[int]string)
  66. cnClassify, e := models.GetAllClassify()
  67. if e != nil {
  68. br.Msg = "获取失败"
  69. br.ErrMsg = "获取中文分类失败, Err: " + e.Error()
  70. return
  71. }
  72. for _, v := range cnClassify {
  73. cnClassifyIdName[v.Id] = v.ClassifyName
  74. }
  75. enClassify, e := models.GetEnglishClassifies()
  76. if e != nil {
  77. br.Msg = "获取失败"
  78. br.ErrMsg = "获取英文分类失败, Err: " + e.Error()
  79. return
  80. }
  81. enRootIdMap := make(map[int]int) // 英文分类的一级分类ID
  82. for _, v := range enClassify {
  83. enClassifyIdName[v.Id] = v.ClassifyName
  84. enRootIdMap[v.Id] = v.RootId
  85. }
  86. // 分页
  87. var startSize int
  88. if params.PageSize <= 0 {
  89. params.PageSize = utils.PageSize20
  90. }
  91. if params.CurrentIndex <= 0 {
  92. params.CurrentIndex = 1
  93. }
  94. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  95. resp := new(report_approve.ReportApproveListResp)
  96. respTotal := 0
  97. respList := make([]*report_approve.ReportApproveItem, 0)
  98. timeField := map[int]string{1: fmt.Sprintf("a.%s", report_approve.ReportApproveCols.CreateTime), 2: fmt.Sprintf("a.%s", report_approve.ReportApproveRecordCols.ApproveTime), 3: fmt.Sprintf("b.%s", report_approve.ReportApproveCols.ApproveTime)}
  99. mineTimeField := map[int]string{1: fmt.Sprintf("%s", report_approve.ReportApproveCols.CreateTime), 3: fmt.Sprintf("%s", report_approve.ReportApproveCols.ApproveTime)}
  100. orderRules := map[int]string{1: "ASC", 2: "DESC"}
  101. ormList := make([]*report_approve.ReportApproveItemOrm, 0)
  102. // 待处理
  103. if params.ListType == 1 {
  104. cond := fmt.Sprintf(` AND a.%s = ? AND b.%s = ? AND a.%s = ?`, report_approve.ReportApproveRecordCols.State, report_approve.ReportApproveCols.State, report_approve.ReportApproveRecordCols.ApproveUserId)
  105. pars := make([]interface{}, 0)
  106. pars = append(pars, report_approve.ReportApproveStateApproving, report_approve.ReportApproveStateApproving, sysUser.AdminId)
  107. order := ""
  108. // 筛选条件
  109. if params.ReportType > 0 && params.ClassifySecondId > 0 {
  110. cond += fmt.Sprintf(` AND b.%s = ? AND b.%s = ?`, report_approve.ReportApproveCols.ReportType, report_approve.ReportApproveCols.ClassifySecondId)
  111. pars = append(pars, params.ReportType, params.ClassifySecondId)
  112. }
  113. if params.TimeType <= 0 {
  114. params.TimeType = 1
  115. }
  116. if params.TimeType == 1 && params.StartTime != "" && params.EndTime != "" {
  117. _, e := time.Parse(utils.FormatDate, params.StartTime)
  118. if e != nil {
  119. br.Msg = "开始时间格式有误"
  120. return
  121. }
  122. _, e = time.Parse(utils.FormatDate, params.EndTime)
  123. if e != nil {
  124. br.Msg = "结束时间格式有误"
  125. return
  126. }
  127. st := fmt.Sprintf("%s %s", params.StartTime, "00:00:00")
  128. ed := fmt.Sprintf("%s %s", params.EndTime, "23:59:59")
  129. cond += fmt.Sprintf(` AND (b.%s BETWEEN ? AND ?)`, report_approve.ReportApproveCols.CreateTime)
  130. pars = append(pars, st, ed)
  131. }
  132. params.Keyword = strings.TrimSpace(params.Keyword)
  133. if params.Keyword != "" {
  134. kw := fmt.Sprint("%", params.Keyword, "%")
  135. cond += fmt.Sprintf(` AND b.%s LIKE ?`, report_approve.ReportApproveCols.ReportTitle)
  136. pars = append(pars, kw)
  137. }
  138. if params.SortField > 0 && params.SortRule > 0 {
  139. orderField := timeField[params.SortField]
  140. if orderField == "" {
  141. br.Msg = "时间排序字段有误"
  142. return
  143. }
  144. orderRule := orderRules[params.SortRule]
  145. if orderRule == "" {
  146. br.Msg = "时间排序方式有误"
  147. return
  148. }
  149. order = fmt.Sprintf("%s %s", orderField, orderRule)
  150. }
  151. total, e := report_approve.GetApprovingReportApproveCount(cond, pars)
  152. if e != nil {
  153. br.Msg = "获取失败"
  154. br.ErrMsg = "GetApprovingReportApproveCount err: " + e.Error()
  155. return
  156. }
  157. respTotal = total
  158. list, e := report_approve.GetApprovingReportApprovePageList(cond, pars, order, startSize, params.PageSize)
  159. if e != nil {
  160. br.Msg = "获取失败"
  161. br.ErrMsg = "GetApprovingReportApprovePageList err: " + e.Error()
  162. return
  163. }
  164. ormList = list
  165. }
  166. // 已处理
  167. if params.ListType == 2 {
  168. cond := fmt.Sprintf(` AND a.%s = ? AND a.%s IN (%s)`, report_approve.ReportApproveRecordCols.ApproveUserId, report_approve.ReportApproveRecordCols.NodeState, utils.GetOrmInReplace(2))
  169. pars := make([]interface{}, 0)
  170. pars = append(pars, sysUser.AdminId, []int{report_approve.ReportApproveStatePass, report_approve.ReportApproveStateRefuse})
  171. order := ""
  172. // 筛选条件
  173. if params.ReportType > 0 && params.ClassifySecondId > 0 {
  174. cond += fmt.Sprintf(` AND b.%s = ? AND b.%s = ?`, report_approve.ReportApproveCols.ReportType, report_approve.ReportApproveCols.ClassifySecondId)
  175. pars = append(pars, params.ReportType, params.ClassifySecondId)
  176. }
  177. if params.TimeType > 0 && params.StartTime != "" && params.EndTime != "" {
  178. _, e := time.Parse(utils.FormatDate, params.StartTime)
  179. if e != nil {
  180. br.Msg = "开始时间格式有误"
  181. return
  182. }
  183. _, e = time.Parse(utils.FormatDate, params.EndTime)
  184. if e != nil {
  185. br.Msg = "结束时间格式有误"
  186. return
  187. }
  188. st := fmt.Sprintf("%s %s", params.StartTime, "00:00:00")
  189. ed := fmt.Sprintf("%s %s", params.EndTime, "23:59:59")
  190. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, timeField[params.TimeType])
  191. pars = append(pars, st, ed)
  192. }
  193. params.Keyword = strings.TrimSpace(params.Keyword)
  194. if params.Keyword != "" {
  195. kw := fmt.Sprint("%", params.Keyword, "%")
  196. cond += fmt.Sprintf(` AND b.%s LIKE ?`, report_approve.ReportApproveCols.ReportTitle)
  197. pars = append(pars, kw)
  198. }
  199. if params.SortField > 0 && params.SortRule > 0 {
  200. orderField := timeField[params.SortField]
  201. if orderField == "" {
  202. br.Msg = "时间排序字段有误"
  203. return
  204. }
  205. orderRule := orderRules[params.SortRule]
  206. if orderRule == "" {
  207. br.Msg = "时间排序方式有误"
  208. return
  209. }
  210. order = fmt.Sprintf("%s %s", orderField, orderRule)
  211. }
  212. if params.ApproveState > 0 {
  213. cond += fmt.Sprintf(` AND a.%s = ?`, report_approve.ReportApproveRecordCols.State)
  214. pars = append(pars, params.ApproveState)
  215. }
  216. total, e := report_approve.GetApprovedReportApproveCount(cond, pars)
  217. if e != nil {
  218. br.Msg = "获取失败"
  219. br.ErrMsg = "GetApprovedReportApproveCount err: " + e.Error()
  220. return
  221. }
  222. respTotal = total
  223. list, e := report_approve.GetApprovedReportApprovePageList(cond, pars, order, startSize, params.PageSize)
  224. if e != nil {
  225. br.Msg = "获取失败"
  226. br.ErrMsg = "GetApprovedReportApprovePageList err: " + e.Error()
  227. return
  228. }
  229. for _, v := range list {
  230. // 这个时候的状态,用审批状态
  231. v.RecordState = v.NodeState
  232. v.ApproveTime = v.NodeApproveTime
  233. }
  234. ormList = list
  235. }
  236. // 我发起的
  237. if params.ListType == 3 {
  238. cond := fmt.Sprintf(` AND a.%s = ?`, report_approve.ReportApproveCols.ApplyUserId)
  239. pars := make([]interface{}, 0)
  240. pars = append(pars, sysUser.AdminId)
  241. order := ""
  242. // 筛选条件
  243. if params.ReportType > 0 && params.ClassifySecondId > 0 {
  244. cond += fmt.Sprintf(` AND a.%s = ? AND a.%s = ?`, report_approve.ReportApproveCols.ReportType, report_approve.ReportApproveCols.ClassifySecondId)
  245. pars = append(pars, params.ReportType, params.ClassifySecondId)
  246. }
  247. if params.TimeType > 0 && params.StartTime != "" && params.EndTime != "" {
  248. _, e := time.Parse(utils.FormatDate, params.StartTime)
  249. if e != nil {
  250. br.Msg = "开始时间格式有误"
  251. return
  252. }
  253. _, e = time.Parse(utils.FormatDate, params.EndTime)
  254. if e != nil {
  255. br.Msg = "结束时间格式有误"
  256. return
  257. }
  258. st := fmt.Sprintf("%s %s", params.StartTime, "00:00:00")
  259. ed := fmt.Sprintf("%s %s", params.EndTime, "23:59:59")
  260. field := mineTimeField[params.TimeType]
  261. if field == "" {
  262. br.Msg = "筛选时间有误"
  263. return
  264. }
  265. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, field)
  266. pars = append(pars, st, ed)
  267. }
  268. params.Keyword = strings.TrimSpace(params.Keyword)
  269. if params.Keyword != "" {
  270. kw := fmt.Sprint("%", params.Keyword, "%")
  271. cond += fmt.Sprintf(` AND a.%s LIKE ?`, report_approve.ReportApproveCols.ReportTitle)
  272. pars = append(pars, kw)
  273. }
  274. if params.SortField > 0 && params.SortRule > 0 {
  275. orderField := mineTimeField[params.SortField]
  276. if orderField == "" {
  277. br.Msg = "时间排序字段有误"
  278. return
  279. }
  280. orderRule := orderRules[params.SortRule]
  281. if orderRule == "" {
  282. br.Msg = "时间排序方式有误"
  283. return
  284. }
  285. order = fmt.Sprintf("%s %s", orderField, orderRule)
  286. }
  287. if params.ApproveState > 0 {
  288. cond += fmt.Sprintf(` AND a.%s = ?`, report_approve.ReportApproveRecordCols.State)
  289. pars = append(pars, params.ApproveState)
  290. }
  291. total, e := report_approve.GetApplyReportApproveCount(cond, pars)
  292. if e != nil {
  293. br.Msg = "获取失败"
  294. br.ErrMsg = "GetApplyReportApproveCount err: " + e.Error()
  295. return
  296. }
  297. respTotal = total
  298. list, e := report_approve.GetApplyReportApprovePageList(cond, pars, order, startSize, params.PageSize)
  299. if e != nil {
  300. br.Msg = "获取失败"
  301. br.ErrMsg = "GetApplyReportApprovePageList err: " + e.Error()
  302. return
  303. }
  304. ormList = list
  305. }
  306. // 审批通过的报告显示下载按钮
  307. reportImg := make(map[string]string)
  308. reportPdf := make(map[string]string)
  309. {
  310. var reportIds, enReportIds []int
  311. for _, v := range ormList {
  312. if v.State != report_approve.ReportApproveStatePass {
  313. continue
  314. }
  315. if v.ReportType == report_approve.FlowReportTypeChinese {
  316. reportIds = append(reportIds, v.ReportId)
  317. }
  318. if v.ReportType == report_approve.FlowReportTypeEnglish {
  319. enReportIds = append(enReportIds, v.ReportId)
  320. }
  321. }
  322. if len(reportIds) > 0 {
  323. reports, e := models.GetReportFieldsByIds(reportIds, []string{"id", "detail_img_url", "detail_pdf_url"})
  324. if e != nil {
  325. br.Msg = "获取失败"
  326. br.ErrMsg = fmt.Sprintf("GetReportFieldsByIds, %v", e)
  327. return
  328. }
  329. for _, r := range reports {
  330. k := fmt.Sprintf("%d-%d", report_approve.FlowReportTypeChinese, r.Id)
  331. reportImg[k] = r.DetailImgUrl
  332. reportPdf[k] = r.DetailPdfUrl
  333. }
  334. }
  335. if len(enReportIds) > 0 {
  336. enReports, e := models.GetEnglishReportFieldsByIds(enReportIds, []string{"id", "detail_img_url", "detail_pdf_url"})
  337. if e != nil {
  338. br.Msg = "获取失败"
  339. br.ErrMsg = fmt.Sprintf("GetEnglishReportFieldsByIds, %v", e)
  340. return
  341. }
  342. for _, r := range enReports {
  343. k := fmt.Sprintf("%d-%d", report_approve.FlowReportTypeEnglish, r.Id)
  344. reportImg[k] = r.DetailImgUrl
  345. reportPdf[k] = r.DetailPdfUrl
  346. }
  347. }
  348. }
  349. // 格式化列表
  350. for _, v := range ormList {
  351. t := report_approve.FormatReportApproveOrm2Item(v)
  352. if v.ReportType == report_approve.FlowReportTypeEnglish {
  353. t.ReportClassify = fmt.Sprintf("%s/%s/%s/%s", report_approve.FlowReportTypeMap[v.ReportType], enClassifyIdName[enRootIdMap[v.ClassifySecondId]], enClassifyIdName[v.ClassifyFirstId], enClassifyIdName[v.ClassifySecondId])
  354. } else {
  355. //t.ReportClassify = fmt.Sprintf("%s/%s/%s", report_approve.FlowReportTypeMap[v.ReportType], cnClassifyIdName[v.ClassifyFirstId], cnClassifyIdName[v.ClassifySecondId])
  356. reportClassify := fmt.Sprintf("%s/%s", report_approve.FlowReportTypeMap[v.ReportType], cnClassifyIdName[v.ClassifyFirstId])
  357. if v.ClassifySecondId > 0 {
  358. reportClassify = fmt.Sprintf("%s/%s", reportClassify, cnClassifyIdName[v.ClassifySecondId])
  359. }
  360. if v.ClassifyThirdId > 0 {
  361. reportClassify = fmt.Sprintf("%s/%s", reportClassify, cnClassifyIdName[v.ClassifyThirdId])
  362. }
  363. t.ReportClassify = reportClassify
  364. }
  365. k := fmt.Sprintf("%d-%d", t.ReportType, t.ReportId)
  366. t.DetailImgUrl = reportImg[k]
  367. t.DetailPdfUrl = reportPdf[k]
  368. respList = append(respList, t)
  369. }
  370. page := paging.GetPaging(params.CurrentIndex, params.PageSize, respTotal)
  371. resp.Paging = page
  372. resp.List = respList
  373. br.Data = resp
  374. br.Ret = 200
  375. br.Success = true
  376. br.Msg = "获取成功"
  377. }
  378. // Detail
  379. // @Title 审批详情
  380. // @Description 审批详情
  381. // @Param ReportApproveId query int true "审批ID"
  382. // @Success 200 {object} report_approve.ReportApproveDetail
  383. // @router /detail [get]
  384. func (this *ReportApproveController) Detail() {
  385. br := new(models.BaseResponse).Init()
  386. defer func() {
  387. if br.ErrMsg == "" {
  388. br.IsSendEmail = false
  389. }
  390. this.Data["json"] = br
  391. this.ServeJSON()
  392. }()
  393. sysUser := this.SysUser
  394. if sysUser == nil {
  395. br.Msg = "请登录"
  396. br.ErrMsg = "请登录,SysUser Is Empty"
  397. br.Ret = 408
  398. return
  399. }
  400. approveId, _ := this.GetInt("ReportApproveId")
  401. if approveId <= 0 {
  402. br.Msg = "参数有误"
  403. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveId: %d", approveId)
  404. return
  405. }
  406. approveOb := new(report_approve.ReportApprove)
  407. approveItem, e := approveOb.GetItemById(approveId)
  408. if e != nil {
  409. if e.Error() == utils.ErrNoRow() {
  410. br.Msg = "审批已被删除, 请刷新页面"
  411. return
  412. }
  413. br.Msg = "获取失败"
  414. br.ErrMsg = "获取审批失败, Err: " + e.Error()
  415. return
  416. }
  417. // 审批信息
  418. detail := new(report_approve.ReportApproveDetail)
  419. detail.Approve = new(report_approve.ReportApproveDetailItem)
  420. detail.Approve.ReportApproveId = approveItem.ReportApproveId
  421. detail.Approve.State = approveItem.State
  422. detail.Approve.FlowId = approveItem.FlowId
  423. detail.Approve.FlowVersion = approveItem.FlowVersion
  424. detail.Approve.StartNodeId = approveItem.StartNodeId
  425. detail.Approve.CurrNodeId = approveItem.CurrNodeId
  426. detail.Approve.ApplyUserId = approveItem.ApplyUserId
  427. detail.Approve.ApplyUserName = approveItem.ApplyUserName
  428. detail.Approve.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ApproveTime)
  429. detail.Approve.CreateTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.CreateTime)
  430. detail.Approve.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ModifyTime)
  431. // 审批流
  432. //flowOb := new(report_approve.ReportApproveFlow)
  433. //flowItem, e := flowOb.GetItemById(approveItem.FlowId)
  434. //if e != nil {
  435. // if e.Error() == utils.ErrNoRow() {
  436. // br.Msg = "审批流已被删除, 请刷新页面"
  437. // return
  438. // }
  439. // br.Msg = "获取失败"
  440. // br.ErrMsg = "获取审批流失败, Err: " + e.Error()
  441. // return
  442. //}
  443. //detail.ApproveFlow = report_approve.FormatReportApproveFlow2Item(flowItem)
  444. // 审批节点
  445. nodeOb := new(report_approve.ReportApproveNode)
  446. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  447. nodePars := make([]interface{}, 0)
  448. nodePars = append(nodePars, approveItem.FlowId, approveItem.FlowVersion)
  449. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  450. if e != nil {
  451. br.Msg = "获取失败"
  452. br.ErrMsg = "获取审批节点失败, Err: " + e.Error()
  453. return
  454. }
  455. // 审批记录
  456. recordOb := new(report_approve.ReportApproveRecord)
  457. recordCond := fmt.Sprintf(` AND %s = ?`, report_approve.ReportApproveRecordCols.ReportApproveId)
  458. recordPars := make([]interface{}, 0)
  459. recordPars = append(recordPars, approveItem.ReportApproveId)
  460. recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, fmt.Sprintf("%s DESC", report_approve.ReportApproveRecordCols.ApproveTime))
  461. if e != nil {
  462. br.Msg = "获取失败"
  463. br.ErrMsg = "获取审批记录失败, Err: " + e.Error()
  464. return
  465. }
  466. recordMap := make(map[string]*report_approve.ReportApproveRecord)
  467. for _, v := range recordItems {
  468. k := fmt.Sprintf("%d-%d", v.NodeId, v.ApproveUserId)
  469. recordMap[k] = v
  470. }
  471. // 审批流节点详情
  472. detail.ApproveFlowNodes = make([]*report_approve.ReportApproveDetailNodes, 0)
  473. for _, v := range nodeItems {
  474. t := new(report_approve.ReportApproveDetailNodes)
  475. t.ReportApproveNodeId = v.ReportApproveNodeId
  476. t.ReportApproveFlowId = v.ReportApproveFlowId
  477. t.PrevNodeId = v.PrevNodeId
  478. t.NextNodeId = v.NextNodeId
  479. t.NodeType = v.NodeType
  480. t.ApproveType = v.ApproveType
  481. t.Users = make([]*report_approve.ReportApproveDetailNodeUser, 0)
  482. us := make([]*report_approve.ReportApproveNodeUserReq, 0)
  483. if v.Users != "" {
  484. e = json.Unmarshal([]byte(v.Users), &us)
  485. if e != nil {
  486. br.Msg = "获取失败"
  487. br.ErrMsg = "节点用户解析失败, Err: " + e.Error()
  488. return
  489. }
  490. }
  491. for _, vu := range us {
  492. u := new(report_approve.ReportApproveDetailNodeUser)
  493. u.UserType = vu.UserType
  494. u.UserId = vu.UserId
  495. u.UserName = vu.UserName
  496. u.Sort = vu.Sort
  497. // 审批记录
  498. k := fmt.Sprintf("%d-%d", v.ReportApproveNodeId, vu.UserId)
  499. r := recordMap[k]
  500. if r != nil {
  501. u.ApproveRecord = new(report_approve.ReportApproveDetailNodeUserRecord)
  502. u.ApproveRecord.ReportApproveRecordId = r.ReportApproveRecordId
  503. u.ApproveRecord.State = r.State
  504. u.ApproveRecord.ApproveUserId = r.ApproveUserId
  505. u.ApproveRecord.ApproveUserName = r.ApproveUserName
  506. u.ApproveRecord.ApproveRemark = r.ApproveRemark
  507. u.ApproveRecord.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, r.ApproveTime)
  508. }
  509. t.Users = append(t.Users, u)
  510. }
  511. sort.Slice(t.Users, func(k, j int) bool {
  512. return t.Users[k].Sort < t.Users[j].Sort
  513. })
  514. detail.ApproveFlowNodes = append(detail.ApproveFlowNodes, t)
  515. }
  516. // 报告信息
  517. cnClassifyIdName, enClassifyIdName := make(map[int]string), make(map[int]string)
  518. cnClassify, e := models.GetAllClassify()
  519. if e != nil {
  520. br.Msg = "获取失败"
  521. br.ErrMsg = "获取中文分类失败, Err: " + e.Error()
  522. return
  523. }
  524. for _, v := range cnClassify {
  525. cnClassifyIdName[v.Id] = v.ClassifyName
  526. }
  527. enClassify, e := models.GetEnglishClassifies()
  528. if e != nil {
  529. br.Msg = "获取失败"
  530. br.ErrMsg = "获取英文分类失败, Err: " + e.Error()
  531. return
  532. }
  533. enRootIdMap := make(map[int]int) // 英文分类的一级分类ID
  534. for _, v := range enClassify {
  535. enClassifyIdName[v.Id] = v.ClassifyName
  536. enRootIdMap[v.Id] = v.RootId
  537. }
  538. detail.Report = new(report_approve.ReportApproveDetailReport)
  539. detail.Report.ReportType = approveItem.ReportType
  540. detail.Report.ReportId = approveItem.ReportId
  541. detail.Report.ReportTitle = approveItem.ReportTitle
  542. detail.Report.ReportLayout = 1
  543. // 报告分类路由
  544. if approveItem.ReportType == report_approve.FlowReportTypeChinese {
  545. reportInfo, e := models.GetReportLayoutByReportId(approveItem.ReportId)
  546. if e != nil {
  547. br.Msg = "获取失败"
  548. br.ErrMsg = "获取报告失败, Err: " + e.Error()
  549. return
  550. }
  551. detail.Report.ReportLayout = reportInfo.ReportLayout
  552. detail.Report.ReportCode = utils.MD5(strconv.Itoa(approveItem.ReportId))
  553. detail.Report.ReportClassify = fmt.Sprintf("%s/%s/%s", report_approve.FlowReportTypeMap[approveItem.ReportType], cnClassifyIdName[approveItem.ClassifyFirstId], cnClassifyIdName[approveItem.ClassifySecondId])
  554. }
  555. if approveItem.ReportType == report_approve.FlowReportTypeEnglish {
  556. detail.Report.ReportCode = utils.MD5(strconv.Itoa(approveItem.ReportId))
  557. detail.Report.ReportClassify = fmt.Sprintf("%s/%s/%s/%s", report_approve.FlowReportTypeMap[approveItem.ReportType], enClassifyIdName[enRootIdMap[approveItem.ClassifySecondId]], enClassifyIdName[approveItem.ClassifyFirstId], enClassifyIdName[approveItem.ClassifySecondId])
  558. }
  559. if approveItem.ReportType == report_approve.FlowReportTypeSmart {
  560. detail.Report.ReportLayout = 2
  561. detail.Report.ReportCode = utils.MD5(fmt.Sprint("smart_", approveItem.ReportId))
  562. detail.Report.ReportClassify = fmt.Sprintf("%s/%s/%s", report_approve.FlowReportTypeMap[approveItem.ReportType], cnClassifyIdName[approveItem.ClassifyFirstId], cnClassifyIdName[approveItem.ClassifySecondId])
  563. }
  564. br.Data = detail
  565. br.Ret = 200
  566. br.Success = true
  567. br.Msg = "获取成功"
  568. }
  569. // Approve
  570. // @Title 通过审批
  571. // @Description 通过审批
  572. // @Param request body report_approve.ReportApprovePassReq true "type json string"
  573. // @Success 200 string "操作成功"
  574. // @router /approve [post]
  575. func (this *ReportApproveController) Approve() {
  576. br := new(models.BaseResponse).Init()
  577. defer func() {
  578. if br.ErrMsg == "" {
  579. br.IsSendEmail = false
  580. }
  581. this.Data["json"] = br
  582. this.ServeJSON()
  583. }()
  584. sysUser := this.SysUser
  585. if sysUser == nil {
  586. br.Msg = "请登录"
  587. br.ErrMsg = "请登录,SysUser Is Empty"
  588. br.Ret = 408
  589. return
  590. }
  591. var req report_approve.ReportApprovePassReq
  592. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  593. br.Msg = "参数有误"
  594. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  595. return
  596. }
  597. if req.ReportApproveId <= 0 {
  598. br.Msg = "参数有误"
  599. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveId: %d", req.ReportApproveId)
  600. return
  601. }
  602. approveOb := new(report_approve.ReportApprove)
  603. approveItem, e := approveOb.GetItemById(req.ReportApproveId)
  604. if e != nil {
  605. if e.Error() == utils.ErrNoRow() {
  606. br.Msg = "审批不存在, 请刷新页面"
  607. return
  608. }
  609. br.Msg = "操作失败"
  610. br.ErrMsg = "获取审批信息失败, Err: " + e.Error()
  611. return
  612. }
  613. if approveItem.State != report_approve.ReportApproveStateApproving {
  614. br.Msg = "审批状态有误, 请刷新页面"
  615. br.ErrMsg = fmt.Sprintf("审批状态有误, State: %d", approveItem.State)
  616. return
  617. }
  618. // 校验审批记录和审批
  619. recordOb := new(report_approve.ReportApproveRecord)
  620. recordCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveRecordCols.ReportApproveId, report_approve.ReportApproveRecordCols.ApproveUserId, report_approve.ReportApproveRecordCols.State)
  621. recordPars := make([]interface{}, 0)
  622. recordPars = append(recordPars, approveItem.ReportApproveId, sysUser.AdminId, report_approve.ReportApproveStateApproving)
  623. recordItem, e := recordOb.GetItemByCondition(recordCond, recordPars, "")
  624. if e != nil {
  625. if e.Error() == utils.ErrNoRow() {
  626. br.Msg = "无权审批"
  627. return
  628. }
  629. br.Msg = "操作失败"
  630. br.ErrMsg = "获取审批记录失败, Err: " + e.Error()
  631. return
  632. }
  633. // 通过审批
  634. tips, e := services.PassReportApprove(approveItem, recordItem, sysUser.AdminId, req.ReportUrl)
  635. if e != nil {
  636. br.Msg = "操作失败"
  637. br.ErrMsg = "通过审批失败, Err: " + e.Error()
  638. return
  639. }
  640. if tips != "" {
  641. br.Msg = tips
  642. return
  643. }
  644. br.Ret = 200
  645. br.Success = true
  646. br.Msg = "操作成功"
  647. }
  648. // Refuse
  649. // @Title 驳回审批
  650. // @Description 驳回审批
  651. // @Param request body report_approve.ReportApproveRefuseReq true "type json string"
  652. // @Success 200 string "操作成功"
  653. // @router /refuse [post]
  654. func (this *ReportApproveController) Refuse() {
  655. br := new(models.BaseResponse).Init()
  656. defer func() {
  657. if br.ErrMsg == "" {
  658. br.IsSendEmail = false
  659. }
  660. this.Data["json"] = br
  661. this.ServeJSON()
  662. }()
  663. sysUser := this.SysUser
  664. if sysUser == nil {
  665. br.Msg = "请登录"
  666. br.ErrMsg = "请登录,SysUser Is Empty"
  667. br.Ret = 408
  668. return
  669. }
  670. var req report_approve.ReportApproveRefuseReq
  671. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  672. br.Msg = "参数有误"
  673. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  674. return
  675. }
  676. if req.ReportApproveId <= 0 {
  677. br.Msg = "参数有误"
  678. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveId: %d", req.ReportApproveId)
  679. return
  680. }
  681. approveOb := new(report_approve.ReportApprove)
  682. approveItem, e := approveOb.GetItemById(req.ReportApproveId)
  683. if e != nil {
  684. if e.Error() == utils.ErrNoRow() {
  685. br.Msg = "审批不存在, 请刷新页面"
  686. return
  687. }
  688. br.Msg = "操作失败"
  689. br.ErrMsg = "获取审批信息失败, Err: " + e.Error()
  690. return
  691. }
  692. if approveItem.State != report_approve.ReportApproveStateApproving {
  693. br.Msg = "审批状态有误, 请刷新页面"
  694. br.ErrMsg = fmt.Sprintf("审批状态有误, State: %d", approveItem.State)
  695. return
  696. }
  697. // 校验审批记录和审批
  698. recordOb := new(report_approve.ReportApproveRecord)
  699. recordCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveRecordCols.ReportApproveId, report_approve.ReportApproveRecordCols.ApproveUserId, report_approve.ReportApproveRecordCols.State)
  700. recordPars := make([]interface{}, 0)
  701. recordPars = append(recordPars, approveItem.ReportApproveId, sysUser.AdminId, report_approve.ReportApproveStateApproving)
  702. recordItem, e := recordOb.GetItemByCondition(recordCond, recordPars, "")
  703. if e != nil {
  704. if e.Error() == utils.ErrNoRow() {
  705. br.Msg = "无权审批"
  706. return
  707. }
  708. br.Msg = "操作失败"
  709. br.ErrMsg = "获取审批记录失败, Err: " + e.Error()
  710. return
  711. }
  712. // 驳回审批
  713. if e = services.RefuseReportApprove(approveItem, recordItem, req.ApproveRemark, sysUser.AdminId); e != nil {
  714. br.Msg = "操作失败"
  715. br.ErrMsg = "驳回审批失败, Err: " + e.Error()
  716. return
  717. }
  718. br.Ret = 200
  719. br.Success = true
  720. br.Msg = "操作成功"
  721. }
  722. // Cancel
  723. // @Title 撤销审批
  724. // @Description 撤销审批
  725. // @Param request body report_approve.ReportApproveCancelReq true "type json string"
  726. // @Success 200 string "操作成功"
  727. // @router /cancel [post]
  728. func (this *ReportApproveController) Cancel() {
  729. br := new(models.BaseResponse).Init()
  730. defer func() {
  731. if br.ErrMsg == "" {
  732. br.IsSendEmail = false
  733. }
  734. this.Data["json"] = br
  735. this.ServeJSON()
  736. }()
  737. sysUser := this.SysUser
  738. if sysUser == nil {
  739. br.Msg = "请登录"
  740. br.ErrMsg = "请登录,SysUser Is Empty"
  741. br.Ret = 408
  742. return
  743. }
  744. var req report_approve.ReportApproveCancelReq
  745. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  746. br.Msg = "参数有误"
  747. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  748. return
  749. }
  750. if req.ReportApproveId <= 0 {
  751. br.Msg = "参数有误"
  752. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveId: %d", req.ReportApproveId)
  753. return
  754. }
  755. approveOb := new(report_approve.ReportApprove)
  756. approveItem, e := approveOb.GetItemById(req.ReportApproveId)
  757. if e != nil {
  758. if e.Error() == utils.ErrNoRow() {
  759. br.Msg = "审批已被删除, 请刷新页面"
  760. return
  761. }
  762. br.Msg = "操作失败"
  763. br.ErrMsg = "获取审批失败, Err: " + e.Error()
  764. return
  765. }
  766. if approveItem.ApplyUserId != sysUser.AdminId {
  767. br.Msg = "非申请人不可撤销"
  768. return
  769. }
  770. // 撤销审批
  771. e = services.CancelReportApprove(approveItem.ReportType, approveItem.ReportId, approveItem.ReportApproveId, sysUser.AdminId, sysUser.RealName)
  772. if e != nil {
  773. br.Msg = "操作失败"
  774. br.ErrMsg = "撤销审批失败, Err: " + e.Error()
  775. return
  776. }
  777. br.Ret = 200
  778. br.Success = true
  779. br.Msg = "操作成功"
  780. }
  781. // MessageList
  782. // @Title 审批消息列表
  783. // @Description 审批消息列表
  784. // @Param PageSize query int true "每页数据条数"
  785. // @Param CurrentIndex query int true "当前页页码"
  786. // @Success 200 {object} report_approve.ReportApproveMessageListResp
  787. // @router /message/list [get]
  788. func (this *ReportApproveController) MessageList() {
  789. br := new(models.BaseResponse).Init()
  790. defer func() {
  791. if br.ErrMsg == "" {
  792. br.IsSendEmail = false
  793. }
  794. this.Data["json"] = br
  795. this.ServeJSON()
  796. }()
  797. sysUser := this.SysUser
  798. if sysUser == nil {
  799. br.Msg = "请登录"
  800. br.ErrMsg = "请登录,SysUser Is Empty"
  801. br.Ret = 408
  802. return
  803. }
  804. params := new(report_approve.ReportApproveListReq)
  805. if e := this.ParseForm(params); e != nil {
  806. br.Msg = "获取失败"
  807. br.ErrMsg = "入参解析失败, Err: " + e.Error()
  808. return
  809. }
  810. // 分页
  811. var startSize int
  812. if params.PageSize <= 0 {
  813. params.PageSize = utils.PageSize20
  814. }
  815. if params.CurrentIndex <= 0 {
  816. params.CurrentIndex = 1
  817. }
  818. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  819. resp := new(report_approve.ReportApproveMessageListResp)
  820. resp.List = make([]*report_approve.ReportApproveMessageItem, 0)
  821. cond := fmt.Sprintf(` AND %s = ?`, report_approve.ReportApproveMessageCols.ReceiveUserId)
  822. pars := make([]interface{}, 0)
  823. pars = append(pars, sysUser.AdminId)
  824. order := fmt.Sprintf(`%s ASC, %s DESC`, report_approve.ReportApproveMessageCols.IsRead, report_approve.ReportApproveMessageCols.CreateTime)
  825. messageOb := new(report_approve.ReportApproveMessage)
  826. total, e := messageOb.GetCountByCondition(cond, pars)
  827. if e != nil {
  828. br.Msg = "获取失败"
  829. br.ErrMsg = "获取审批消息列表总数失败, Err: " + e.Error()
  830. return
  831. }
  832. list, e := messageOb.GetPageItemsByCondition(cond, pars, []string{}, order, startSize, params.PageSize)
  833. if e != nil {
  834. br.Msg = "获取失败"
  835. br.ErrMsg = "获取审批消息列表失败, Err: " + e.Error()
  836. return
  837. }
  838. for _, v := range list {
  839. t := report_approve.FormatReportApproveMessage2Item(v)
  840. resp.List = append(resp.List, t)
  841. }
  842. // 未读消息数
  843. cond += fmt.Sprintf(` AND %s = ?`, report_approve.ReportApproveMessageCols.IsRead)
  844. pars = append(pars, 0)
  845. unreadTotal, e := messageOb.GetCountByCondition(cond, pars)
  846. if e != nil {
  847. br.Msg = "获取失败"
  848. br.ErrMsg = "获取审批消息列表总数失败, Err: " + e.Error()
  849. return
  850. }
  851. resp.UnreadTotal = unreadTotal
  852. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  853. resp.Paging = page
  854. br.Data = resp
  855. br.Ret = 200
  856. br.Success = true
  857. br.Msg = "获取成功"
  858. }
  859. // MessageRead
  860. // @Title 消息已读
  861. // @Description 消息已读
  862. // @Param request body report_approve.ReportApproveMessageReadReq true "type json string"
  863. // @Success 200 string "操作成功"
  864. // @router /message/read [post]
  865. func (this *ReportApproveController) MessageRead() {
  866. br := new(models.BaseResponse).Init()
  867. defer func() {
  868. if br.ErrMsg == "" {
  869. br.IsSendEmail = false
  870. }
  871. this.Data["json"] = br
  872. this.ServeJSON()
  873. }()
  874. sysUser := this.SysUser
  875. if sysUser == nil {
  876. br.Msg = "请登录"
  877. br.ErrMsg = "请登录,SysUser Is Empty"
  878. br.Ret = 408
  879. return
  880. }
  881. var req report_approve.ReportApproveMessageReadReq
  882. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  883. br.Msg = "参数有误"
  884. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  885. return
  886. }
  887. if req.MessageId <= 0 {
  888. br.Msg = "参数有误"
  889. br.ErrMsg = fmt.Sprintf("参数有误, MessageId: %d", req.MessageId)
  890. return
  891. }
  892. messageOb := new(report_approve.ReportApproveMessage)
  893. messageItem, e := messageOb.GetItemById(req.MessageId)
  894. if e != nil {
  895. if e.Error() == utils.ErrNoRow() {
  896. br.Msg = "消息不存在, 请刷新页面"
  897. return
  898. }
  899. br.Msg = "获取失败"
  900. br.ErrMsg = "获取审批消息失败, Err: " + e.Error()
  901. return
  902. }
  903. messageItem.IsRead = 1
  904. messageItem.ModifyTime = time.Now().Local()
  905. cols := []string{"IsRead", "ModifyTime"}
  906. if e = messageItem.Update(cols); e != nil {
  907. br.Msg = "操作失败"
  908. br.ErrMsg = "更新审批消息已读失败, Err: " + e.Error()
  909. return
  910. }
  911. br.Ret = 200
  912. br.Success = true
  913. br.Msg = "操作成功"
  914. }
  915. // CheckApproveOpen
  916. // @Title 校验分类是否开启审批
  917. // @Description 校验分类是否开启审批
  918. // @Param request body report_approve.ReportApproveCheckApproveOpenReq true "type json string"
  919. // @Success 200 string "操作成功"
  920. // @router /classify/check_open [post]
  921. func (this *ReportApproveController) CheckApproveOpen() {
  922. br := new(models.BaseResponse).Init()
  923. defer func() {
  924. if br.ErrMsg == "" {
  925. br.IsSendEmail = false
  926. }
  927. this.Data["json"] = br
  928. this.ServeJSON()
  929. }()
  930. sysUser := this.SysUser
  931. if sysUser == nil {
  932. br.Msg = "请登录"
  933. br.ErrMsg = "请登录,SysUser Is Empty"
  934. br.Ret = 408
  935. return
  936. }
  937. var req report_approve.ReportApproveCheckApproveOpenReq
  938. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  939. br.Msg = "参数有误"
  940. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  941. return
  942. }
  943. // 校验是否开启了审批流
  944. opening, e := services.CheckReportOpenApprove(req.ReportType, req.ClassifyFirstId, req.ClassifySecondId, req.ClassifyThirdId)
  945. if e != nil {
  946. br.Msg = "操作失败"
  947. br.ErrMsg = "校验报告是否开启审批流失败, Err: " + e.Error()
  948. return
  949. }
  950. br.Data = opening
  951. br.Ret = 200
  952. br.Success = true
  953. br.Msg = "操作成功"
  954. }