package company

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

type ChartPermission struct {
	ChartPermissionId   int       `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:"分类"`
	PermissionType      int       `description:"1主观，2客观"`
	Checked             bool      `description:"选中状态"`
}

type PermissionSetItem struct {
	ChartPermissionId int                  `description:"权限id"`
	PermissionName    string               `description:"权限名称"`
	PermissionType    int                  `description:"1主观，2客观"`
	Checked           bool                 `description:"选中状态"`
	Child             []*PermissionSetItem `description:"具体的主客观-方便前端的排版用的"`
}

type PermissionSetItemType struct {
	PermissionName string `description:"权限名称"`
	Checked        bool   `description:"选中状态"`
	CheckedMinate  bool   `description:"不确定状态"`
	NoClicking     bool   `description:"是否禁止点击"`
	Items          []*PermissionLookItem
}

type PermissionSetList struct {
	PermissionName string `description:"分类"`
	Child          []*PermissionSetItem
	CheckList      []int
}

type PermissionSetListType struct {
	PermissionName string `description:"分类"`
	Child          []*PermissionSetItemType
	CheckList      []int
}

type PermissionSetResp struct {
	List []*PermissionSetList
	//ListUpgrade []*PermissionSetList     `description:"升级权限列表"`
	ListType []*PermissionSetListType `description:"主观客观列表"`
}

func GetPermissionSetItems(productId int, classifyName string) (items []*PermissionSetItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM chart_permission WHERE 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
}

type PermissionLookItem struct {
	ChartPermissionId  int                   `description:"权限id"`
	PermissionName     string                `description:"权限名称"`
	StartDate          string                `description:"权限开始日期"`
	EndDate            string                `description:"权限结束日期"`
	Status             string                `description:"'正式','试用','关闭'"`
	ExpireDay          string                `description:"到期天数"`
	ClassifyName       string                `description:"分类"`
	PermissionType     int                   `description:"1主观，2客观"`
	PermissionTypeName string                `description:"主观、客观"`
	Checked            bool                  `description:"选中状态"`
	Remark             string                `description:"备注"`
	IsMerge            bool                  `description:"是否合并行业, 给前端的标识, 暂时仅权益使用"`
	RaiBothHas         bool                  `description:"权益-是否主客观都有"`
	IsUpgrade          int                   `description:"是否升级，1是，0否"`
	Child              []*PermissionLookItem `description:"子权限"`
}

type PermissionVarietyResp struct {
	List []*PermissionVarietyList
}

type PermissionVarietyItem struct {
	ChartPermissionId int    `description:"权限id"`
	ClassifyName      string `orm:"column(permission_name)" description:"权限名称"`
}

type PermissionVarietyList struct {
	ChartPermissionId int    `description:"父级id"`
	ClassifyName      string `description:"分类"`
	Items             []*PermissionVarietyItem
}

func GetPermissionVarietyItems(productId int, classifyName string) (items []*PermissionVarietyItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM chart_permission WHERE enabled=1 AND product_id=? AND classify_name=? GROUP BY permission_name ORDER BY sort ASC `
	_, err = o.Raw(sql, productId, classifyName).QueryRows(&items)
	return
}

// GetChartPermissionListById 根据权限id获取产品权限详情
func GetChartPermissionListById(chartPermissionId int) (item *ChartPermission, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM chart_permission WHERE chart_permission_id =? `
	err = o.Raw(sql, chartPermissionId).QueryRow(&item)
	return
}

func GetParentIdFromGroup(gid int) (items *int, err error) {
	o := orm.NewOrm()
	sql := `SELECT parent_id FROM sys_group WHERE group_id=? `
	err = o.Raw(sql, gid).QueryRow(&items)
	return
}

func GetGroupNamesById(gid int) (items *string, err error) {
	o := orm.NewOrm()
	sql := `SELECT group_name FROM sys_group WHERE group_id=? `
	err = o.Raw(sql, gid).QueryRow(&items)
	return
}
