package my_chart_classify

import (
	"errors"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
)

// GetClassifyListByCondition 根据条件获取图表分类列表
func GetClassifyListByCondition(condition map[string]interface{}, sort string) (list []*MyChartClassify, err error) {
	maps, values, buildErr := utils.WhereBuild(condition)
	if buildErr != nil {
		err = errors.New("系统异常，生成查询语句失败")
		return
	}
	err = global.MYSQL["data"].Model(MyChartClassify{}).Where(maps, values...).Order(sort).Scan(&list).Error

	return
}

// GetClassifyById 根据主键获取分类信息
func GetClassifyById(chartClassifyId int) (item *MyChartClassify, err error) {
	err = global.MYSQL["data"].Model(MyChartClassify{}).Where("my_chart_classify_id = ?", chartClassifyId).First(&item).Error

	return
}

// GetClassifyByIdAndAdminId 根据ID及adminId获取分类信息
func GetClassifyByIdAndAdminId(adminId, myChartClassifyId int) (item *MyChartClassify, err error) {
	err = global.MYSQL["data"].Model(MyChartClassify{}).Where("my_chart_classify_id = ? AND admin_id = ?", myChartClassifyId, adminId).First(&item).Error

	return
}

// GetFirstClassifyByAdminId 获取账号排序第一条的分类数据
func GetFirstClassifyByAdminId(adminId int) (item *MyChartClassify, err error) {
	sql := ` SELECT * FROM my_chart_classify WHERE admin_id=? order by sort asc,my_chart_classify_id asc limit 1`
	err = global.MYSQL["data"].Raw(sql, adminId).Scan(&item).Error

	return
}
