package controllers

import (
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/services"
	"hongze/hongze_cygx/utils"
)

// 品种
type ChartPermissionController struct {
	BaseCommonController
}

type ChartPermissionAuthController struct {
	BaseAuthController
}

// @Title 获取所有品种
// @Description 获取用户详情接口
// @Success 200 {object} models.ChartPermissionResp
// @router /all [get]
func (this *ChartPermissionAuthController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	var condition string
	var userType int
	if user.CompanyId > 1 {
		total, err := models.GetCountCompanyDetailByIdGroup(user.CompanyId)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		if total == 0 {
			userType = 0
		} else {
			companyDetail, err := models.GetCompanyDetailByIdGroup(user.CompanyId)
			if err != nil {
				br.Msg = "获取信息失败！"
				br.ErrMsg = "获取客户详情失败，Err:" + err.Error()
				return
			}
			if companyDetail.Status == "永续" {
				userType = 1
			}
		}
	}
	if userType == 1 {
		condition += ` AND permission_name  != '研选'`
	}
	condition += ` AND permission_name  != '医药'`
	list, err := models.GetChartPermissionAll(condition)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	resp := new(models.ChartPermissionResp)
	for k, v := range list {
		//研选订阅包不进行展示
		if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
			continue
		}
		//固收不进行展示
		if v.ChartPermissionId == utils.GU_SHOU_ID_CHART {
			continue
		}
		//周期不进行展示
		if v.PermissionName == utils.ZHOU_QI_NAME {
			continue
		}
		if userType == 1 {
			if v.PermissionName != "研选" {
				resp.List = append(resp.List, v)
			}
		} else {
			if v.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
				v.PermissionName = "买方研选"
			}
			resp.List = append(resp.List, v)
		}
		if services.GetShowSustainable() && v.PermissionName == "研选" {
			resp.List[k].IsShowSustainable = true
		}
	}
	//resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 获取报告所有品种
// @Description 获取报告所有品种接口
// @Success 200 {object} models.ChartPermissionResp
// @router /reportall [get]
func (this *ChartPermissionAuthController) ReportDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	var condition string
	var userType int
	if user.CompanyId > 1 {
		total, err := models.GetCountCompanyDetailByIdGroup(user.CompanyId)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		if total == 0 {
			userType = 0
		} else {
			companyDetail, err := models.GetCompanyDetailByIdGroup(user.CompanyId)
			if err != nil {
				br.Msg = "获取信息失败！"
				br.ErrMsg = "获取客户详情失败，Err:" + err.Error()
				return
			}
			if companyDetail.Status == "永续" {
				userType = 1
			}
		}
	}
	if userType == 1 {
		condition += ` AND permission_name  != '买方研选'`
	}
	list, err := models.GetChartPermissionReportAll(condition)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	resp := new(models.ChartPermissionResp)
	for k, v := range list {
		if userType == 1 {
			if v.PermissionName != "研选" {
				resp.List = append(resp.List, v)
			}
		} else {
			resp.List = append(resp.List, v)
		}
		if services.GetShowSustainable() && v.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
			resp.List[k].IsShowSustainable = true
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 获取策略下的所有分类
// @Description 获取策略下的所有分类接口
// @Param   ChartPermissionId   query   int  false     "行业id"
// @Success 200 {object} models.ReportMappingResp
// @router /strategyAll [get]
func (this *ChartPermissionAuthController) StrategyDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	//默认给策略的ID
	chartPermissionId, _ := this.GetInt("ChartPermissionId")
	if chartPermissionId == 0 {
		chartPermissionId = utils.CE_LUE_ID
	}
	uid := user.UserId
	list, err := models.GetReportMappingStrategyHomeAllByCygx(chartPermissionId)

	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	for k, v := range list {
		count, err := models.GetUserIsReadThisNewCategoryArticleCount(v.CategoryId, uid)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取品种信息失败，Err:" + err.Error()
			return
		}
		Newdetail, err := models.GetNewArticleByCategoryId(v.CategoryId)
		if err != nil {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取信息失败,Err:" + err.Error()
			return
		}
		if count == 0 && user.CreatedTime.Before(utils.StrTimeToTime(Newdetail.PublishDate)) && utils.StrTimeToTime(utils.OnlineTime).Before(utils.StrTimeToTime(Newdetail.PublishDate)) {
			list[k].IsRed = true
		}
	}
	cf, err := models.GetConfigByCode(utils.CYGX_TACTICS_TIME_LINE_STATUS)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp := new(models.ReportMappingHomeResp)
	if chartPermissionId != utils.GU_SHOU_ID_CHART && chartPermissionId != utils.ZHOU_QI_ID {
		if cf.ConfigValue == "1" || user.CompanyId == utils.HZ_COMPANY_ID {
			item := new(models.ReportMappingHome)
			item.CategoryId = utils.TIME_LINE_ID
			item.SubCategoryName = utils.TIME_LINE_NAME
			item.MatchTypeName = utils.TIME_LINE_NAME
			item.ListGroup = 1
			resp.List = append(resp.List, item)
		}
	}

	cfGushou, err := models.GetConfigByCode(utils.CYGX_GUSHOU_TIME_LINE_STATUS)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	if chartPermissionId == utils.GU_SHOU_ID_CHART {
		if cfGushou.ConfigValue == "1" || user.CompanyId == utils.HZ_COMPANY_ID {
			item := new(models.ReportMappingHome)
			item.CategoryId = utils.TIME_LINE_ID
			item.SubCategoryName = utils.TIME_LINE_NAME
			item.MatchTypeName = utils.TIME_LINE_NAME
			item.ListGroup = 1
			resp.List = append(resp.List, item)
		}
	}
	for _, v := range list {
		resp.List = append(resp.List, v)
	}

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

