package yb

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/yb"
	"hongze/hz_crm_api/models/yb/request"
	"hongze/hz_crm_api/models/yb/response"
	"hongze/hz_crm_api/utils"
	"time"
)

// 音频播报板块
type VoiceSectionController struct {
	controllers.BaseAuthController
}

// VoiceSectionList
// @Title 语音播报板块配置列表
// @Description 语音播报板块配置列表接口
// @Success 200 {object} response.VoiceSectionResp
// @router /voice/section/list [get]
func (this *VoiceSectionController) VoiceSectionList() {
	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")
	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	list, err := yb.GetVoiceSectionList(startSize, pageSize)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取语音播报板块信息失败"
		br.ErrMsg = "获取语音播报板块信息失败,Err:" + err.Error()
		return
	}

	total, err := yb.GetVoiceSectionTotal()
	if err != nil {
		br.Msg = "获取留言列表总数失败!"
		br.ErrMsg = "获取留言列表总数失败,Err:" + err.Error()
		return
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = response.VoiceSectionResp{
		List:   list,
		Paging: page,
	}
	return
}

// AddVoiceSection
// @Title 新增语音播报板块
// @Description 新增语音播报板块接口
// @Param	request	body request.VoiceSectionReq true "type json string"
// @Success Ret=200 操作成功
// @router /voice/section/add [post]
func (this *VoiceSectionController) AddVoiceSection() {
	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 request.VoiceSectionReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	// 校验参数
	if req.VarietyName == "" || req.SectionName == "" || req.VarietyId == 0 {
		br.Msg = "参数有误,请重新填写"
		br.ErrMsg = "参数有误,请重新填写"
		return
	}

	item := yb.VoiceSection{
		SectionName: req.SectionName,
		ImgUrl:      req.ImgUrl,
		VarietyId:   req.VarietyId,
		VarietyName: req.VarietyName,
		Status:      1,
		CreateTime:  time.Now().Format(utils.FormatDateTime),
	}

	err = yb.AddVoiceSection(item)
	if err != nil {
		br.Msg = "新增失败"
		br.ErrMsg = "AddVoiceSection error" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// EditVoiceSection
// @Title 禁用语音播报板块
// @Description 禁用语音播报板块接口
// @Param   SectionId        query   int    false		"板块id"
// @Param   SectionName        query   string    false		"板块名称"
// @Param   ImgUrl        query   string    false		"背景图url"
// @Param   Enable        query   int    false		"0禁用,1启用"
// @Success Ret=200 操作成功
// @router /voice/section/edit [get]
func (this *VoiceSectionController) EditVoiceSection() {
	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 item yb.VoiceSection
	cols := make([]string, 0)

	sectionId, _ := this.GetInt("SectionId", 0)
	if sectionId == 0 {
		br.Msg = "板块id错误"
		br.ErrMsg = "板块id错误"
		return
	}
	item.SectionId = sectionId

	enable, _ := this.GetInt("Enable", -1)
	if enable != -1 {
		item.Status = enable
		cols = append(cols, "status")
	}

	sectionName := this.GetString("SectionName", "")
	if sectionName != "" {
		item.SectionName = sectionName
		cols = append(cols, "section_name")
		//更新统计表中的sectionName
		err := yb.UpdateVoiceBroadcastName(sectionId, sectionName)
		if err != nil {
			br.Msg = "修改失败"
			br.ErrMsg = "修改失败" + err.Error()
			return
		}
	}

	imgUrl := this.GetString("ImgUrl", "")
	if imgUrl != "" {
		item.ImgUrl = imgUrl
		cols = append(cols, "img_url")
	}

	total, err := yb.GetVoiceSectionEnableTotal()
	if err != nil {
		br.Msg = "查询失败"
		br.ErrMsg = "查询失败" + err.Error()
		return
	}

	if enable == 0 && total < 2 {
		br.Ret = 403
		br.Success = true
		br.Msg = "禁用失败,请至少启用一个版块!"

		return
	} else {
		err = item.Update(cols)
		if err != nil {
			br.Msg = "修改失败"
			br.ErrMsg = "修改失败" + err.Error()
			return
		}
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"

		//if imgUrl != "" {
		//	go yb.UpdateVoiceBroadcast(sectionId, imgUrl)
		//}
		return
	}
}