package services

import (
	"eta/eta_api/models/company"
	"eta/eta_api/models/roadshow"
	"eta/eta_api/models/system"
	"eta/eta_api/utils"
	"fmt"
)

func GetRoleTypeCode(roleType string) string {
	var roleTypeCode string
	switch roleType {
	case utils.ROLE_TYPE_ADMIN:
		roleTypeCode = utils.ROLE_TYPE_CODE_ADMIN
	case utils.ROLE_TYPE_FICC_ADMIN:
		roleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
	case utils.ROLE_TYPE_FICC_SELLER:
		roleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
	case utils.ROLE_TYPE_RAI_ADMIN:
		roleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
	case utils.ROLE_TYPE_FICC_GROUP, utils.ROLE_TYPE_FICC_MANAGER:
		roleTypeCode = utils.ROLE_TYPE_CODE_FICC_GROUP
	case utils.ROLE_TYPE_RAI_GROUP:
		roleTypeCode = utils.ROLE_TYPE_CODE_RAI_GROUP
	case utils.ROLE_TYPE_FICC_DEPARTMENT:
		roleTypeCode = utils.ROLE_TYPE_CODE_FICC_DEPARTMENT
	case utils.ROLE_TYPE_RAI_DEPARTMENT:
		roleTypeCode = utils.ROLE_TYPE_CODE_RAI_DEPARTMENT
	case utils.ROLE_TYPE_FICC_RESEARCHR:
		roleTypeCode = utils.ROLE_TYPE_CODE_FICC_RESEARCHR
	case utils.ROLE_TYPE_RAI_RESEARCHR:
		roleTypeCode = utils.ROLE_TYPE_CODE_RAI_RESEARCHR
	default:
		roleTypeCode = utils.ROLE_TYPE_ADMIN
	}
	return roleTypeCode
}

type AdminGroup struct {
	GroupId        int    `description:"分组id"`
	GroupName      string `description:"分组名称"`
	ResearcherList []*roadshow.Researcher
}

// CheckAdminIsSameBigGroup 判断是否两个系统用户是否同一个大组内
func CheckAdminIsSameBigGroup(adminInfo1, adminInfo2 *system.Admin) (isSame bool, err error) {
	// 如果销售和创建人是同一个小组
	if adminInfo1.GroupId == adminInfo2.GroupId {
		isSame = true
		return
	}
	var admin1BigGroupId, admin2BigGroupId int

	//获取第一个系统用户的大组id
	{
		//获取该账号的大组id
		pid, tmpErr := company.GetParentIdFromGroup(adminInfo1.GroupId)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		if pid != nil && *pid > 0 {
			//该账号放在了三级分组下
			admin1BigGroupId = *pid
		} else {
			//该账号放在了二级分组下
			admin1BigGroupId = adminInfo1.GroupId
		}
	}

	//获取第二个系统用户的大组id
	{
		//获取该账号的大组id
		pid, tmpErr := company.GetParentIdFromGroup(adminInfo2.GroupId)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		if pid != nil && *pid > 0 {
			//该账号放在了三级分组下
			admin2BigGroupId = *pid
		} else {
			//该账号放在了二级分组下
			admin2BigGroupId = adminInfo2.GroupId
		}
	}

	if admin1BigGroupId == admin2BigGroupId {
		isSame = true
		return
	}
	return
}

// CheckRaiAdmin 判断是否为权益内部人员
func CheckRaiAdmin(adminId int) (isRai bool, err error) {
	raiAdmins, e := system.GetRaiAdmin()
	if e != nil {
		err = fmt.Errorf("获取权益内部人员失败, GetRaiAdmin Err: %s", e.Error())
		return
	}
	for _, r := range raiAdmins {
		if adminId == r.AdminId {
			isRai = true
			break
		}
	}
	return
}

// GetMenuTreeRecursive 递归菜单树
func GetMenuTreeRecursive(list []*system.SysMenuItem, parentId int) []*system.SysMenuItem {
	res := make([]*system.SysMenuItem, 0)
	for _, v := range list {
		if v.ParentId == parentId {
			v.Children = GetMenuTreeRecursive(list, v.MenuId)
			res = append(res, v)
		}
	}
	return res
}
