media_controller.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. reportService "eta/eta_mini_ht_api/domian/report"
  8. "eta/eta_mini_ht_api/service/media"
  9. "eta/eta_mini_ht_api/service/user"
  10. )
  11. type MediaController struct {
  12. controllers.ListController
  13. }
  14. func isLogin(listType string) bool {
  15. if listType == "logout" {
  16. logger.Info("当前用户未登录,展示部分详情")
  17. return false
  18. }
  19. if listType == "login" {
  20. return true
  21. }
  22. return false
  23. }
  24. // Search 搜索报告列表
  25. // @Description 搜索报告列表
  26. // @Success 200 {object}
  27. // @router /search [get]
  28. func (m *MediaController) Search(mediaType string, key string) {
  29. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  30. result = m.InitWrapData("分页搜索媒体列表失败")
  31. if key == "" {
  32. err = exception.New(exception.SearchKeyEmptyError)
  33. m.FailedResult("分页搜索报告列表失败", result)
  34. return
  35. }
  36. detailType := m.Data["detailType"].(string)
  37. userInfo := m.Data["user"].(user.User)
  38. pageRes := page.Page{
  39. Current: m.PageInfo.Current,
  40. PageSize: m.PageInfo.PageSize,
  41. }
  42. //获取当前可以被搜索的报告原始ID
  43. var mediaIds []int
  44. //先要限制查询的id范围
  45. pageRes.Total, pageRes.LatestId, mediaIds = media.RangeSearch(mediaType, isLogin(detailType), userInfo.Id)
  46. if len(mediaIds) == 0 {
  47. reports := new(page.PageResult)
  48. reports.Data = []reportService.ReportDTO{}
  49. reports.Page = pageRes
  50. logger.Info("没有可以查询的报告列表")
  51. m.SuccessResult("分页搜索报告列表成功", reports, result)
  52. return
  53. }
  54. if m.PageInfo.LatestId == 0 {
  55. //pageRes.Total, pageRes.LatestId = media.SearchMaxMediaId(mediaType, key)
  56. m.PageInfo.LatestId = pageRes.LatestId
  57. } else {
  58. pageRes.LatestId = m.PageInfo.LatestId
  59. pageRes.Total = m.PageInfo.Total
  60. }
  61. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  62. list, err := media.SearchMediaList(mediaType, key, mediaIds, m.PageInfo, isLogin(detailType))
  63. if err != nil {
  64. m.FailedResult("分页搜索报告列表失败", result)
  65. return
  66. }
  67. reports := new(page.PageResult)
  68. reports.Data = list
  69. reports.Page = pageRes
  70. m.SuccessResult("分页搜索报告列表成功", reports, result)
  71. return
  72. })
  73. }
  74. // List 获取媒体列表
  75. // @Description 获取媒体列表
  76. // @Success 200 {object}
  77. // @router /list [get]
  78. func (m *MediaController) List(mediaType string, permissionIds string) {
  79. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  80. result = m.InitWrapData("分页查询媒体列表失败")
  81. if mediaType == "" || !m.CheckMediaType(mediaType) {
  82. err = exception.New(exception.MediaTypeError)
  83. m.FailedResult("分页查询媒体列表失败", result)
  84. return
  85. }
  86. permissionIdList, err := m.TransPermissionIds(permissionIds)
  87. if err != nil {
  88. logger.Error("品种列表解析错误:%v", err)
  89. m.FailedResult("分页查询媒体列表失败", result)
  90. err = exception.New(exception.QueryReportPageFailed)
  91. return
  92. }
  93. pageRes := page.Page{
  94. Current: m.PageInfo.Current,
  95. PageSize: m.PageInfo.PageSize,
  96. }
  97. detailType := m.Data["detailType"].(string)
  98. userInfo := m.Data["user"].(user.User)
  99. var mediaIds []int
  100. pageRes.Total, pageRes.LatestId, mediaIds = media.GetTotalPageCountByPermissionIds(mediaType, permissionIdList, isLogin(detailType), userInfo.Id)
  101. if err != nil {
  102. logger.Error("分页查询媒体列表失败:%v", err)
  103. m.FailedResult("分页查询媒体列表失败", result)
  104. return
  105. }
  106. if m.PageInfo.LatestId == 0 {
  107. m.PageInfo.LatestId = pageRes.LatestId
  108. m.PageInfo.Total = pageRes.Total
  109. } else {
  110. pageRes.LatestId = m.PageInfo.LatestId
  111. pageRes.Total = m.PageInfo.Total
  112. }
  113. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  114. list, err := media.GetMediaPageByIds(mediaType, m.PageInfo, mediaIds, isLogin(detailType))
  115. if err != nil {
  116. m.FailedResult("分页查询媒体列表失败", result)
  117. return
  118. }
  119. mediaList := new(page.PageResult)
  120. mediaList.Data = list
  121. mediaList.Page = pageRes
  122. m.SuccessResult("分页查询媒体列表成功", mediaList, result)
  123. return
  124. })
  125. }
  126. type RecordCountReq struct {
  127. MediaId int `json:"MediaId"`
  128. TraceId string `json:"TraceId"`
  129. MediaType string `json:"MediaType"`
  130. }
  131. // Count 获取品种列表
  132. // @Description 获取最新发布的报告列表
  133. // @Success 200 {object}
  134. // @router /count [post]
  135. func (m *MediaController) Count() {
  136. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  137. result = m.InitWrapData("媒体点击记录成功")
  138. var userInfo user.User
  139. userInfo = m.Data["user"].(user.User)
  140. recordReq := new(RecordCountReq)
  141. m.GetPostParams(recordReq)
  142. if recordReq.MediaType == "" || !m.CheckMediaType(recordReq.MediaType) {
  143. err = exception.New(exception.MediaTypeError)
  144. m.FailedResult("媒体点击记录失败", result)
  145. return
  146. }
  147. record := convertToRecordCount(recordReq)
  148. record.UserId = userInfo.Id
  149. record.Mobile = userInfo.Mobile
  150. traceId, err := media.CountMedia(record)
  151. if err != nil {
  152. err = exception.New(exception.MediaRecordClickCountFailed)
  153. m.FailedResult("媒体点击记录失败", result)
  154. return
  155. }
  156. countResp := struct {
  157. TraceId string `json:"traceId"`
  158. }{TraceId: traceId}
  159. m.SuccessResult("媒体点击记录成功", countResp, result)
  160. return
  161. })
  162. }
  163. // GetMedia 获取媒体详情
  164. // @Description 获取媒体详情
  165. // @Success 200 {object}
  166. // @router /media [get]
  167. func (m *MediaController) GetMedia(mediaType string, mediaId int) {
  168. controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
  169. result = m.InitWrapData("获取媒体详情失败")
  170. if mediaType == "" || !m.CheckMediaType(mediaType) {
  171. err = exception.New(exception.MediaTypeError)
  172. m.FailedResult("获取媒体详情失败", result)
  173. return
  174. }
  175. userInfo := m.Data["user"].(user.User)
  176. detailType := m.Data["detailType"].(string)
  177. mediaDetail, err := media.GetMediaById(mediaType, mediaId, isLogin(detailType), userInfo.Id)
  178. if err != nil {
  179. m.FailedResult("获取媒体详情失败", result)
  180. return
  181. }
  182. m.SuccessResult("获取媒体详情成功", mediaDetail, result)
  183. return
  184. })
  185. }
  186. func convertToRecordCount(req *RecordCountReq) media.RecordCount {
  187. return media.RecordCount{
  188. MediaId: req.MediaId,
  189. TraceId: req.TraceId,
  190. MediaType: req.MediaType,
  191. Additional: "",
  192. }
  193. }