video.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/video"
  7. "hongze/hz_crm_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 视频
  13. type VideoController struct {
  14. BaseAuthController
  15. }
  16. // @Title 获取视频标签列表
  17. // @Description 获取视频标签列表接口
  18. // @Success 200 {object} video.VideoTagsListResp
  19. // @router /tags/list [get]
  20. func (this *VideoController) TagsList() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. sysUser := this.SysUser
  27. if sysUser == nil {
  28. br.Msg = "请登录"
  29. br.ErrMsg = "请登录,SysUser Is Empty"
  30. return
  31. }
  32. list, err := video.GetVideoTagsList()
  33. if err != nil {
  34. br.Msg = "获取失败"
  35. br.ErrMsg = "获取失败,Err:" + err.Error()
  36. return
  37. }
  38. resp := new(video.VideoTagsListResp)
  39. resp.List = list
  40. br.Ret = 200
  41. br.Success = true
  42. br.Msg = "获取成功"
  43. br.Data = resp
  44. }
  45. // @Title 视频列表
  46. // @Description 视频列表接口
  47. // @Param PageSize query int true "每页数据条数"
  48. // @Param CurrentIndex query int true "当前页页码,从1开始"
  49. // @Param KeyWord query string true "搜索关键词"
  50. // @Success 200 {object} video.VideoListResp
  51. // @router /list [get]
  52. func (this *VideoController) List() {
  53. br := new(models.BaseResponse).Init()
  54. defer func() {
  55. this.Data["json"] = br
  56. this.ServeJSON()
  57. }()
  58. sysUser := this.SysUser
  59. if sysUser == nil {
  60. br.Msg = "请登录"
  61. br.ErrMsg = "请登录,SysUser Is Empty"
  62. br.Ret = 408
  63. return
  64. }
  65. pageSize, _ := this.GetInt("PageSize")
  66. currentIndex, _ := this.GetInt("CurrentIndex")
  67. keyWord := this.GetString("KeyWord")
  68. var startSize int
  69. if pageSize <= 0 {
  70. pageSize = utils.PageSize20
  71. }
  72. if currentIndex <= 0 {
  73. currentIndex = 1
  74. }
  75. startSize = paging.StartIndex(currentIndex, pageSize)
  76. var condition string
  77. var pars []interface{}
  78. if keyWord != "" {
  79. videoIdStr, err := video.GetVideoIdByKeyWord(keyWord)
  80. if err != nil {
  81. br.Msg = "获取失败"
  82. br.ErrMsg = "获取失败,Err:" + err.Error()
  83. return
  84. }
  85. if videoIdStr != "" {
  86. condition += ` AND (a.title LIKE '%` + keyWord + `%' OR a.video_id IN(` + videoIdStr + `) ) `
  87. } else {
  88. condition += ` AND a.title LIKE '%` + keyWord + `%' `
  89. }
  90. }
  91. total, err := video.GetVideoListCount(condition, pars)
  92. if err != nil {
  93. br.Msg = "获取失败"
  94. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  95. return
  96. }
  97. list, err := video.GetVideoList(condition, pars, startSize, pageSize)
  98. if err != nil {
  99. br.Msg = "获取失败"
  100. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  101. return
  102. }
  103. page := paging.GetPaging(currentIndex, pageSize, total)
  104. resp := new(video.VideoListResp)
  105. resp.List = list
  106. resp.Paging = page
  107. br.Ret = 200
  108. br.Success = true
  109. br.Msg = "获取成功"
  110. br.Data = resp
  111. }
  112. // @Title 保存视频
  113. // @Description 保存视频接口
  114. // @Param request body video.VideoSaveReq true "type json string"
  115. // @Success 200 {object} video.VideoSaveResp
  116. // @router /save [post]
  117. func (this *VideoController) Save() {
  118. br := new(models.BaseResponse).Init()
  119. defer func() {
  120. this.Data["json"] = br
  121. this.ServeJSON()
  122. }()
  123. sysUser := this.SysUser
  124. if sysUser == nil {
  125. br.Msg = "请登录"
  126. br.ErrMsg = "请登录,SysUser Is Empty"
  127. br.Ret = 408
  128. return
  129. }
  130. var req video.VideoSaveReq
  131. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  132. if err != nil {
  133. br.Msg = "参数解析异常!"
  134. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  135. return
  136. }
  137. if req.Title == "" {
  138. br.Msg = "标题不能为空"
  139. return
  140. }
  141. //if req.VideoUrl == "" {
  142. // br.Msg = "音频地址不能为空"
  143. // return
  144. //}
  145. videoCount, err := video.GetVideoByTitle(req.Title)
  146. if err != nil {
  147. br.Msg = "判断标题是否存在失败!"
  148. br.ErrMsg = "判断标题是否存在失败,Err:" + err.Error()
  149. return
  150. }
  151. if videoCount > 0 {
  152. br.Msg = "标题已存在!"
  153. br.IsSendEmail = false
  154. return
  155. }
  156. item := new(video.Video)
  157. item.Title = req.Title
  158. item.VideoUrl = req.VideoUrl
  159. item.VideoCoverUrl = req.VideoCoverUrl
  160. item.Status = 0
  161. item.CreateTime = time.Now()
  162. item.ModifyTime = time.Now()
  163. item.PlaySeconds = req.PlaySeconds
  164. newId, err := video.AddVideo(item)
  165. if err != nil {
  166. br.Msg = "保存失败"
  167. br.ErrMsg = "保存失败,Err:" + err.Error()
  168. return
  169. }
  170. if req.TagsIdStr != "" {
  171. tagsArr := strings.Split(req.TagsIdStr, ",")
  172. for _, v := range tagsArr {
  173. tagId, _ := strconv.Atoi(v)
  174. tagItem := new(video.VideoTags)
  175. tagItem.VideoId = int(newId)
  176. tagItem.TagsId = tagId
  177. err = video.AddVideoTags(tagItem)
  178. if err != nil {
  179. br.Msg = "保存失败"
  180. br.ErrMsg = "保存失败,Err:" + err.Error()
  181. return
  182. }
  183. }
  184. }
  185. resp := new(video.VideoSaveResp)
  186. resp.VideoId = int(newId)
  187. br.Ret = 200
  188. br.Success = true
  189. br.Msg = "保存成功"
  190. br.Data = resp
  191. }
  192. // @Title 删除视频
  193. // @Description 删除视频接口
  194. // @Param request body video.VideoDeleteReq true "type json string"
  195. // @Success Ret=200 保存成功
  196. // @router /delete [post]
  197. func (this *VideoController) Delete() {
  198. br := new(models.BaseResponse).Init()
  199. defer func() {
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. sysUser := this.SysUser
  204. if sysUser == nil {
  205. br.Msg = "请登录"
  206. br.ErrMsg = "请登录,SysUser Is Empty"
  207. br.Ret = 408
  208. return
  209. }
  210. var req video.VideoDeleteReq
  211. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  212. if err != nil {
  213. br.Msg = "参数解析异常!"
  214. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  215. return
  216. }
  217. if req.VideoId <= 0 {
  218. br.Msg = "参数错误"
  219. return
  220. }
  221. err = video.DeleteVideo(req.VideoId)
  222. if err != nil {
  223. br.Msg = "删除失败!"
  224. br.ErrMsg = "删除失败,Err:" + err.Error()
  225. return
  226. }
  227. br.Ret = 200
  228. br.Success = true
  229. br.Msg = "删除成功"
  230. }
  231. // @Title 批量发布视频
  232. // @Description 批量发布视频接口
  233. // @Param request body video.VideoPublishReq true "type json string"
  234. // @Success Ret=200 保存成功
  235. // @router /publish [post]
  236. func (this *VideoController) Publish() {
  237. br := new(models.BaseResponse).Init()
  238. defer func() {
  239. this.Data["json"] = br
  240. this.ServeJSON()
  241. }()
  242. sysUser := this.SysUser
  243. if sysUser == nil {
  244. br.Msg = "请登录"
  245. br.ErrMsg = "请登录,SysUser Is Empty"
  246. br.Ret = 408
  247. return
  248. }
  249. var req video.VideoPublishReq
  250. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  251. if err != nil {
  252. br.Msg = "参数解析异常!"
  253. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  254. return
  255. }
  256. if req.VideoIds == "" {
  257. br.Msg = "参数错误"
  258. return
  259. }
  260. err = video.PublishVideo(req.VideoIds)
  261. if err != nil {
  262. br.Msg = "发布失败"
  263. br.ErrMsg = "发布失败,Err:" + err.Error()
  264. return
  265. }
  266. br.Ret = 200
  267. br.Success = true
  268. br.Msg = "发布成功"
  269. }
  270. // @Title 编辑视频
  271. // @Description 编辑视频接口
  272. // @Param request body video.VideoEditReq true "type json string"
  273. // @Success Ret=200 编辑成功
  274. // @router /edit [post]
  275. func (this *VideoController) Edit() {
  276. br := new(models.BaseResponse).Init()
  277. defer func() {
  278. this.Data["json"] = br
  279. this.ServeJSON()
  280. }()
  281. sysUser := this.SysUser
  282. if sysUser == nil {
  283. br.Msg = "请登录"
  284. br.ErrMsg = "请登录,SysUser Is Empty"
  285. br.Ret = 408
  286. return
  287. }
  288. var req video.VideoEditReq
  289. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  290. if err != nil {
  291. br.Msg = "参数解析异常!"
  292. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  293. return
  294. }
  295. if req.Title == "" {
  296. br.Msg = "标题不能为空"
  297. return
  298. }
  299. if req.VideoUrl == "" {
  300. br.Msg = "音频地址不能为空"
  301. return
  302. }
  303. item := new(video.Video)
  304. item.Title = req.Title
  305. item.VideoUrl = req.VideoUrl
  306. item.VideoCoverUrl = req.VideoCoverUrl
  307. item.Status = 0
  308. item.CreateTime = time.Now()
  309. item.ModifyTime = time.Now()
  310. item.PlaySeconds = req.PlaySeconds
  311. item.VideoId = req.VideoId
  312. err = video.EditVideo(item, req.TagsIdStr)
  313. if err != nil {
  314. br.Msg = "编辑失败"
  315. br.ErrMsg = "编辑失败,Err:" + err.Error()
  316. return
  317. }
  318. br.Ret = 200
  319. br.Success = true
  320. br.Msg = "编辑成功"
  321. }
  322. // @Title 取消视频发布
  323. // @Description 取消视频发布接口
  324. // @Param request body video.VideoPublishReq true "type json string"
  325. // @Success Ret=200 保存成功
  326. // @router /publish/cancel [post]
  327. func (this *VideoController) PublishCancel() {
  328. br := new(models.BaseResponse).Init()
  329. defer func() {
  330. this.Data["json"] = br
  331. this.ServeJSON()
  332. }()
  333. sysUser := this.SysUser
  334. if sysUser == nil {
  335. br.Msg = "请登录"
  336. br.ErrMsg = "请登录,SysUser Is Empty"
  337. br.Ret = 408
  338. return
  339. }
  340. var req video.VideoPublishReq
  341. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  342. if err != nil {
  343. br.Msg = "参数解析异常!"
  344. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  345. return
  346. }
  347. if req.VideoIds == "" {
  348. br.Msg = "参数错误"
  349. return
  350. }
  351. err = video.PublishVideoCancel(req.VideoIds)
  352. if err != nil {
  353. br.Msg = "取消发布失败"
  354. br.ErrMsg = "取消发布失败,Err:" + err.Error()
  355. return
  356. }
  357. br.Ret = 200
  358. br.Success = true
  359. br.Msg = "取消发布成功"
  360. }