package models

import (
	"encoding/json"
	"eta/eta_index_lib/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type EdbInfoReq struct {
	EdbInfoId int `description:"图表id,新增时传0"`
}

// DeleteChartInfoDataRedis 清除图表缓存
func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
	var req EdbInfoReq
	err = json.Unmarshal(bodyByte, &req)
	if err != nil {
		return
	}
	if req.EdbInfoId > 0 {
		list, tmpErr := GetChartEdbMappingListByEdbInfoId(req.EdbInfoId)
		if tmpErr != nil {
			err = tmpErr
			return
		}

		// 删除图表的id
		for _, v := range list {
			_ = utils.Rc.Delete(GetChartInfoDataKey(v.ChartInfoId))
		}
	}
	return
}

// GetChartInfoDataKey 获取图表缓存的key
func GetChartInfoDataKey(chartInfoId int) string {
	key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
	return key
}

type ChartInfo struct {
	ChartInfoId     int    `orm:"column(chart_info_id);pk"`
	ChartName       string `description:"来源名称"`
	ChartNameEn     string `description:"英文图表名称"`
	ChartClassifyId int    `description:"图表分类id"`
	SysUserId       int
	SysUserRealName string
	UniqueCode      string `description:"图表唯一编码"`
	CreateTime      time.Time
	ModifyTime      time.Time
	DateType        int    `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
	StartDate       string `description:"自定义开始日期"`
	EndDate         string `description:"自定义结束日期"`
	IsSetName       int    `description:"设置名称"`
	EdbInfoIds      string `description:"指标id"`
	ChartType       int    `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图"`
	Calendar        string `description:"公历/农历"`
	SeasonStartDate string `description:"季节性图开始日期"`
	SeasonEndDate   string `description:"季节性图开始日期"`
	ChartImage      string `description:"图表图片"`
	Sort            int    `description:"排序字段,数字越小越排前面"`
	LeftMin         string `description:"图表左侧最小值"`
	LeftMax         string `description:"图表左侧最大值"`
	RightMin        string `description:"图表右侧最小值"`
	RightMax        string `description:"图表右侧最大值"`
	Disabled        int    `description:"是否禁用,0:启用,1:禁用,默认:0"`
	BarConfig       string `description:"柱方图的配置,json数据"`
	Source          int    `description:"1:ETA图库;2:商品价格曲线"`
	ExtraConfig     string `description:"图表额外配置,json数据"`
}

// GetChartInfoById 通过id获取图表信息
func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
	err = o.Raw(sql, chartInfoId).QueryRow(&item)
	return
}