package controllers

import (
	"eta/eta_mobile/models"
	"eta/eta_mobile/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
)

// 分类
type ClassifyController struct {
	BaseAuthController
}

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

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}

	startSize = utils.StartIndex(currentIndex, pageSize)
	list, err := models.GetClassifyList(startSize, pageSize, keyWord, companyType, hideDayWeek)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	total, err := models.GetClassifyListCount(keyWord, companyType, hideDayWeek)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}

	parentIds := make([]int, 0)
	for i := range list {
		parentIds = append(parentIds, list[i].Id)
	}
	parentIdLen := len(parentIds)
	if parentIdLen == 0 {
		resp := &models.ClassifyListResp{
			List:   list,
			Paging: paging.GetPaging(currentIndex, pageSize, 0),
		}
		br.Data = resp
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		return
	}

	// 获取一级分类-子目录列表
	menuListMap := make(map[int][]*models.ClassifyMenu, 0)
	var menuCond string
	var menuPars []interface{}
	menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
	menuPars = append(menuPars, parentIds)
	parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取一级分类子目录列表失败"
		return
	}
	for i := range parentMenus {
		if menuListMap[parentMenus[i].ClassifyId] == nil {
			menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
		}
		menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
	}

	// 获取子分类
	children, e := models.GetClassifyChildByParentIds(parentIds, keyWord)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取子分类失败"
		return
	}
	childrenIds := make([]int, 0)
	for i := range children {
		childrenIds = append(childrenIds, children[i].Id)
	}
	childrenIdsLen := len(childrenIds)

	// 获取二级分类-子目录关联
	relateMap := make(map[int]int, 0)
	if childrenIdsLen > 0 {
		var relateCond string
		var relatePars []interface{}
		relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(childrenIdsLen) + `)`
		relatePars = append(relatePars, childrenIds)
		relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
		if e != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
			return
		}
		for i := range relates {
			relateMap[relates[i].ClassifyId] = relates[i].MenuId
		}
	}

	// 二级分类
	childrenMap := make(map[int][]*models.ClassifyItem, 0)
	for i := range children {
		if childrenMap[children[i].ParentId] == nil {
			childrenMap[children[i].ParentId] = make([]*models.ClassifyItem, 0)
		}
		childrenMap[children[i].ParentId] = append(childrenMap[children[i].ParentId], &models.ClassifyItem{
			Classify:       *children[i],
			ClassifyMenuId: relateMap[children[i].Id],
		})
	}

	// 一级分类
	for i := range list {
		list[i].ClassifyMenuList = menuListMap[list[i].Id]
		list[i].Child = childrenMap[list[i].Id]
	}

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(models.ClassifyListResp)
	resp.List = list
	resp.Paging = page
	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}
