report_approve_flow.go 24 KB

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