report_approve_flow.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. "strings"
  12. "sync"
  13. "time"
  14. )
  15. // ReportApproveFlowController 报告审批流
  16. type ReportApproveFlowController 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 ReportType query int false "报告类型:1-中文研报;2-英文研报;3-智能研报"
  25. // @Param ClassifyIdFirst query int false "一级分类ID"
  26. // @Param ClassifyIdSecond query int false "二级分类ID"
  27. // @Param Keyword query string false "搜索关键词"
  28. // @Param SortRule query int false "排序方式: 1-正序; 2-倒序(默认)"
  29. // @Success 200 {object} report_approve.ReportApproveFlowListResp
  30. // @router /flow/list [get]
  31. func (this *ReportApproveFlowController) List() {
  32. br := new(models.BaseResponse).Init()
  33. defer func() {
  34. if br.ErrMsg == "" {
  35. br.IsSendEmail = false
  36. }
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. sysUser := this.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. br.Ret = 408
  45. return
  46. }
  47. params := new(report_approve.ReportApproveFlowListReq)
  48. if e := this.ParseForm(params); e != nil {
  49. br.Msg = "获取失败"
  50. br.ErrMsg = "入参解析失败, Err: " + e.Error()
  51. return
  52. }
  53. var cond, orderRule string
  54. var pars []interface{}
  55. // 筛选项
  56. {
  57. keyword := strings.TrimSpace(params.Keyword)
  58. if keyword != "" {
  59. kw := fmt.Sprint("%", keyword, "%")
  60. cond += fmt.Sprintf(` AND %s LIKE ?`, report_approve.ReportApproveFlowCols.FlowName)
  61. pars = append(pars, kw)
  62. }
  63. if params.ReportType > 0 && params.ClassifySecondId > 0 {
  64. cond += fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifySecondId)
  65. pars = append(pars, params.ReportType, params.ClassifySecondId)
  66. }
  67. if params.SortRule > 0 {
  68. orderMap := map[int]string{1: "ASC", 2: "DESC"}
  69. orderRule = fmt.Sprintf("%s %s", report_approve.ReportApproveFlowCols.CreateTime, orderMap[params.SortRule])
  70. }
  71. }
  72. resp := new(report_approve.ReportApproveFlowListResp)
  73. flowOb := new(report_approve.ReportApproveFlow)
  74. total, e := flowOb.GetCountByCondition(cond, pars)
  75. if e != nil {
  76. br.Msg = "获取失败"
  77. br.ErrMsg = "获取审批流总数失败, Err:" + e.Error()
  78. return
  79. }
  80. if total <= 0 {
  81. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  82. resp.Paging = page
  83. br.Ret = 200
  84. br.Success = true
  85. br.Msg = "获取成功"
  86. br.Data = resp
  87. return
  88. }
  89. // 分页列表
  90. var startSize int
  91. if params.PageSize <= 0 {
  92. params.PageSize = utils.PageSize20
  93. }
  94. if params.CurrentIndex <= 0 {
  95. params.CurrentIndex = 1
  96. }
  97. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  98. list, e := flowOb.GetPageItemsByCondition(cond, pars, []string{}, orderRule, startSize, params.PageSize)
  99. if e != nil {
  100. br.Msg = "获取失败"
  101. br.ErrMsg = "获取审批流分页列表失败, Err:" + e.Error()
  102. return
  103. }
  104. // 指标分类
  105. cnClassifyIdName, enClassifyIdName := make(map[int]string), make(map[int]string)
  106. cnClassify, e := models.GetAllClassify()
  107. if e != nil {
  108. br.Msg = "获取失败"
  109. br.ErrMsg = "获取中文分类失败, Err: " + e.Error()
  110. return
  111. }
  112. for _, v := range cnClassify {
  113. cnClassifyIdName[v.Id] = v.ClassifyName
  114. }
  115. enClassify, e := models.GetEnglishClassifies()
  116. if e != nil {
  117. br.Msg = "获取失败"
  118. br.ErrMsg = "获取英文分类失败, Err: " + e.Error()
  119. return
  120. }
  121. enRootIdMap := make(map[int]int) // 英文分类的一级分类ID
  122. for _, v := range enClassify {
  123. enClassifyIdName[v.Id] = v.ClassifyName
  124. enRootIdMap[v.Id] = v.RootId
  125. }
  126. for _, v := range list {
  127. t := report_approve.FormatReportApproveFlow2Item(v)
  128. if v.ReportType == report_approve.FlowReportTypeEnglish {
  129. t.ReportClassify = fmt.Sprintf("%s/%s/%s/%s", report_approve.FlowReportTypeMap[v.ReportType], enClassifyIdName[enRootIdMap[v.ClassifySecondId]], enClassifyIdName[v.ClassifyFirstId], enClassifyIdName[v.ClassifySecondId])
  130. } else {
  131. t.ReportClassify = fmt.Sprintf("%s/%s/%s", report_approve.FlowReportTypeMap[v.ReportType], cnClassifyIdName[v.ClassifyFirstId], cnClassifyIdName[v.ClassifySecondId])
  132. }
  133. resp.List = append(resp.List, t)
  134. }
  135. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  136. resp.Paging = page
  137. br.Ret = 200
  138. br.Success = true
  139. br.Msg = "获取成功"
  140. br.Data = resp
  141. }
  142. // Add
  143. // @Title 新增审批流
  144. // @Description 新增审批流
  145. // @Param request body report_approve.ReportApproveFlowAddReq true "type json string"
  146. // @Success 200 {object} report_approve.ReportApproveFlowDetailItem
  147. // @router /flow/add [post]
  148. func (this *ReportApproveFlowController) Add() {
  149. br := new(models.BaseResponse).Init()
  150. defer func() {
  151. if br.ErrMsg == "" {
  152. br.IsSendEmail = false
  153. }
  154. this.Data["json"] = br
  155. this.ServeJSON()
  156. }()
  157. sysUser := this.SysUser
  158. if sysUser == nil {
  159. br.Msg = "请登录"
  160. br.ErrMsg = "请登录,SysUser Is Empty"
  161. br.Ret = 408
  162. return
  163. }
  164. var req report_approve.ReportApproveFlowAddReq
  165. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  166. br.Msg = "参数有误"
  167. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  168. return
  169. }
  170. req.FlowName = strings.TrimSpace(req.FlowName)
  171. if req.FlowName == "" {
  172. br.Msg = "请输入审批流名称"
  173. return
  174. }
  175. if len([]rune(req.FlowName)) > 20 {
  176. br.Msg = "审批流名称最多输入20个字符"
  177. return
  178. }
  179. reportTypes := []int{report_approve.FlowReportTypeChinese, report_approve.FlowReportTypeEnglish, report_approve.FlowReportTypeSmart}
  180. if !utils.InArrayByInt(reportTypes, req.ReportType) {
  181. br.Msg = "审批流报告类型有误"
  182. br.ErrMsg = fmt.Sprintf("审批流报告类型有误, ReportType: %d", req.ReportType)
  183. return
  184. }
  185. if req.ClassifyFirstId <= 0 || req.ClassifySecondId <= 0 {
  186. br.Msg = "请选择报告分类"
  187. return
  188. }
  189. if len(req.Nodes) <= 0 {
  190. br.Msg = "请添加审批流程"
  191. return
  192. }
  193. approveTypes := []int{report_approve.NodeApproveTypeRoll, report_approve.NodeApproveTypeAll, report_approve.NodeApproveTypeAny}
  194. approveUserTypes := []string{report_approve.NodeUserTypeNormal, report_approve.NodeUserTypeRole}
  195. for _, v := range req.Nodes {
  196. if !utils.InArrayByInt(approveTypes, v.ApproveType) {
  197. br.Msg = "审批流类型有误"
  198. br.ErrMsg = fmt.Sprintf("审批流类型有误, ApproveType: %d", v.ApproveType)
  199. return
  200. }
  201. for _, v2 := range v.Users {
  202. if !utils.InArrayByStr(approveUserTypes, v2.UserType) {
  203. br.Msg = "审批流用户类型有误"
  204. br.ErrMsg = fmt.Sprintf("审批流用户类型有误, UserType: %d", v2.UserType)
  205. return
  206. }
  207. }
  208. }
  209. // 审批流是否已存在
  210. {
  211. flowOb := new(report_approve.ReportApproveFlow)
  212. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId)
  213. existPars := make([]interface{}, 0)
  214. existPars = append(existPars, req.ReportType, req.ClassifyFirstId, req.ClassifySecondId)
  215. exist, e := flowOb.GetItemByCondition(existCond, existPars, "")
  216. if e != nil && e.Error() != utils.ErrNoRow() {
  217. br.Msg = "获取失败"
  218. br.ErrMsg = "获取审批流是否已存在失败, Err: " + e.Error()
  219. return
  220. }
  221. if exist != nil {
  222. br.Msg = "该分类已有审批流, 请勿重复添加"
  223. return
  224. }
  225. }
  226. flowItem := new(report_approve.ReportApproveFlow)
  227. flowItem.FlowName = req.FlowName
  228. flowItem.ReportType = req.ReportType
  229. flowItem.ClassifyFirstId = req.ClassifyFirstId
  230. flowItem.ClassifySecondId = req.ClassifySecondId
  231. flowItem.CurrVersion = 1
  232. flowItem.CreateTime = time.Now().Local()
  233. flowItem.ModifyTime = time.Now().Local()
  234. nodeItems := make([]*report_approve.ReportApproveNode, 0)
  235. for _, v := range req.Nodes {
  236. n := new(report_approve.ReportApproveNode)
  237. n.ApproveType = v.ApproveType
  238. n.CurrVersion = flowItem.CurrVersion
  239. j, _ := json.Marshal(v.Users)
  240. n.Users = string(j)
  241. n.CreateTime = time.Now().Local()
  242. nodeItems = append(nodeItems, n)
  243. }
  244. // 新增审批流和节点
  245. if e := flowItem.CreateFlowAndNodes(flowItem, nodeItems); e != nil {
  246. br.Msg = "操作失败"
  247. br.ErrMsg = "新增审批流和节点失败, Err: " + e.Error()
  248. return
  249. }
  250. // 返回详情
  251. detail, e := report_approve.FormatFlowAndNodesItem2Detail(flowItem, nodeItems)
  252. if e != nil {
  253. br.Msg = "获取失败"
  254. br.ErrMsg = "获取审批详情失败, Err: " + e.Error()
  255. return
  256. }
  257. // 更新审批对应的报告状态:未发布->待提交
  258. go func() {
  259. _ = services.FlowOperateResetReportState(flowItem.ReportType, flowItem.ClassifyFirstId, flowItem.ClassifySecondId, models.ReportStateUnpublished, models.ReportStateWaitSubmit)
  260. }()
  261. br.Data = detail
  262. br.Ret = 200
  263. br.Success = true
  264. br.Msg = "操作成功"
  265. }
  266. // Edit
  267. // @Title 编辑审批流
  268. // @Description 编辑审批流
  269. // @Param request body report_approve.ReportApproveFlowEditReq true "type json string"
  270. // @Success 200 {object} report_approve.ReportApproveFlowDetailItem
  271. // @router /flow/edit [post]
  272. func (this *ReportApproveFlowController) Edit() {
  273. br := new(models.BaseResponse).Init()
  274. defer func() {
  275. if br.ErrMsg == "" {
  276. br.IsSendEmail = false
  277. }
  278. this.Data["json"] = br
  279. this.ServeJSON()
  280. }()
  281. sysUser := this.SysUser
  282. if sysUser == nil {
  283. br.Msg = "请登录"
  284. br.ErrMsg = "请登录,SysUser Is Empty"
  285. br.Ret = 408
  286. return
  287. }
  288. var req report_approve.ReportApproveFlowEditReq
  289. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  290. br.Msg = "参数有误"
  291. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  292. return
  293. }
  294. if req.ReportApproveFlowId <= 0 {
  295. br.Msg = "参数有误"
  296. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveFlowId: %d", req.ReportApproveFlowId)
  297. return
  298. }
  299. req.FlowName = strings.TrimSpace(req.FlowName)
  300. if req.FlowName == "" {
  301. br.Msg = "请输入审批流名称"
  302. return
  303. }
  304. if len([]rune(req.FlowName)) > 20 {
  305. br.Msg = "审批流名称最多输入20个字符"
  306. return
  307. }
  308. reportTypes := []int{report_approve.FlowReportTypeChinese, report_approve.FlowReportTypeEnglish, report_approve.FlowReportTypeSmart}
  309. if !utils.InArrayByInt(reportTypes, req.ReportType) {
  310. br.Msg = "审批流报告类型有误"
  311. br.ErrMsg = fmt.Sprintf("审批流报告类型有误, ReportType: %d", req.ReportType)
  312. return
  313. }
  314. if req.ClassifyFirstId <= 0 || req.ClassifySecondId <= 0 {
  315. br.Msg = "请选择报告分类"
  316. return
  317. }
  318. if len(req.Nodes) <= 0 {
  319. br.Msg = "请添加审批流程"
  320. return
  321. }
  322. approveTypes := []int{report_approve.NodeApproveTypeRoll, report_approve.NodeApproveTypeAll, report_approve.NodeApproveTypeAny}
  323. approveUserTypes := []string{report_approve.NodeUserTypeNormal, report_approve.NodeUserTypeRole}
  324. for _, v := range req.Nodes {
  325. if !utils.InArrayByInt(approveTypes, v.ApproveType) {
  326. br.Msg = "审批流类型有误"
  327. br.ErrMsg = fmt.Sprintf("审批流类型有误, ApproveType: %d", v.ApproveType)
  328. return
  329. }
  330. for _, v2 := range v.Users {
  331. if !utils.InArrayByStr(approveUserTypes, v2.UserType) {
  332. br.Msg = "审批流用户类型有误"
  333. br.ErrMsg = fmt.Sprintf("审批流用户类型有误, UserType: %d", v2.UserType)
  334. return
  335. }
  336. }
  337. }
  338. flowOb := new(report_approve.ReportApproveFlow)
  339. flowItem, e := flowOb.GetItemById(req.ReportApproveFlowId)
  340. if e != nil {
  341. if e.Error() == utils.ErrNoRow() {
  342. br.Msg = "审批流已被删除, 请刷新页面"
  343. return
  344. }
  345. br.Msg = "操作失败"
  346. br.ErrMsg = "获取审批流信息失败, Err: " + e.Error()
  347. return
  348. }
  349. // 审批流是否已存在
  350. {
  351. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ? AND %s <> ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId, report_approve.ReportApproveFlowCols.ReportApproveFlowId)
  352. existPars := make([]interface{}, 0)
  353. existPars = append(existPars, req.ReportType, req.ClassifyFirstId, req.ClassifySecondId, req.ReportApproveFlowId)
  354. exist, e := flowOb.GetItemByCondition(existCond, existPars, "")
  355. if e != nil && e.Error() != utils.ErrNoRow() {
  356. br.Msg = "操作失败"
  357. br.ErrMsg = "获取审批流是否已存在失败, Err: " + e.Error()
  358. return
  359. }
  360. if exist != nil {
  361. br.Msg = "该分类已有审批流, 请勿重复添加"
  362. return
  363. }
  364. }
  365. // 校验审批流是否关联了进行中的审批
  366. {
  367. approvingOb := new(report_approve.ReportApprove)
  368. approvingCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveCols.FlowId, report_approve.ReportApproveCols.FlowVersion, report_approve.ReportApproveCols.State)
  369. approvingPars := make([]interface{}, 0)
  370. approvingPars = append(approvingPars, flowItem.ReportApproveFlowId, flowItem.CurrVersion, report_approve.ReportApproveStateApproving)
  371. count, e := approvingOb.GetCountByCondition(approvingCond, approvingPars)
  372. if e != nil {
  373. br.Msg = "操作失败"
  374. br.ErrMsg = "获取审批流关联进行中的审批数失败. Err: " + e.Error()
  375. return
  376. }
  377. if count > 0 {
  378. br.Msg = "当前有未走完流程的报告,请走完流程后再做变更"
  379. return
  380. }
  381. }
  382. // 变更了报告分类时, 判断是否允许变更
  383. if req.ReportType != flowItem.ReportType || req.ClassifyFirstId != flowItem.ClassifyFirstId || req.ClassifySecondId != flowItem.ClassifySecondId {
  384. checkOk, e := services.CheckReportApproveFlowChange(flowItem.ReportType, flowItem.ClassifyFirstId, flowItem.ClassifySecondId)
  385. if e != nil {
  386. br.Msg = "操作失败"
  387. br.ErrMsg = "校验审批流是否可变更失败, Err: " + e.Error()
  388. return
  389. }
  390. if !checkOk {
  391. br.Msg = "当前有未走完流程的报告, 请走完流程后再做变更!"
  392. return
  393. }
  394. }
  395. flowItem.FlowName = req.FlowName
  396. flowItem.ReportType = req.ReportType
  397. flowItem.ClassifyFirstId = req.ClassifyFirstId
  398. flowItem.ClassifySecondId = req.ClassifySecondId
  399. flowItem.CurrVersion += 1
  400. flowItem.ModifyTime = time.Now().Local()
  401. nodeItems := make([]*report_approve.ReportApproveNode, 0)
  402. for _, v := range req.Nodes {
  403. n := new(report_approve.ReportApproveNode)
  404. n.ApproveType = v.ApproveType
  405. n.CurrVersion = flowItem.CurrVersion
  406. j, _ := json.Marshal(v.Users)
  407. n.Users = string(j)
  408. n.CreateTime = time.Now().Local()
  409. nodeItems = append(nodeItems, n)
  410. }
  411. // 更新审批流和节点
  412. if e := flowItem.UpdateFlowAndNodes(flowItem, nodeItems); e != nil {
  413. br.Msg = "操作失败"
  414. br.ErrMsg = "更新审批流和节点失败, Err: " + e.Error()
  415. return
  416. }
  417. // 返回详情
  418. detail, e := report_approve.FormatFlowAndNodesItem2Detail(flowItem, nodeItems)
  419. if e != nil {
  420. br.Msg = "获取失败"
  421. br.ErrMsg = "获取审批详情失败, Err: " + e.Error()
  422. return
  423. }
  424. br.Data = detail
  425. br.Ret = 200
  426. br.Success = true
  427. br.Msg = "操作成功"
  428. }
  429. // Detail
  430. // @Title 审批流详情
  431. // @Description 审批流详情
  432. // @Param ReportApproveFlowId query int true "审批流ID"
  433. // @Success 200 {object} report_approve.ReportApproveFlowDetailItem
  434. // @router /flow/detail [get]
  435. func (this *ReportApproveFlowController) Detail() {
  436. br := new(models.BaseResponse).Init()
  437. defer func() {
  438. if br.ErrMsg == "" {
  439. br.IsSendEmail = false
  440. }
  441. this.Data["json"] = br
  442. this.ServeJSON()
  443. }()
  444. sysUser := this.SysUser
  445. if sysUser == nil {
  446. br.Msg = "请登录"
  447. br.ErrMsg = "请登录,SysUser Is Empty"
  448. br.Ret = 408
  449. return
  450. }
  451. flowId, _ := this.GetInt("ReportApproveFlowId")
  452. if flowId <= 0 {
  453. br.Msg = "参数有误"
  454. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveFlowId: %d", flowId)
  455. return
  456. }
  457. flowOb := new(report_approve.ReportApproveFlow)
  458. flowItem, e := flowOb.GetItemById(flowId)
  459. if e != nil {
  460. if e.Error() == utils.ErrNoRow() {
  461. br.Msg = "审批流已被删除, 请刷新页面"
  462. return
  463. }
  464. br.Msg = "获取失败"
  465. br.ErrMsg = "获取审批流信息失败, Err: " + e.Error()
  466. return
  467. }
  468. // 审批节点
  469. nodeOb := new(report_approve.ReportApproveNode)
  470. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  471. nodePars := make([]interface{}, 0)
  472. nodePars = append(nodePars, flowItem.ReportApproveFlowId, flowItem.CurrVersion)
  473. nodes, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  474. if e != nil {
  475. br.Msg = "获取失败"
  476. br.ErrMsg = "获取审批节点失败, Err: " + e.Error()
  477. return
  478. }
  479. detail, e := report_approve.FormatFlowAndNodesItem2Detail(flowItem, nodes)
  480. if e != nil {
  481. br.Msg = "获取失败"
  482. br.ErrMsg = "获取审批详情失败, Err: " + e.Error()
  483. return
  484. }
  485. br.Data = detail
  486. br.Ret = 200
  487. br.Success = true
  488. br.Msg = "获取成功"
  489. }
  490. // Remove
  491. // @Title 删除审批流
  492. // @Description 删除审批流
  493. // @Param request body report_approve.ReportApproveFlowRemoveReq true "type json string"
  494. // @Success 200 string "操作成功"
  495. // @router /flow/remove [post]
  496. func (this *ReportApproveFlowController) Remove() {
  497. br := new(models.BaseResponse).Init()
  498. defer func() {
  499. if br.ErrMsg == "" {
  500. br.IsSendEmail = false
  501. }
  502. this.Data["json"] = br
  503. this.ServeJSON()
  504. }()
  505. sysUser := this.SysUser
  506. if sysUser == nil {
  507. br.Msg = "请登录"
  508. br.ErrMsg = "请登录,SysUser Is Empty"
  509. br.Ret = 408
  510. return
  511. }
  512. var req report_approve.ReportApproveFlowRemoveReq
  513. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  514. br.Msg = "参数有误"
  515. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  516. return
  517. }
  518. if req.ReportApproveFlowId <= 0 {
  519. br.Msg = "参数有误"
  520. br.ErrMsg = fmt.Sprintf("参数有误, ReportApproveFlowId: %d", req.ReportApproveFlowId)
  521. return
  522. }
  523. flowOb := new(report_approve.ReportApproveFlow)
  524. flowItem, e := flowOb.GetItemById(req.ReportApproveFlowId)
  525. if e != nil {
  526. if e.Error() == utils.ErrNoRow() {
  527. br.Msg = "审批流已被删除, 请刷新页面"
  528. return
  529. }
  530. br.Msg = "操作失败"
  531. br.ErrMsg = "获取审批流信息失败, Err: " + e.Error()
  532. return
  533. }
  534. // 校验是否允许删除
  535. checkOk, e := services.CheckReportApproveFlowChange(flowItem.ReportType, flowItem.ClassifyFirstId, flowItem.ClassifySecondId)
  536. if e != nil {
  537. br.Msg = "操作失败"
  538. br.ErrMsg = "校验审批流是否可变更失败, Err: " + e.Error()
  539. return
  540. }
  541. if !checkOk {
  542. br.Msg = "当前有未走完流程的报告, 请走完流程后再做删除!"
  543. return
  544. }
  545. // 删除审批流, 保留审批节点, 历史审批回显会用得到
  546. if e = flowItem.Del(); e != nil {
  547. br.Msg = "操作失败"
  548. br.ErrMsg = "删除审批流失败, Err: " + e.Error()
  549. return
  550. }
  551. // 更新审批对应的报告状态:待提交->未发布
  552. go func() {
  553. _ = services.FlowOperateResetReportState(flowItem.ReportType, flowItem.ClassifyFirstId, flowItem.ClassifySecondId, models.ReportStateWaitSubmit, models.ReportStateUnpublished)
  554. }()
  555. br.Ret = 200
  556. br.Success = true
  557. br.Msg = "操作成功"
  558. }
  559. // ReportClassifyTree
  560. // @Title 报告分类树
  561. // @Description 报告分类树
  562. // @Param ReportApproveFlowId query int false "审批流ID"
  563. // @Success 200 {object} report_approve.ReportClassifyTreeItem
  564. // @router /report/classify_tree [get]
  565. func (this *ReportApproveFlowController) ReportClassifyTree() {
  566. br := new(models.BaseResponse).Init()
  567. defer func() {
  568. if br.ErrMsg == "" {
  569. br.IsSendEmail = false
  570. }
  571. this.Data["json"] = br
  572. this.ServeJSON()
  573. }()
  574. sysUser := this.SysUser
  575. if sysUser == nil {
  576. br.Msg = "请登录"
  577. br.ErrMsg = "请登录,SysUser Is Empty"
  578. br.Ret = 408
  579. return
  580. }
  581. flowId, _ := this.GetInt("ReportApproveFlowId")
  582. // 获取审批流信息, 用于查询分类是否可选
  583. var flowKey string
  584. if flowId > 0 {
  585. flowOb := new(report_approve.ReportApproveFlow)
  586. flowItem, e := flowOb.GetItemById(flowId)
  587. if e != nil {
  588. if e.Error() == utils.ErrNoRow() {
  589. br.Msg = "审批流已被删除, 请刷新页面"
  590. return
  591. }
  592. br.Msg = "获取失败"
  593. br.ErrMsg = "获取审批流信息失败, Err: " + e.Error()
  594. return
  595. }
  596. flowKey = fmt.Sprintf("%d-%d-%d", flowItem.ReportType, flowItem.ClassifyFirstId, flowItem.ClassifySecondId)
  597. }
  598. // 获取审批流列表
  599. flowOb := new(report_approve.ReportApproveFlow)
  600. flows, e := flowOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  601. if e != nil {
  602. br.Msg = "获取失败"
  603. br.ErrMsg = "获取审批流列表失败, Err: " + e.Error()
  604. return
  605. }
  606. hasFlowMap := make(map[string]bool)
  607. for _, v := range flows {
  608. k := fmt.Sprintf("%d-%d-%d", v.ReportType, v.ClassifyFirstId, v.ClassifySecondId)
  609. if k == flowKey {
  610. // 当前审批流对应的分类标记为可选状态
  611. continue
  612. }
  613. hasFlowMap[k] = true
  614. }
  615. resp, cnTree, smartTree, enTree := make([]*report_approve.ReportClassifyTreeItem, 0), make([]*report_approve.ReportClassifyTreeItem, 0), make([]*report_approve.ReportClassifyTreeItem, 0), make([]*report_approve.ReportClassifyTreeItem, 0)
  616. var cnErr, enErr error
  617. wg := sync.WaitGroup{}
  618. wg.Add(2)
  619. // 中文/智能研报分类
  620. go func() {
  621. defer func() {
  622. wg.Done()
  623. }()
  624. classify, e := models.GetAllClassify()
  625. if e != nil {
  626. cnErr = fmt.Errorf("GetAllClassify err: %s", e.Error())
  627. return
  628. }
  629. cnTree = services.GetReportClassifyTreeRecursive(classify, 0)
  630. for _, v := range cnTree {
  631. for _, v2 := range v.Children {
  632. k := fmt.Sprintf("%d-%d-%d", report_approve.FlowReportTypeChinese, v.ClassifyId, v2.ClassifyId)
  633. v2.HasFlow = hasFlowMap[k]
  634. }
  635. }
  636. smartTree = services.GetReportClassifyTreeRecursive(classify, 0)
  637. for _, v := range smartTree {
  638. for _, v2 := range v.Children {
  639. k := fmt.Sprintf("%d-%d-%d", report_approve.FlowReportTypeSmart, v.ClassifyId, v2.ClassifyId)
  640. v2.HasFlow = hasFlowMap[k]
  641. }
  642. }
  643. }()
  644. // 英文研报分类
  645. go func() {
  646. defer func() {
  647. wg.Done()
  648. }()
  649. classify, e := models.GetAllEnglishClassify()
  650. if e != nil {
  651. enErr = fmt.Errorf("GetAllEnglishClassify err: %s", e.Error())
  652. return
  653. }
  654. enTree = services.GetReportClassifyTreeRecursive(classify, 0)
  655. for _, v := range enTree {
  656. for _, v2 := range v.Children {
  657. k := fmt.Sprintf("%d-%d-%d", report_approve.FlowReportTypeEnglish, v.ClassifyId, v2.ClassifyId)
  658. v2.HasFlow = hasFlowMap[k]
  659. }
  660. }
  661. }()
  662. wg.Wait()
  663. if cnErr != nil {
  664. br.Msg = "获取失败"
  665. br.ErrMsg = "获取中文分类失败, Err: " + cnErr.Error()
  666. return
  667. }
  668. if enErr != nil {
  669. br.Msg = "获取失败"
  670. br.ErrMsg = "获取英文分类失败, Err: " + enErr.Error()
  671. return
  672. }
  673. resp = append(resp, &report_approve.ReportClassifyTreeItem{
  674. ClassifyId: report_approve.FlowReportTypeChinese,
  675. ClassifyName: "研报列表",
  676. Children: cnTree,
  677. }, &report_approve.ReportClassifyTreeItem{
  678. ClassifyId: report_approve.FlowReportTypeEnglish,
  679. ClassifyName: "英文研报",
  680. Children: enTree,
  681. }, &report_approve.ReportClassifyTreeItem{
  682. ClassifyId: report_approve.FlowReportTypeSmart,
  683. ClassifyName: "智能研报",
  684. Children: smartTree,
  685. })
  686. br.Data = resp
  687. br.Ret = 200
  688. br.Success = true
  689. br.Msg = "获取成功"
  690. }