package english_report

import (
	"fmt"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/utils"
	"sort"
)

// ListClassify
// @Title 获取分类列表
// @Description 获取分类列表
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   KeyWord   query   string  true       "检索关键词"
// @Param   CompanyType   query   string  false       "产品类型，枚举值：'ficc','权益';不传默认返回全部"
// @Success 200 {object} models.EnglishClassifyListResp
// @router /classify/list [get]
func (this *EnglishReportController) ListClassify() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	keyWord := this.GetString("KeyWord")
	reqEnabled, _ := this.GetInt("Enabled", -1)
	enabled := -1
	if reqEnabled == 1 {
		enabled = reqEnabled
	}
	resp := new(models.EnglishClassifyListResp)

	// 处理一级分类分页的情况
	rootList, err := models.GetEnglishClassifyRootId(keyWord, enabled)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	if len(rootList) == 0 {
		resp.List = make([]*models.EnglishClassifyList, 0)

		br.Data = resp
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		return
	}
	var ids []int
	var rootIds []int
	rootMap := make(map[int]struct{}, 0)
	for _, v := range rootList {
		rootIds = append(rootIds, v.Id)
		rootMap[v.Id] = struct{}{}
	}

	//获取相关的分类ID
	idList, err := models.GetEnglishClassifyListByRootId(rootIds, keyWord, enabled)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	secondListMap := make(map[int][]*models.EnglishClassifyList)
	thirdListMap := make(map[int][]*models.EnglishClassifyList)
	var thirdIds []int
	var sortChildList models.RSChildClassifyList
	// 三级分类-品种权限
	permissionMap := make(map[int][]int)

	if len(idList) > 0 {
		childIdMap := make(map[int]struct{}, 0)
		for _, v := range idList {
			if _, ok := childIdMap[v.ParentId]; !ok {
				ids = append(ids, v.ParentId)
				childIdMap[v.ParentId] = struct{}{}
			}
			if _, ok := childIdMap[v.Id]; !ok {
				ids = append(ids, v.Id)
				childIdMap[v.Id] = struct{}{}
			}
		}
		tmpList, err := models.GetEnglishClassifyChildByIds(ids)
		if err != nil {
			br.Msg = "获取二级分类失败"
			br.ErrMsg = "获取二级分类失败，Err:" + err.Error()
			return
		}
		for _, v := range tmpList {
			if _, ok := rootMap[v.ParentId]; !ok {
				thirdIds = append(thirdIds, v.Id)
			}
		}
		{
			classifyIds := thirdIds
			if len(classifyIds) > 0 {
				cond := fmt.Sprintf(` AND %s IN (%s)`, models.EnClassifyPermissionColumns.EnClassifyId, utils.GetOrmInReplace(len(classifyIds)))
				pars := make([]interface{}, 0)
				pars = append(pars, classifyIds)
				ob := new(models.EnClassifyPermission)
				items, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
				if e != nil {
					br.Msg = "获取失败"
					br.ErrMsg = "获取客户权限列表失败, Err: " + e.Error()
					return
				}
				for _, v := range items {
					if permissionMap[v.EnClassifyId] == nil {
						permissionMap[v.EnClassifyId] = make([]int, 0)
					}
					permissionMap[v.EnClassifyId] = append(permissionMap[v.EnClassifyId], v.EnPermissionId)
				}
			}
		}
		// 处理三级分类
		for _, v := range tmpList {
			if _, ok := rootMap[v.ParentId]; !ok {
				if p, ok1 := permissionMap[v.Id]; ok1 {
					v.EnPermissions = p
				}
				thirdListMap[v.ParentId] = append(thirdListMap[v.ParentId], v)
			}
		}

		//处理二级分类
		for _, v := range tmpList {
			if _, ok := rootMap[v.ParentId]; ok {
				if child, ok1 := thirdListMap[v.Id]; ok1 {
					sortChildList = child
					sort.Sort(sortChildList)
					v.Child = sortChildList
				}
				secondListMap[v.ParentId] = append(secondListMap[v.ParentId], v)
			}
		}
	}

	//处理一级分类
	var sortList models.RSClassifyList
	for _, v := range rootList {
		if child, ok := secondListMap[v.Id]; ok {
			sortChildList = child
			sort.Sort(sortChildList)
			v.Child = sortChildList
		}
		sortList = append(sortList, v)
	}

	sort.Sort(sortList)

	resp.List = sortList

	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}
