report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 && err.Error() != utils.ErrNoRow() {
  148. br.Msg = "操作失败"
  149. br.ErrMsg = "获取资源库版头失败, Err: " + err.Error()
  150. return
  151. }
  152. if headResource != nil && headResource.ResourceId > 0 {
  153. item.HeadImg = headResource.ImgUrl
  154. item.HeadStyle = headResource.Style
  155. }
  156. }
  157. if item.EndResourceId > 0 {
  158. endResource, err := models.GetResourceItemById(item.EndResourceId)
  159. if err != nil && err.Error() != utils.ErrNoRow() {
  160. br.Msg = "操作失败"
  161. br.ErrMsg = "获取资源库版尾失败, Err: " + err.Error()
  162. return
  163. }
  164. if endResource != nil && endResource.ResourceId > 0 {
  165. item.EndImg = endResource.ImgUrl
  166. item.EndStyle = endResource.Style
  167. }
  168. }
  169. resp := models.FormatReport2Item(item, chapterList)
  170. br.Data = resp
  171. br.Ret = 200
  172. br.Msg = "获取成功"
  173. }
  174. // Approve
  175. // @Title 审批
  176. // @Description 报告审批
  177. // @Param request body models.PublishReq true "type json string"
  178. // @Success 200 Ret=200 发布成功
  179. // @router /approve [post]
  180. func (this *ReportController) Approve() {
  181. br := new(models.BaseResponse).Init()
  182. defer func() {
  183. this.Data["json"] = br
  184. this.ServeJSON()
  185. }()
  186. var req models.ReportApproveReq
  187. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  188. if err != nil {
  189. br.Msg = "参数解析异常!"
  190. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  191. return
  192. }
  193. if req.ReportId <= 0 {
  194. br.Msg = "参数错误"
  195. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  196. return
  197. }
  198. item, e := models.GetReportById(req.ReportId)
  199. if e != nil {
  200. if e.Error() == utils.ErrNoRow() {
  201. br.Msg = "报告已被删除"
  202. return
  203. }
  204. br.Msg = "操作失败"
  205. br.ErrMsg = "获取报告信息失败, Err:" + e.Error()
  206. return
  207. }
  208. if item.State != models.ReportStateWaitApprove {
  209. br.Msg = "报告状态有误"
  210. br.ErrMsg = fmt.Sprintf("报告状态有误, State: %d", item.State)
  211. return
  212. }
  213. if item.HasChapter != 1 && item.Content == "" {
  214. br.Msg = "报告内容为空,请检查内容"
  215. return
  216. }
  217. // 校验审批配置
  218. confMap, e := models.GetBusinessConf()
  219. if e != nil {
  220. br.Msg = "操作失败"
  221. br.ErrMsg = "获取审批配置失败, Err: " + e.Error()
  222. return
  223. }
  224. if confMap[models.BusinessConfIsReportApprove] != "true" {
  225. br.Msg = "未开启审批, 请开启后重新操作"
  226. return
  227. }
  228. if confMap[models.BusinessConfReportApproveType] != models.BusinessConfReportApproveTypeOther {
  229. br.Msg = "未开启第三方审批, 请开启后重新操作"
  230. return
  231. }
  232. stateMap := map[bool]int{false: models.ReportStateRefused, true: models.ReportStatePass}
  233. // 驳回
  234. now := time.Now().Local()
  235. if !req.Pass {
  236. cols := []string{"State", "ModifyTime", "ApproveTime"}
  237. item.State = stateMap[req.Pass]
  238. item.ModifyTime = now
  239. item.ApproveTime = now
  240. if e = item.UpdateReport(cols); e != nil {
  241. br.Msg = "审批失败"
  242. br.ErrMsg = "报告审批驳回失败, Err: " + e.Error()
  243. return
  244. }
  245. }
  246. // 通过审批
  247. if req.Pass {
  248. cols := []string{"State", "ModifyTime", "ApproveTime", "PublishTime"}
  249. item.State = stateMap[req.Pass]
  250. item.ModifyTime = now
  251. item.ApproveTime = now
  252. item.PublishTime = now
  253. if e = item.UpdateReport(cols); e != nil {
  254. br.Msg = "审批失败"
  255. br.ErrMsg = "报告审批通过失败, Err: " + e.Error()
  256. return
  257. }
  258. // 更新报告Es
  259. go func() {
  260. _ = services.UpdateReportEs(req.ReportId, models.ReportStatePublished) // 注:状态用已发布的, 兼容以前的代码
  261. }()
  262. }
  263. // 记录报告变更状态
  264. go func() {
  265. recordItem := &models.ReportStateRecord{
  266. ReportId: req.ReportId,
  267. ReportType: models.ReportTypeChinese,
  268. State: item.State,
  269. CreateTime: time.Now(),
  270. }
  271. _, _ = models.AddReportStateRecord(recordItem)
  272. }()
  273. br.Ret = 200
  274. br.Msg = "审批成功"
  275. }
  276. // @Title 获取分类列表
  277. // @Description 获取分类列表
  278. // @Param PageSize query int true "每页数据条数"
  279. // @Param CurrentIndex query int true "当前页页码,从1开始"
  280. // @Param KeyWord query string true "检索关键词"
  281. // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
  282. // @Param HideDayWeek query int false "是否隐藏晨周报"
  283. // @Success 200 {object} models.Classify
  284. // @router /classify/list/bak [get]
  285. func (this *ReportController) ListClassify() {
  286. br := new(models.BaseResponse).Init()
  287. defer func() {
  288. this.Data["json"] = br
  289. this.ServeJSON()
  290. }()
  291. pageSize, _ := this.GetInt("PageSize")
  292. currentIndex, _ := this.GetInt("CurrentIndex")
  293. keyWord := this.GetString("KeyWord")
  294. //companyType := this.GetString("CompanyType")
  295. companyType := ""
  296. br.Msg = "该接口已启用,请调用V2接口"
  297. return
  298. var startSize int
  299. if pageSize <= 0 {
  300. pageSize = utils.PageSize20
  301. }
  302. if currentIndex <= 0 {
  303. currentIndex = 1
  304. }
  305. startSize = utils.StartIndex(currentIndex, pageSize)
  306. list, err := models.GetClassifyList(startSize, pageSize, keyWord, companyType)
  307. if err != nil {
  308. br.Msg = "获取失败"
  309. br.ErrMsg = "获取失败,Err:" + err.Error()
  310. return
  311. }
  312. total, err := models.GetClassifyListCount(keyWord, companyType)
  313. if err != nil {
  314. br.Msg = "获取失败"
  315. br.ErrMsg = "获取失败,Err:" + err.Error()
  316. return
  317. }
  318. parentIds := make([]int, 0)
  319. for i := range list {
  320. parentIds = append(parentIds, list[i].Id)
  321. }
  322. parentIdLen := len(parentIds)
  323. if parentIdLen == 0 {
  324. resp := &models.ClassifyListResp{
  325. List: list,
  326. Paging: paging.GetPaging(currentIndex, pageSize, 0),
  327. }
  328. br.Data = resp
  329. br.Ret = 200
  330. br.Msg = "获取成功"
  331. return
  332. }
  333. // 获取一级分类-子目录列表
  334. menuListMap := make(map[int][]*models.ClassifyMenu, 0)
  335. var menuCond string
  336. var menuPars []interface{}
  337. menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
  338. menuPars = append(menuPars, parentIds)
  339. parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
  340. if e != nil {
  341. br.Msg = "获取失败"
  342. br.ErrMsg = "获取一级分类子目录列表失败"
  343. return
  344. }
  345. for i := range parentMenus {
  346. if menuListMap[parentMenus[i].ClassifyId] == nil {
  347. menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
  348. }
  349. menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
  350. }
  351. // 获取子分类
  352. children, e := models.GetClassifyChildByParentIds(parentIds, keyWord)
  353. if e != nil {
  354. br.Msg = "获取失败"
  355. br.ErrMsg = "获取子分类失败"
  356. return
  357. }
  358. childrenIds := make([]int, 0)
  359. for i := range children {
  360. childrenIds = append(childrenIds, children[i].Id)
  361. }
  362. childrenIdsLen := len(childrenIds)
  363. // 获取二级分类-子目录关联
  364. relateMap := make(map[int]int, 0)
  365. if childrenIdsLen > 0 {
  366. var relateCond string
  367. var relatePars []interface{}
  368. relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(childrenIdsLen) + `)`
  369. relatePars = append(relatePars, childrenIds)
  370. relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
  371. if e != nil {
  372. br.Msg = "获取失败"
  373. br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
  374. return
  375. }
  376. for i := range relates {
  377. relateMap[relates[i].ClassifyId] = relates[i].MenuId
  378. }
  379. }
  380. // 二级分类
  381. childrenMap := make(map[int][]*models.ClassifyItem, 0)
  382. for i := range children {
  383. if childrenMap[children[i].ParentId] == nil {
  384. childrenMap[children[i].ParentId] = make([]*models.ClassifyItem, 0)
  385. }
  386. childrenMap[children[i].ParentId] = append(childrenMap[children[i].ParentId], &models.ClassifyItem{
  387. Classify: *children[i],
  388. //ClassifyMenuId: relateMap[children[i].Id],
  389. })
  390. }
  391. // 一级分类
  392. for i := range list {
  393. //list[i].ClassifyMenuList = menuListMap[list[i].Id]
  394. list[i].Child = childrenMap[list[i].Id]
  395. }
  396. page := paging.GetPaging(currentIndex, pageSize, total)
  397. resp := new(models.ClassifyListResp)
  398. resp.List = list
  399. resp.Paging = page
  400. br.Data = resp
  401. br.Ret = 200
  402. br.Msg = "获取成功"
  403. }
  404. // ListClassifyV2
  405. // @Title 获取所有分类列表(2024-6-26 13:49:03)
  406. // @Description 获取所有分类列表(2024-6-26 13:49:03)
  407. // @Param KeyWord query string true "检索关键词"
  408. // @Success 200 {object} models.ClassifyListV2Resp
  409. // @router /classify/list [get]
  410. func (this *ReportController) ListClassifyV2() {
  411. br := new(models.BaseResponse).Init()
  412. defer func() {
  413. this.Data["json"] = br
  414. this.ServeJSON()
  415. }()
  416. resp := new(models.ClassifyListV2Resp)
  417. keyWord := this.GetString("KeyWord")
  418. list, err := models.GetClassifyListByKeywordV2(keyWord, -1)
  419. if err != nil {
  420. br.Msg = "获取失败"
  421. br.ErrMsg = "获取失败,Err:" + err.Error()
  422. return
  423. }
  424. if keyWord != `` {
  425. idMap := make(map[int]bool)
  426. currParentClassifyIdList := make([]int, 0)
  427. for _, v := range list {
  428. idMap[v.Id] = true
  429. if v.ParentId > 0 {
  430. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  431. }
  432. }
  433. findList := list
  434. list = make([]*models.ClassifyListV2, 0)
  435. tmpList, tmpErr := services.GetParentClassifyListByParentIdList(currParentClassifyIdList)
  436. if tmpErr != nil {
  437. br.Msg = "获取失败"
  438. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  439. return
  440. }
  441. for _, v := range tmpList {
  442. if _, ok := idMap[v.Id]; !ok {
  443. list = append(list, v)
  444. }
  445. }
  446. list = append(list, findList...)
  447. }
  448. classifyIdList := make([]int, 0)
  449. for i := range list {
  450. classifyIdList = append(classifyIdList, list[i].Id)
  451. }
  452. parentIdLen := len(classifyIdList)
  453. if parentIdLen == 0 {
  454. resp.List = list
  455. br.Data = resp
  456. br.Ret = 200
  457. br.Msg = "获取成功"
  458. return
  459. }
  460. // 先将分类列表排序
  461. services.SortClassifyListBySortAndCreateTime(list)
  462. // 接着转换结构
  463. list = services.GetClassifyListTreeRecursive(list, 0)
  464. resp.List = list
  465. br.Data = resp
  466. br.Ret = 200
  467. br.Msg = "获取成功"
  468. }