media_controller.go 7.0 KB

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