video.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package community
  2. import (
  3. "errors"
  4. response2 "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/models/response"
  6. "hongze/hongze_yb/models/tables/chart_permission"
  7. "hongze/hongze_yb/models/tables/company_product"
  8. "hongze/hongze_yb/models/tables/yb_community_question_comment"
  9. "hongze/hongze_yb/models/tables/yb_community_question_like_tease"
  10. "hongze/hongze_yb/models/tables/yb_community_video"
  11. "hongze/hongze_yb/models/tables/yb_community_video_play_log"
  12. "hongze/hongze_yb/models/tables/yb_road_video"
  13. "hongze/hongze_yb/services/collection"
  14. "hongze/hongze_yb/services/company"
  15. "hongze/hongze_yb/services/user"
  16. "hongze/hongze_yb/utils"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // GetVideoList 获取视频列表
  22. func GetVideoList(userId, pageIndex, pageSize, videoId, varietyTagId int, keywords string) (resp []*response.CommunityVideoItem, err error) {
  23. condition := make(map[string]interface{})
  24. // 分享点进来的直接定位到具体视频
  25. if videoId > 0 {
  26. condition["community_video_id ="] = videoId
  27. } else {
  28. if varietyTagId > 0 {
  29. condition["variety_tag_id ="] = varietyTagId
  30. }
  31. if keywords != "" {
  32. condition["title like"] = "%" + keywords + "%"
  33. }
  34. }
  35. resp = make([]*response.CommunityVideoItem, 0)
  36. list, e := yb_community_video.GetPageListByCondition(condition, pageIndex, pageSize)
  37. if e != nil {
  38. err = errors.New("获取视频列表失败, Err:" + e.Error())
  39. return
  40. }
  41. if len(list) <= 0 {
  42. return
  43. }
  44. videoIds := make([]int, 0)
  45. for _, v := range list {
  46. item := &response.CommunityVideoItem{
  47. CommunityVideoID: v.CommunityVideoID,
  48. Title: v.Title,
  49. VarietyTagId: v.VarietyTagId,
  50. VarietyTagName: v.VarietyTagName,
  51. CoverImgUrl: v.CoverImgURL,
  52. VideoUrl: v.VideoURL,
  53. VideoSeconds: v.VideoSeconds,
  54. PublishState: v.PublishState,
  55. PublishTime: v.PublishTime.Format(utils.FormatDateTime),
  56. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  57. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  58. ChartPermissionName: v.VarietyTagName,
  59. TencentId: getSubTencentUrl(v.TencentURL),
  60. }
  61. resp = append(resp, item)
  62. videoIds = append(videoIds, v.CommunityVideoID)
  63. }
  64. // 收藏
  65. collectMap, e := collection.GetUserCollectByList(userId, collection.CollectionTypeVideo, videoIds)
  66. if e != nil {
  67. err = e
  68. return
  69. }
  70. for i := range resp {
  71. resp[i].CollectionId = collectMap[resp[i].CommunityVideoID]
  72. }
  73. return
  74. }
  75. // SaveVideoPlayLog 记录用户播放视频日志
  76. func SaveVideoPlayLog(userInfo user.UserInfo, videoId, sourceAgent int, videoType int8) (errMsg string, err error) {
  77. if videoType == 1 {
  78. _, e := yb_community_video.GetItemById(videoId)
  79. if e != nil {
  80. errMsg = "视频不存在或未发布"
  81. err = errors.New("获取视频信息失败, Err: " + e.Error())
  82. return
  83. }
  84. } else {
  85. _, e := yb_road_video.GetItemById(videoId)
  86. if e != nil {
  87. errMsg = "视频不存在或未发布"
  88. err = errors.New("获取视频信息失败, Err: " + e.Error())
  89. return
  90. }
  91. }
  92. companyInfo, e := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  93. if e != nil && e != utils.ErrNoRow {
  94. errMsg = "保存失败"
  95. err = errors.New("获取客户信息失败, Err: " + e.Error())
  96. return
  97. }
  98. companyName := "潜在客户"
  99. companyStatus := "潜在"
  100. sellerId := 0
  101. if companyInfo != nil {
  102. if companyInfo.CompanyID > 0 {
  103. companyName = companyInfo.CompanyName
  104. companyStatus = companyInfo.Status
  105. }
  106. sellerId = companyInfo.SellerID
  107. }
  108. item := &yb_community_video_play_log.YbCommunityVideoPlayLog{
  109. CommunityVideoID: videoId,
  110. UserID: int(userInfo.UserID),
  111. Mobile: userInfo.Mobile,
  112. RealName: userInfo.RealName,
  113. NickName: userInfo.NickName,
  114. CompanyID: int(userInfo.CompanyID),
  115. CompanyName: companyName,
  116. CompanyStatus: companyStatus,
  117. SourceAgent: sourceAgent,
  118. SellerID: sellerId,
  119. Type: videoType,
  120. CreateTime: time.Now().Local(),
  121. }
  122. if e = item.Create(); e != nil {
  123. errMsg = "操作失败"
  124. err = errors.New("新增播放视频日志失败, Err:" + e.Error())
  125. return
  126. }
  127. return
  128. }
  129. // getSubTencentUrl 获取腾讯视频链接子字符串
  130. func getSubTencentUrl(tencentUrl string) (sub string) {
  131. if tencentUrl != "" {
  132. st := strings.LastIndex(tencentUrl, "/")
  133. ed := strings.LastIndex(tencentUrl, ".")
  134. if st > 0 && ed > st {
  135. sub = tencentUrl[st+1 : ed]
  136. }
  137. }
  138. return
  139. }
  140. // GetRoadVideoList 获取线上路演视频列表
  141. func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, chartPermissionId int, keywords string) (resp response.RoadVideoItemResp, err error, code int) {
  142. list := make([]*response.RoadVideoItem, 0)
  143. //获取有权限的permissionID
  144. validPermissionList, err := company.GetValidPermissionByCompany2ProductId(userInfo.CompanyID, 1)
  145. if err != nil {
  146. return
  147. }
  148. ficcPermissionList, err := chart_permission.GetFiccListExceptTacticByProductId()
  149. if err != nil {
  150. return
  151. }
  152. permissionIds := ""
  153. validPermissionMap := make(map[string]struct{})
  154. ParentPermissionNameMap := make(map[string]string)
  155. ParentPermissionChildMap := make(map[string]int)
  156. for _, v := range validPermissionList {
  157. permissionIds += "'" + strconv.Itoa(v.ChartPermissionID) + "'|"
  158. validPermissionMap["'"+strconv.Itoa(v.ChartPermissionID)+"'"] = struct{}{}
  159. }
  160. for _, v := range ficcPermissionList {
  161. ParentPermissionNameMap["'"+strconv.Itoa(int(v.ChartPermissionID))+"'"] = v.ClassifyName
  162. ParentPermissionChildMap[v.ClassifyName] += 1
  163. }
  164. if permissionIds == "" {
  165. resp.List = list
  166. resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
  167. return
  168. }
  169. permissionIds = strings.Trim(permissionIds, "|")
  170. condition := `is_deleted = 0 AND publish_state = 1 and chart_permission_ids REGEXP "(` + permissionIds + `)"`
  171. var par []interface{}
  172. // 分享点进来的直接定位到具体视频
  173. var videoList []*yb_road_video.YbRoadVideo
  174. var total int64
  175. if videoId > 0 {
  176. videoInfo, e := yb_road_video.GetItemById(videoId)
  177. if e != nil {
  178. if e != utils.ErrNoRow {
  179. err = errors.New("获取视频信息失败, Err:" + e.Error())
  180. return
  181. }
  182. }
  183. if videoInfo != nil && videoInfo.RoadVideoID > 0 {
  184. if videoInfo.IsDeleted != 0 || videoInfo.PublishState != 1 {
  185. resp.List = list
  186. resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
  187. return
  188. }
  189. permissionIdsSlice := strings.Split(permissionIds, "|")
  190. videoPermissionIdsSlice := strings.Split(videoInfo.ChartPermissionIds, ",")
  191. hasPermission := false
  192. for _, v1 := range permissionIdsSlice {
  193. for _, v2 := range videoPermissionIdsSlice {
  194. if v1 == v2 {
  195. hasPermission = true
  196. break
  197. }
  198. }
  199. }
  200. if !hasPermission { //无权限
  201. code = response2.SPECIFIC_FAIL_CODE
  202. err = errors.New("无查看该视频的权限")
  203. return
  204. }
  205. videoList = append(videoList, videoInfo)
  206. total = 1
  207. }
  208. } else {
  209. if chartPermissionId > 0 {
  210. condition += ` and FIND_IN_SET("'` + strconv.Itoa(chartPermissionId) + `'", chart_permission_ids)`
  211. }
  212. if keywords != "" {
  213. condition += " and title like ? "
  214. par = append(par, "%"+keywords+"%")
  215. }
  216. videoList, total, err = yb_road_video.GetPageListByCondition(condition, par, pageIndex, pageSize)
  217. if err != nil {
  218. err = errors.New("获取视频列表失败, Err:" + err.Error())
  219. return
  220. }
  221. }
  222. if len(videoList) <= 0 {
  223. resp.List = list
  224. resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
  225. return
  226. }
  227. chartPermissionIdSlice := make([]int, 0)
  228. chartNameMap := make(map[string]string)
  229. for _, v := range videoList {
  230. tmp := strings.Split(v.ChartPermissionIds, ",")
  231. for _, t1 := range tmp {
  232. i, _ := strconv.Atoi(strings.Trim(t1, "'"))
  233. chartPermissionIdSlice = append(chartPermissionIdSlice, i)
  234. }
  235. }
  236. if len(chartPermissionIdSlice) > 0 {
  237. chartList, e := chart_permission.GetListByIds(chartPermissionIdSlice)
  238. if e != nil {
  239. err = errors.New("获取品种信息失败, Err:" + e.Error())
  240. return
  241. }
  242. for _, v := range chartList {
  243. chartNameMap["'"+strconv.Itoa(int(v.ChartPermissionID))+"'"] = v.PermissionName
  244. }
  245. }
  246. var chartPermissionNames string
  247. videoIds := make([]int, 0)
  248. for _, v := range videoList {
  249. chartPermissionNames = ""
  250. itemParentPermissionNum := make(map[string]int)
  251. tmpSlice := strings.Split(v.ChartPermissionIds, ",")
  252. if len(tmpSlice) > 0 {
  253. // 拼接一级标签
  254. for _, cid := range tmpSlice {
  255. if p, ok := ParentPermissionNameMap[cid]; ok {
  256. if _, ok1 := itemParentPermissionNum[p]; !ok1 {
  257. itemParentPermissionNum[p] = 0
  258. chartPermissionNames += p + ","
  259. }
  260. }
  261. }
  262. // 查询可显示的所有品种是否包含所有一级品种的二级品种
  263. for _, cid := range tmpSlice {
  264. if _, ok := chartNameMap[cid]; ok {
  265. if _, ok1 := validPermissionMap[cid]; ok1 {
  266. itemParentPermissionNum[ParentPermissionNameMap[cid]] += 1
  267. }
  268. }
  269. }
  270. for _, cid := range tmpSlice {
  271. if n, ok := ParentPermissionChildMap[ParentPermissionNameMap[cid]]; ok && n != itemParentPermissionNum[ParentPermissionNameMap[cid]] {
  272. chartPermissionNames = ""
  273. break
  274. }
  275. }
  276. if chartPermissionNames == "" {
  277. for _, cid := range tmpSlice {
  278. if name, ok := chartNameMap[cid]; ok {
  279. if _, ok1 := validPermissionMap[cid]; ok1 {
  280. chartPermissionNames += name + ","
  281. }
  282. }
  283. }
  284. }
  285. if chartPermissionNames != "" {
  286. chartPermissionNames = strings.Trim(chartPermissionNames, ",")
  287. }
  288. }
  289. item := &response.RoadVideoItem{
  290. RoadVideoID: v.RoadVideoID,
  291. Title: v.Title,
  292. ChartPermissionIds: v.ChartPermissionIds,
  293. ChartPermissionName: chartPermissionNames,
  294. CoverImgUrl: v.CoverImgURL,
  295. VideoUrl: v.VideoURL,
  296. VideoSeconds: v.VideoSeconds,
  297. PublishState: v.PublishState,
  298. AdminId: v.AdminId,
  299. AdminRealName: v.AdminRealName,
  300. PublishTime: v.PublishTime.Format(utils.FormatDateTime),
  301. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  302. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  303. }
  304. list = append(list, item)
  305. videoIds = append(videoIds, item.RoadVideoID)
  306. }
  307. // 收藏
  308. collectMap, e := collection.GetUserCollectByList(int(userInfo.UserID), collection.CollectionTypeRoadVideo, videoIds)
  309. if e != nil {
  310. err = e
  311. return
  312. }
  313. for i := range list {
  314. list[i].CollectionId = collectMap[list[i].RoadVideoID]
  315. }
  316. resp.List = list
  317. resp.Paging = response.GetPaging(pageIndex, pageSize, int(total))
  318. return
  319. }
  320. // HandleLikeOrTeaseByCommunityVideoItemList 视频社区 点赞/吐槽 数据
  321. func HandleLikeOrTeaseByCommunityVideoItemList(userId uint64, videoList []*response.CommunityVideoItem) (err error) {
  322. listLen := len(videoList)
  323. if listLen == 0 {
  324. return
  325. }
  326. idArr := make([]uint32, 0)
  327. for i := 0; i < listLen; i++ {
  328. idArr = append(idArr, uint32(videoList[i].CommunityVideoID))
  329. }
  330. // 注:此处视频社区CommunityVideoID在点赞吐槽表中为CommunityQuestionID, 以source区分主键
  331. // 获取点赞和吐槽数据
  332. ybCommunityQuestionLikeTeaseMap := make(map[uint32]*yb_community_question_like_tease.YbCommunityQuestionLikeTease)
  333. ybCommunityQuestionLikeTeaseList, err := yb_community_question_like_tease.GetByUserIdAndCommunityQuestionIds(userId, idArr, yb_community_question_like_tease.SourceVideo)
  334. if err != nil {
  335. return
  336. }
  337. for _, v := range ybCommunityQuestionLikeTeaseList {
  338. ybCommunityQuestionLikeTeaseMap[v.CommunityQuestionID] = v
  339. }
  340. // 获取点赞和吐槽汇总数
  341. likeMap := make(map[uint32]int)
  342. teaseMap := make(map[uint32]int)
  343. likeList, err := yb_community_question_like_tease.GetLikeNumCommentByCommunityQuestionIds(idArr, yb_community_question_like_tease.SourceVideo)
  344. if err != nil {
  345. return
  346. }
  347. for _, v := range likeList {
  348. likeMap[v.CommunityQuestionID] = v.Total
  349. }
  350. teaseList, err := yb_community_question_like_tease.GetTeaseNumCommentByCommunityQuestionIds(idArr, yb_community_question_like_tease.SourceVideo)
  351. if err != nil {
  352. return
  353. }
  354. for _, v := range teaseList {
  355. teaseMap[v.CommunityQuestionID] = v.Total
  356. }
  357. for _, v := range videoList {
  358. if tmpTotal, ok := likeMap[uint32(v.CommunityVideoID)]; ok {
  359. v.LikeTotal = tmpTotal
  360. }
  361. if tmpTotal, ok := teaseMap[uint32(v.CommunityVideoID)]; ok {
  362. v.TeaseTotal = tmpTotal
  363. }
  364. if ybCommunityQuestionLikeTease, ok := ybCommunityQuestionLikeTeaseMap[uint32(v.CommunityVideoID)]; ok {
  365. //类型. 1-点赞 2-吐槽
  366. v.OpType = ybCommunityQuestionLikeTease.OpType
  367. }
  368. }
  369. return
  370. }
  371. // HandleCommentByCommunityVideoItemList 视频 评论 数据
  372. func HandleCommentByCommunityVideoItemList(questionList []*response.CommunityVideoItem) (err error) {
  373. listLen := len(questionList)
  374. if listLen == 0 {
  375. return
  376. }
  377. idArr := make([]uint32, 0)
  378. // 注:此处视频社区CommunityVideoID在评论表中为CommunityQuestionID, 以source区分主键
  379. // 问题ID-精选评论列表
  380. questionIdCommentsMap := make(map[uint32][]*response.CommunityQuestionCommentListItem, 0)
  381. for i := 0; i < listLen; i++ {
  382. idArr = append(idArr, uint32(questionList[i].CommunityVideoID))
  383. questionIdCommentsMap[uint32(questionList[i].CommunityVideoID)] = make([]*response.CommunityQuestionCommentListItem, 0)
  384. }
  385. // 精选评论数据
  386. hotList, err := yb_community_question_comment.GetHotListByCommunityQuestionIds(idArr, yb_community_question_comment.SourceVideo)
  387. if err != nil {
  388. return
  389. }
  390. for _, v := range hotList {
  391. questionIdCommentsMap[v.CommunityQuestionID] = append(questionIdCommentsMap[v.CommunityQuestionID], &response.CommunityQuestionCommentListItem{
  392. QaAvatarUrl: v.QaAvatarUrl,
  393. Comment: v.Content,
  394. })
  395. }
  396. for _, v := range questionList {
  397. comments := questionIdCommentsMap[uint32(v.CommunityVideoID)]
  398. v.CommentTotal = len(comments)
  399. v.CommentList = comments
  400. }
  401. return
  402. }