package advisory

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

type ChartPermission struct {
	ChartPermissionId int    `description:"权限id"`
	ClassifyName      string `description:"分类名称"`
	PermissionName    string `description:"名称"`
	ImageUrl          string `description:"图片地址"`
	IsHaveNoRead      bool   `description:"是否存在未阅读的文章"`
	ChildPermission   []*ChartPermission
}

type ChartPermissionResp struct {
	List []*ChartPermission
}

// 用户关注的分类
type ChartPermissionListResp struct {
	ChartPermissionId   int    `description:"权限id"`
	ChartPermissionName string `description:"名称"`
	PermissionName      string `description:"名称"`
	ClassifyName        string `description:"分类名称"`
	ImageUrl            string `description:"图片地址"`
	IsFllow             bool   `description:"是否关注过分类"`
}

type GetChartPermissionAllByChartIdResp struct {
	ChartPermissionId int `description:"权限id"`
}

// 用户关注的分类
type MyChartPermission struct {
	ChartPermissionId   int    `description:"权限id"`
	ClassifyName        string `description:"分类名称"`
	PermissionName      string `description:"名称"`
	ChartPermissionName string `description:"名称"`
	ImageUrl            string `description:"图片地址"`
}

type MyChartPermissionResp struct {
	List []*MyChartPermission
}

type AdminInfoResp struct {
	AdminName   string `description:"系统用户名称"`
	CreatedTime string `description:"注册时间"`
	Headimgurl  string `description:"头像"`
	Mobile      string `description:"手机号"`
	ArticleNum  int    `description:"文章数量"`
	IsFllow     bool   `description:"是否关注过分类"`
	List        []*MyChartPermission
}

type NoAdminInfoResp struct {
	List []*MyChartPermission
}

func GetCategoryInfoById(chartPermissionId int) (item *ChartPermission, err error) {
	o := orm.NewOrm()
	//o.Using("rddp")
	sql := `SELECT * FROM chart_permission WHERE chart_permission_id=? AND product_id = 1  AND product_name = 'ficc' `
	err = o.Raw(sql, chartPermissionId).QueryRow(&item)
	return
}

// 获取分类列表
func GetChartPermissionList() (items []*MyChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT  * FROM chart_permission as ch  WHERE product_id = 1  AND product_name = 'ficc' and parent_id > 0  ORDER BY sort ASC; `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type GetBuChartPermissionResp struct {
	ChartPermissionId int    `description:"权限id"`
	PermissionName    string `description:"分类名称"`
	ImageUrl          string `description:"图片链接"`
}

type GetBuChartPermissionRespLIst struct {
	List []*GetBuChartPermissionResp
}

// 获取用户购买的分类信息
func GetBuChartPermission(CompanyId int, pars []interface{}) (items []*GetBuChartPermissionResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT ch.chart_permission_id , ch.permission_name ,ch.image_url  FROM chart_permission  as ch INNER JOIN company_report_permission as co ON co.chart_permission_id = ch.chart_permission_id WHERE co.company_id = ? `
	_, err = o.Raw(sql, CompanyId, pars).QueryRows(&items)
	return
}

// 获取分类列表
func GetPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*ChartPermission, err error) {
	sql := ` SELECT * FROM chart_permission WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  sort ASC LIMIT ?,? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 获取用户权限的一级分类名称
func GetFirstChartPermissionAllByUser(Ids string) (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT  * FROM chart_permission as ch  WHERE product_id = 1  AND product_name = 'ficc' AND chart_permission_id IN (` + Ids + `)` + ` GROUP BY ch.classify_name ORDER BY sort ASC; `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取用户权限的二级分类名称
func GetChartToClassifyNameByUser(ClassifyName, Ids string) (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission  WHERE product_id = 1  AND product_name = 'ficc' AND classify_name = ?   AND chart_permission_id IN (` + Ids + `)`
	_, err = o.Raw(sql, ClassifyName).QueryRows(&items)
	return
}

func GetChartPermissionAllByChartId(chartPermissionId int) (items []*GetChartPermissionAllByChartIdResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT chart_permission_id FROM chart_permission WHERE classify_name = (SELECT classify_name FROM chart_permission WHERE chart_permission_id = ?)`
	_, err = o.Raw(sql, chartPermissionId).QueryRows(&items)
	return
}
