package controllers

import (
	"encoding/json"
	"eta/eta_mini_crm_ht/models"
	"eta/eta_mini_crm_ht/models/request"
	"eta/eta_mini_crm_ht/models/response"
	"eta/eta_mini_crm_ht/services"
	"eta/eta_mini_crm_ht/services/elastic"
	"eta/eta_mini_crm_ht/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
	"github.com/tcolgate/mp3"
	"os"
	"path"
	"strconv"
	"time"
)

type AudioController struct {
	BaseAuthController
}

// UploadAudio @Title 上传视频
// @Description 上传视频
// @Param   File   query   file  true       "文件"
// @Success 200 {object} models.ReportAuthorResp
// @router /uploadAudio [post]
func (this *AudioController) UploadAudio() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	f, h, err := this.GetFile("File")
	if err != nil {
		br.Msg = "获取资源信息失败"
		br.ErrMsg = "获取资源信息失败，Err:" + err.Error()
		return
	}
	defer f.Close()
	size, err := strconv.Atoi(utils.UPLOAD_AUDIO_SIZE)
	if err != nil {
		size = 100
	}
	if h.Size > 1024*1024*int64(size) {
		br.Msg = fmt.Sprintf("音频大小不能超过%dM", size)
		br.ErrMsg = "音频上传失败，Err:" + err.Error()
		return
	}
	ext := path.Ext(h.Filename)
	if ext != ".mp3" {
		br.Msg = "音频格式不正确"
		br.ErrMsg = "音频上传失败，Err:" + err.Error()
		return
	}
	dateDir := time.Now().Format("20060102")
	uploadDir := utils.STATIC_DIR + "ht/audio" + dateDir
	err = os.MkdirAll(uploadDir, utils.DIR_MOD)
	if err != nil {
		br.Msg = "存储目录创建失败"
		br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
		return
	}
	randStr := utils.GetRandStringNoSpecialChar(28)
	fileName := randStr + ext
	fpath := uploadDir + "/" + fileName
	err = this.SaveToFile("File", fpath)
	if err != nil {
		br.Msg = "音频上传失败"
		br.ErrMsg = "音频上传失败，Err:" + err.Error()
		return
	}
	mp3File := mp3.NewDecoder(f)
	if err != nil {
		br.Msg = "音频上传失败"
		br.ErrMsg = "解析音频时常失败，Err:" + err.Error()
		return
	}
	var totalDuration time.Duration
	skipped := 0
	for {
		frame := mp3.Frame{}
		if err = mp3File.Decode(&frame, &skipped); err != nil {
			break
		}
		totalDuration += frame.Duration()
	}
	duration := int(totalDuration.Milliseconds())

	audioUploadDir := utils.RESOURCE_DIR + "audio/"
	savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/")
	audioName := utils.GetRandStringNoSpecialChar(28)
	savePdfToOssPath += audioName + ext

	defer func() {
		err = os.Remove(fpath)
		fmt.Sprintf("删除文件失败:%v", err)
	}()
	ossClient := services.NewOssClient()
	if ossClient == nil {
		br.Msg = "音频上传失败"
		br.ErrMsg = "初始化OSS服务失败"
		return
	}
	mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
	if err != nil {
		br.Msg = "音频上传失败"
		br.ErrMsg = "音频上传失败，Err:" + err.Error()
		return
	}
	base := path.Base(h.Filename)
	resp := new(response.MediaUploadResp)
	resp.Url = mp3Url
	resp.FileName = base
	resp.DurationMillisecond = duration
	br.Data = resp
	br.Msg = "上传成功"
	br.Ret = 200
	br.Success = true
}

// AddAudio @Title 新增音频
// @Description 新增音频
// @Param   File   query   file  true       "文件"
// @Success 200 {object} models.ReportAuthorResp
// @router /addAudio [post]
func (this *AudioController) AddAudio() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req request.AudioReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析失败"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.AudioName == "" {
		br.Msg = "音频名称为空"
		return
	}
	if req.AnalystId == 0 {
		br.Msg = "研究员ID为空"
		return
	}
	if req.AnalystName == "" {
		br.Msg = "研究员名称为空"
		return
	}
	if req.DurationMillisecond == 0 {
		br.Msg = "音频时长为空"
		return
	}
	if req.SrcUrl == "" {
		br.Msg = "音频地址为空"
		return
	}
	var err error

	audioInsert := &models.Media{
		AuthorId:              req.AnalystId,
		AuthorName:            req.AnalystName,
		MediaType:             models.Audio,
		Src:                   req.SrcUrl,
		MediaName:             req.AudioName,
		SourceType:            "mp3",
		MediaPlayMilliseconds: req.DurationMillisecond,
		PermissionIds:         req.PermissionIds,
		PublishedTime:         time.Now(),
		Deleted:               0,
		CreatedTime:           time.Now(),
	}

	err = models.InsertMedia(audioInsert)
	if err != nil {
		br.Msg = "添加失败"
		br.ErrMsg = "音频新增失败，Err:" + err.Error()
		return
	}
	//audioInsert.Id = int(insertId)
	// 添加es
	go func(audio *models.ESMedia) {
		docId := strconv.Itoa(audio.MediaId)
		err = elastic.EsAddOrEditMedia(utils.MEDIA_INDEX, docId, audio)
		if err != nil {
			utils.FileLog.Info("音频记录es新增失败，Err:" + err.Error())
			return
		}
		utils.FileLog.Info("音频记录es新增成功, pdfId:" + docId)
	}(audioInsert.ToView())
	// 创建消息
	_, _ = services.CreateMeta(audioInsert.AuthorName, audioInsert.AuthorId, audioInsert.Id, audioInsert.PublishedTime.Format(time.DateTime), models.AudioSourceType)
	br.Msg = "添加成功"
	br.Ret = 200
	br.Success = true
}

