report_approve.go 32 KB

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