package services

import (
	"errors"
	"fmt"
	"hongze/hongze_yb/models/tables/research_variety_tag_relation"
	"hongze/hongze_yb/models/tables/variety_classify"
	"hongze/hongze_yb/models/tables/variety_tag"
	"hongze/hongze_yb/models/tables/yb_community_question_process"
	"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"`
	AllowSelect bool   `json:"allow_select" description:"是否允许选中"`
}

// GetTagTree 获取标签树
func GetTagTree(questionId int) (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
	}

	// 标签研究员组
	relationList := make([]*research_variety_tag_relation.ResearchVarietyTagRelationItem, 0)
	relationList, e = research_variety_tag_relation.GetResearchVarietyTagRelationList()
	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
	}

	// 问答社区-研究员是否允许被二次选中
	processMap := make(map[string]bool)
	if questionId > 0 {
		processList, e := yb_community_question_process.GetListByQuestionId(questionId)
		if e != nil {
			err = errors.New("获取问答流程列表失败, Err:" + e.Error())
			return
		}
		for i := range processList {
			k := fmt.Sprintf("%d-%d", processList[i].VarietyTagID, processList[i].ReplierAdminID)
			processMap[k] = true
		}
	}

	// 研究员、价格驱动状态
	tagsArr := make([]*TagItem, 0)
	tagLen := len(tagList)
	for i := 0; i < tagLen; i++ {
		item := tagList[i]
		priceState := priceTagMap[item.VarietyTagID]
		tmp := &TagItem{
			TagId:             item.VarietyTagID,
			TagName:           item.TagName,
			Sort:              item.Sort,
			ClassifyId:        item.VarietyClassifyID,
			ChartPermissionId: item.ChartPermissionID,
			PriceDrivenState:  priceState,
		}
		members := make([]*TagResearcher, 0)
		for _, r := range relationList {
			if item.VarietyTagID == r.VarietyTagId {
				k := fmt.Sprintf("%d-%d", item.VarietyTagID, r.AdminId)
				as := true
				if processMap[k] {
					as = false
				}
				members = append(members, &TagResearcher{
					AdminId:     r.AdminId,
					AdminName:   r.AdminName,
					AllowSelect: as,
				})
			}
		}
		tmp.ResearcherList = members
		tagsArr = append(tagsArr, tmp)
	}

	// 分类标签
	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
}
