package services

import (
	"errors"
	"hongze/hongze_yb/models/tables/variety_classify"
	"hongze/hongze_yb/models/tables/variety_tag"
	"hongze/hongze_yb/models/tables/yb_price_driven_tag"
)

// TagTreeItem 标签树
type TagTreeItem struct {
	ClassifyId   int    `json:"classify_id" description:"分类ID"`
	ClassifyName string `json:"classify_name" description:"分类名称"`
	//TagId        int        `json:"tag_id" description:"分类ID(用于前端组件)"`
	//TagName      string     `json:"tag_name" description:"分类名称(用于前端组件)"`
	Sort int        `json:"sort" description:"排序"`
	Tags []*TagItem `json:"tags" description:"标签"`
}

// TagItem 标签
type TagItem struct {
	TagId             int    `json:"tag_id" description:"标签ID"`
	TagName           string `json:"tag_name" description:"标签名称"`
	Sort              int    `json:"sort" description:"排序"`
	ClassifyId        int    `json:"classify_id" description:"分类ID"`
	ChartPermissionId int    `json:"chart_permission_id" description:"品种权限ID"`
	PriceDrivenState  int    `json:"price_driven_state" description:"价格驱动状态：0-关闭 1-开启"`
	//ResearcherList    []*TagResearcher `json:"researcher_list" description:"研究员列表"`
}

// TagResearcher 研究员信息
type TagResearcher struct {
	AdminId   int    `json:"admin_id"`
	AdminName string `json:"admin_name"`
}

// 获取标签树
func GetTagTree() (respList []*TagTreeItem, err error) {
	respList = make([]*TagTreeItem, 0)
	// 获取标签分类
	classifyList, e := variety_classify.GetVarietyClassifyList()
	if e != nil {
		err = errors.New("获取标签分类列表失败, Err: " + e.Error())
		return
	}
	classifyLen := len(classifyList)
	if classifyLen == 0 {
		return
	}
	// 获取标签列表
	tagList, e := variety_tag.GetVarietyTagList()
	if e != nil {
		err = errors.New("获取标签列表失败, Err: " + e.Error())
		return
	}
	// 价格驱动标签
	priceTagList, e := yb_price_driven_tag.GetPriceDrivenTagList()
	if e != nil {
		err = errors.New("获取价格驱动标签列表失败, Err:" + e.Error())
		return
	}
	priceTagLen := len(priceTagList)
	priceTagMap := make(map[int]int, 0)
	for i := 0; i < priceTagLen; i++ {
		priceTagMap[priceTagList[i].VarietyTagID] = priceTagList[i].State
	}
	// 价格驱动状态
	tagsArr := make([]*TagItem, 0)
	tagLen := len(tagList)
	for i := 0; i < tagLen; i++ {
		item := tagList[i]
		priceState := priceTagMap[item.VarietyTagID]
		tagsArr = append(tagsArr, &TagItem{
			TagId:             item.VarietyTagID,
			TagName:           item.TagName,
			Sort:              item.Sort,
			ClassifyId:        item.VarietyClassifyID,
			ChartPermissionId: item.ChartPermissionID,
			PriceDrivenState:  priceState,
		})
	}
	// 分类标签
	for i := 0; i < classifyLen; i++ {
		classifyItem := new(TagTreeItem)
		classifyItem.ClassifyId = classifyList[i].VarietyClassifyID
		classifyItem.ClassifyName = classifyList[i].ClassifyName
		//classifyItem.TagId = classifyList[i].VarietyClassifyID
		//classifyItem.TagName = classifyList[i].ClassifyName
		classifyItem.Sort = classifyList[i].Sort
		classifyItem.Tags = make([]*TagItem, 0)
		for _, t := range tagsArr {
			if classifyItem.ClassifyId == t.ClassifyId {
				classifyItem.Tags = append(classifyItem.Tags, t)
			}
		}
		respList = append(respList, classifyItem)
	}
	return
}
