report_approve_flow.go 25 KB

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