package controllers

import (
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/company"
	"eta/eta_mobile/services"
	"eta/eta_mobile/utils"
)

// CompanyPermissionController 客户权限管理
type CompanyPermissionController struct {
	BaseAuthController
}

// List
// @Title 获取权限设置基础信息
// @Description 获取权限设置基础信息接口
// @Param   CompanyType   query   string  true       "客户类型：传空字符串或者不传为全部,'ficc','权益'"
// @Param   NoUpgrade   query   bool  false       "是否不展示升级权限，默认为 false"
// @Success 200 {object} company.PermissionSetResp
// @router /permission/list [get]
func (this *CompanyPermissionController) List() {
	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
	}
	// 非自用无需查询
	resp := new(company.PermissionSetResp)
	if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox && utils.BusinessCode != utils.BusinessCodeDebug {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		return
	}

	// Tips:目前这个接口权益在添加客户、申请转正、领取客户、续约申请均有用到

	companyType := this.GetString("CompanyType")
	var productId int

	roleTypeCode := sysUser.RoleTypeCode
	productId = services.GetProductId(roleTypeCode)

	// FICC
	if productId == utils.COMPANY_PRODUCT_FICC_ID || companyType == utils.COMPANY_PRODUCT_FICC_NAME {
		/*for _, v := range utils.PermissionFiccClassifyArr {
			checkList := make([]int, 0)
			p := new(company.PermissionSetList)
			p.ClassifyName = v
			items, err := company.GetPermissionSetItems(utils.COMPANY_PRODUCT_FICC_ID, v)
			if err != nil {
				br.Msg = "获取失败"
				br.ErrMsg = "获取权限信息失败，Err:" + err.Error()
				return
			}
			p.Items = items
			if v == "宏观经济" {
				checkList = append(checkList, 1)
			}
			p.CheckList = checkList
			resp.List = append(resp.List, p)
		}*/
		list, err := services.GetChartPermissionEnabledList()
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取权限信息失败，Err:" + err.Error()
			return
		}
		resp.List = list
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		return
	}

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

// PermissionVariety
// @Title 品种
// @Description 品种接口
// @Param   CompanyType   query   string  true       "客户类型：传空字符串或者不传为全部,'ficc','权益'"
// @Success 200 {object} company.PermissionVarietyResp
// @router /permission/variety [get]
func (this *CompanyPermissionController) PermissionVariety() {
	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
	}
	if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox && utils.BusinessCode != utils.BusinessCodeDebug {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		return
	}

	companyType := this.GetString("CompanyType")

	var productId int
	resp := new(company.PermissionVarietyResp)

	roleTypeCode := sysUser.RoleTypeCode
	if roleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
		productId = 2
	} else if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
		productId = 1
	} else {
		productId = 0
	}

	if productId == 1 {
		for k, v := range utils.PermissionFiccClassifyArr {
			p := new(company.PermissionVarietyList)
			p.ClassifyName = v
			p.ChartPermissionId = k + 100
			items, err := company.GetPermissionVarietyItems(productId, v)
			if err != nil {
				br.Msg = "获取失败"
				br.ErrMsg = "获取权限信息失败，Err:" + err.Error()
				return
			}
			p.Items = items
			resp.List = append(resp.List, p)
		}
	} else if productId == 2 {
		v := "权益"
		p := new(company.PermissionVarietyList)
		p.ClassifyName = v
		p.ChartPermissionId = 105
		items, err := company.GetPermissionVarietyItems(productId, v)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取权限信息失败，Err:" + err.Error()
			return
		}
		p.Items = items
		resp.List = append(resp.List, p)
	} else {
		if companyType == utils.COMPANY_PRODUCT_FICC_NAME {
			for k, v := range utils.PermissionFiccClassifyArr {
				p := new(company.PermissionVarietyList)
				p.ClassifyName = v
				p.ChartPermissionId = k + 100
				items, err := company.GetPermissionVarietyItems(1, v)
				if err != nil {
					br.Msg = "获取失败"
					br.ErrMsg = "获取权限信息失败，Err:" + err.Error()
					return
				}
				p.Items = items
				resp.List = append(resp.List, p)
			}
		} else if companyType == utils.COMPANY_PRODUCT_RAI_NAME {
			v := "权益"
			p := new(company.PermissionVarietyList)
			p.ClassifyName = v
			p.ChartPermissionId = 105
			items, err := company.GetPermissionVarietyItems(2, v)
			if err != nil {
				br.Msg = "获取失败"
				br.ErrMsg = "获取权限信息失败，Err:" + err.Error()
				return
			}
			p.Items = items
			resp.List = append(resp.List, p)
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
