package chart_permission

import (
	"errors"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
)

// GetListByProductId 根据产品id获取所有权限列表
func GetListByProductId(productId int64) (list []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Where(" product_id = ?", productId).Find(&list).Error
	return
}

// GetFiccListExceptTacticByProductId 获取ficc 除了市场策略的所有权限
func GetFiccListExceptTacticByProductId() (list []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Where(" enabled = 1 AND permission_type = 0 AND product_id = 1 and parent_id >0 and classify_name != '市场策略'").Order("sort asc").Find(&list).Error
	return
}

// GetClassNameListByProductId 根据权限id获取权限分类
func GetClassNameListByProductId(productId int64) (list []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Where(" product_id = ?", productId).Group("classify_name").Find(&list).Error
	return
}

// GetByWhereMap 根据查询条件map获取信息
func GetByWhereMap(where map[string]interface{}) (list []*ChartPermission, err error) {
	cond, whereVal, buildErr := utils.WhereBuild(where)
	if buildErr != nil {
		err = errors.New("系统异常，生成查询语句失败")
		return
	}
	err = global.MYSQL["rddp"].Where(cond, whereVal...).Find(&list).Error
	return
}

// GetListByIds 通过IDs获取图表权限集合
func GetListByIds(permissionIds []int) (list []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Model(ChartPermission{}).Where("chart_permission_id IN (?)", permissionIds).Scan(&list).Error
	return
}

// GetListByProductIdAndPermissionType 根据product及classify_name获取集合
func GetListByProductIdAndPermissionType(productId int) (items []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Model(ChartPermission{}).Where("enabled = 1 AND permission_type = 0 AND product_id = ? ", productId).Order("sort ASC").Scan(&items).Error
	return
}

// GetByChartPermissionId 根据chartPermissionId 查找权限基本信息
func GetByChartPermissionId(chartPermissionId int) (item *ChartPermission, err error) {
	err = global.MYSQL["rddp"].Model(ChartPermission{}).Where("chart_permission_id = ?", chartPermissionId).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetFiccFirstListExceptTacticByProductId 获取ficc 除了市场策略的所有父级
func GetFiccFirstListExceptTacticByProductId() (list []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Where(" enabled = 1 AND permission_type = 0 AND product_id = 1 and classify_name != '市场策略' AND parent_id = 0").Order("sort asc").Find(&list).Error
	return
}

// GetChartPermissionsByProductId 获取权限列表
func GetChartPermissionsByProductId() (list []*ChartPermission, err error) {
	err = global.MYSQL["rddp"].Where(" enabled = 1 AND product_id = 1").Order("parent_id ASC, sort ASC, created_time ASC").Find(&list).Error
	return
}

type SimpleChartPermission struct {
	ChartPermissionId   int                      `description:"品种ID" json:"chart_permission_id"`
	ChartPermissionName string                   `description:"品种名称" json:"chart_permission_name"`
	Sort                int                      `description:"排序" json:"sort"`
	Children            []*SimpleChartPermission `description:"子分类" json:"children"`
}

func FormatChartPermission2Simple(origin *ChartPermission) (item *SimpleChartPermission) {
	if origin == nil {
		return
	}
	item = new(SimpleChartPermission)
	item.ChartPermissionId = int(origin.ChartPermissionID)
	item.ChartPermissionName = origin.PermissionName
	item.Sort = origin.Sort
	return
}

type FaCalendarPermissionResp struct {
	List                  []*SimpleChartPermission
	CheckedPermissionId   int    `json:"checked_permission_id"`
	CheckedPermissionName string `json:"checked_permission_name"`
}
