eta_training_video.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package controller
  2. import (
  3. "eta/eta_docs/controller/resp"
  4. "eta/eta_docs/global"
  5. "eta/eta_docs/models/base"
  6. "eta/eta_docs/models/crm"
  7. "eta/eta_docs/models/request"
  8. "eta/eta_docs/models/response"
  9. "eta/eta_docs/services"
  10. "eta/eta_docs/utils"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "github.com/go-playground/validator/v10"
  14. "strings"
  15. "text/template"
  16. "time"
  17. )
  18. // EtaTrainingVideoController 培训视频
  19. type EtaTrainingVideoController struct{}
  20. // ClassifyTree
  21. // @Description 分类树
  22. // @Success 200 {string} string "获取成功"
  23. // @Router /classify/tree [get]
  24. func (a *EtaTrainingVideoController) ClassifyTree(c *gin.Context) {
  25. result := new(response.EtaTrainingVideoClassifyResp)
  26. result.List = make([]*response.EtaTrainingVideoClassifyItem, 0)
  27. // 获取所有分类
  28. ob := new(crm.EtaTrainingVideoClassify)
  29. classifies, e := ob.GetItemsByCondition(``, make([]interface{}, 0), ``)
  30. if e != nil {
  31. resp.FailMsg("获取失败", "获取分类列表失败, Err: "+e.Error(), c)
  32. return
  33. }
  34. items := make([]*response.EtaTrainingVideoClassifyItem, 0)
  35. for _, v := range classifies {
  36. t := &response.EtaTrainingVideoClassifyItem{
  37. ClassifyId: v.EtaTrainingVideoClassifyId,
  38. ClassifyName: v.ClassifyName,
  39. ParentId: v.ParentId,
  40. Sort: v.Sort,
  41. Children: make([]*response.EtaTrainingVideoClassifyItem, 0),
  42. }
  43. items = append(items, t)
  44. }
  45. // 递归返回树形结构
  46. items = services.GetClassifyTreeRecursive(items, 0)
  47. result.List = items
  48. resp.OkData("获取成功", result, c)
  49. }
  50. // TagList
  51. // @Description 标签列表
  52. // @Success 200 {string} string "获取成功"
  53. // @Router /tag/list [get]
  54. func (a *EtaTrainingVideoController) TagList(c *gin.Context) {
  55. // 获取列表数据
  56. tagOB := new(crm.EtaTrainingVideoTag)
  57. list, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), "")
  58. if e != nil {
  59. resp.FailMsg("获取失败", "获取标签列表失败, Err: "+e.Error(), c)
  60. return
  61. }
  62. result := make([]*response.EtaTrainingVideoTagItem, 0)
  63. for _, v := range list {
  64. t := new(response.EtaTrainingVideoTagItem)
  65. t.TagId = v.EtaTrainingVideoTagId
  66. t.TagName = v.TagName
  67. t.VideoTotal = v.VideoTotal
  68. t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  69. t.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
  70. result = append(result, t)
  71. }
  72. resp.OkData("获取成功", result, c)
  73. }
  74. // VideoPageList
  75. // @Description 视频列表-分页
  76. // @Success 200 {string} string "获取成功"
  77. // @Router /video/page_list [get]
  78. func (a *EtaTrainingVideoController) VideoPageList(c *gin.Context) {
  79. var req request.TrainingVideoPageListReq
  80. if e := c.BindQuery(&req); e != nil {
  81. err, ok := e.(validator.ValidationErrors)
  82. if !ok {
  83. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  84. return
  85. }
  86. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  87. return
  88. }
  89. // 筛选项
  90. cond := fmt.Sprintf(`%s = ?`, crm.EtaTrainingVideoColumns.PublishState)
  91. pars := make([]interface{}, 0)
  92. pars = append(pars, 1)
  93. {
  94. // 关键词-标题/简介
  95. keyword := strings.TrimSpace(req.Keyword)
  96. keyword = template.HTMLEscapeString(keyword)
  97. if keyword != "" {
  98. kw := fmt.Sprint("%", keyword, "%")
  99. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, crm.EtaTrainingVideoColumns.Title, crm.EtaTrainingVideoColumns.Introduce)
  100. pars = append(pars, kw, kw)
  101. }
  102. // 分类ID
  103. classifyId := req.ClassifyId
  104. if classifyId > 0 {
  105. cond += fmt.Sprintf(` AND FIND_IN_SET(?, %s)`, crm.EtaTrainingVideoColumns.ClassifyIds)
  106. pars = append(pars, classifyId)
  107. }
  108. // 标签IDs
  109. strTagIds := req.TagIds
  110. if strTagIds != "" {
  111. strTags := strings.Split(strTagIds, ",")
  112. if len(strTags) > 0 {
  113. joinArr := make([]string, 0)
  114. cond += ` AND (`
  115. for _, s := range strTags {
  116. joinArr = append(joinArr, fmt.Sprintf(`FIND_IN_SET(?, %s)`, crm.EtaTrainingVideoColumns.TagIds))
  117. pars = append(pars, s)
  118. }
  119. cond += strings.Join(joinArr, ` OR `)
  120. cond += `)`
  121. } else {
  122. cond += ` AND 1=2`
  123. }
  124. }
  125. }
  126. page := new(base.Page)
  127. page.SetPageSize(req.PageSize)
  128. page.SetCurrent(req.CurrentIndex)
  129. // 排序
  130. if req.IsHot {
  131. page.AddOrderItem(base.OrderItem{Column: crm.EtaTrainingVideoColumns.ViewTotal, Asc: false})
  132. } else {
  133. // 默认和最新均为发布时间排序
  134. page.AddOrderItem(base.OrderItem{Column: crm.EtaTrainingVideoColumns.PublishTime, Asc: false})
  135. }
  136. // 获取列表
  137. videoOb := new(crm.EtaTrainingVideo)
  138. total, list, e := videoOb.GetPageItems(page, cond, pars)
  139. if e != nil {
  140. resp.FailData("获取失败", "获取视频列表失败, Err: "+e.Error(), c)
  141. return
  142. }
  143. items, e := services.FormatVideosToVideoItems(list)
  144. if e != nil {
  145. resp.FailData("获取失败", "格式化视频列表失败, Err: "+e.Error(), c)
  146. return
  147. }
  148. page.SetTotal(total)
  149. baseData := new(base.BaseData)
  150. baseData.SetPage(page)
  151. baseData.SetList(items)
  152. resp.OkData("获取成功", baseData, c)
  153. }
  154. // VideoDetail
  155. // @Description 视频详情
  156. // @Success 200 {string} string "获取成功"
  157. // @Router /video/detail [get]
  158. func (a *EtaTrainingVideoController) VideoDetail(c *gin.Context) {
  159. var req request.TrainingVideoDetailReq
  160. if e := c.BindQuery(&req); e != nil {
  161. err, ok := e.(validator.ValidationErrors)
  162. if !ok {
  163. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  164. return
  165. }
  166. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  167. return
  168. }
  169. req.VideoCode = strings.TrimSpace(req.VideoCode)
  170. if req.VideoCode == "" {
  171. resp.Fail("参数有误", c)
  172. return
  173. }
  174. req.BusinessCode = strings.TrimSpace(req.BusinessCode)
  175. // 获取视频
  176. ob := new(crm.EtaTrainingVideo)
  177. cond := fmt.Sprintf(`%s = ? AND %s = ?`, crm.EtaTrainingVideoColumns.VideoCode, crm.EtaTrainingVideoColumns.PublishState)
  178. pars := make([]interface{}, 0)
  179. pars = append(pars, req.VideoCode, 1)
  180. item, e := ob.GetItem(cond, pars)
  181. if e != nil {
  182. if e == utils.ErrNoRow {
  183. resp.Fail("视频已下架", c)
  184. return
  185. }
  186. resp.FailMsg("获取失败", "获取视频失败, Err: "+e.Error(), c)
  187. return
  188. }
  189. list := make([]*crm.EtaTrainingVideo, 0)
  190. list = append(list, item)
  191. formats, e := services.FormatVideosToVideoItems(list)
  192. if e != nil {
  193. resp.FailMsg("获取失败", "格式化视频信息失败, Err: "+e.Error(), c)
  194. return
  195. }
  196. result := new(response.EtaTrainingVideoItem)
  197. if len(formats) > 0 {
  198. result = formats[0]
  199. }
  200. // 更新访问量、访问记录
  201. go func() {
  202. _ = ob.UpdateViewTotal(item.EtaTrainingVideoId)
  203. if req.BusinessCode != "" {
  204. businessOB := new(crm.EtaBusiness)
  205. businessCond := `code_encrypt = ?`
  206. businessPars := make([]interface{}, 0)
  207. businessPars = append(businessPars, req.BusinessCode)
  208. business, _ := businessOB.GetItemByCondition(businessCond, businessPars, "")
  209. if business == nil {
  210. return
  211. }
  212. newLog := new(crm.EtaTrainingVideoViewLog)
  213. newLog.EtaTrainingVideoId = item.EtaTrainingVideoId
  214. newLog.EtaBusinessId = business.EtaBusinessId
  215. newLog.EtaBusinessCodeEncrypt = business.CodeEncrypt
  216. newLog.CreateTime = time.Now().Local()
  217. _ = newLog.Create()
  218. }
  219. }()
  220. resp.OkData("获取成功", result, c)
  221. }