package yb

import (
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/yb"
	ybService "hongze/hz_crm_api/services/yb"
	"sort"
)

// ActivityTypeList
// @Title 获取活动分类列表
// @Description 获取活动分类列表
// @Success 200 {object} []yb.ActivityTypeList
// @router /activity/type/list [get]
func (this *ActivityController) ActivityTypeList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	// 获取所有的分类
	list, err := ybService.GetAllActivityType()
	if err != nil {
		br.Msg = "获取活动类型列表失败！"
		br.ErrMsg = "获取活动类型列表失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
}

// ActivityTypeTelList
// @Title 报告电话会-分类树
// @Description 报告电话会-分类树
// @Success 200 {object} []yb.ActivityTypeList
// @router /activity/type/tel_list [get]
func (this *ActivityController) ActivityTypeTelList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	// Tips: 历史原因此处的电话会分类最初是取的yb_activity_type且是手动维护
	// 后来迭代成为与classify表中的relate_tel=1的分类同步, 后续并没有直接切换而是兼容处理
	// classify有变更时会同步更新yb_activity_type的分类名、权限、显示状态等
	// 但是yb_activity_type表中无一级分类, 而需求需要分一二级展示
	// 所以这里一级分类用classify表的, 二级分类ID用yb_activity_type, 二级分类名用哪个表的都可以
	resp := make([]*yb.ActivityTypeList, 0)

	// 获取分析师电话会分类
	types, e := yb.GetActivityTypeListByActivityTypePId(1)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取分析师电话会分类失败"
		return
	}
	typeNameId := make(map[string]int)
	for _, t := range types {
		typeNameId[t.ActivityTypeName] = t.ActivityTypeId
	}

	// 获取分类列表
	list, e := models.GetAllClassify()
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取路演视频可选报告分类失败, Err: " + e.Error()
		return
	}

	// 初始化一级分类
	parentMap := make(map[int]*yb.ActivityTypeList)
	classifyMap := make(map[int]*models.Classify)
	for _, c := range list {
		classifyMap[c.Id] = c
		if c.ParentId > 0 && c.RelateTel == 1 {
			if parentMap[c.ParentId] == nil {
				parentMap[c.ParentId] = new(yb.ActivityTypeList)
			}
		}
	}

	// 遍历一级分类
	for k, v := range parentMap {
		p := classifyMap[k]
		if p == nil {
			continue
		}
		v.ActivityTypeName = p.ClassifyName
		v.Sort = p.Sort
		v.FrontIsShow = 1
		v.CreateTime = p.CreateTime
		v.ChildList = make([]*yb.ActivityTypeList, 0)

		// 遍历子分类
		for _, c := range list {
			if c.ParentId == p.Id && c.RelateTel == 1 {
				v.ChildList = append(v.ChildList, &yb.ActivityTypeList{
					ActivityTypeId:   typeNameId[c.ClassifyName],
					ActivityTypeName: c.ClassifyName,
					Pid:              1,
					Sort:             c.Sort,
					FrontIsShow:      1,
					CreateTime:       c.CreateTime,
				})
			}
		}
		sort.Slice(v.ChildList, func(i, j int) bool {
			return v.ChildList[j].Sort > v.ChildList[i].Sort
		})
		resp = append(resp, v)
	}
	sort.Slice(resp, func(i, j int) bool {
		return resp[j].Sort > resp[i].Sort
	})

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