video.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package community
  2. import (
  3. "errors"
  4. "hongze/hongze_yb/models/response"
  5. "hongze/hongze_yb/models/tables/chart_permission"
  6. "hongze/hongze_yb/models/tables/company_product"
  7. "hongze/hongze_yb/models/tables/yb_community_video"
  8. "hongze/hongze_yb/models/tables/yb_community_video_play_log"
  9. "hongze/hongze_yb/models/tables/yb_road_video"
  10. "hongze/hongze_yb/services/company"
  11. "hongze/hongze_yb/services/user"
  12. "hongze/hongze_yb/utils"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // GetVideoList 获取视频列表
  18. func GetVideoList(pageIndex, pageSize, videoId, varietyTagId int, keywords string) (resp []*response.CommunityVideoItem, err error) {
  19. condition := make(map[string]interface{})
  20. // 分享点进来的直接定位到具体视频
  21. if videoId > 0 {
  22. condition["community_video_id ="] = videoId
  23. } else {
  24. if varietyTagId > 0 {
  25. condition["variety_tag_id ="] = varietyTagId
  26. }
  27. if keywords != "" {
  28. condition["title like"] = "%" + keywords + "%"
  29. }
  30. }
  31. resp = make([]*response.CommunityVideoItem, 0)
  32. list, e := yb_community_video.GetPageListByCondition(condition, pageIndex, pageSize)
  33. if e != nil {
  34. err = errors.New("获取视频列表失败, Err:" + e.Error())
  35. return
  36. }
  37. if len(list) <= 0 {
  38. return
  39. }
  40. for _, v := range list {
  41. item := &response.CommunityVideoItem{
  42. CommunityVideoID: v.CommunityVideoID,
  43. Title: v.Title,
  44. VarietyTagId: v.VarietyTagId,
  45. VarietyTagName: v.VarietyTagName,
  46. CoverImgUrl: v.CoverImgURL,
  47. VideoUrl: v.VideoURL,
  48. VideoSeconds: v.VideoSeconds,
  49. PublishState: v.PublishState,
  50. PublishTime: v.PublishTime.Format(utils.FormatDateTime),
  51. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  52. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  53. ChartPermissionName: v.VarietyTagName,
  54. TencentId: getSubTencentUrl(v.TencentURL),
  55. }
  56. resp = append(resp, item)
  57. }
  58. return
  59. }
  60. // SaveVideoPlayLog 记录用户播放视频日志
  61. func SaveVideoPlayLog(userInfo user.UserInfo, videoId, sourceAgent int, videoType int8) (errMsg string, err error) {
  62. if videoType == 1 {
  63. _, e := yb_community_video.GetItemById(videoId)
  64. if e != nil {
  65. errMsg = "视频不存在或未发布"
  66. err = errors.New("获取视频信息失败, Err: " + e.Error())
  67. return
  68. }
  69. }else{
  70. _, e := yb_road_video.GetItemById(videoId)
  71. if e != nil {
  72. errMsg = "视频不存在或未发布"
  73. err = errors.New("获取视频信息失败, Err: " + e.Error())
  74. return
  75. }
  76. }
  77. companyInfo, e := company_product.GetByCompany2ProductId(userInfo.CompanyID, 1)
  78. if e != nil && e != utils.ErrNoRow {
  79. errMsg = "保存失败"
  80. err = errors.New("获取客户信息失败, Err: " + e.Error())
  81. return
  82. }
  83. companyName := "潜在客户"
  84. companyStatus := "潜在"
  85. sellerId := 0
  86. if companyInfo != nil && companyInfo.CompanyID > 0 {
  87. companyName = companyInfo.CompanyName
  88. companyStatus = companyInfo.Status
  89. sellerId = companyInfo.SellerID
  90. }
  91. item := &yb_community_video_play_log.YbCommunityVideoPlayLog{
  92. CommunityVideoID: videoId,
  93. UserID: int(userInfo.UserID),
  94. Mobile: userInfo.Mobile,
  95. RealName: userInfo.RealName,
  96. NickName: userInfo.NickName,
  97. CompanyID: int(userInfo.CompanyID),
  98. CompanyName: companyName,
  99. CompanyStatus: companyStatus,
  100. SourceAgent: sourceAgent,
  101. SellerID: sellerId,
  102. Type: videoType,
  103. CreateTime: time.Now().Local(),
  104. }
  105. if e = item.Create(); e != nil {
  106. errMsg = "操作失败"
  107. err = errors.New("新增播放视频日志失败, Err:" + e.Error())
  108. return
  109. }
  110. return
  111. }
  112. // getSubTencentUrl 获取腾讯视频链接子字符串
  113. func getSubTencentUrl(tencentUrl string) (sub string) {
  114. if tencentUrl != "" {
  115. st := strings.LastIndex(tencentUrl, "/")
  116. ed := strings.LastIndex(tencentUrl, ".")
  117. if st > 0 && ed > st {
  118. sub = tencentUrl[st+1 : ed]
  119. }
  120. }
  121. return
  122. }
  123. // GetRoadVideoList 获取线上路演视频列表
  124. func GetRoadVideoList(userInfo user.UserInfo, pageIndex, pageSize, videoId, chartPermissionId int, keywords string) (resp response.RoadVideoItemResp, err error) {
  125. list := make([]*response.RoadVideoItem, 0)
  126. //获取有权限的permissionID
  127. validPermissionList, err := company.GetValidPermissionByCompany2ProductId(userInfo.CompanyID, 1)
  128. if err != nil {
  129. return
  130. }
  131. ficcPermissionList, err := chart_permission.GetFiccListExceptTacticByProductId()
  132. if err != nil {
  133. return
  134. }
  135. permissionIds := ""
  136. validPermissionMap := make(map[string]struct{})
  137. ParentPermissionNameMap := make(map[string]string)
  138. ParentPermissionChildMap := make(map[string]int)
  139. for _, v := range validPermissionList {
  140. permissionIds += "'"+strconv.Itoa(v.ChartPermissionID) + "'|"
  141. validPermissionMap["'"+strconv.Itoa(v.ChartPermissionID)+"'"] = struct{}{}
  142. }
  143. for _, v := range ficcPermissionList {
  144. ParentPermissionNameMap["'"+strconv.Itoa(int(v.ChartPermissionID))+"'"] = v.ClassifyName
  145. ParentPermissionChildMap[v.ClassifyName] += 1
  146. }
  147. if permissionIds == "" {
  148. resp.List = list
  149. resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
  150. return
  151. }
  152. permissionIds = strings.Trim(permissionIds,"|")
  153. condition := `is_deleted = 0 AND publish_state = 1 and chart_permission_ids REGEXP "(`+permissionIds+`)"`
  154. var par []interface{}
  155. // 分享点进来的直接定位到具体视频
  156. if videoId > 0 {
  157. condition += " and road_video_id = ?"
  158. par = append(par, videoId)
  159. } else {
  160. if chartPermissionId > 0 {
  161. condition += ` and FIND_IN_SET("'`+strconv.Itoa(chartPermissionId)+`'", chart_permission_ids)`
  162. }
  163. if keywords != "" {
  164. condition += " and title like ? "
  165. par = append(par,"%" + keywords + "%")
  166. }
  167. }
  168. videoList, total, e := yb_road_video.GetPageListByCondition(condition, par, pageIndex, pageSize)
  169. if e != nil {
  170. err = errors.New("获取视频列表失败, Err:" + e.Error())
  171. return
  172. }
  173. if len(videoList) <= 0 {
  174. resp.List = list
  175. resp.Paging = response.GetPaging(pageIndex, pageSize, 0)
  176. return
  177. }
  178. chartPermissionIdSlice := make([]int, 0)
  179. chartNameMap := make(map[string]string)
  180. for _, v := range videoList {
  181. tmp := strings.Split(v.ChartPermissionIds, ",")
  182. for _, t1 := range tmp {
  183. i, _ := strconv.Atoi(strings.Trim(t1, "'"))
  184. chartPermissionIdSlice = append(chartPermissionIdSlice, i)
  185. }
  186. }
  187. if len(chartPermissionIdSlice) > 0 {
  188. chartList, e := chart_permission.GetListByIds(chartPermissionIdSlice)
  189. if e != nil {
  190. err = errors.New("获取品种信息失败, Err:" + e.Error())
  191. return
  192. }
  193. for _, v := range chartList {
  194. chartNameMap["'"+strconv.Itoa(int(v.ChartPermissionID))+"'"] = v.PermissionName
  195. }
  196. }
  197. var chartPermissionNames string
  198. var parentPermissionName string
  199. for _, v := range videoList {
  200. chartPermissionNames = ""
  201. parentPermissionName = ""
  202. tmpSlice := strings.Split(v.ChartPermissionIds, ",")
  203. if len(tmpSlice) > 0 {
  204. // 判断是否有跨品种的chartPermissionID
  205. for _, cid := range tmpSlice {
  206. if p, ok := ParentPermissionNameMap[cid]; ok {
  207. if parentPermissionName == "" {
  208. parentPermissionName = p
  209. }else if parentPermissionName != p {
  210. parentPermissionName = ""
  211. break
  212. }
  213. }
  214. }
  215. //如果都是一个品种内,如果全部包含二级品种,则显示一级品种名称,否则展示全部二级品种
  216. if parentPermissionName != "" {
  217. hasNum := 0
  218. for _, cid := range tmpSlice {
  219. if _, ok := chartNameMap[cid]; ok {
  220. if _, ok1 := validPermissionMap[cid]; ok1 {
  221. hasNum += 1
  222. }
  223. }
  224. }
  225. if n, ok := ParentPermissionChildMap[parentPermissionName]; ok && n == hasNum {
  226. chartPermissionNames = parentPermissionName
  227. }
  228. }
  229. //如果包含跨品种则
  230. if chartPermissionNames == "" {
  231. for _, cid := range tmpSlice {
  232. if name, ok := chartNameMap[cid]; ok {
  233. if _, ok1 := validPermissionMap[cid]; ok1 {
  234. chartPermissionNames += name + ","
  235. }
  236. }
  237. }
  238. if chartPermissionNames != "" {
  239. chartPermissionNames = strings.Trim(chartPermissionNames, ",")
  240. }
  241. }
  242. }
  243. item := &response.RoadVideoItem{
  244. RoadVideoID: v.RoadVideoID,
  245. Title: v.Title,
  246. ChartPermissionIds: v.ChartPermissionIds,
  247. ChartPermissionName: chartPermissionNames,
  248. CoverImgUrl: v.CoverImgURL,
  249. VideoUrl: v.VideoURL,
  250. VideoSeconds: v.VideoSeconds,
  251. PublishState: v.PublishState,
  252. AdminId: v.AdminId,
  253. AdminRealName: v.AdminRealName,
  254. PublishTime: v.PublishTime.Format(utils.FormatDateTime),
  255. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  256. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  257. }
  258. list = append(list, item)
  259. }
  260. resp.List = list
  261. resp.Paging = response.GetPaging(pageIndex, pageSize, int(total))
  262. return
  263. }