package controllers

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/utils"
)

type VarietyTagController struct {
	BaseAuthController
}

// ClassifyList
// @Title 标签分类列表
// @Description 标签分类列表
// @Success 200 string "获取成功"
// @router /variety_tag/classify_list [get]
func (this *VarietyTagController) ClassifyList() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var startSize int
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	condition := ""
	pars := make([]interface{}, 0)
	total, resp, e := services.GetVarietyClassifyPageList(condition, pars, "", startSize, pageSize)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取分类列表失败, Err: " + e.Error()
		return
	}
	resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// ClassifySave
// @Title 保存标签分类
// @Description 保存标签分类
// @Success 200 string "操作成功"
// @router /variety_tag/classify_save [post]
func (this *VarietyTagController) ClassifySave() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req services.VarietyClassifySaveReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ClassifyName == "" {
		br.Msg = "请输入标签分类名称"
		return
	}
	errMsg, err := services.SaveVarietyClassify(req.VarietyClassifyId, req.ClassifyName)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// ClassifyState
// @Title 切换标签分类状态
// @Description 切换标签分类状态
// @Success 200 string "操作成功"
// @router /variety_tag/classify_state [post]
func (this *VarietyTagController) ClassifyState() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req services.VarietyClassifyStateReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VarietyClassifyId <= 0 {
		br.Msg = "参数有误"
		return
	}
	errMsg, err := services.SwitchVarietyClassifyState(req.VarietyClassifyId, req.State)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// TagList
// @Title 标签列表
// @Description 标签列表
// @Success 200 string "获取成功"
// @router /variety_tag/tag_list [get]
func (this *VarietyTagController) TagList() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	var startSize int
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	condition := ""
	pars := make([]interface{}, 0)
	keywords := this.GetString("Keywords")
	classifyId, _ := this.GetInt("ClassifyId")
	if classifyId > 0 {
		condition += ` AND variety_classify_id = ?`
		pars = append(pars, classifyId)
	}
	if keywords != "" {
		keywords = "%" + keywords + "%"
		condition += ` AND tag_name LIKE ?`
		pars = append(pars, keywords)
	}
	total, resp, e := services.GetVarietyTagPageList(condition, pars, "", startSize, pageSize)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取标签列表失败, Err: " + e.Error()
		return
	}
	resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// TagSave
// @Title 保存标签
// @Description 保存标签
// @Success 200 string "操作成功"
// @router /variety_tag/tag_save [post]
func (this *VarietyTagController) TagSave() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req services.VarietyTagSaveReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.TagName == "" {
		br.Msg = "请输入标签名称"
		return
	}
	if req.VarietyClassifyId <= 0 {
		br.Msg = "请选择标签分类"
		return
	}
	if req.ChartPermissionId <= 0 {
		br.Msg = "请选择品种"
		return
	}
	errMsg, err := services.SaveVarietyTag(req.VarietyTagId, req.VarietyClassifyId, req.ChartPermissionId, req.TagName)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// TagState
// @Title 切换标签状态
// @Description 切换标签状态
// @Success 200 string "操作成功"
// @router /variety_tag/tag_state [post]
func (this *VarietyTagController) TagState() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req services.VarietyTagStateReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.VarietyTagId <= 0 {
		br.Msg = "参数有误"
		return
	}
	errMsg, err := services.SwitchVarietyTagState(req.VarietyTagId, req.State)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// TagTree
// @Title 标签树
// @Description 标签树
// @Success 200 string "获取成功"
// @router /variety_tag/tag_tree [get]
func (this *VarietyTagController) TagTree() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	hasResearcher, _ := this.GetInt("HasResearcher")    // 是否展示研究员：1展示， 0不展示
	include, _ := this.GetInt("IncludeDisableMember")   // 是否包含被禁用的研究员标签
	questionId, _ := this.GetInt("CommunityQuestionId") // 问答社区ID-用于区分哪些研究员不可选
	resp, e := services.GetTagTree(hasResearcher, include, questionId)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "TagTree-获取标签树失败, Err: " + e.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
