report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/services"
  6. "eta/eta_hub/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "strings"
  10. "time"
  11. )
  12. // ReportController 报告
  13. type ReportController struct {
  14. BaseAuthController
  15. }
  16. // List
  17. // @Title 获取报告列表接口
  18. // @Description 获取报告列表
  19. // @Param PageSize query int true "每页数据条数"
  20. // @Param CurrentIndex query int true "当前页页码,从1开始"
  21. // @Param Keyword query string false "搜索关键词"
  22. // @Param State query int false "状态"
  23. // @Param IsPublicPublish query int false "是否公开发布,1:是,2:否"
  24. // @Success 200 {object} models.ReportListResp
  25. // @router /list [get]
  26. func (this *ReportController) List() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. var startSize int
  35. if pageSize <= 0 {
  36. pageSize = utils.PageSize20
  37. }
  38. if currentIndex <= 0 {
  39. currentIndex = 1
  40. }
  41. startSize = utils.StartIndex(currentIndex, pageSize)
  42. var condition string
  43. var pars []interface{}
  44. keyword := this.GetString("Keyword")
  45. keyword = strings.TrimSpace(keyword)
  46. if keyword != "" {
  47. kw := fmt.Sprint("%", keyword, "%")
  48. condition += ` AND (title LIKE ? OR author LIKE ? OR admin_real_name LIKE ?) `
  49. pars = append(pars, kw, kw, kw)
  50. }
  51. state, _ := this.GetInt("State")
  52. if state > 0 {
  53. condition += ` AND state = ? `
  54. pars = append(pars, state)
  55. }
  56. isPublicPublish, _ := this.GetInt("IsPublicPublish")
  57. if isPublicPublish > 0 {
  58. condition += ` AND is_public_publish = ? `
  59. pars = append(pars, isPublicPublish)
  60. }
  61. adminId, _ := this.GetInt("AdminId")
  62. if adminId > 0 {
  63. condition += ` AND admin_id = ? `
  64. pars = append(pars, adminId)
  65. }
  66. classifyId, _ := this.GetInt("ClassifyId")
  67. if classifyId > 0 {
  68. condition += ` AND (classify_id_first = ? OR classify_id_second = ? OR classify_id_third = ?) `
  69. pars = append(pars, classifyId, classifyId, classifyId)
  70. }
  71. total, err := models.GetReportListCount(condition, pars, "")
  72. if err != nil {
  73. br.Msg = "获取失败"
  74. br.ErrMsg = "获取失败,Err:" + err.Error()
  75. return
  76. }
  77. list, err := models.GetReportList(condition, pars, startSize, pageSize)
  78. if err != nil {
  79. br.Msg = "获取失败"
  80. br.ErrMsg = "获取失败,Err:" + err.Error()
  81. return
  82. }
  83. results := make([]*models.ReportItem, 0)
  84. chapterList := make([]*models.ReportChapterItem, 0)
  85. for _, v := range list {
  86. t := models.FormatReport2Item(v, chapterList)
  87. results = append(results, t)
  88. }
  89. page := paging.GetPaging(currentIndex, pageSize, total)
  90. resp := new(models.ReportListResp)
  91. resp.Paging = page
  92. resp.List = results
  93. br.Ret = 200
  94. br.Msg = "获取成功"
  95. br.Data = resp
  96. }
  97. // Detail
  98. // @Title 获取报告详情接口
  99. // @Description 获取报告详情
  100. // @Param ReportId query int true "报告ID"
  101. // @Success 200 {object} models.ReportItem
  102. // @router /detail [get]
  103. func (this *ReportController) Detail() {
  104. br := new(models.BaseResponse).Init()
  105. defer func() {
  106. this.Data["json"] = br
  107. this.ServeJSON()
  108. }()
  109. reportId, err := this.GetInt("ReportId")
  110. if err != nil {
  111. br.Msg = "获取参数失败!"
  112. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  113. return
  114. }
  115. if reportId <= 0 {
  116. br.Msg = "参数错误"
  117. return
  118. }
  119. item, err := models.GetReportById(reportId)
  120. if err != nil {
  121. if err.Error() == utils.ErrNoRow() {
  122. br.Msg = "报告已被删除"
  123. return
  124. }
  125. br.Msg = "获取失败"
  126. br.ErrMsg = "获取失败,Err:" + err.Error()
  127. return
  128. }
  129. chapterList := make([]*models.ReportChapterItem, 0)
  130. if item.HasChapter == 1 {
  131. // 获取章节内容
  132. tmpChapterList, err := models.GetChapterListByReportId(item.Id)
  133. if err != nil {
  134. br.Msg = "获取失败"
  135. br.ErrMsg = "获取晨/周报章节列表失败, Err: " + err.Error()
  136. return
  137. }
  138. if len(tmpChapterList) > 0 {
  139. // 章节类型的字段赋值
  140. for _, item := range tmpChapterList {
  141. chapterList = append(chapterList, models.FormatReportChapterItem(item))
  142. }
  143. }
  144. }
  145. if item.HeadResourceId > 0 {
  146. headResource, err := models.GetResourceItemById(item.HeadResourceId)
  147. if err != nil {
  148. br.Msg = "操作失败"
  149. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  150. return
  151. }
  152. item.HeadImg = headResource.ImgUrl
  153. item.HeadStyle = headResource.Style
  154. }
  155. if item.EndResourceId > 0 {
  156. endResource, err := models.GetResourceItemById(item.EndResourceId)
  157. if err != nil {
  158. br.Msg = "操作失败"
  159. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  160. return
  161. }
  162. item.EndImg = endResource.ImgUrl
  163. item.EndStyle = endResource.Style
  164. }
  165. resp := models.FormatReport2Item(item, chapterList)
  166. br.Data = resp
  167. br.Ret = 200
  168. br.Msg = "获取成功"
  169. }
  170. // Approve
  171. // @Title 审批
  172. // @Description 报告审批
  173. // @Param request body models.PublishReq true "type json string"
  174. // @Success 200 Ret=200 发布成功
  175. // @router /approve [post]
  176. func (this *ReportController) Approve() {
  177. br := new(models.BaseResponse).Init()
  178. defer func() {
  179. this.Data["json"] = br
  180. this.ServeJSON()
  181. }()
  182. var req models.ReportApproveReq
  183. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  184. if err != nil {
  185. br.Msg = "参数解析异常!"
  186. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  187. return
  188. }
  189. if req.ReportId <= 0 {
  190. br.Msg = "参数错误"
  191. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  192. return
  193. }
  194. item, e := models.GetReportById(req.ReportId)
  195. if e != nil {
  196. if e.Error() == utils.ErrNoRow() {
  197. br.Msg = "报告已被删除"
  198. return
  199. }
  200. br.Msg = "操作失败"
  201. br.ErrMsg = "获取报告信息失败, Err:" + e.Error()
  202. return
  203. }
  204. if item.State != models.ReportStateWaitApprove {
  205. br.Msg = "报告状态有误"
  206. br.ErrMsg = fmt.Sprintf("报告状态有误, State: %d", item.State)
  207. return
  208. }
  209. if item.HasChapter != 1 && item.Content == "" {
  210. br.Msg = "报告内容为空,请检查内容"
  211. return
  212. }
  213. // 校验审批配置
  214. confMap, e := models.GetBusinessConf()
  215. if e != nil {
  216. br.Msg = "操作失败"
  217. br.ErrMsg = "获取审批配置失败, Err: " + e.Error()
  218. return
  219. }
  220. if confMap[models.BusinessConfIsReportApprove] != "true" {
  221. br.Msg = "未开启审批, 请开启后重新操作"
  222. return
  223. }
  224. if confMap[models.BusinessConfReportApproveType] != models.BusinessConfReportApproveTypeOther {
  225. br.Msg = "未开启第三方审批, 请开启后重新操作"
  226. return
  227. }
  228. stateMap := map[bool]int{false: models.ReportStateRefused, true: models.ReportStatePass}
  229. // 驳回
  230. now := time.Now().Local()
  231. if !req.Pass {
  232. cols := []string{"State", "ModifyTime", "ApproveTime"}
  233. item.State = stateMap[req.Pass]
  234. item.ModifyTime = now
  235. item.ApproveTime = now
  236. if e = item.UpdateReport(cols); e != nil {
  237. br.Msg = "审批失败"
  238. br.ErrMsg = "报告审批驳回失败, Err: " + e.Error()
  239. return
  240. }
  241. }
  242. // 通过审批
  243. if req.Pass {
  244. cols := []string{"State", "ModifyTime", "ApproveTime", "PublishTime"}
  245. item.State = stateMap[req.Pass]
  246. item.ModifyTime = now
  247. item.ApproveTime = now
  248. item.PublishTime = now
  249. if e = item.UpdateReport(cols); e != nil {
  250. br.Msg = "审批失败"
  251. br.ErrMsg = "报告审批通过失败, Err: " + e.Error()
  252. return
  253. }
  254. // 更新报告Es
  255. go func() {
  256. _ = services.UpdateReportEs(req.ReportId, models.ReportStatePublished) // 注:状态用已发布的, 兼容以前的代码
  257. }()
  258. }
  259. // 记录报告变更状态
  260. go func() {
  261. recordItem := &models.ReportStateRecord{
  262. ReportId: req.ReportId,
  263. ReportType: models.ReportTypeChinese,
  264. State: item.State,
  265. CreateTime: time.Now(),
  266. }
  267. _, _ = models.AddReportStateRecord(recordItem)
  268. }()
  269. br.Ret = 200
  270. br.Msg = "审批成功"
  271. }
  272. // @Title 获取分类列表
  273. // @Description 获取分类列表
  274. // @Param PageSize query int true "每页数据条数"
  275. // @Param CurrentIndex query int true "当前页页码,从1开始"
  276. // @Param KeyWord query string true "检索关键词"
  277. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  278. // @Param HideDayWeek query int false "是否隐藏晨周报"
  279. // @Success 200 {object} models.Classify
  280. // @router /classify/list/bak [get]
  281. func (this *ReportController) ListClassify() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. this.Data["json"] = br
  285. this.ServeJSON()
  286. }()
  287. pageSize, _ := this.GetInt("PageSize")
  288. currentIndex, _ := this.GetInt("CurrentIndex")
  289. keyWord := this.GetString("KeyWord")
  290. //companyType := this.GetString("CompanyType")
  291. companyType := ""
  292. br.Msg = "该接口已启用,请调用V2接口"
  293. return
  294. var startSize int
  295. if pageSize <= 0 {
  296. pageSize = utils.PageSize20
  297. }
  298. if currentIndex <= 0 {
  299. currentIndex = 1
  300. }
  301. startSize = utils.StartIndex(currentIndex, pageSize)
  302. list, err := models.GetClassifyList(startSize, pageSize, keyWord, companyType)
  303. if err != nil {
  304. br.Msg = "获取失败"
  305. br.ErrMsg = "获取失败,Err:" + err.Error()
  306. return
  307. }
  308. total, err := models.GetClassifyListCount(keyWord, companyType)
  309. if err != nil {
  310. br.Msg = "获取失败"
  311. br.ErrMsg = "获取失败,Err:" + err.Error()
  312. return
  313. }
  314. parentIds := make([]int, 0)
  315. for i := range list {
  316. parentIds = append(parentIds, list[i].Id)
  317. }
  318. parentIdLen := len(parentIds)
  319. if parentIdLen == 0 {
  320. resp := &models.ClassifyListResp{
  321. List: list,
  322. Paging: paging.GetPaging(currentIndex, pageSize, 0),
  323. }
  324. br.Data = resp
  325. br.Ret = 200
  326. br.Msg = "获取成功"
  327. return
  328. }
  329. // 获取一级分类-子目录列表
  330. menuListMap := make(map[int][]*models.ClassifyMenu, 0)
  331. var menuCond string
  332. var menuPars []interface{}
  333. menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  334. menuPars = append(menuPars, parentIds)
  335. parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
  336. if e != nil {
  337. br.Msg = "获取失败"
  338. br.ErrMsg = "获取一级分类子目录列表失败"
  339. return
  340. }
  341. for i := range parentMenus {
  342. if menuListMap[parentMenus[i].ClassifyId] == nil {
  343. menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
  344. }
  345. menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
  346. }
  347. // 获取子分类
  348. children, e := models.GetClassifyChildByParentIds(parentIds, keyWord)
  349. if e != nil {
  350. br.Msg = "获取失败"
  351. br.ErrMsg = "获取子分类失败"
  352. return
  353. }
  354. childrenIds := make([]int, 0)
  355. for i := range children {
  356. childrenIds = append(childrenIds, children[i].Id)
  357. }
  358. childrenIdsLen := len(childrenIds)
  359. // 获取二级分类-子目录关联
  360. relateMap := make(map[int]int, 0)
  361. if childrenIdsLen > 0 {
  362. var relateCond string
  363. var relatePars []interface{}
  364. relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(childrenIdsLen) + `)`
  365. relatePars = append(relatePars, childrenIds)
  366. relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
  367. if e != nil {
  368. br.Msg = "获取失败"
  369. br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
  370. return
  371. }
  372. for i := range relates {
  373. relateMap[relates[i].ClassifyId] = relates[i].MenuId
  374. }
  375. }
  376. // 二级分类
  377. childrenMap := make(map[int][]*models.ClassifyItem, 0)
  378. for i := range children {
  379. if childrenMap[children[i].ParentId] == nil {
  380. childrenMap[children[i].ParentId] = make([]*models.ClassifyItem, 0)
  381. }
  382. childrenMap[children[i].ParentId] = append(childrenMap[children[i].ParentId], &models.ClassifyItem{
  383. Classify: *children[i],
  384. //ClassifyMenuId: relateMap[children[i].Id],
  385. })
  386. }
  387. // 一级分类
  388. for i := range list {
  389. //list[i].ClassifyMenuList = menuListMap[list[i].Id]
  390. list[i].Child = childrenMap[list[i].Id]
  391. }
  392. page := paging.GetPaging(currentIndex, pageSize, total)
  393. resp := new(models.ClassifyListResp)
  394. resp.List = list
  395. resp.Paging = page
  396. br.Data = resp
  397. br.Ret = 200
  398. br.Msg = "获取成功"
  399. }
  400. // ListClassifyV2
  401. // @Title 获取所有分类列表(2024-6-26 13:49:03)
  402. // @Description 获取所有分类列表(2024-6-26 13:49:03)
  403. // @Param KeyWord query string true "检索关键词"
  404. // @Success 200 {object} models.ClassifyListV2Resp
  405. // @router /classify/list [get]
  406. func (this *ReportController) ListClassifyV2() {
  407. br := new(models.BaseResponse).Init()
  408. defer func() {
  409. this.Data["json"] = br
  410. this.ServeJSON()
  411. }()
  412. resp := new(models.ClassifyListV2Resp)
  413. keyWord := this.GetString("KeyWord")
  414. list, err := models.GetClassifyListByKeywordV2(keyWord, -1)
  415. if err != nil {
  416. br.Msg = "获取失败"
  417. br.ErrMsg = "获取失败,Err:" + err.Error()
  418. return
  419. }
  420. if keyWord != `` {
  421. idMap := make(map[int]bool)
  422. currParentClassifyIdList := make([]int, 0)
  423. for _, v := range list {
  424. idMap[v.Id] = true
  425. if v.ParentId > 0 {
  426. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  427. }
  428. }
  429. findList := list
  430. list = make([]*models.ClassifyListV2, 0)
  431. tmpList, tmpErr := services.GetParentClassifyListByParentIdList(currParentClassifyIdList)
  432. if tmpErr != nil {
  433. br.Msg = "获取失败"
  434. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  435. return
  436. }
  437. for _, v := range tmpList {
  438. if _, ok := idMap[v.Id]; !ok {
  439. list = append(list, v)
  440. }
  441. }
  442. list = append(list, findList...)
  443. }
  444. classifyIdList := make([]int, 0)
  445. for i := range list {
  446. classifyIdList = append(classifyIdList, list[i].Id)
  447. }
  448. parentIdLen := len(classifyIdList)
  449. if parentIdLen == 0 {
  450. resp.List = list
  451. br.Data = resp
  452. br.Ret = 200
  453. br.Msg = "获取成功"
  454. return
  455. }
  456. // 先将分类列表排序
  457. services.SortClassifyListBySortAndCreateTime(list)
  458. // 接着转换结构
  459. list = services.GetClassifyListTreeRecursive(list, 0)
  460. resp.List = list
  461. br.Data = resp
  462. br.Ret = 200
  463. br.Msg = "获取成功"
  464. }