package my_chart_classify_mapping

import (
	"hongze/hongze_yb/global"
)

// GetMyChartSort 获取我的图表排序
func GetMyChartSort(adminId, myChartClassifyId, sort int) (item *MyChartClassifyMapping, err error) {
	sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND admin_id = ? AND my_chart_classify_id = ? `
	if sort == 1 {
		sql += ` ORDER BY sort DESC LIMIT 1 `
	} else {
		sql += ` ORDER BY sort ASC LIMIT 1 `
	}
	err = global.MYSQL["data"].Raw(sql, adminId, myChartClassifyId).First(&item).Error

	return
}

// GetMyChartClassifyMapping 获取图表分类关联信息
func GetMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (item *MyChartClassifyMapping, err error) {
	sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
	err = global.MYSQL["data"].Raw(sql, adminId, myChartId, myChartClassifyId).First(&item).Error

	return
}

type MyChartSortInfo struct {
	ChartInfoId         int     `description:"图表ID"`
	ChartInfoSource     int     `description:"1：ETA图库；2：商品价格"`
	MyChartId           int     `description:"我的图表ID"`
	MyChartClassifyId   int     `description:"我的图表分类ID"`
	MyChartClassifyName string  `description:"我的图表分类名称"`
	UniqueCode          string  `description:"图表唯一编码"`
	Sort                float64 `description:"排序"`
}

// GetClassifyChartSort 获取指定分类图表排序
func GetClassifyChartSort(myChartClassifyId, sort int) (item []*MyChartClassifyMapping, err error) {
	sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND my_chart_classify_id = ? `
	if sort == 1 {
		sql += ` ORDER BY sort DESC `
	} else {
		sql += ` ORDER BY sort ASC `
	}
	err = global.MYSQL["data"].Raw(sql, myChartClassifyId).Scan(&item).Error

	return
}

// GetMyChartSortInfoList 获取我的图表信息
func GetMyChartSortInfoList(myChartClassifyId string, sort int) (item []*MyChartSortInfo, err error) {
	sql := `SELECT
				a.my_chart_id,
				a.my_chart_classify_id,
				a.sort,
				c.chart_info_id,
				c.source as chart_info_source,
				c.unique_code,
				d.my_chart_classify_name
			FROM
				my_chart_classify_mapping AS a
			INNER JOIN
				my_chart AS b ON a.my_chart_id = b.my_chart_id
			INNER JOIN
				chart_info AS c ON b.chart_info_id = c.chart_info_id
			INNER JOIN
				my_chart_classify AS d ON a.my_chart_classify_id = d.my_chart_classify_id
			WHERE
				1 = 1
			AND a.my_chart_classify_id = ? `
	//GROUP BY c.chart_info_id `
	if sort == 1 {
		sql += ` ORDER BY sort DESC `
	} else {
		sql += ` ORDER BY sort ASC `
	}
	err = global.MYSQL["data"].Raw(sql, myChartClassifyId).Scan(&item).Error

	return
}
