package models

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/utils"
	"time"
)

// ChartPermission 报告权限表
type ChartPermission struct {
	ChartPermissionId   int       `orm:"column(chart_permission_id);pk" description:"问题ID"`
	ChartPermissionName string    `description:"名称"`
	PermissionName      string    `description:"权限名"`
	Sort                int       `description:"排序"`
	Enabled             int       `description:"是否可用"`
	CreatedTime         time.Time `description:"创建时间"`
	LastUpdatedTime     time.Time `description:"更新时间"`
	TeleconferenceSort  int       `description:"电话会类型排序"`
	Remark              string    `description:"备注"`
	ClassifyName        string    `description:"分类名称"`
	ProductName         string    `description:"产品名称"`
	ProductId           int       `description:"产品ID"`
	ImageURL            string    `description:"图片地址"`
	ShowType            int       `description:"1:查研观向小程序展示"`
	IsOther             int       `description:"是否是其他，用于查研观向小程序后台展示"`
	IsReport            int       `description:"是否是报告，用于查研观向小程序前台报告展示"`
	CygxAuth            int       `description:"是否是权限，用于查研观向小程序前台权限校验"`
	YbImgUrl            string    `description:"研报小程序报告列表icon"`
	PriceDrivenState    int       `description:"品种价格驱动开启状态 0-关闭 1-开启"`
	ParentId            int       `description:"父级权限id"`
	IsPublic            int       `description:"是否是公有权限1:公有权限，0私有权限"`
}

// GetChartPermissionById 主键获取权限
func GetChartPermissionById(permissionId int) (item *ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE chart_permission_id = ? LIMIT 1`
	err = o.Raw(sql, permissionId).QueryRow(&item)
	return
}

// Update 更新
func (chartPermissionInfo *ChartPermission) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(chartPermissionInfo, cols...)
	return
}

// YbChartPermissionFirst 研报一级品种分类
type YbChartPermissionFirst struct {
	Id           int
	ClassifyName string `description:"权限一级分类名称，与chart_permission的classify_name一致"`
	YbIndexShow  int    `description:"是否展示在研报小程序首页页面  1-展示；0-不展示"`
	YbIndexName  string `description:"研报小程序首页简称"`
	YbIndexSort  string `description:"研报小程序首页排序"`
	YbIndexIcon  string `description:"研报小程序首页图标地址"`
}

// GetYbChartPermissionFirst 获取研报一级品种分类
func GetYbChartPermissionFirst() (list []*YbChartPermissionFirst, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission_first ORDER BY yb_index_sort ASC`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetFiccPermissionExceptTactic 获取除了市场策略外的ficc权限
func GetFiccPermissionExceptTactic() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE enabled = 1 AND permission_type = 0 AND product_id = 1 and classify_name != '市场策略' ORDER BY sort ASC`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetFiccPermissionSecondPublic 获取ficc公有的二级品种
func GetFiccPermissionSecondPublic() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE enabled = 1 AND permission_type = 0 AND product_id = 1 and parent_id>0 and is_public=1 ORDER BY sort ASC`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetPermissionFirstByProductId 获取一级品种
func GetPermissionFirstByProductId(productId int) (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE enabled = 1 AND product_id = ? and parent_id=0  ORDER BY sort ASC`
	_, err = o.Raw(sql, productId).QueryRows(&items)
	return
}

// GetPermissionFirst 获取一级品种
func GetPermissionFirst() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE enabled = 1 and parent_id=0  ORDER BY sort ASC`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetPermissionByProductIdAndClassifyName 获取子分类
func GetPermissionByProductIdAndClassifyName(productId int, classifyName string) (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE parent_id > 0 and enabled = 1 AND product_id = ? AND classify_name = ? AND permission_type = 0 ORDER BY sort ASC`
	_, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
	return
}

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

// GetChartPermissionByIds 主键获取权限
func GetChartPermissionByIds(permissionIds []string) (list []*ChartPermission, err error) {
	qb, _ := orm.NewQueryBuilder("mysql")
	// 构建查询对象
	qb.Select("*").From("chart_permission").
		Where("parent_id > 0 and chart_permission_id").In(permissionIds...)
	// 导出 SQL 语句
	sql := qb.String()

	// 执行 SQL 语句
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetChartPermissionByIdList 主键获取权限
func GetChartPermissionByIdList(chartPermissionIdList []int) (list []*ChartPermission, err error) {
	num := len(chartPermissionIdList)
	if num <= 0 {
		return
	}

	o := orm.NewOrm()
	sql := `select * from chart_permission where parent_id > 0 and chart_permission_id in (` + utils.GetOrmInReplace(num) + `)`
	_, err = o.Raw(sql, chartPermissionIdList).QueryRows(&list)

	return
}

// GetChartPermissionByNames 主键获取权限
func GetChartPermissionByNames(permissionNames []string) (list []*ChartPermission, err error) {
	qb, _ := orm.NewQueryBuilder("mysql")
	// 构建查询对象
	qb.Select("*").From("chart_permission").
		Where("parent_id > 0 and chart_permission_name").In(permissionNames...)
	// 导出 SQL 语句
	sql := qb.String()

	// 执行 SQL 语句
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

func GetChartPermissionByNamesByRai(permissionNames []string) (list []*ChartPermission, err error) {
	qb, _ := orm.NewQueryBuilder("mysql")
	// 构建查询对象
	qb.Select("*").From("chart_permission").
		Where("parent_id > 0 AND  product_id = 2 and chart_permission_name").In(permissionNames...)
	// 导出 SQL 语句
	sql := qb.String()

	// 执行 SQL 语句
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

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

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

// 获取权益主观权限
func GetChartPermissionListRaiSubjectivity() (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	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
}

func GetChartPermissionListByCondition(condition string, pars []interface{}) (items []*ChartPermission, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM chart_permission WHERE  parent_id > 0 `
	if condition != `` {
		sql += condition
	}
	sql += `  ORDER BY sort ASC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
