package system

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

type AdminConfig struct {
	ConfigId    int       `orm:"column(config_id);pk" description:"配置id"`
	AdminId     int       `description:"系统客户id"`
	ConfigCode  string    `description:"配置编码"`
	ConfigValue string    `description:"配置值"`
	Remark      string    `description:"备注信息"`
	CreateTime  time.Time `description:"创建时间"`
}

// Update 更新基础信息
func (item *AdminConfig) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(item, cols...)
	return
}

func (item *AdminConfig) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(item)
	if err != nil {
		return
	}
	item.ConfigId = int(id)
	return
}

var EdbLanguageVar = `edb_language`
var PredictEdbLanguageVar = `predict_edb_language`
var ChartLanguageVar = `chart_language`

// GetConfigDetailByCode 获取详情
func GetConfigDetailByCode(adminId int, configCode string) (item *AdminConfig, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM admin_config WHERE admin_id = ? AND  config_code=? `
	err = o.Raw(sql, adminId, configCode).QueryRow(&item)
	return
}

// GetDefaultConfigDetailByCode 获取默认配置详情
func GetDefaultConfigDetailByCode(configCode string) (item *AdminConfig, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM admin_config WHERE admin_id = ? AND  config_code=? `
	err = o.Raw(sql, 0, configCode).QueryRow(&item)
	return
}

// SetAdminConfigReq 设置用户的配置
type SetAdminConfigReq struct {
	ConfigCode  string `description:"配置编码"`
	ConfigValue string `description:"配置值"`
}
