package pc

import (
	"errors"
	"fmt"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/response/pc"
	"hongze/hongze_yb/models/tables/company_product"
	"hongze/hongze_yb/models/tables/rddp/classify"
	"hongze/hongze_yb/services/company"
	"hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"sort"
	"strings"
)

// GetClassifyFirstList 获取一级分类列表
func GetClassifyFirstList(user user.UserInfo) (resp pc.ClassifyFirstList, err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("GetClassifyFirstList: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()

	classifyParents, err := classify.GetParentList()
	if err != nil {
		errMsg = err.Error()
		err = errors.New("分类查询出错")
		return
	}
	if len(classifyParents) == 0 {
		err = errors.New("分类不存在")
		return
	}

	classifyIconMap := make(map[string]string)
	classifySortMap := make(map[string]int)
	var names []string
	for _, v := range classifyParents {
		classifyIconMap[v.ClassifyName] = v.YbFiccPcIcon
		classifySortMap[v.ClassifyName] = v.YbFiccSort
		names = append(names, v.ClassifyName)
	}

	reportList, err := pc.GetLatestStage(names)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("报告查询出错")
		return
	}
	var list pc.ClassifyFirstList
	for _, item := range classifyParents {
		temp := new(pc.ClassifyFirstListItem)
		temp.ClassifyIdFirst = item.Id
		temp.ClassifyNameFirst = item.ClassifyName
		temp.BackImgUrl = classifyIconMap[item.ClassifyName]
		temp.Sort = classifySortMap[item.ClassifyName]
		//if temp.Sort == 0 {
		//	continue
		//}
		// ShowType展示类型：1-列表 2-专栏 3-品种; RedirectType跳转类型：1-专栏列表 2-报告列表 3-专栏详情 4-品种类型列表
		temp.RedirectType = 2
		if item.ShowType == 2 {
			temp.RedirectType = 1
			classifyChild, _ := classify.GetChildByPid(item.Id)
			if classifyChild != nil && len(classifyChild) == 1 {
				// 存在二级分类且仅有一个时直接跳转专栏详情
				temp.ClassifyIdSecond = classifyChild[0].Id
				temp.ClassifyNameSecond = classifyChild[0].ClassifyName
				temp.RedirectType = 3
			}
		} else if item.ShowType == 3 {
			temp.RedirectType = 4
		}
		list = append(list, temp)
	}
	// 判断用户状态是否是正常和永续
	var productAuthOk bool
	companyProduct, err := company_product.GetByCompany2ProductId(user.CompanyID, 1)
	if err == utils.ErrNoRow {
		err = nil
	}

	if err != nil && err != utils.ErrNoRow {
		errMsg = err.Error()
		err = errors.New("查询用户购买产品出错")
		return
	}
	if companyProduct != nil {
		// 无FICC权限的客户不可见
		if companyProduct.CompanyProductID > 0 {
			// 已购或者试用用户可见
			if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
				productAuthOk = true
			}
		}
	}

	// 获取有效的权限id列表
	var validPermissionIdList []int
	//var allClassifySecondIds []int
	if productAuthOk {
		validPermissionIdList, err = company.GetValidPermissionIdListByCompany2ProductId(user.CompanyID, 1)
		if err != nil && err != utils.ErrNoRow {
			errMsg = err.Error()
			err = errors.New("查询分类出错")
			return
		}
	}

	//classifyNames, err := chart_permission_search_key_word_mapping.GetKeyWordsByChartPermissionByIds(validPermissionIdList)
	//if err != nil {
	//	errMsg = err.Error()
	//	err = errors.New("查询权限对应的分类出错")
	//	return
	//}
	//
	//if len(classifyNames) > 0 {
	//	allClassifySecondIds, err = classify.GetSecondIdsByClassifyNames(classifyNames)
	//	if err != nil {
	//		errMsg = err.Error()
	//		err = errors.New("查询分类出错")
	//		return
	//	}
	//}

	for _, item := range list {
		//无权限用户关闭品种类别入口
		if item.RedirectType == 4 && !productAuthOk {
			continue
		}

		//只有宏观经济权限的用户关闭品种类别入口
		if len(validPermissionIdList) == 1 && validPermissionIdList[0] == 1 && item.RedirectType == 4 {
			continue
		}

		for _, report := range reportList {
			if report.ClassifyNameFirst == item.ClassifyNameFirst {
				item.Latest = report.Stage
			}
		}
		resp = append(resp, item)
	}

	if len(resp) > 0 {
		sort.Sort(resp)
	}

	return
}
