community_video.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package yb
  2. import (
  3. "errors"
  4. "hongze/hz_crm_api/models"
  5. "hongze/hz_crm_api/models/company"
  6. "hongze/hz_crm_api/models/yb"
  7. "hongze/hz_crm_api/models/yb/response"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // GetCommunityVideoList 视频列表
  15. func GetCommunityVideoList(condition string, pars []interface{}, orderStr string, startSize, pageSize int) (total int, resp *response.CommunityVideoListResp, err error) {
  16. resp = new(response.CommunityVideoListResp)
  17. total, list, e := yb.GetCommunityVideoList(condition, pars, orderStr, startSize, pageSize)
  18. if e != nil {
  19. err = errors.New("获取问题列表失败 Err:" + e.Error())
  20. return
  21. }
  22. if len(list) == 0 {
  23. return
  24. }
  25. respList := make([]*response.CommunityVideoItem, 0)
  26. for _, v := range list {
  27. item := &response.CommunityVideoItem{
  28. CommunityVideoId: v.CommunityVideoId,
  29. Title: v.Title,
  30. VarietyTagId: v.VarietyTagId,
  31. VarietyTagName: v.VarietyTagName,
  32. CoverImgUrl: v.CoverImgUrl,
  33. VideoUrl: v.VideoUrl,
  34. VideoSeconds: v.VideoSeconds,
  35. PublishState: v.PublishState,
  36. SendThsState: v.SendThsState,
  37. SendTemplateState: v.SendTemplateState,
  38. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  39. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  40. ClickNum: v.ClickNum,
  41. }
  42. respList = append(respList, item)
  43. }
  44. resp.List = respList
  45. return
  46. }
  47. // SaveCommunityVideo 保存视频
  48. func SaveCommunityVideo(videoId, varietyTagId int, title, coverImgUrl, videoUrl, videoSeconds string) (resp *response.CommunityVideoItem, errMsg string, err error) {
  49. //chartPermissionName := ""
  50. varietyTagName := ""
  51. if varietyTagId > 0 {
  52. varietyTag, e := models.GetVarietyTagById(varietyTagId)
  53. //chartPermission, e := models.GetChartPermissionById(chartPermissionId)
  54. if e != nil {
  55. errMsg = "品种信息有误"
  56. err = errors.New("获取品种信息失败, Err:" + e.Error())
  57. return
  58. }
  59. varietyTagName = varietyTag.TagName
  60. }
  61. resp = new(response.CommunityVideoItem)
  62. // 更新
  63. nowTime := time.Now().Local()
  64. if videoId > 0 {
  65. item, e := yb.GetVideoById(videoId)
  66. if e != nil {
  67. errMsg = "视频信息有误"
  68. err = errors.New("获取视频信息失败, Err:" + e.Error())
  69. return
  70. }
  71. updateCols := make([]string, 0)
  72. updateCols = append(updateCols, "Title", "VarietyTagId", "VarietyTagName", "CoverImgUrl", "VideoUrl", "VideoSeconds", "ModifyTime")
  73. item.Title = title
  74. item.VarietyTagId = varietyTagId
  75. item.VarietyTagName = varietyTagName
  76. item.CoverImgUrl = coverImgUrl
  77. item.VideoUrl = videoUrl
  78. item.VideoSeconds = videoSeconds
  79. item.ModifyTime = nowTime
  80. if e := item.Update(updateCols); e != nil {
  81. errMsg = "更新失败"
  82. err = errors.New("更新视频信息失败, Err:" + e.Error())
  83. }
  84. resp = formatCommunityVideoItem(item)
  85. return
  86. }
  87. // 新增
  88. videoItem := &yb.CommunityVideo{
  89. Title: title,
  90. VarietyTagId: varietyTagId,
  91. VarietyTagName: varietyTagName,
  92. CoverImgUrl: coverImgUrl,
  93. VideoUrl: videoUrl,
  94. VideoSeconds: videoSeconds,
  95. CreateTime: nowTime,
  96. ModifyTime: nowTime,
  97. }
  98. if _, e := videoItem.Add(); e != nil {
  99. errMsg = "新增失败"
  100. err = errors.New("新增视频信息失败, Err:" + e.Error())
  101. }
  102. resp = formatCommunityVideoItem(videoItem)
  103. return
  104. }
  105. func formatCommunityVideoItem(item *yb.CommunityVideo) (resp *response.CommunityVideoItem) {
  106. resp = new(response.CommunityVideoItem)
  107. if item != nil {
  108. resp.CommunityVideoId = item.CommunityVideoId
  109. resp.Title = item.Title
  110. resp.VarietyTagId = item.VarietyTagId
  111. resp.VarietyTagName = item.VarietyTagName
  112. resp.CoverImgUrl = item.CoverImgUrl
  113. resp.VideoUrl = item.VideoUrl
  114. resp.VideoSeconds = item.VideoSeconds
  115. resp.PublishState = item.PublishState
  116. resp.SendThsState = item.SendThsState
  117. resp.SendTemplateState = item.SendTemplateState
  118. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  119. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  120. }
  121. return
  122. }
  123. // SoftDeleteCommunityVideo 删除视频
  124. func SoftDeleteCommunityVideo(videoId int) (errMsg string, err error) {
  125. if _, e := yb.GetVideoById(videoId); e != nil {
  126. errMsg = "视频信息有误"
  127. err = errors.New("获取视频信息失败, Err:" + e.Error())
  128. return
  129. }
  130. if e := yb.DeleteVideo(videoId); e != nil {
  131. errMsg = "删除失败"
  132. err = errors.New("删除视频失败, Err:" + e.Error())
  133. }
  134. return
  135. }
  136. // PublishCommunityVideo 发布/取消发布视频
  137. func PublishCommunityVideo(videoId, publishState int) (errMsg string, err error) {
  138. item, e := yb.GetVideoById(videoId)
  139. if e != nil {
  140. errMsg = "视频信息有误"
  141. err = errors.New("获取视频信息失败, Err:" + e.Error())
  142. return
  143. }
  144. nowTime := time.Now().Local()
  145. updateCols := make([]string, 0)
  146. updateCols = append(updateCols, "PublishState", "ModifyTime")
  147. if publishState == 1 {
  148. updateCols = append(updateCols, "PublishTime")
  149. item.PublishTime = nowTime
  150. }
  151. item.PublishState = publishState
  152. item.ModifyTime = nowTime
  153. if e := item.Update(updateCols); e != nil {
  154. errMsg = "操作失败"
  155. err = errors.New("更新视频信息失败, Err:" + e.Error())
  156. }
  157. return
  158. }
  159. // SendCommunityVideoThsMsg 推送视频客群消息
  160. func SendCommunityVideoThsMsg(videoId int) (errMsg string, err error) {
  161. item, e := yb.GetVideoById(videoId)
  162. if e != nil {
  163. errMsg = "视频信息有误"
  164. err = errors.New("获取视频信息失败, Err:" + e.Error())
  165. return
  166. }
  167. if item.PublishState != 1 {
  168. errMsg = "视频未发布, 不可推送"
  169. err = errors.New("视频未发布, 不可推送")
  170. return
  171. }
  172. if item.SendThsState != 0 {
  173. errMsg = "请勿重复推送"
  174. err = errors.New("请勿重复推送客群消息")
  175. return
  176. }
  177. nowTime := time.Now().Local()
  178. updateCols := make([]string, 0)
  179. updateCols = append(updateCols, "SendThsState", "SendThsTime")
  180. item.SendThsState = 1
  181. item.SendThsTime = nowTime
  182. if e := item.Update(updateCols); e != nil {
  183. err = errors.New("更新视频信息失败, Err:" + e.Error())
  184. }
  185. //go func() {
  186. // _ = services.SendYbCommunityVideoThs(item.CommunityVideoId, item.Title)
  187. //}()
  188. return
  189. }
  190. func GetVideoPlayLogList(videoId, videoType, startSize, pageSize, sortType int) (pageTotal int64, resp *response.VideoPlayLogListResp, err error) {
  191. var videoInfo *yb.CommunityVideo
  192. var RoadVideoInfo *yb.RoadVideo
  193. var title string
  194. if videoType == 1 {
  195. videoInfo, err = yb.GetVideoById(videoId)
  196. if err != nil {
  197. err = errors.New("视频信息有误 Err:" + err.Error())
  198. return
  199. }
  200. title = videoInfo.Title
  201. } else {
  202. RoadVideoInfo, err = yb.GetRoadVideoById(videoId)
  203. if err != nil {
  204. err = errors.New("视频信息有误 Err:" + err.Error())
  205. return
  206. }
  207. title = RoadVideoInfo.Title
  208. }
  209. // 播放时长排序:默认倒序; 1-倒序; 2-正序
  210. orderRule := ``
  211. if sortType == 2 {
  212. orderRule = ` ORDER BY stop_seconds ASC, create_time DESC`
  213. }
  214. resp = new(response.VideoPlayLogListResp)
  215. resp.Title = title
  216. list, e := yb.GetVideoPlayLogListByVideoIdV2(videoId, videoType, startSize, pageSize, orderRule)
  217. if e != nil {
  218. err = errors.New("获取视频列表失败 Err:" + e.Error())
  219. return
  220. }
  221. //pageTotal, err = yb.GetVideoPlayListTotalByVideoId(videoId, videoType)
  222. //if err != nil {
  223. // err = errors.New("查询视频列表总点击量失败 Err:" + err.Error())
  224. // return
  225. //}
  226. count, err := yb.GetVideoPlayTotalByVideoIdV2(videoId, videoType)
  227. if err != nil {
  228. err = errors.New("查询视频总点击量失败 Err:" + err.Error())
  229. return
  230. }
  231. pageTotal = count
  232. resp.Count = int(count)
  233. if len(list) == 0 {
  234. return
  235. }
  236. userIds := ""
  237. for _, v := range list {
  238. userIds += ", " + (strconv.Itoa(v.UserId))
  239. }
  240. companyUsersMap := make(map[int]*company.CompanyUser)
  241. if userIds != "" {
  242. userIds = strings.Trim(userIds, ",")
  243. //查询用户相关的客户信息
  244. companyUserList, tErr := company.GetFiccCompanyUserByUserIds(userIds)
  245. if tErr != nil {
  246. err = errors.New("获取客户信息失败 Err:" + tErr.Error())
  247. return
  248. }
  249. for _, v := range companyUserList {
  250. companyUsersMap[int(v.UserId)] = v
  251. }
  252. }
  253. respList := make([]*response.VideoPlayLogListItem, 0)
  254. for _, v := range list {
  255. tmp := new(response.VideoPlayLogListItem)
  256. tmp.UserId = v.UserId
  257. tmp.CreateTime = v.CreateTime
  258. tmp.StopSeconds = v.StopSeconds
  259. //tmp.ClickNum = v.ClickNum
  260. //tmp.LastCreateTime = v.LastCreateTime
  261. tmp.SourceAgent = v.NewSourceAgent
  262. if u, ok := companyUsersMap[v.UserId]; ok {
  263. tmp.RealName = u.RealName
  264. tmp.CompanyName = u.CompanyName
  265. tmp.CompanyStatus = u.Status
  266. tmp.CompanyId = u.CompanyId
  267. }
  268. respList = append(respList, tmp)
  269. }
  270. resp.List = respList
  271. return
  272. }
  273. // SendCommunityVideoTemplateMsg 推送模板消息
  274. func SendCommunityVideoTemplateMsg(videoId int) (errMsg string, err error) {
  275. item, e := yb.GetVideoById(videoId)
  276. if e != nil {
  277. errMsg = "视频信息有误"
  278. err = errors.New("获取视频信息失败, Err:" + e.Error())
  279. return
  280. }
  281. if item.PublishState != 1 {
  282. errMsg = "视频未发布, 不可推送"
  283. err = errors.New("视频未发布, 不可推送")
  284. return
  285. }
  286. if item.SendTemplateState != 0 {
  287. errMsg = "请勿重复推送"
  288. err = errors.New("请勿重复推送模板消息")
  289. return
  290. }
  291. updateCols := make([]string, 0)
  292. updateCols = append(updateCols, "SendTemplateState", "SendTemplateTime")
  293. item.SendTemplateState = 1
  294. item.SendTemplateTime = time.Now().Local()
  295. if e := item.Update(updateCols); e != nil {
  296. err = errors.New("更新视频信息失败, Err:" + e.Error())
  297. }
  298. go func() {
  299. _ = services.SendYbCommunityVideoWxMsg(item.CommunityVideoId, item.VarietyTagName, item.Title, item.PublishTime.Format(utils.FormatDateTime))
  300. }()
  301. return
  302. }
  303. // GetCommunityVideoCoverList 封面库列表
  304. func GetCommunityVideoCoverList(condition string, pars []interface{}, orderRule string, startSize, pageSize int) (total int, resp *response.CommunityVideoCoverListResp, err error) {
  305. resp = new(response.CommunityVideoCoverListResp)
  306. total, list, e := yb.GetCommunityVideoCoverPageList(condition, pars, orderRule, startSize, pageSize)
  307. if e != nil {
  308. err = errors.New("获取封面库列表失败, Err:" + e.Error())
  309. return
  310. }
  311. if len(list) == 0 {
  312. return
  313. }
  314. respList := make([]*response.CommunityVideoCoverItem, 0)
  315. for _, v := range list {
  316. item := &response.CommunityVideoCoverItem{
  317. CommunityVideoCoverId: v.CommunityVideoCoverId,
  318. CoverName: v.CoverName,
  319. CoverUrl: v.CoverUrl,
  320. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  321. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  322. Type: v.Type,
  323. }
  324. respList = append(respList, item)
  325. }
  326. resp.List = respList
  327. return
  328. }
  329. // SaveCommunityVideoCover 保存视频
  330. func SaveCommunityVideoCover(coverId int, coverName, coverUrl string, coverType int8) (resp *response.CommunityVideoCoverItem, errMsg string, err error) {
  331. resp = new(response.CommunityVideoCoverItem)
  332. nowTime := time.Now().Local()
  333. // 更新
  334. if coverId > 0 {
  335. item, e := yb.GetCommunityVideoCoverById(coverId)
  336. if e != nil {
  337. errMsg = "封面信息有误"
  338. err = errors.New("获取封面图失败, Err:" + e.Error())
  339. return
  340. }
  341. updateCols := make([]string, 0)
  342. updateCols = append(updateCols, "CoverName", "CoverUrl", "ModifyTime", "type")
  343. item.CoverName = coverName
  344. item.CoverUrl = coverUrl
  345. item.ModifyTime = nowTime
  346. item.Type = coverType
  347. if e := item.Update(updateCols); e != nil {
  348. errMsg = "更新失败"
  349. err = errors.New("更新封面图失败, Err:" + e.Error())
  350. }
  351. resp = formatCommunityVideoCoverItem(item)
  352. return
  353. }
  354. // 新增
  355. newCover := &yb.CommunityVideoCover{
  356. CoverName: coverName,
  357. CoverUrl: coverUrl,
  358. CreateTime: nowTime,
  359. ModifyTime: nowTime,
  360. Type: coverType,
  361. }
  362. if _, e := newCover.Add(); e != nil {
  363. errMsg = "新增失败"
  364. err = errors.New("新增封面图失败, Err:" + e.Error())
  365. }
  366. resp = formatCommunityVideoCoverItem(newCover)
  367. return
  368. }
  369. func formatCommunityVideoCoverItem(item *yb.CommunityVideoCover) (resp *response.CommunityVideoCoverItem) {
  370. resp = new(response.CommunityVideoCoverItem)
  371. if item != nil {
  372. resp.CommunityVideoCoverId = item.CommunityVideoCoverId
  373. resp.CoverName = item.CoverName
  374. resp.CoverUrl = item.CoverUrl
  375. resp.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  376. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  377. resp.Type = item.Type
  378. }
  379. return
  380. }
  381. // SoftDeleteCommunityVideoCover 删除封面图
  382. func SoftDeleteCommunityVideoCover(coverId int) (errMsg string, err error) {
  383. if _, e := yb.GetCommunityVideoCoverById(coverId); e != nil {
  384. errMsg = "封面图信息有误"
  385. err = errors.New("获取封面图失败, Err:" + e.Error())
  386. return
  387. }
  388. if e := yb.DeleteVideoCover(coverId); e != nil {
  389. errMsg = "删除失败"
  390. err = errors.New("删除封面图失败, Err:" + e.Error())
  391. }
  392. return
  393. }