media_controller.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package media
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. "eta/eta_mini_ht_api/controllers"
  7. "eta/eta_mini_ht_api/service/media"
  8. "eta/eta_mini_ht_api/service/user"
  9. )
  10. type MediaController struct {
  11. controllers.ListController
  12. }
  13. // Search 搜索报告列表
  14. // @Description 搜索报告列表
  15. // @Success 200 {object}
  16. // @router /search [get]
  17. func (m *MediaController) Search(mediaType string, key string) {
  18. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  19. result = m.InitWrapData("分页搜索媒体列表失败")
  20. if key == "" {
  21. err = exception.New(exception.SearchKeyEmptyError)
  22. m.FailedResult("分页搜索报告列表失败", result)
  23. return
  24. }
  25. if mediaType == "" || !m.CheckMediaType(mediaType) {
  26. err = exception.New(exception.MediaTypeError)
  27. m.FailedResult("分页查询媒体列表失败", result)
  28. return
  29. }
  30. pageRes := page.Page{
  31. Current: m.PageInfo.Current,
  32. PageSize: m.PageInfo.PageSize,
  33. }
  34. if m.PageInfo.LatestId == 0 {
  35. pageRes.Total, pageRes.LatestId = media.SearchMaxMediaId(mediaType, key)
  36. m.PageInfo.LatestId = pageRes.LatestId
  37. } else {
  38. pageRes.LatestId = m.PageInfo.LatestId
  39. pageRes.Total = m.PageInfo.Total
  40. }
  41. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  42. list, err := media.SearchMediaList(mediaType, key, m.PageInfo)
  43. if err != nil {
  44. m.FailedResult("分页搜索报告列表失败", result)
  45. return
  46. }
  47. reports := new(page.PageResult)
  48. reports.Data = list
  49. reports.Page = pageRes
  50. m.SuccessResult("分页搜索报告列表成功", reports, result)
  51. return
  52. })
  53. }
  54. // List 获取媒体列表
  55. // @Description 获取媒体列表
  56. // @Success 200 {object}
  57. // @router /list [get]
  58. func (m *MediaController) List(mediaType string, permissionIds string) {
  59. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  60. result = m.InitWrapData("分页查询媒体列表失败")
  61. if mediaType == "" || !m.CheckMediaType(mediaType) {
  62. err = exception.New(exception.MediaTypeError)
  63. m.FailedResult("分页查询媒体列表失败", result)
  64. return
  65. }
  66. permissionIdList, err := m.TransPermissionIds(permissionIds)
  67. if err != nil {
  68. logger.Error("品种列表解析错误:%v", err)
  69. m.FailedResult("分页查询媒体列表失败", result)
  70. err = exception.New(exception.QueryReportPageFailed)
  71. return
  72. }
  73. pageRes := page.Page{
  74. Current: m.PageInfo.Current,
  75. PageSize: m.PageInfo.PageSize,
  76. }
  77. var mediaIds []int
  78. if m.PageInfo.LatestId == 0 {
  79. pageRes.Total, pageRes.LatestId, mediaIds = media.GetTotalPageCountByPermissionIds(mediaType, permissionIdList)
  80. m.PageInfo.LatestId = pageRes.LatestId
  81. m.PageInfo.Total = pageRes.Total
  82. } else {
  83. pageRes.LatestId = m.PageInfo.LatestId
  84. pageRes.Total = m.PageInfo.Total
  85. }
  86. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  87. list, err := media.GetMediaPageByIds(mediaType, m.PageInfo, mediaIds)
  88. if err != nil {
  89. m.FailedResult("分页查询媒体列表失败", result)
  90. return
  91. }
  92. mediaList := new(page.PageResult)
  93. mediaList.Data = list
  94. mediaList.Page = pageRes
  95. m.SuccessResult("分页查询媒体列表成功", mediaList, result)
  96. return
  97. })
  98. }
  99. // GetMedia 获取媒体详情
  100. // @Description 获取媒体详情
  101. // @Success 200 {object}
  102. // @router /media [get]
  103. func (m *MediaController) GetMedia(mediaType string, mediaId int) {
  104. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  105. result = m.InitWrapData("获取媒体详情失败")
  106. if mediaType == "" || !m.CheckMediaType(mediaType) {
  107. err = exception.New(exception.MediaTypeError)
  108. m.FailedResult("获取媒体详情失败", result)
  109. return
  110. }
  111. mediaDetail, err := media.GetMediaById(mediaType, mediaId)
  112. if err != nil {
  113. m.FailedResult("获取媒体详情失败", result)
  114. return
  115. }
  116. m.SuccessResult("获取媒体详情成功", mediaDetail, result)
  117. return
  118. })
  119. }
  120. type RecordCountReq struct {
  121. MediaId int `json:"MediaId"`
  122. MediaType string `json:"MediaType"`
  123. }
  124. // Count 获取品种列表
  125. // @Description 获取最新发布的报告列表
  126. // @Success 200 {object}
  127. // @router /count [post]
  128. func (m *MediaController) Count() {
  129. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  130. result = m.InitWrapData("媒体点击记录成功")
  131. var userInfo user.User
  132. userInfo = m.Data["user"].(user.User)
  133. recordReq := new(RecordCountReq)
  134. m.GetPostParams(recordReq)
  135. if recordReq.MediaType == "" || !m.CheckMediaType(recordReq.MediaType) {
  136. err = exception.New(exception.MediaTypeError)
  137. m.FailedResult("媒体点击记录失败", result)
  138. return
  139. }
  140. record := convertToRecordCount(recordReq)
  141. record.UserId = userInfo.Id
  142. record.Mobile = userInfo.Mobile
  143. err = media.CountMedia(record)
  144. if err != nil {
  145. err = exception.New(exception.MediaRecordClickCountFailed)
  146. m.FailedResult("媒体点击记录失败", result)
  147. return
  148. }
  149. m.SuccessResult("媒体点击记录成功", nil, result)
  150. return
  151. })
  152. }
  153. func convertToRecordCount(req *RecordCountReq) media.RecordCount {
  154. return media.RecordCount{
  155. MediaId: req.MediaId,
  156. MediaType: req.MediaType,
  157. Additional: "",
  158. }
  159. }