// @Title 获取所有品种（无需token）
// @Description 获取所有品种（无需token）
// @Success 200 {object} models.ChartPermissionResp
// @router /allPublic [get]
func (this *ChartPermissionController) DetailPublic() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var condition string
	list, err := models.GetChartPermissionAll(condition)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	resp := new(models.ChartPermissionResp)
	for k, v := range list {
		resp.List = append(resp.List, v)
		if services.GetShowSustainable() && v.PermissionName == "研选" {
			resp.List[k].IsShowSustainable = true
		}
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 获取报告所有品种7.0版本
// @Description 获取报告所有品种接口7.0版本
// @Success 200 {object} models.ChartPermissionResp
// @router /reportallV7 [get]
func (this *ChartPermissionAuthController) ReportDetailV7() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	var condition string
	//var userType int
	//if user.CompanyId > 1 {
	//	total, err := models.GetCountCompanyDetailByIdGroup(user.CompanyId)
	//	if err != nil {
	//		br.Msg = "获取失败"
	//		br.ErrMsg = "获取失败，Err:" + err.Error()
	//		return
	//	}
	//	if total == 0 {
	//		userType = 0
	//	} else {
	//		companyDetail, err := models.GetCompanyDetailByIdGroup(user.CompanyId)
	//		if err != nil {
	//			br.Msg = "获取信息失败！"
	//			br.ErrMsg = "获取客户详情失败，Err:" + err.Error()
	//			return
	//		}
	//		if companyDetail.Status == "永续" {
	//			userType = 1
	//		}
	//	}
	//}
	condition += ` AND chart_permission_id  NOT  IN  (31,52)`
	list, err := models.GetChartPermissionReportAll(condition)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	resp := new(models.ChartPermissionResp)
	for k, v := range list {
		//if userType == 1 {
		//	if v.PermissionName != "研选" {
		//		resp.List = append(resp.List, v)
		//	}
		//} else {
		//	resp.List = append(resp.List, v)
		//}
		resp.List = append(resp.List, v)
		//if  strings.Contains(v.PermissionName,"")
		if services.GetShowSustainable() && v.PermissionName == utils.CHART_PERMISSION_NAME_MF_YANXUAN {
			resp.List[k].IsShowSustainable = true
		}
	}
	//item := new(models.ChartPermission)
	//item.PermissionName = utils.GU_SHOU_NAME
	//item.ChartPermissionId = utils.GU_SHOU_ID
	//resp.List = append(resp.List, item)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 获取报告医药、消费、科技、智造、策略权限列表(没有买方研选)
// @Description 获取报告医药、消费、科技、智造、策略权限列表(没有买方研选)
// @Success 200 {object} models.ChartPermissionResp
// @router /noyx [get]
func (this *ChartPermissionAuthController) Noyx() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	var condition string
	condition += ` AND permission_name IN ('医药1','消费','科技','智造','策略','固收','周期') `
	list, err := models.GetChartPermissionReportAll(condition)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
		return
	}
	resp := new(models.ChartPermissionResp)
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
