video.go 16 KB

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