community_video.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. package yb
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. ybModels "hongze/hz_crm_api/models/yb"
  9. "hongze/hz_crm_api/models/yb/request"
  10. "hongze/hz_crm_api/services"
  11. "hongze/hz_crm_api/services/yb"
  12. "hongze/hz_crm_api/utils"
  13. "strings"
  14. "time"
  15. )
  16. // 研报视频社区
  17. type CommunityVideoController struct {
  18. controllers.BaseAuthController
  19. }
  20. // List
  21. // @Title 视频列表
  22. // @Description 视频列表
  23. // @Param Keywords query string false "关键词"
  24. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'ClickNum':点击量"
  25. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  26. // @Success 200 {object} response.CommunityQuestionListResp
  27. // @router /community/video/list [get]
  28. func (this *CommunityVideoController) List() {
  29. br := new(models.BaseResponse).Init()
  30. br.IsSendEmail = false
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. sysUser := this.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录,SysUser Is Empty"
  39. br.Ret = 408
  40. return
  41. }
  42. condition := ""
  43. pars := make([]interface{}, 0)
  44. keywords := this.GetString("Keywords", "")
  45. if keywords != "" {
  46. keywords = "%" + keywords + "%"
  47. condition += ` AND v.title LIKE ? `
  48. pars = append(pars, keywords)
  49. }
  50. var startSize int
  51. pageSize, _ := this.GetInt("PageSize")
  52. currentIndex, _ := this.GetInt("CurrentIndex")
  53. if pageSize <= 0 {
  54. pageSize = utils.PageSize20
  55. }
  56. if currentIndex <= 0 {
  57. currentIndex = 1
  58. }
  59. startSize = paging.StartIndex(currentIndex, pageSize)
  60. // 排序相关
  61. sortParam := this.GetString("SortParam")
  62. sortType := this.GetString("SortType")
  63. orderStr := ""
  64. if sortType != "" && strings.ToLower(sortType) != "desc" && strings.ToLower(sortType) != "asc" {
  65. br.Msg = "请输入正确的排序类型"
  66. return
  67. }
  68. if sortParam == "ClickNum" {
  69. orderStr = " ORDER BY l.click_num"
  70. if sortType != "" {
  71. orderStr += " " + sortType
  72. } else {
  73. orderStr += " DESC"
  74. }
  75. orderStr += ", v.modify_time DESC"
  76. }
  77. total, resp, err := yb.GetCommunityVideoList(condition, pars, orderStr, startSize, pageSize)
  78. if err != nil {
  79. br.Msg = "获取视频列表失败"
  80. br.ErrMsg = "Yb-VideoList ErrMsg:" + err.Error()
  81. return
  82. }
  83. page := paging.GetPaging(currentIndex, pageSize, total)
  84. resp.Paging = page
  85. br.Ret = 200
  86. br.Success = true
  87. br.Msg = "获取成功"
  88. br.Data = resp
  89. }
  90. // Save
  91. // @Title 保存视频
  92. // @Description 保存视频
  93. // @Param request body request.CommunityVideoSaveReq true "type json string"
  94. // @Success 200 string "操作成功"
  95. // @router /community/video/save [post]
  96. func (this *CommunityVideoController) Save() {
  97. br := new(models.BaseResponse).Init()
  98. br.IsSendEmail = false
  99. defer func() {
  100. this.Data["json"] = br
  101. this.ServeJSON()
  102. }()
  103. sysUser := this.SysUser
  104. if sysUser == nil {
  105. br.Msg = "请登录"
  106. br.ErrMsg = "请登录,SysUser Is Empty"
  107. br.Ret = 408
  108. return
  109. }
  110. var req request.CommunityVideoSaveReq
  111. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  112. br.Msg = "参数解析异常!"
  113. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  114. return
  115. }
  116. if req.Title == "" {
  117. br.Msg = "请输入标题"
  118. return
  119. }
  120. if req.VarietyTagId == 0 {
  121. br.Msg = "请选择标签"
  122. return
  123. }
  124. if req.CoverImgUrl == "" {
  125. br.Msg = "请上传封面图"
  126. return
  127. }
  128. if req.VideoUrl == "" {
  129. br.Msg = "请上传视频"
  130. return
  131. }
  132. data, errMsg, err := yb.SaveCommunityVideo(req.VideoId, req.VarietyTagId, req.Title, req.CoverImgUrl, req.VideoUrl, req.VideoSeconds)
  133. if err != nil {
  134. br.Msg = errMsg
  135. br.ErrMsg = "Yb-SaveVideo ErrMsg:" + err.Error()
  136. return
  137. }
  138. br.Ret = 200
  139. br.Success = true
  140. br.Msg = "操作成功"
  141. br.Data = data
  142. }
  143. // Delete
  144. // @Title 删除视频
  145. // @Description 删除视频
  146. // @Param request body request.CommunityVideoOptionReq true "type json string"
  147. // @Success 200 string "操作成功"
  148. // @router /community/video/delete [post]
  149. func (this *CommunityVideoController) Delete() {
  150. br := new(models.BaseResponse).Init()
  151. br.IsSendEmail = false
  152. defer func() {
  153. this.Data["json"] = br
  154. this.ServeJSON()
  155. }()
  156. sysUser := this.SysUser
  157. if sysUser == nil {
  158. br.Msg = "请登录"
  159. br.ErrMsg = "请登录,SysUser Is Empty"
  160. br.Ret = 408
  161. return
  162. }
  163. var req request.CommunityVideoOptionReq
  164. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  165. br.Msg = "参数解析异常!"
  166. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  167. return
  168. }
  169. if req.VideoId <= 0 {
  170. br.Msg = "参数有误"
  171. return
  172. }
  173. errMsg, err := yb.SoftDeleteCommunityVideo(req.VideoId)
  174. if err != nil {
  175. br.Msg = errMsg
  176. br.ErrMsg = "Yb-DeleteVideo ErrMsg:" + err.Error()
  177. return
  178. }
  179. br.Ret = 200
  180. br.Success = true
  181. br.Msg = "操作成功"
  182. }
  183. // Publish
  184. // @Title 发布/取消视频
  185. // @Description 发布/取消视频
  186. // @Param request body request.CommunityVideoPublishReq true "type json string"
  187. // @Success 200 string "操作成功"
  188. // @router /community/video/publish [post]
  189. func (this *CommunityVideoController) Publish() {
  190. br := new(models.BaseResponse).Init()
  191. br.IsSendEmail = false
  192. defer func() {
  193. this.Data["json"] = br
  194. this.ServeJSON()
  195. }()
  196. sysUser := this.SysUser
  197. if sysUser == nil {
  198. br.Msg = "请登录"
  199. br.ErrMsg = "请登录,SysUser Is Empty"
  200. br.Ret = 408
  201. return
  202. }
  203. var req request.CommunityVideoPublishReq
  204. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  205. br.Msg = "参数解析异常!"
  206. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  207. return
  208. }
  209. if req.VideoId <= 0 {
  210. br.Msg = "参数有误"
  211. return
  212. }
  213. errMsg, err := yb.PublishCommunityVideo(req.VideoId, req.PublishState)
  214. if err != nil {
  215. br.Msg = errMsg
  216. br.ErrMsg = "Yb-PublishVideo ErrMsg:" + err.Error()
  217. return
  218. }
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "操作成功"
  222. }
  223. // SendThs
  224. // @Title 推送客群消息
  225. // @Description 推送客群消息
  226. // @Param request body request.CommunityVideoOptionReq true "type json string"
  227. // @Success 200 string "操作成功"
  228. // @router /community/video/send_ths [post]
  229. func (this *CommunityVideoController) SendThs() {
  230. br := new(models.BaseResponse).Init()
  231. br.IsSendEmail = false
  232. defer func() {
  233. this.Data["json"] = br
  234. this.ServeJSON()
  235. }()
  236. sysUser := this.SysUser
  237. if sysUser == nil {
  238. br.Msg = "请登录"
  239. br.ErrMsg = "请登录,SysUser Is Empty"
  240. br.Ret = 408
  241. return
  242. }
  243. var req request.CommunityVideoOptionReq
  244. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  245. br.Msg = "参数解析异常!"
  246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  247. return
  248. }
  249. if req.VideoId <= 0 {
  250. br.Msg = "参数有误"
  251. return
  252. }
  253. errMsg, err := yb.SendCommunityVideoThsMsg(req.VideoId)
  254. if err != nil {
  255. br.Msg = errMsg
  256. br.ErrMsg = "Yb-SendVideoThsMsg ErrMsg:" + err.Error()
  257. return
  258. }
  259. br.Ret = 200
  260. br.Success = true
  261. br.Msg = "操作成功"
  262. }
  263. // VideoPlayLogList
  264. // @Title 获取视频点击量列表
  265. // @Description 获取视频点击量列表
  266. // @Param VideoId query int false "视频ID"
  267. // @Param SortType query int false "播放时长排序:默认倒序; 1-倒序(多一个数方便前端回显); 2-正序"
  268. // @Success 200 {object} response.QuestionClickLogListResp
  269. // @router /community/video/click/logs [get]
  270. func (this *CommunityVideoController) VideoPlayLogList() {
  271. br := new(models.BaseResponse).Init()
  272. defer func() {
  273. this.Data["json"] = br
  274. this.ServeJSON()
  275. }()
  276. sysUser := this.SysUser
  277. if sysUser == nil {
  278. br.Msg = "请登录"
  279. br.ErrMsg = "请登录,SysUser Is Empty"
  280. br.Ret = 408
  281. return
  282. }
  283. videoId, _ := this.GetInt("VideoId")
  284. if videoId <= 0 {
  285. br.Msg = "请输入正确的视频ID"
  286. return
  287. }
  288. // 播放时长排序:1-DESC; 2-ASC;
  289. sortType, _ := this.GetInt("SortType")
  290. var startSize int
  291. pageSize, _ := this.GetInt("PageSize")
  292. currentIndex, _ := this.GetInt("CurrentIndex")
  293. if pageSize <= 0 {
  294. pageSize = utils.PageSize20
  295. }
  296. if currentIndex <= 0 {
  297. currentIndex = 1
  298. }
  299. startSize = paging.StartIndex(currentIndex, pageSize)
  300. total, resp, err := yb.GetVideoPlayLogList(videoId, 1, startSize, pageSize, sortType)
  301. if err != nil {
  302. br.Msg = "获取问答列表失败"
  303. br.ErrMsg = "QuestionList ErrMsg:" + err.Error()
  304. return
  305. }
  306. page := paging.GetPaging(currentIndex, pageSize, int(total))
  307. resp.Paging = page
  308. br.Ret = 200
  309. br.Success = true
  310. br.Msg = "获取成功"
  311. br.Data = resp
  312. }
  313. // SendTemplateMsg
  314. // @Title 推送模板消息
  315. // @Description 推送模板消息
  316. // @Param request body request.CommunityVideoOptionReq true "type json string"
  317. // @Success 200 string "操作成功"
  318. // @router /community/video/send_template_msg [post]
  319. func (this *CommunityVideoController) SendTemplateMsg() {
  320. br := new(models.BaseResponse).Init()
  321. br.IsSendEmail = false
  322. defer func() {
  323. this.Data["json"] = br
  324. this.ServeJSON()
  325. }()
  326. sysUser := this.SysUser
  327. if sysUser == nil {
  328. br.Msg = "请登录"
  329. br.ErrMsg = "请登录,SysUser Is Empty"
  330. br.Ret = 408
  331. return
  332. }
  333. var req request.CommunityVideoOptionReq
  334. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  335. br.Msg = "参数解析异常!"
  336. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  337. return
  338. }
  339. if req.VideoId <= 0 {
  340. br.Msg = "参数有误"
  341. return
  342. }
  343. errMsg, err := yb.SendCommunityVideoTemplateMsg(req.VideoId)
  344. if err != nil {
  345. br.Msg = errMsg
  346. br.ErrMsg = "Yb-SendCommunityVideoTemplateMsg ErrMsg:" + err.Error()
  347. return
  348. }
  349. br.Ret = 200
  350. br.Success = true
  351. br.Msg = "操作成功"
  352. }
  353. // CoverList
  354. // @Title 封面库列表
  355. // @Description 封面库
  356. // @Param PageSize query int false "每页数据量"
  357. // @Param CurrentIndex query int false "页码"
  358. // @Success 200 {object} response.CommunityVideoCoverListResp
  359. // @router /community/video/cover_list [get]
  360. func (this *CommunityVideoController) CoverList() {
  361. br := new(models.BaseResponse).Init()
  362. br.IsSendEmail = false
  363. defer func() {
  364. this.Data["json"] = br
  365. this.ServeJSON()
  366. }()
  367. sysUser := this.SysUser
  368. if sysUser == nil {
  369. br.Msg = "请登录"
  370. br.ErrMsg = "请登录,SysUser Is Empty"
  371. br.Ret = 408
  372. return
  373. }
  374. condition := ""
  375. pars := make([]interface{}, 0)
  376. var startSize int
  377. pageSize, _ := this.GetInt("PageSize")
  378. currentIndex, _ := this.GetInt("CurrentIndex")
  379. if pageSize <= 0 {
  380. pageSize = utils.PageSize20
  381. }
  382. if currentIndex <= 0 {
  383. currentIndex = 1
  384. }
  385. startSize = paging.StartIndex(currentIndex, pageSize)
  386. coverType, _ := this.GetInt("Type", 0)
  387. if coverType > 0 {
  388. condition += " and type=?"
  389. pars = append(pars, coverType)
  390. }
  391. total, resp, err := yb.GetCommunityVideoCoverList(condition, pars, "", startSize, pageSize)
  392. if err != nil {
  393. br.Msg = "获取封面库列表失败"
  394. br.ErrMsg = "Yb-GetCommunityVideoCoverList ErrMsg:" + err.Error()
  395. return
  396. }
  397. page := paging.GetPaging(currentIndex, pageSize, total)
  398. resp.Paging = page
  399. br.Ret = 200
  400. br.Success = true
  401. br.Msg = "获取成功"
  402. br.Data = resp
  403. }
  404. // CoverSave
  405. // @Title 保存封面图
  406. // @Description 保存封面图
  407. // @Param request body request.CommunityVideoCoverSaveReq true "type json string"
  408. // @Success 200 string "操作成功"
  409. // @router /community/video/cover_save [post]
  410. func (this *CommunityVideoController) CoverSave() {
  411. br := new(models.BaseResponse).Init()
  412. br.IsSendEmail = false
  413. defer func() {
  414. this.Data["json"] = br
  415. this.ServeJSON()
  416. }()
  417. sysUser := this.SysUser
  418. if sysUser == nil {
  419. br.Msg = "请登录"
  420. br.ErrMsg = "请登录,SysUser Is Empty"
  421. br.Ret = 408
  422. return
  423. }
  424. var req request.CommunityVideoCoverSaveReq
  425. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  426. br.Msg = "参数解析异常!"
  427. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  428. return
  429. }
  430. if req.CoverName == "" {
  431. br.Msg = "请输入封面名称"
  432. return
  433. }
  434. if req.CoverUrl == "" {
  435. br.Msg = "请上传封面图"
  436. return
  437. }
  438. if req.Type == 0 {
  439. br.Msg = "选择封面类型"
  440. return
  441. }
  442. data, errMsg, err := yb.SaveCommunityVideoCover(req.CoverId, req.CoverName, req.CoverUrl, req.Type)
  443. if err != nil {
  444. br.Msg = errMsg
  445. br.ErrMsg = "Yb-SaveCommunityVideoCover ErrMsg:" + err.Error()
  446. return
  447. }
  448. br.Ret = 200
  449. br.Success = true
  450. br.Msg = "操作成功"
  451. br.Data = data
  452. }
  453. // CoverDelete
  454. // @Title 删除封面图
  455. // @Description 删除封面图
  456. // @Param request body request.CommunityVideoCoverOptionReq true "type json string"
  457. // @Success 200 string "操作成功"
  458. // @router /community/video/cover_delete [post]
  459. func (this *CommunityVideoController) CoverDelete() {
  460. br := new(models.BaseResponse).Init()
  461. br.IsSendEmail = false
  462. defer func() {
  463. this.Data["json"] = br
  464. this.ServeJSON()
  465. }()
  466. sysUser := this.SysUser
  467. if sysUser == nil {
  468. br.Msg = "请登录"
  469. br.ErrMsg = "请登录,SysUser Is Empty"
  470. br.Ret = 408
  471. return
  472. }
  473. var req request.CommunityVideoCoverOptionReq
  474. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  475. br.Msg = "参数解析异常!"
  476. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  477. return
  478. }
  479. if req.CoverId <= 0 {
  480. br.Msg = "参数有误"
  481. return
  482. }
  483. errMsg, err := yb.SoftDeleteCommunityVideoCover(req.CoverId)
  484. if err != nil {
  485. br.Msg = errMsg
  486. br.ErrMsg = "Yb-SoftDeleteCommunityVideoCover ErrMsg:" + err.Error()
  487. return
  488. }
  489. br.Ret = 200
  490. br.Success = true
  491. br.Msg = "操作成功"
  492. }
  493. // SendMsg
  494. // @Title 推送消息(模板/客群)
  495. // @Description 推送消息(模板/客群)
  496. // @Param request body request.CommunityVideoOptionReq true "type json string"
  497. // @Success 200 string "操作成功"
  498. // @router /community/video/send_msg [post]
  499. func (this *CommunityVideoController) SendMsg() {
  500. br := new(models.BaseResponse).Init()
  501. br.IsSendEmail = false
  502. defer func() {
  503. this.Data["json"] = br
  504. this.ServeJSON()
  505. }()
  506. sysUser := this.SysUser
  507. if sysUser == nil {
  508. br.Msg = "请登录"
  509. br.ErrMsg = "请登录,SysUser Is Empty"
  510. br.Ret = 408
  511. return
  512. }
  513. var req request.CommunityVideoOptionReq
  514. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  515. br.Msg = "参数解析异常!"
  516. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  517. return
  518. }
  519. if req.VideoId <= 0 {
  520. br.Msg = "参数有误"
  521. return
  522. }
  523. // 加锁,避免重复点击造成的多次推送
  524. {
  525. redisKey := fmt.Sprint("community_video:send:msg:", req.VideoId)
  526. ok := utils.Rc.SetNX(redisKey, 1, time.Second*300)
  527. if !ok {
  528. br.Msg = "消息已推送, 请勿重复推送"
  529. return
  530. }
  531. defer func() {
  532. _ = utils.Rc.Delete(redisKey)
  533. }()
  534. }
  535. item, e := ybModels.GetVideoById(req.VideoId)
  536. if e != nil {
  537. br.Msg = "视频不存在或已被删除"
  538. br.ErrMsg = "获取视频信息失败, Err: " + e.Error()
  539. return
  540. }
  541. if item.PublishState != 1 {
  542. br.Msg = "视频未发布, 不可推送"
  543. return
  544. }
  545. if item.SendThsState == 1 && item.SendTemplateState == 1 {
  546. br.Msg = "请勿重复推送"
  547. return
  548. }
  549. nowTime := time.Now().Local()
  550. updateCols := make([]string, 0)
  551. // 可能存在历史数据两个只推了一个所以此处加个判断
  552. // 模板消息
  553. if item.SendTemplateState == 0 {
  554. updateCols = append(updateCols, "SendTemplateState", "SendTemplateTime")
  555. item.SendTemplateState = 1
  556. item.SendTemplateTime = nowTime
  557. go func() {
  558. _ = services.SendYbCommunityVideoWxMsgV2(item.CommunityVideoId, item.VarietyTagId, item.VarietyTagName, item.Title, item.PublishTime.Format(utils.FormatDateTime))
  559. }()
  560. }
  561. // 客群消息
  562. if item.SendThsState == 0 {
  563. updateCols = append(updateCols, "SendThsState", "SendThsTime")
  564. item.SendThsState = 1
  565. item.SendThsTime = nowTime
  566. //go func() {
  567. // _ = services.SendYbCommunityVideoThs(item.CommunityVideoId, item.Title)
  568. //}()
  569. }
  570. // 更新状态
  571. if e := item.Update(updateCols); e != nil {
  572. br.Msg = "推送失败"
  573. br.ErrMsg = "更新视频信息失败, Err: " + e.Error()
  574. return
  575. }
  576. br.Ret = 200
  577. br.Success = true
  578. br.Msg = "操作成功"
  579. }