product_census.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package yb
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/yb"
  8. "hongze/hz_crm_api/models/yb/response"
  9. "hongze/hz_crm_api/utils"
  10. "sync"
  11. )
  12. type ProductCensusController struct {
  13. controllers.BaseAuthController
  14. }
  15. // UserVisitCount
  16. // @Title 用户阅读统计
  17. // @Description 用户阅读统计
  18. // @Param UserId query int true "用户ID"
  19. // @Param ClickSort query int false "点击量排序:1-升序 2-降序"
  20. // @Param PageSize query int false "页码"
  21. // @param CurrentIndex query int false "每页数据量"
  22. // @Success 200 {object} response.ProductCensusUserVisitCountResp
  23. // @router /product_census/user/visit_count [get]
  24. func (this *ProductCensusController) UserVisitCount() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. userId, _ := this.GetInt("UserId")
  38. if userId <= 0 {
  39. br.Msg = "参数有误"
  40. return
  41. }
  42. clickSort, _ := this.GetInt("ClickSort")
  43. orderRule := ``
  44. if clickSort == 1 {
  45. orderRule = `visit_count ASC`
  46. }
  47. if clickSort == 2 {
  48. orderRule = `visit_count DESC`
  49. }
  50. pageSize, _ := this.GetInt("PageSize")
  51. currentIndex, _ := this.GetInt("CurrentIndex")
  52. var startSize int
  53. if pageSize <= 0 {
  54. pageSize = utils.PageSize20
  55. }
  56. if currentIndex <= 0 {
  57. currentIndex = 1
  58. }
  59. startSize = paging.StartIndex(currentIndex, pageSize)
  60. total, list, e := yb.GetProductVisitCountByUserId(userId, startSize, pageSize, orderRule)
  61. if e != nil {
  62. br.Msg = "获取失败"
  63. br.ErrMsg = "获取分产品阅读统计详情失败, Err: " + e.Error()
  64. return
  65. }
  66. page := paging.GetPaging(currentIndex, pageSize, total)
  67. resp := response.ProductCensusUserVisitCountResp{
  68. List: list,
  69. Paging: page,
  70. }
  71. br.Ret = 200
  72. br.Success = true
  73. br.Msg = "获取成功"
  74. br.Data = resp
  75. return
  76. }
  77. // UserVisitCountDetail
  78. // @Title 用户阅读统计详情
  79. // @Description 用户阅读统计详情
  80. // @Param UserId query int true "用户ID"
  81. // @Param ProductType query int true "产品类型:1-语音播报 2-视频社区 3-问答社区 99-全部"
  82. // @Param ProductId query int false "产品ID:仅语音播报传该参数"
  83. // @Param ClickSort query int false "点击量排序:1-升序 2-降序"
  84. // @Param PageSize query int false "页码"
  85. // @param CurrentIndex query int false "每页数据量"
  86. // @Success 200 {object} ybResp.CommunityQuestionUserVisitCountResp
  87. // @router /product_census/user/visit_count_detail [get]
  88. func (this *ProductCensusController) UserVisitCountDetail() {
  89. br := new(models.BaseResponse).Init()
  90. defer func() {
  91. this.Data["json"] = br
  92. this.ServeJSON()
  93. }()
  94. sysUser := this.SysUser
  95. if sysUser == nil {
  96. br.Msg = "请登录"
  97. br.ErrMsg = "请登录,SysUser is Empty"
  98. br.Ret = 408
  99. return
  100. }
  101. userId, _ := this.GetInt("UserId")
  102. if userId <= 0 {
  103. br.Msg = "参数有误"
  104. return
  105. }
  106. productType, _ := this.GetInt("ProductType")
  107. if productType <= 0 {
  108. br.Msg = "参数有误"
  109. return
  110. }
  111. productId, _ := this.GetInt("ProductId")
  112. if productType == 1 && productId <= 0 {
  113. br.Msg = "参数有误"
  114. return
  115. }
  116. clickSort, _ := this.GetInt("ClickSort")
  117. orderRule := ``
  118. if clickSort == 1 {
  119. orderRule = `visit_count ASC`
  120. }
  121. if clickSort == 2 {
  122. orderRule = `visit_count DESC`
  123. }
  124. pageSize, _ := this.GetInt("PageSize")
  125. currentIndex, _ := this.GetInt("CurrentIndex")
  126. var startSize int
  127. if pageSize <= 0 {
  128. pageSize = utils.PageSize20
  129. }
  130. if currentIndex <= 0 {
  131. currentIndex = 1
  132. }
  133. startSize = paging.StartIndex(currentIndex, pageSize)
  134. total := 0
  135. respList := make([]*response.ProductCensusUserVisitCountDetail, 0)
  136. // 语音播报
  137. if productType == 1 {
  138. t, list, e := yb.GetVoiceBroadcastVisitCountByUserId(userId, productId, startSize, pageSize, orderRule)
  139. if e != nil {
  140. br.Msg = "获取失败"
  141. br.ErrMsg = "获取语音播报用户点击量统计失败, Err: " + e.Error()
  142. return
  143. }
  144. total = t
  145. for _, v := range list {
  146. respList = append(respList, &response.ProductCensusUserVisitCountDetail{
  147. VisitCount: v.VisitCount,
  148. Title: v.BroadcastName,
  149. Source: v.NewSource,
  150. RecentTime: v.RecentTime,
  151. })
  152. }
  153. }
  154. // 视频社区
  155. if productType == 2 {
  156. t, list, e := yb.GetCommunityVideoVisitCountByUserId(userId, productId, startSize, pageSize, orderRule)
  157. if e != nil {
  158. br.Msg = "获取失败"
  159. br.ErrMsg = "获取视频社区用户点击量统计失败, Err: " + e.Error()
  160. return
  161. }
  162. total = t
  163. // 视频标题
  164. videoIds := make([]int, 0)
  165. for _, v := range list {
  166. videoIds = append(videoIds, v.CommunityVideoId)
  167. }
  168. idTitleMap := make(map[int]string, 0)
  169. if productId == 1 {
  170. videoList, tErr := yb.GetCommunityVideoListByIds(videoIds)
  171. if tErr != nil {
  172. br.Msg = "获取失败"
  173. br.ErrMsg = "获取视频社区列表失败, Err: " + tErr.Error()
  174. return
  175. }
  176. for _, v := range videoList {
  177. idTitleMap[v.CommunityVideoId] = v.Title
  178. }
  179. } else {
  180. videoList, tErr := yb.GetRoadVideoListByIds(videoIds)
  181. if tErr != nil {
  182. br.Msg = "获取失败"
  183. br.ErrMsg = "获取视频社区列表失败, Err: " + tErr.Error()
  184. return
  185. }
  186. for _, v := range videoList {
  187. idTitleMap[v.RoadVideoId] = v.Title
  188. }
  189. }
  190. for _, v := range list {
  191. respList = append(respList, &response.ProductCensusUserVisitCountDetail{
  192. VisitCount: v.VisitCount,
  193. Title: idTitleMap[v.CommunityVideoId],
  194. Source: v.NewSource,
  195. RecentTime: v.RecentTime,
  196. })
  197. }
  198. }
  199. // 问答社区
  200. if productType == 3 {
  201. t, list, e := yb.GetCommunityQuestionVisitCountByUserId(userId, startSize, pageSize, orderRule)
  202. if e != nil {
  203. br.Msg = "获取失败"
  204. br.ErrMsg = "获取问答社区用户点击量统计失败, Err: " + e.Error()
  205. return
  206. }
  207. total = t
  208. // 问题内容
  209. questionIds := make([]int, 0)
  210. for _, v := range list {
  211. questionIds = append(questionIds, v.CommunityQuestionId)
  212. }
  213. questionList, e := yb.GetQuestionListByIds(questionIds)
  214. if e != nil {
  215. br.Msg = "获取失败"
  216. br.ErrMsg = "获取问答列表失败, Err: " + e.Error()
  217. return
  218. }
  219. idTitleMap := make(map[int]string, 0)
  220. for _, v := range questionList {
  221. idTitleMap[v.CommunityQuestionId] = v.QuestionContent
  222. }
  223. for _, v := range list {
  224. respList = append(respList, &response.ProductCensusUserVisitCountDetail{
  225. VisitCount: v.VisitCount,
  226. Title: idTitleMap[v.CommunityQuestionId],
  227. Source: v.NewSource,
  228. RecentTime: v.RecentTime,
  229. })
  230. }
  231. }
  232. // 全部
  233. if productType == 99 {
  234. listTotal, list, e := yb.GetUserAllYbProductVisitCountPageList(userId, startSize, pageSize, orderRule)
  235. if e != nil {
  236. br.Msg = "获取失败"
  237. br.ErrMsg = "获取用户所有产品点击量失败, Err: " + e.Error()
  238. return
  239. }
  240. total = listTotal
  241. sectionIds := make([]int, 0)
  242. videoIds := make([]int, 0)
  243. rsVideoIds := make([]int, 0)
  244. questionIds := make([]int, 0)
  245. for _, v := range list {
  246. // 语音播报-取出板块ID
  247. if v.ProductType == 1 && v.SectionId > 0 && !utils.InArrayByInt(sectionIds, v.SectionId) {
  248. sectionIds = append(sectionIds, v.SectionId)
  249. }
  250. // 视频社区-取出视频ID
  251. if v.ProductType == 2 && v.TitleId > 0 {
  252. if v.VideoType == 1 && !utils.InArrayByInt(videoIds, v.TitleId) {
  253. videoIds = append(videoIds, v.TitleId)
  254. }
  255. if v.VideoType == 2 && !utils.InArrayByInt(rsVideoIds, v.TitleId) {
  256. rsVideoIds = append(rsVideoIds, v.TitleId)
  257. }
  258. }
  259. // 问答社区-取出问题ID
  260. if v.ProductType == 3 && v.TitleId > 0 && !utils.InArrayByInt(questionIds, v.TitleId) {
  261. questionIds = append(questionIds, v.TitleId)
  262. }
  263. }
  264. var sectionErr, videoErr, questionErr error
  265. wg := sync.WaitGroup{}
  266. wg.Add(3)
  267. // 板块名称map
  268. sectionNameMap := make(map[int]string, 0)
  269. go func() {
  270. defer func() {
  271. wg.Done()
  272. }()
  273. sectionList, e := yb.GetVoiceSectionByIds(sectionIds)
  274. if e != nil {
  275. sectionErr = fmt.Errorf("获取语音播报板块列表失败, Err: %s", e.Error())
  276. return
  277. }
  278. for _, v := range sectionList {
  279. sectionNameMap[v.SectionId] = v.SectionName
  280. }
  281. }()
  282. // 视频社区标题map
  283. videoTitleMap := make(map[int]string, 0)
  284. rsVideoTitleMap := make(map[int]string, 0)
  285. go func() {
  286. defer func() {
  287. wg.Done()
  288. }()
  289. videoList, e := yb.GetCommunityVideoListByIds(videoIds)
  290. if e != nil {
  291. videoErr = fmt.Errorf("获取视频社区列表失败, Err: %s", e.Error())
  292. return
  293. }
  294. for _, v := range videoList {
  295. videoTitleMap[v.CommunityVideoId] = v.Title
  296. }
  297. rsVideoList, e := yb.GetRoadVideoListByIds(rsVideoIds)
  298. if e != nil {
  299. videoErr = fmt.Errorf("获取路演视频列表失败, Err: %s", e.Error())
  300. return
  301. }
  302. for _, v := range rsVideoList {
  303. rsVideoTitleMap[v.RoadVideoId] = v.Title
  304. }
  305. }()
  306. // 问答社区标题map
  307. questionTitleMap := make(map[int]string, 0)
  308. go func() {
  309. defer func() {
  310. wg.Done()
  311. }()
  312. questionList, e := yb.GetQuestionListByIds(questionIds)
  313. if e != nil {
  314. questionErr = fmt.Errorf("获取问答列表失败, Err: %s", e.Error())
  315. return
  316. }
  317. for _, v := range questionList {
  318. questionTitleMap[v.CommunityQuestionId] = v.QuestionContent
  319. }
  320. }()
  321. wg.Wait()
  322. if sectionErr != nil {
  323. br.Msg = "获取失败"
  324. br.ErrMsg = sectionErr.Error()
  325. return
  326. }
  327. if videoErr != nil {
  328. br.Msg = "获取失败"
  329. br.ErrMsg = videoErr.Error()
  330. return
  331. }
  332. if questionErr != nil {
  333. br.Msg = "获取失败"
  334. br.ErrMsg = questionErr.Error()
  335. return
  336. }
  337. // 填充产品名/标题
  338. for _, v := range list {
  339. d := new(response.ProductCensusUserVisitCountDetail)
  340. d.VisitCount = v.VisitCount
  341. d.Source = v.NewSource
  342. d.RecentTime = v.RecentTime
  343. if v.ProductType == 1 {
  344. d.ProductName = sectionNameMap[v.SectionId]
  345. d.Title = v.Title
  346. }
  347. if v.ProductType == 2 {
  348. if v.VideoType == 1 {
  349. d.ProductName = "5分钟小视频"
  350. d.Title = videoTitleMap[v.TitleId]
  351. }
  352. if v.VideoType == 2 {
  353. d.ProductName = "线上路演"
  354. d.Title = rsVideoTitleMap[v.TitleId]
  355. }
  356. }
  357. if v.ProductType == 3 {
  358. d.ProductName = "问答社区"
  359. d.Title = questionTitleMap[v.TitleId]
  360. }
  361. respList = append(respList, d)
  362. }
  363. }
  364. page := paging.GetPaging(currentIndex, pageSize, total)
  365. resp := response.ProductCensusUserVisitCountDetailResp{
  366. List: respList,
  367. Paging: page,
  368. }
  369. br.Ret = 200
  370. br.Success = true
  371. br.Msg = "获取成功"
  372. br.Data = resp
  373. return
  374. }