package controllers

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/video"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

// 视频
type VideoController struct {
	BaseAuthController
}

// @Title 获取视频标签列表
// @Description 获取视频标签列表接口
// @Success 200 {object} video.VideoTagsListResp
// @router /tags/list [get]
func (this *VideoController) TagsList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		return
	}
	list, err := video.GetVideoTagsList()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}

	resp := new(video.VideoTagsListResp)
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 视频列表
// @Description 视频列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Success 200 {object} video.VideoListResp
// @router /list [get]
func (this *VideoController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		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")
	keyWord := this.GetString("KeyWord")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	var condition string
	var pars []interface{}

	if keyWord != "" {
		videoIdStr, err := video.GetVideoIdByKeyWord(keyWord)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败,Err:" + err.Error()
			return
		}
		if videoIdStr != "" {
			condition += ` AND (a.title LIKE '%` + keyWord + `%' OR a.video_id IN(` + videoIdStr + `) ) `
		} else {
			condition += ` AND a.title LIKE '%` + keyWord + `%' `
		}
	}

	total, err := video.GetVideoListCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据总数失败，Err:" + err.Error()
		return
	}

	list, err := video.GetVideoList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(video.VideoListResp)
	resp.List = list
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 保存视频
// @Description 保存视频接口
// @Param	request	body video.VideoSaveReq true "type json string"
// @Success 200 {object} video.VideoSaveResp
// @router /save [post]
func (this *VideoController) Save() {
	br := new(models.BaseResponse).Init()
	defer func() {
		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 video.VideoSaveReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Title == "" {
		br.Msg = "标题不能为空"
		return
	}
	//if req.VideoUrl == "" {
	//	br.Msg = "音频地址不能为空"
	//	return
	//}
	videoCount, err := video.GetVideoByTitle(req.Title)
	if err != nil {
		br.Msg = "判断标题是否存在失败！"
		br.ErrMsg = "判断标题是否存在失败，Err:" + err.Error()
		return
	}
	if videoCount > 0 {
		br.Msg = "标题已存在！"
		br.IsSendEmail = false
		return
	}
	item := new(video.Video)
	item.Title = req.Title
	item.VideoUrl = req.VideoUrl
	item.VideoCoverUrl = req.VideoCoverUrl
	item.Status = 0
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	item.PlaySeconds = req.PlaySeconds
	newId, err := video.AddVideo(item)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存失败,Err:" + err.Error()
		return
	}
	if req.TagsIdStr != "" {
		tagsArr := strings.Split(req.TagsIdStr, ",")
		for _, v := range tagsArr {
			tagId, _ := strconv.Atoi(v)
			tagItem := new(video.VideoTags)
			tagItem.VideoId = int(newId)
			tagItem.TagsId = tagId
			err = video.AddVideoTags(tagItem)
			if err != nil {
				br.Msg = "保存失败"
				br.ErrMsg = "保存失败,Err:" + err.Error()
				return
			}
		}
	}
	resp := new(video.VideoSaveResp)
	resp.VideoId = int(newId)
	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
	br.Data = resp
}

// @Title 删除视频
// @Description 删除视频接口
// @Param	request	body video.VideoDeleteReq true "type json string"
// @Success Ret=200 保存成功
// @router /delete [post]
func (this *VideoController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		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 video.VideoDeleteReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VideoId <= 0 {
		br.Msg = "参数错误"
		return
	}
	err = video.DeleteVideo(req.VideoId)
	if err != nil {
		br.Msg = "删除失败！"
		br.ErrMsg = "删除失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
}

// @Title 批量发布视频
// @Description 批量发布视频接口
// @Param	request	body video.VideoPublishReq true "type json string"
// @Success Ret=200 保存成功
// @router /publish [post]
func (this *VideoController) Publish() {
	br := new(models.BaseResponse).Init()
	defer func() {
		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 video.VideoPublishReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VideoIds == "" {
		br.Msg = "参数错误"
		return
	}
	err = video.PublishVideo(req.VideoIds)
	if err != nil {
		br.Msg = "发布失败"
		br.ErrMsg = "发布失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "发布成功"
}

// @Title 编辑视频
// @Description 编辑视频接口
// @Param	request	body video.VideoEditReq true "type json string"
// @Success Ret=200 编辑成功
// @router /edit [post]
func (this *VideoController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		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 video.VideoEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Title == "" {
		br.Msg = "标题不能为空"
		return
	}
	if req.VideoUrl == "" {
		br.Msg = "音频地址不能为空"
		return
	}
	item := new(video.Video)
	item.Title = req.Title
	item.VideoUrl = req.VideoUrl
	item.VideoCoverUrl = req.VideoCoverUrl
	item.Status = 0
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	item.PlaySeconds = req.PlaySeconds
	item.VideoId = req.VideoId
	err = video.EditVideo(item, req.TagsIdStr)
	if err != nil {
		br.Msg = "编辑失败"
		br.ErrMsg = "编辑失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "编辑成功"
}

// @Title 取消视频发布
// @Description 取消视频发布接口
// @Param	request	body video.VideoPublishReq true "type json string"
// @Success Ret=200 保存成功
// @router /publish/cancel [post]
func (this *VideoController) PublishCancel() {
	br := new(models.BaseResponse).Init()
	defer func() {
		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 video.VideoPublishReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VideoIds == "" {
		br.Msg = "参数错误"
		return
	}
	err = video.PublishVideoCancel(req.VideoIds)
	if err != nil {
		br.Msg = "取消发布失败"
		br.ErrMsg = "取消发布失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "取消发布成功"
}
