video.go 13 KB

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