package controllers

import (
	"encoding/json"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/services"
)

type TagController struct {
	BaseAuthController
}

// @Title 获取标签列表-自定义顺序
// @Description 获取标签列表-自定义顺序接口
// @Success 200 {object} cygx.ChartPermissionResp
// @router /list/custom [get]
func (this *TagController) TagCustomizeList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysUser := this.User
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var condition string
	//hasPermission, err := services.GetUserHasPermissionSimple(sysUser)
	//if err != nil {
	//	br.Msg = "获取信息失败"
	//	br.ErrMsg = "判断是否已申请过试用失败，Err:" + err.Error()
	//	return
	//}
	//if hasPermission > 1 {
	//	list := make([]*models.CygxTagList, 0)
	//	br.Ret = 200
	//	br.Success = true
	//	br.Msg = "获取成功"
	//	br.Data = list
	//	return
	//}

	list, err := models.GetCygxTagList(condition)
	for _, v := range list {
		if v.ArticleTypes != "" {
			v.CheckList = append(v.CheckList, "A")
		}
		if v.ActivityTypes != "" {
			v.CheckList = append(v.CheckList, "B")
		}
		if v.Industries != "" {
			v.CheckList = append(v.CheckList, "C")
		}
		if v.SubjectNames != "" {
			v.CheckList = append(v.CheckList, "D")
		}
		//固定标签默认都属于，前端互斥使用
		if v.TagType > 0 {
			v.CheckList = []string{"A", "B", "C", "D"}
		}
	}
	if err != nil {
		br.Msg = "获取标签失败"
		br.ErrMsg = "获取标签信息失败,Err:" + err.Error()
		return
	}

	condition = ` AND permission_name IN ('医药1','消费','科技','智造','策略','固收','周期') `

	listPermission, err := models.GetChartPermissionReportAll(condition)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	//item := new(models.ChartPermission)
	//item.ChartPermissionId = utils.GU_SHOU_ID
	//item.PermissionName = utils.GU_SHOU_NAME
	//listPermission = append(listPermission, item)
	resp := new(models.CygxTagListResp)
	resp.List = list
	resp.ListPermission = listPermission
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 记录点击信息
// @Description 记录点击信息
// @Param	request	body cygx.CygxTagIdReq true "type json string"
// @Success 200 Ret=200 发布成功
// @router /add/history [post]
func (this *TagController) History() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req models.CygxTagIdReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	tagId := req.TagId
	if tagId == 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误，id不可为空"
		return
	}
	go services.AddCygxTagHistory(user, tagId)
	br.Ret = 200
	br.Success = true
	br.Msg = "记录成功"
}
