package eta_training_video import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hz_crm_api/controllers" "hongze/hz_crm_api/models" "hongze/hz_crm_api/models/eta_training_video" etaTrainingVideoService "hongze/hz_crm_api/services/eta_training_video" "hongze/hz_crm_api/utils" "html/template" "strings" "time" ) // EtaTrainingVideoController ETA培训视频 type EtaTrainingVideoController struct { controllers.BaseAuthController } // PageList // @Title 视频列表-分页 // @Description 视频列表-分页 // @Param PageSize query int true "每页数据量" // @Param CurrentIndex query int true "当前页码" // @Param Keyword query string false "关键词: 视频名称/社会信用码/视频编码" // @Param StartTime query string false "创建时间区间: 开始时间" // @Param EndTime query string false "创建时间区间: 结束时间" // @Param ClassifyId query int false "分类ID" // @Param TagIds query string false "标签IDs, 英文逗号拼接" // @Param PublishState query int false "发布状态: 1-未发布; 2-已发布" // @Success 200 Ret=200 获取成功 // @router /page_list [get] func (this *EtaTrainingVideoController) PageList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 分页 pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = utils.StartIndex(currentIndex, pageSize) // 筛选项 cond := `` pars := make([]interface{}, 0) { // 关键词 keyword := this.GetString("Keyword", "") keyword = strings.TrimSpace(keyword) keyword = template.HTMLEscapeString(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") cond += fmt.Sprintf(` AND (%s LIKE ?)`, eta_training_video.VideoColumns.Title) pars = append(pars, kw) } // 创建时间 startTime := this.GetString("StartTime", "") endTime := this.GetString("EndTime", "") if startTime != "" && endTime != "" { st := fmt.Sprint(startTime, " 00:00:00") ed := fmt.Sprint(endTime, " 23:59:59") cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, eta_training_video.VideoColumns.CreateTime) pars = append(pars, st, ed) } // 分类ID classifyId, _ := this.GetInt("ClassifyId", 0) if classifyId > 0 { cond += fmt.Sprintf(` AND FIND_IN_SET(?, %s)`, eta_training_video.VideoColumns.ClassifyIds) pars = append(pars, classifyId) } // 标签IDs strTagIds := this.GetString("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)`, eta_training_video.VideoColumns.TagIds)) pars = append(pars, s) } cond += strings.Join(joinArr, ` OR `) cond += `)` } else { cond += ` AND 1=2` } } // 发布状态 publishState, _ := this.GetInt("PublishState", 0) if publishState > 0 { stateMap := map[int]int{1: 0, 2: 1} cond += fmt.Sprintf(` AND %s = ?`, eta_training_video.VideoColumns.PublishState) pars = append(pars, stateMap[publishState]) } } // 获取列表 videoOb := new(eta_training_video.EtaTrainingVideo) total, e := videoOb.GetCountByCondition(cond, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取视频总数失败, Err: " + e.Error() return } list, e := videoOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, pageSize) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取视频列表失败, Err: " + e.Error() return } items, e := etaTrainingVideoService.FormatVideosToVideoItems(list) if e != nil { br.Msg = "获取失败" br.ErrMsg = e.Error() return } resp := new(eta_training_video.EtaTrainingVideoListResp) resp.List = items resp.Paging = paging.GetPaging(currentIndex, pageSize, total) br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Add // @Title 新增视频 // @Description 新增视频 // @Param request body eta_training_video.EtaTrainingVideoAddReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /add [post] func (this *EtaTrainingVideoController) Add() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req eta_training_video.EtaTrainingVideoAddReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.ClassifyId <= 0 { br.Msg = "请选择视频分类" return } req.Title = strings.TrimSpace(req.Title) if req.Title == "" { br.Msg = "请输入视频名称" return } if req.Introduce != "" { req.Introduce = strings.TrimSpace(req.Introduce) req.Introduce = template.HTMLEscapeString(req.Introduce) } req.CoverImg = strings.TrimSpace(req.CoverImg) if req.CoverImg == "" { br.Msg = "请上传封面图" return } req.VideoUrl = strings.TrimSpace(req.VideoUrl) if req.VideoUrl == "" { br.Msg = "请上传视频" return } // 分类 strClassifyIds := "" classifyRelates := make([]*eta_training_video.EtaTrainingVideoClassifyRelate, 0) { classifyOB := new(eta_training_video.EtaTrainingVideoClassify) classifies, e := classifyOB.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取分类失败, Err: " + e.Error() return } for _, c := range classifies { if c.EtaTrainingVideoClassifyId != req.ClassifyId { continue } strClassifyIds += fmt.Sprintf("%d,", req.ClassifyId) classifyRelates = append(classifyRelates, &eta_training_video.EtaTrainingVideoClassifyRelate{ EtaTrainingVideoClassifyId: c.EtaTrainingVideoClassifyId, ClassifyParentId: c.ParentId, ClassifyName: c.ClassifyName, }) // 分类所有父级 if c.ParentId > 0 { parents := etaTrainingVideoService.GetClassifyParentsRecursive(classifies, c.ParentId) for _, p := range parents { strClassifyIds += fmt.Sprintf("%d,", p.EtaTrainingVideoClassifyId) classifyRelates = append(classifyRelates, &eta_training_video.EtaTrainingVideoClassifyRelate{ EtaTrainingVideoClassifyId: p.EtaTrainingVideoClassifyId, ClassifyParentId: p.ParentId, ClassifyName: p.ClassifyName, }) } } } strClassifyIds = strings.TrimRight(strClassifyIds, ",") } // 标签 strTagIds := "" tagRelates := make([]*eta_training_video.EtaTrainingVideoTagRelate, 0) if len(req.TagIds) > 0 { tagOB := new(eta_training_video.EtaTrainingVideoTag) tags, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取标签失败, Err: " + e.Error() return } tagMap := make(map[int]*eta_training_video.EtaTrainingVideoTag) for _, t := range tags { tagMap[t.EtaTrainingVideoTagId] = t } for _, r := range req.TagIds { t := tagMap[r] if t == nil { continue } tagRelates = append(tagRelates, &eta_training_video.EtaTrainingVideoTagRelate{ EtaTrainingVideoTagId: r, TagName: t.TagName, }) strTagIds += fmt.Sprintf("%d,", r) } strTagIds = strings.TrimRight(strTagIds, ",") } // 视频编码 now := time.Now().Local() videoCode := utils.MD5(fmt.Sprint(now.UnixMicro())) item := new(eta_training_video.EtaTrainingVideo) item.VideoCode = videoCode item.Title = req.Title item.Introduce = req.Introduce item.ClassifyIds = strClassifyIds item.TagIds = strTagIds item.CoverImg = req.CoverImg item.VideoUrl = req.VideoUrl item.CreateTime = now item.ModifyTime = now if e := item.CreateVideoAndRelates(item, classifyRelates, tagRelates); e != nil { br.Msg = "操作失败" br.ErrMsg = "新增视频及关联失败, Err: " + e.Error() return } // 操作日志 go func() { logItem := new(eta_training_video.EtaTrainingVideoOpLog) logItem.EtaTrainingVideoId = item.EtaTrainingVideoId logItem.SysUserId = sysUser.AdminId logItem.SysRealName = sysUser.RealName logItem.OpType = eta_training_video.VideoOpTypeAdd logItem.OpData = string(this.Ctx.Input.RequestBody) logItem.CreateTime = now _ = logItem.Create() }() // 更新标签引用数 go func() { tagOB := new(eta_training_video.EtaTrainingVideoTag) for _, t := range tagRelates { _ = tagOB.UpdateVideoTotal(t.EtaTrainingVideoTagId) } }() br.Data = item.EtaTrainingVideoId br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Edit // @Title 编辑视频 // @Description 编辑视频 // @Param request body eta_training_video.EtaTrainingVideoEditReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /edit [post] func (this *EtaTrainingVideoController) Edit() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req eta_training_video.EtaTrainingVideoEditReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.VideoId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, 视频ID: %d", req.VideoId) return } if req.ClassifyId <= 0 { br.Msg = "请选择视频分类" return } req.Title = strings.TrimSpace(req.Title) if req.Title == "" { br.Msg = "请输入视频名称" return } if req.Introduce == "" { req.Introduce = strings.TrimSpace(req.Introduce) req.Introduce = template.HTMLEscapeString(req.Introduce) } req.CoverImg = strings.TrimSpace(req.CoverImg) if req.CoverImg == "" { br.Msg = "请上传封面图" return } req.VideoUrl = strings.TrimSpace(req.VideoUrl) if req.VideoUrl == "" { br.Msg = "请上传视频" return } ob := new(eta_training_video.EtaTrainingVideo) item, e := ob.GetItemById(req.VideoId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "视频不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取视频失败, Err: " + e.Error() return } // 分类 strClassifyIds := "" classifyRelates := make([]*eta_training_video.EtaTrainingVideoClassifyRelate, 0) { classifyOB := new(eta_training_video.EtaTrainingVideoClassify) classifies, e := classifyOB.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取分类失败, Err: " + e.Error() return } for _, c := range classifies { if c.EtaTrainingVideoClassifyId != req.ClassifyId { continue } strClassifyIds += fmt.Sprintf("%d,", req.ClassifyId) classifyRelates = append(classifyRelates, &eta_training_video.EtaTrainingVideoClassifyRelate{ EtaTrainingVideoClassifyId: c.EtaTrainingVideoClassifyId, ClassifyParentId: c.ParentId, ClassifyName: c.ClassifyName, }) // 分类所有父级 if c.ParentId > 0 { parents := etaTrainingVideoService.GetClassifyParentsRecursive(classifies, c.ParentId) for _, p := range parents { strClassifyIds += fmt.Sprintf("%d,", p.EtaTrainingVideoClassifyId) classifyRelates = append(classifyRelates, &eta_training_video.EtaTrainingVideoClassifyRelate{ EtaTrainingVideoClassifyId: p.EtaTrainingVideoClassifyId, ClassifyParentId: p.ParentId, ClassifyName: p.ClassifyName, }) } } } strClassifyIds = strings.TrimRight(strClassifyIds, ",") } // 标签 strTagIds := "" tagRelates := make([]*eta_training_video.EtaTrainingVideoTagRelate, 0) if len(req.TagIds) > 0 { tagOB := new(eta_training_video.EtaTrainingVideoTag) tags, e := tagOB.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = "获取标签失败, Err: " + e.Error() return } tagMap := make(map[int]*eta_training_video.EtaTrainingVideoTag) for _, t := range tags { tagMap[t.EtaTrainingVideoTagId] = t } for _, r := range req.TagIds { t := tagMap[r] if t == nil { continue } tagRelates = append(tagRelates, &eta_training_video.EtaTrainingVideoTagRelate{ EtaTrainingVideoTagId: r, TagName: t.TagName, }) strTagIds += fmt.Sprintf("%d,", r) } strTagIds = strings.TrimRight(strTagIds, ",") } now := time.Now().Local() item.Title = req.Title item.Introduce = req.Introduce item.ClassifyIds = strClassifyIds item.TagIds = strTagIds item.CoverImg = req.CoverImg item.VideoUrl = req.VideoUrl item.ModifyTime = now cols := []string{"Title", "Introduce", "ClassifyIds", "TagIds", "CoverImg", "VideoUrl", "ModifyTime"} if e := item.UpdateVideoAndRelates(item, cols, classifyRelates, tagRelates); e != nil { br.Msg = "操作失败" br.ErrMsg = "更新视频及关联失败, Err: " + e.Error() return } // 操作日志 go func() { logItem := new(eta_training_video.EtaTrainingVideoOpLog) logItem.EtaTrainingVideoId = item.EtaTrainingVideoId logItem.SysUserId = sysUser.AdminId logItem.SysRealName = sysUser.RealName logItem.OpType = eta_training_video.VideoOpTypeEdit logItem.OpData = string(this.Ctx.Input.RequestBody) logItem.CreateTime = now _ = logItem.Create() }() // 更新标签引用数 go func() { tagOB := new(eta_training_video.EtaTrainingVideoTag) for _, t := range tagRelates { _ = tagOB.UpdateVideoTotal(t.EtaTrainingVideoTagId) } }() br.Data = item.EtaTrainingVideoId br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Publish // @Title 发布/取消视频 // @Description 发布/取消视频 // @Param request body eta_training_video.EtaTrainingVideoPublishReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /publish [post] func (this *EtaTrainingVideoController) Publish() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req eta_training_video.EtaTrainingVideoPublishReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.VideoId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprint("参数有误, 视频ID: ", req.VideoId) return } if req.PublishState != 0 && req.PublishState != 1 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprint("参数有误, 发布状态: ", req.PublishState) return } ob := new(eta_training_video.EtaTrainingVideo) item, e := ob.GetItemById(req.VideoId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "视频不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取ETA培训视频失败, Err: " + e.Error() return } now := time.Now().Local() item.PublishState = req.PublishState item.ModifyTime = now cols := []string{"PublishState", "ModifyTime"} if item.PublishState == 1 { item.PublishTime = now cols = append(cols, "PublishTime") } if e := item.Update(cols); e != nil { br.Msg = "操作失败" br.ErrMsg = "发布ETA培训视频失败, Err: " + e.Error() return } // 操作日志 go func() { logItem := new(eta_training_video.EtaTrainingVideoOpLog) logItem.EtaTrainingVideoId = item.EtaTrainingVideoId logItem.SysUserId = sysUser.AdminId logItem.SysRealName = sysUser.RealName opTypeMap := map[int]int{0: eta_training_video.VideoOpTypeCancelPublish, 1: eta_training_video.VideoOpTypePublish} logItem.OpType = opTypeMap[req.PublishState] logItem.OpData = string(this.Ctx.Input.RequestBody) logItem.CreateTime = now _ = logItem.Create() }() br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Remove // @Title 删除视频 // @Description 删除视频 // @Param request body eta_training_video.EtaTrainingVideoRemoveReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /remove [post] func (this *EtaTrainingVideoController) Remove() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } // 参数校验 var req eta_training_video.EtaTrainingVideoRemoveReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + e.Error() return } if req.VideoId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprint("参数有误, 视频ID: ", req.VideoId) return } ob := new(eta_training_video.EtaTrainingVideo) item, e := ob.GetItemById(req.VideoId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Ret = 200 br.Success = true br.Msg = "操作成功" return } br.Msg = "操作失败" br.ErrMsg = "获取ETA培训视频失败, Err: " + e.Error() return } if e := item.Del(); e != nil { br.Msg = "操作失败" br.ErrMsg = "删除ETA培训视频失败, Err: " + e.Error() return } // 移除分类、标签关联, 更新标签引用数 go func() { cr := new(eta_training_video.EtaTrainingVideoClassifyRelate) _ = cr.RemoveRelateByVideoId(item.EtaTrainingVideoId) tr := new(eta_training_video.EtaTrainingVideoTagRelate) _ = tr.RemoveRelateByVideoId(item.EtaTrainingVideoId) t := new(eta_training_video.EtaTrainingVideoTag) _ = t.RemoveVideoTotalByVideoId(item.EtaTrainingVideoId) }() // 操作日志 go func() { logItem := new(eta_training_video.EtaTrainingVideoOpLog) logItem.EtaTrainingVideoId = item.EtaTrainingVideoId logItem.SysUserId = sysUser.AdminId logItem.SysRealName = sysUser.RealName logItem.OpType = eta_training_video.VideoOpTypeRemove b, e := json.Marshal(item) if e != nil { return } logItem.OpData = string(b) logItem.CreateTime = time.Now().Local() _ = logItem.Create() }() br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Detail // @Title 视频详情 // @Description 视频详情 // @Param VideoId query int false "视频ID" // @Success 200 Ret=200 操作成功 // @router /detail [get] func (this *EtaTrainingVideoController) Detail() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } videoId, _ := this.GetInt("VideoId", 0) if videoId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprint("参数有误, 视频ID: ", videoId) return } ob := new(eta_training_video.EtaTrainingVideo) item, e := ob.GetItemById(videoId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "视频不存在, 请刷新页面" return } br.Msg = "获取失败" br.ErrMsg = "获取ETA培训视频失败, Err: " + e.Error() return } list := make([]*eta_training_video.EtaTrainingVideo, 0) list = append(list, item) formats, e := etaTrainingVideoService.FormatVideosToVideoItems(list) if e != nil { br.Msg = "获取失败" br.ErrMsg = "格式化视频信息失败, Err: " + e.Error() return } result := new(eta_training_video.EtaTrainingVideoItem) if len(formats) > 0 { result = formats[0] } br.Data = result br.Ret = 200 br.Success = true br.Msg = "获取成功" }