video.go 17 KB

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