package my_chart

import (
	"hongze/hongze_yb/global"
	"time"
)

type MyChartList struct {
	MyChartId           int `description:"我的图表主键"`
	ChartInfoId         int
	ChartName           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:季节性图"`
	Calendar            string  `description:"公历/农历"`
	SeasonStartDate     string  `description:"季节性图开始日期"`
	SeasonEndDate       string  `description:"季节性图开始日期"`
	ChartImage          string  `description:"图表图片"`
	Sort                float64 `description:"排序字段，数字越小越排前面"`
	IsAdd               bool    `description:"true:已加入我的图库,false:未加入我的图库"`
	MyChartClassifyName string  `description:"分类名称，多个用英文逗号分割"`
	MyChartClassifyId   string  `description:"分类id，多个用英文逗号分割"`
	EdbEndDate          string  `description:"指标最新更新日期"`
	Source    int    `description:"1：ETA图库；2：商品价格"`
}

type MyChartView struct {
	MyChartId         int       `description:"我的图表id"`
	ChartInfoId       int       `description:"图表id"`
	AdminId           int       `description:"用户id"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
	MyChartClassifyId string    `description:"我的图表分类ID"`
	//Sort			  int		`description:"我的图表排序"`
}

type MoveMyChartReq struct {
	MyChartId         int `description:"移动的，我的图表图表ID"`
	PrevMyChartId     int `description:"移动到的位置，上一级的，我的图表ID,如果没有上一级，则传0"`
	NextMyChartId     int `description:"移动到的位置，下一级的，我的图表ID,如果没有下一级，则传0"`
	MyChartClassifyId int `description:"当前分类id"`
}

type MoveMyChartClassifyReq struct {
	MyChartClassifyId int `description:"分类id"`
	PrevClassifyId    int `description:"上一个兄弟节点分类id"`
	NextClassifyId    int `description:"下一个兄弟节点分类id"`
}

// GetMyChartListByCondition 条件获取我的图表列表
func GetMyChartListByCondition(condition string, pars []interface{}, page, limit int) (list []*MyChartList, err error) {
	sql := ` SELECT a.my_chart_id,c.sort,b.* FROM my_chart AS a
			INNER JOIN chart_info AS b ON a.chart_info_id=b.chart_info_id
			INNER JOIN my_chart_classify_mapping AS c ON a.my_chart_id=c.my_chart_id
			INNER JOIN my_chart_classify AS d ON c.my_chart_classify_id=d.my_chart_classify_id
			WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += " GROUP BY a.chart_info_id ORDER BY c.sort ASC LIMIT ?,? "
	offset := (page - 1) * limit
	pars = append(pars, offset, limit)
	err = global.MYSQL["data"].Raw(sql, pars...).Scan(&list).Error

	return
}

// GetMyChartByCondition 条件获取我的图表
func GetMyChartByCondition(condition string, pars []interface{}) (list []*MyChartView, err error) {
	sql := ` SELECT a.*,GROUP_CONCAT(c.my_chart_classify_id SEPARATOR ',') AS my_chart_classify_id FROM my_chart AS a
			LEFT JOIN  my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
			LEFT JOIN my_chart_classify AS c ON b.my_chart_classify_id=c.my_chart_classify_id AND b.admin_id=c.admin_id
			WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY a.chart_info_id  `
	err = global.MYSQL["data"].Raw(sql, pars).Scan(&list).Error

	return
}

// GetMyChartById 主键获取我的图表
func GetMyChartById(myChartId int, myChartClassifyId string) (item *MyChartView, err error) {
	sql := ` SELECT a.*,b.sort,c.my_chart_classify_id FROM my_chart AS a
			INNER JOIN my_chart_classify_mapping AS b ON a.my_chart_id=b.my_chart_id AND a.admin_id=b.admin_id
			INNER JOIN my_chart_classify AS c ON b.my_chart_classify_id=c.my_chart_classify_id AND b.admin_id=c.admin_id
			WHERE 1=1 AND a.my_chart_id = ? AND b.my_chart_classify_id = ?`
	err = global.MYSQL["data"].Raw(sql, myChartId, myChartClassifyId).First(&item).Error

	return
}
