123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package controller
- import (
- "eta/eta_docs/controller/resp"
- "eta/eta_docs/global"
- "eta/eta_docs/models/base"
- "eta/eta_docs/models/crm"
- "eta/eta_docs/models/request"
- "eta/eta_docs/models/response"
- "eta/eta_docs/services"
- "eta/eta_docs/utils"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "strings"
- "text/template"
- "time"
- )
- // EtaTrainingVideoController 培训视频
- type EtaTrainingVideoController struct{}
- // ClassifyTree
- // @Description 分类树
- // @Success 200 {string} string "获取成功"
- // @Router /classify/tree [get]
- func (a *EtaTrainingVideoController) ClassifyTree(c *gin.Context) {
- result := new(response.EtaTrainingVideoClassifyResp)
- result.List = make([]*response.EtaTrainingVideoClassifyItem, 0)
- // 获取所有分类
- ob := new(crm.EtaTrainingVideoClassify)
- classifies, e := ob.GetItemsByCondition(``, make([]interface{}, 0), ``)
- if e != nil {
- resp.FailMsg("获取失败", "获取分类列表失败, Err: "+e.Error(), c)
- return
- }
- items := make([]*response.EtaTrainingVideoClassifyItem, 0)
- for _, v := range classifies {
- t := &response.EtaTrainingVideoClassifyItem{
- ClassifyId: v.EtaTrainingVideoClassifyId,
- ClassifyName: v.ClassifyName,
- ParentId: v.ParentId,
- Sort: v.Sort,
- Children: make([]*response.EtaTrainingVideoClassifyItem, 0),
- }
- items = append(items, t)
- }
- // 递归返回树形结构
- items = services.GetClassifyTreeRecursive(items, 0)
- result.List = items
- resp.OkData("获取成功", result, c)
- }
- // TagList
- // @Description 标签列表
- // @Success 200 {string} string "获取成功"
- // @Router /tag/list [get]
- func (a *EtaTrainingVideoController) TagList(c *gin.Context) {
- // 获取列表数据
- tagOB := new(crm.EtaTrainingVideoTag)
- list, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), "")
- if e != nil {
- resp.FailMsg("获取失败", "获取标签列表失败, Err: "+e.Error(), c)
- return
- }
- result := make([]*response.EtaTrainingVideoTagItem, 0)
- for _, v := range list {
- t := new(response.EtaTrainingVideoTagItem)
- t.TagId = v.EtaTrainingVideoTagId
- t.TagName = v.TagName
- t.VideoTotal = v.VideoTotal
- t.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
- t.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
- result = append(result, t)
- }
- resp.OkData("获取成功", result, c)
- }
- // VideoPageList
- // @Description 视频列表-分页
- // @Success 200 {string} string "获取成功"
- // @Router /video/page_list [get]
- func (a *EtaTrainingVideoController) VideoPageList(c *gin.Context) {
- var req request.TrainingVideoPageListReq
- if e := c.BindQuery(&req); e != nil {
- err, ok := e.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- return
- }
- resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- return
- }
- // 筛选项
- cond := fmt.Sprintf(`%s = ?`, crm.EtaTrainingVideoColumns.PublishState)
- pars := make([]interface{}, 0)
- pars = append(pars, 1)
- {
- // 关键词-标题/简介
- keyword := strings.TrimSpace(req.Keyword)
- keyword = template.HTMLEscapeString(keyword)
- if keyword != "" {
- kw := fmt.Sprint("%", keyword, "%")
- cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, crm.EtaTrainingVideoColumns.Title, crm.EtaTrainingVideoColumns.Introduce)
- pars = append(pars, kw, kw)
- }
- // 分类ID
- classifyId := req.ClassifyId
- if classifyId > 0 {
- cond += fmt.Sprintf(` AND FIND_IN_SET(?, %s)`, crm.EtaTrainingVideoColumns.ClassifyIds)
- pars = append(pars, classifyId)
- }
- // 标签IDs
- strTagIds := req.TagIds
- if strTagIds != "" {
- strTags := strings.Split(strTagIds, ",")
- if len(strTags) > 0 {
- joinArr := make([]string, 0)
- cond += ` AND (`
- for _, s := range strTags {
- joinArr = append(joinArr, fmt.Sprintf(`FIND_IN_SET(?, %s)`, crm.EtaTrainingVideoColumns.TagIds))
- pars = append(pars, s)
- }
- cond += strings.Join(joinArr, ` OR `)
- cond += `)`
- } else {
- cond += ` AND 1=2`
- }
- }
- }
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.CurrentIndex)
- // 排序
- if req.IsHot {
- page.AddOrderItem(base.OrderItem{Column: crm.EtaTrainingVideoColumns.ViewTotal, Asc: false})
- } else {
- // 默认和最新均为发布时间排序
- page.AddOrderItem(base.OrderItem{Column: crm.EtaTrainingVideoColumns.PublishTime, Asc: false})
- }
- // 获取列表
- videoOb := new(crm.EtaTrainingVideo)
- total, list, e := videoOb.GetPageItems(page, cond, pars)
- if e != nil {
- resp.FailData("获取失败", "获取视频列表失败, Err: "+e.Error(), c)
- return
- }
- items, e := services.FormatVideosToVideoItems(list)
- if e != nil {
- resp.FailData("获取失败", "格式化视频列表失败, Err: "+e.Error(), c)
- return
- }
- page.SetTotal(total)
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- baseData.SetList(items)
- resp.OkData("获取成功", baseData, c)
- }
- // VideoDetail
- // @Description 视频详情
- // @Success 200 {string} string "获取成功"
- // @Router /video/detail [get]
- func (a *EtaTrainingVideoController) VideoDetail(c *gin.Context) {
- var req request.TrainingVideoDetailReq
- if e := c.BindQuery(&req); e != nil {
- err, ok := e.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- return
- }
- resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- return
- }
- req.VideoCode = strings.TrimSpace(req.VideoCode)
- if req.VideoCode == "" {
- resp.Fail("参数有误", c)
- return
- }
- req.BusinessCode = strings.TrimSpace(req.BusinessCode)
- // 获取视频
- ob := new(crm.EtaTrainingVideo)
- cond := fmt.Sprintf(`%s = ? AND %s = ?`, crm.EtaTrainingVideoColumns.VideoCode, crm.EtaTrainingVideoColumns.PublishState)
- pars := make([]interface{}, 0)
- pars = append(pars, req.VideoCode, 1)
- item, e := ob.GetItem(cond, pars)
- if e != nil {
- if e == utils.ErrNoRow {
- resp.Fail("视频已下架", c)
- return
- }
- resp.FailMsg("获取失败", "获取视频失败, Err: "+e.Error(), c)
- return
- }
- list := make([]*crm.EtaTrainingVideo, 0)
- list = append(list, item)
- formats, e := services.FormatVideosToVideoItems(list)
- if e != nil {
- resp.FailMsg("获取失败", "格式化视频信息失败, Err: "+e.Error(), c)
- return
- }
- result := new(response.EtaTrainingVideoItem)
- if len(formats) > 0 {
- result = formats[0]
- }
- // 更新访问量、访问记录
- go func() {
- _ = ob.UpdateViewTotal(item.EtaTrainingVideoId)
- if req.BusinessCode != "" {
- businessOB := new(crm.EtaBusiness)
- businessCond := `code_encrypt = ?`
- businessPars := make([]interface{}, 0)
- businessPars = append(businessPars, req.BusinessCode)
- business, _ := businessOB.GetItemByCondition(businessCond, businessPars, "")
- if business == nil {
- return
- }
- newLog := new(crm.EtaTrainingVideoViewLog)
- newLog.EtaTrainingVideoId = item.EtaTrainingVideoId
- newLog.EtaBusinessId = business.EtaBusinessId
- newLog.EtaBusinessCodeEncrypt = business.CodeEncrypt
- newLog.CreateTime = time.Now().Local()
- _ = newLog.Create()
- }
- }()
- resp.OkData("获取成功", result, c)
- }
|