package models

import (
	"github.com/beego/beego/v2/client/orm"
	"strings"
)

type ChartPermission struct {
	ChartPermissionId   int    `description:"权限id"`
	PermissionName      string `description:"权限名称"`
	ChartPermissionName string `description:"名称"`
	IsShowSustainable   bool   `description:"是否展示限免标签"`
	ImageUrlM           string `description:"图片地址"`
	IsChoose            bool   `description:"是否选择"`
}

type ChartPermissionItemResp struct {
	ChartPermissionId int    `description:"权限id"`
	PermissionName    string `description:"权限名称"`
	IsShowSustainable bool   `description:"是否展示限免标签"`
	ImageUrlM         string `description:"图片地址"`
	IsChoose          bool   `description:"是否选择"`
	MatchTypeName     string `description:"匹配类型名称"`
}

type ActivityChartPermission struct {
	ChartPermissionId int    `description:"权限id"`
	PermissionName    string `description:"权限名称"`
	IsChoose          bool   `description:"是否选择"`
}

// ChartPermission 报告权限表
type ChartPermissionCrm struct {
	ChartPermissionId   int    `orm:"column(chart_permission_id);pk" description:"问题ID"`
	ChartPermissionName string `description:"名称"`
	PermissionName      string `description:"权限名"`
	Remark              string `description:"备注"`
	ProductId           int    `description:"产品ID"`
}

type ChartPermissionResp struct {
	List []*ChartPermission
}

func GetChartPermissionAll(condition string) (items []*ChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE product_id=2 AND show_type=1  AND permission_type!=2  ` + condition + ` ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetChartPermissionAllXzs(condition string, pars []interface{}) (items []*ChartPermissionXzsResp, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE product_id=2 AND is_report=1  AND permission_type!=2  AND chart_permission_id != 52  ` + condition + ` ORDER BY sort ASC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetChartPermissionListRai() (items []*ChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE product_id=2  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetChartPermissionList 获取品种权限列表
func GetChartPermissionList() (list []*ChartPermissionCrm, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE parent_id > 0 ORDER BY product_id ASC, sort ASC`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

func GetCategoryId(chartPermissionId int) (category_id string, err error) {
	o := orm.NewOrm()
	sql := ` SELECT GROUP_CONCAT(a.category_id) AS category_id 
			FROM cygx_report_mapping AS a
			WHERE a.chart_permission_id=? AND a.is_summary = 1
			GROUP BY a.chart_permission_id `
	err = o.Raw(sql, chartPermissionId).QueryRow(&category_id)
	return
}

func GetChartPermissionReportAll(condition string) (items []*ChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE product_id=2 AND is_report=1 AND permission_type!=2 ` + condition + ` ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetChartPermissionById(chartPermissionId int) (item *ChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE chart_permission_id = ? `
	err = o.Raw(sql, chartPermissionId).QueryRow(&item)
	return
}

func GetChartPermissionActivity() (items []*ActivityChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	//sql := `SELECT * FROM chart_permission WHERE chart_permission_id IN (19,20,21,22,23,31,53) ORDER BY sort ASC `
	sql := `SELECT * FROM chart_permission WHERE product_id=2 AND is_report=1 AND permission_type!=2   AND permission_name IN ('医药','消费','科技','智造','策略','固收','研选订阅','周期') ORDER BY sort ASC  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 处理添加主观客观之后的分类ID错误
func GetUserCompanyPermission(companyId int) (items []*ActivityChartPermission, err error) {
	list, err := GetUserCompanyPermissionSandO(companyId)
	if err != nil {
		return
	}
	if len(list) == 0 {
		return
	}
	var chartPermissionName string
	for _, v := range list {
		chartPermissionName += "'" + v.PermissionName + "'" + ","
	}
	chartPermissionName = strings.Trim(chartPermissionName, ",")
	sql := ` SELECT b.*
			FROM company_report_permission AS a
			INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
			INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
			WHERE a.product_id=2
			AND c.is_suspend=0
            AND b.cygx_auth=1
            AND b.is_other=0
			AND c.status IN('正式','试用','永续')
			AND a.status IN('正式','试用','永续') 
			AND b.chart_permission_name IN (` + chartPermissionName + `)
			GROUP BY b.chart_permission_name
			ORDER BY b.sort ASC `
	o := orm.NewOrmUsingDB("weekly_report")
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetUserCompanyPermissionSandO(companyId int) (items []*ActivityChartPermission, err error) {
	sql := ` SELECT b.*
			FROM company_report_permission AS a
			INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
			INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
			WHERE  a.company_id=?
			AND a.product_id=2
			AND c.is_suspend=0
            AND b.cygx_auth=1
            AND b.is_other=0
			AND c.status IN('正式','试用','永续')
			AND a.status IN('正式','试用','永续') 
			AND b.chart_permission_name != '专家'
			GROUP BY b.chart_permission_name
			ORDER BY b.sort ASC `
	o := orm.NewOrmUsingDB("weekly_report")
	_, err = o.Raw(sql, companyId).QueryRows(&items)
	return
}

// 获取策略下面的所有分类
func GetCompanyReportPermissionAll() (items []*CompanyReportPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM company_report_permission WHERE chart_permission_id IN (19,20,21,22) AND status IN ('正式','试用','永续')  AND created_time <= '2021-12-28 15:05:43'`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取海通国际开通的默认权限
func GetCompanyPermissionIdWithHtgj() (chartpermissionids string, err error) {
	sql := ` SELECT GROUP_CONCAT(DISTINCT chart_permission_id  ORDER BY b.sort ASC  SEPARATOR ',') AS chartpermissionids
			FROM chart_permission as  b 
			WHERE chart_permission_name IN ('医药','消费','科技','智造','买方研选')`
	o := orm.NewOrmUsingDB("weekly_report")
	err = o.Raw(sql).QueryRow(&chartpermissionids)
	return
}

// GetChartPermissionById 主键获取权限
func GetChartPermissionByRemark(remark string) (item *ChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE remark = ? LIMIT 1`
	err = o.Raw(sql, remark).QueryRow(&item)
	return
}

func GetChartPermissionIdByName(condition string, pars []interface{}) (chartpermissionids string, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT GROUP_CONCAT(DISTINCT chart_permission_id   SEPARATOR ',') AS chartpermissionids FROM chart_permission  WHERE product_id=2  ` + condition
	err = o.Raw(sql, pars).QueryRow(&chartpermissionids)
	return
}

type ChartPermissionXzsResp struct {
	ChartPermissionId     int                        `description:"权限id"`
	PermissionName        string                     `description:"权限名称"`
	HasCheck              bool                       `description:"是否有权限"`
	AllTracks             string                     `default:"全部赛道";description:"全部赛道"`
	ListFollow            []*IndustrialManagementRep // 重点关注
	CheckListFollow       []int                      // 重点关注的ID
	ListPush              []*IndustrialManagementRep // 接受推送
	CheckListPush         []int                      // 接受推送的ID
	ListUninterested      []*IndustrialManagementRep // 不感兴趣
	CheckListUninterested []int                      // 不感兴趣的ID
}

// 获取权益主观权限
func GetChartPermissionListRaiSubjectivity() (items []*ChartPermission, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM chart_permission WHERE product_id=2  AND permission_type = 1 and parent_id > 0  ORDER BY sort ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
