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) }