report_approve.go 30 KB

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