package chart_theme

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

// ChartTheme
// @Description: 图表主题表
type ChartTheme struct {
	ChartThemeId     int       `description:"图表主题类型ID" orm:"column(chart_theme_id);pk"`
	ChartThemeName   string    `description:"图表主题名称"`
	ChartThemeTypeId int       `description:"图表主题类型ID"`
	ChartImage       string    `description:"缩略图"`
	Config           string    `description:"配置"`
	IsDelete         int       `description:"是否删除，0：未删除；1：已删除"`
	SysUserId        int       `description:"操作人"`
	SysUserRealName  string    `description:"操作人的真实名称"`
	IsSystemTheme    int       `description:"是否是系统主题，0：不是；1：是"`
	ModifyTime       time.Time `description:"修改时间"`
	CreateTime       time.Time `description:"创建时间"`
}

// GetChartThemeId
// @Description: 根据id获取主题
// @author: Roc
// @datetime 2023-12-14 16:05:36
// @param chartThemeId int
// @return item *ChartTheme
// @return err error
func GetChartThemeId(chartThemeId int) (item *ChartTheme, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_theme where chart_theme_id = ? AND is_delete = 0`
	err = o.Raw(sql, chartThemeId).QueryRow(&item)

	return
}

// Add
// @Description: 添加
// @author: Roc
// @receiver m
// @datetime 2023-12-14 16:11:10
// @param cols []string
// @return err error
func (m *ChartTheme) Add() (err error) {
	if m.ChartThemeId > 0 {
		err = errors.New("该配置已存在")
		return
	}
	o := orm.NewOrmUsingDB("data")
	lastId, err := o.Insert(m)
	if err != nil {
		return
	}
	m.ChartThemeId = int(lastId)

	return
}

// Update
// @Description: 更新
// @author: Roc
// @receiver m
// @datetime 2023-12-14 16:11:10
// @param cols []string
// @return err error
func (m *ChartTheme) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(m, cols...)
	return
}

// GetChartThemeListByTypeId
// @Description: 根据图表类型获取关联的图表主题列表
// @author: Roc
// @datetime 2023-12-13 17:39:48
// @return list []*ChartTheme
// @return err error
func GetChartThemeListByTypeId(chartThemeTypeId int) (list []*ChartTheme, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_theme WHERE chart_theme_type_id = ? AND  is_delete=0 ORDER BY chart_theme_id ASC `
	_, err = o.Raw(sql, chartThemeTypeId).QueryRows(&list)

	return
}

// GetAllChartThemeList
// @Description: 获取所有图表主题列表
// @author: Roc
// @datetime 2023-12-13 17:39:48
// @return list []*ChartTheme
// @return err error
func GetAllChartThemeList() (list []*ChartTheme, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_theme WHERE  is_delete=0 ORDER BY chart_theme_id ASC `
	_, err = o.Raw(sql).QueryRows(&list)

	return
}

// ChartThemeItem
// @Description: 图表主题配置
type ChartThemeItem struct {
	ChartThemeId     int    `description:"图表主题类型ID" orm:"column(chart_theme_id);pk"`
	ChartThemeName   string `description:"图表主题名称"`
	ChartThemeTypeId int    `description:"图表主题类型ID"`
	Config           string `description:"配置"`
	ChartImage       string `description:"缩略图"`
	//IsDelete            int       `description:"是否删除，0：未删除；1：已删除"`
	IsSystemTheme       int       `description:"是否是系统主题，0：不是；1：是"`
	SysUserId           int       `description:"操作人"`
	SysUserRealName     string    `description:"操作人的真实名称"`
	ModifyTime          time.Time `description:"修改时间"`
	CreateTime          time.Time `description:"创建时间"`
	DefaultChartThemeId int       `description:"默认使用的图表主题ID"`
}

// GetChartThemeItemList
// @Description: 根据图表类型id获取配置列表
// @author: Roc
// @datetime 2023-12-14 14:26:35
// @param chartThemeTypeId int
// @return list []*ChartThemeConfig
// @return err error
func GetChartThemeItemList(chartThemeTypeId int) (list []*ChartThemeItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT a.*,b.default_chart_theme_id FROM chart_theme a 
         JOIN chart_theme_type b on a.chart_theme_type_id =b.chart_theme_type_id 
         WHERE a.chart_theme_type_id = ? AND a.is_delete=0 ORDER BY a.chart_theme_id ASC `
	_, err = o.Raw(sql, chartThemeTypeId).QueryRows(&list)

	return
}

// GetSystemChartTheme
// @Description: 根据图表类型id获取系统配置
// @author: Roc
// @datetime 2023-12-14 14:26:35
// @param chartThemeTypeId int
// @return item *ChartTheme
// @return err error
func GetSystemChartTheme(chartThemeTypeId int) (item *ChartTheme, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT a.* FROM chart_theme a
         WHERE a.chart_theme_type_id = ? AND a.is_system_theme=1 ORDER BY a.chart_theme_id ASC `
	err = o.Raw(sql, chartThemeTypeId).QueryRow(&item)

	return
}