//	@Title 编辑音频
//
// @Description 编辑音频
// @Success 200 {object} models.ReportAuthorResp
// @router /editAudio [post]
func (this *AudioController) EditAudio() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req request.AudioReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析失败"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.AudioId <= 0 {
		br.Msg = "音频ID为空"
		return
	}
	if req.AudioName == "" {
		br.Msg = "音频名称为空"
		return
	}
	if req.AnalystId == 0 {
		br.Msg = "研究员ID为空"
		return
	}
	if req.AnalystName == "" {
		br.Msg = "研究员名称为空"
		return
	}
	if req.DurationMillisecond == 0 {
		br.Msg = "音频时长为空"
		return
	}
	if req.SrcUrl == "" {
		br.Msg = "音频地址为空"
		return
	}
	var err error

	audioEdit := &models.Media{
		Id:                    req.AudioId,
		AuthorId:              req.AnalystId,
		AuthorName:            req.AnalystName,
		MediaType:             models.Audio,
		Src:                   req.SrcUrl,
		MediaName:             req.AudioName,
		SourceType:            "mp3",
		MediaPlayMilliseconds: req.DurationMillisecond,
		PermissionIds:         req.PermissionIds,
		PublishedTime:         time.Now(),
		Deleted:               0,
		CreatedTime:           time.Now(),
	}

	_, err = models.UpdateMedia(audioEdit)
	if err != nil {
		br.Msg = "更新失败"
		br.ErrMsg = "音频更新失败，Err:" + err.Error()
		return
	}
	// 添加es
	go func(audio *models.ESMedia) {
		docId := strconv.Itoa(audio.MediaId)
		err = elastic.EsAddOrEditMedia(utils.MEDIA_INDEX, docId, audio)
		if err != nil {
			utils.FileLog.Info("音频记录es更新失败，Err:" + err.Error())
			return
		}
		utils.FileLog.Info("音频记录es更新成功, pdfId:" + docId)
	}(audioEdit.ToView())
	br.Msg = "编辑成功"
	br.Ret = 200
	br.Success = true
}

// DeleteAudio
// @Title 删除音频
//
// @Description 删除音频
// @Success 200 {object} models.ReportAuthorResp
// @router /deleteAudio [post]
func (this *AudioController) DeleteAudio() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var req request.AudioReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析失败"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.AudioId <= 0 {
		br.Msg = "音频ID为空"
		return
	}
	var err error

	audioDelete := &models.Media{
		Id:      req.AudioId,
		Deleted: 1,
	}

	err = models.DeleteMedia(audioDelete)
	if err != nil {
		br.Msg = "删除失败"
		br.ErrMsg = "音频删除失败，Err:" + err.Error()
		return
	}
	// 添加es
	go func(audio *models.ESMedia) {
		docId := strconv.Itoa(audio.MediaId)
		err = elastic.EsDeleteData(utils.MEDIA_INDEX, docId)
		if err != nil {
			utils.FileLog.Info("音频记录es删除失败，Err:" + err.Error())
			return
		}
		utils.FileLog.Info("音频记录es删除成功, pdfId:" + docId)
	}(audioDelete.ToView())
	br.Msg = "删除成功"
	br.Ret = 200
	br.Success = true
}

// AudioList
// @Title 研报列表
// @Description pdf研报列表
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   ClassifyIds   query   string  true       "二级分类id,可多选用英文,隔开"
// @Param   KeyWord   query   string  true       "报告标题/创建人"
// @Param   SortType   query   string  true       "排序方式"
// @Success 200 {object} models.ReportAuthorResp
// @router /audioList [get]
func (this *AudioController) AudioList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	sortType := this.GetString("SortType")
	var condition string
	var pars []interface{}

	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}

	sortCondition := " ORDER BY published_time "
	if sortType == "" {
		sortType = "DESC"
	}
	sortCondition = sortCondition + sortType
	total, err := models.GetMediaCountByCondition(models.Audio, condition, pars)
	if err != nil {
		br.Msg = "获取音频列表失败"
		br.ErrMsg = "获取音频列表统计失败，Err:" + err.Error()
		return
	}

	startSize := utils.StartIndex(currentIndex, pageSize)
	reportList, err := models.GetMediaByCondition(models.Audio, condition, sortCondition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取音频列表失败"
		br.ErrMsg = "获取音频列表失败，Err:" + err.Error()
		return
	}
	var reportViewList []*models.MediaView
	for _, report := range reportList {
		reportView := report.ToMediaView()
		reportViewList = append(reportViewList, reportView)
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(response.MediaListResp)
	resp.List = reportViewList
	resp.Paging = page

	br.Ret = 200
	br.Success = true
	br.Data = resp
	br.Msg = "获取成功"
}
