package classify

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

func GetSimpleAll() (list []*Classify, err error) { // 过滤权益研报的分类
	err = global.MYSQL["rddp"].Select("id, classify_name, yb_icon_url, yb_bg_url, parent_id").Model(Classify{}).Where("is_show = 1 AND parent_id != 56 AND id != 56").Scan(&list).Error
	return
}

// GetByClassifyName 根据分类名称查找专栏详情
func GetByClassifyName(classifyName string) (item *Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Where("classify_name = ?", classifyName).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetByClassifyNameFirst 根据一级分类名称查找一级分类
func GetByClassifyNameFirst(classifyName string) (item *Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Where("classify_name = ? and parent_id = 0", classifyName).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetSecondIdsByClassifyNames 根据权限相关的二级分类名称获取名称ID
func GetSecondIdsByClassifyNames(names []string) (ids []int, err error) {
	var list []*Classify
	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id >0 AND is_show = 1 AND enabled = 1 AND parent_id != 56", names).Scan(&list).Error
	if err != nil {
		if err == utils.ErrNoRow {
			err = nil
		}
		return
	}
	for _, v := range list {
		ids = append(ids, v.Id)
	}
	return
}

// GetParentList 查询所有一级分类
func GetParentList() (list []*Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).
		Select("id, classify_name, show_type, yb_ficc_icon, yb_ficc_pc_icon, yb_ficc_sort").
		Where("parent_id = 0 and classify_name != '权益研报' AND is_show = 1 AND enabled = 1").
		Order("sort asc, id asc").Scan(&list).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetByClassifyId 根据分类ID查询分类详情
func GetByClassifyId(id int) (item *Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Where("id = ? AND is_show = 1 AND enabled = 1", id).First(&item).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetListByPid 根据分类名称查找专栏列表
func GetListByPid(pid int) (list []*Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).
		Select("id, classify_name, parent_id, abstract, report_author, author_descript, home_img_url, vip_title, avatar_img_url").
		Where("parent_id = ? AND is_show = 1 AND enabled = 1", pid).Order("sort asc, id asc").Scan(&list).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetIdsByClassifyNameAndParentId 查询
func GetIdsByClassifyNameAndParentId(names []string, parentId int) (ids []int, err error) {
	var list []*Classify
	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id = ? AND is_show = 1 AND enabled = 1", names, parentId).Scan(&list).Error
	if err != nil {
		return
	}
	for _, v := range list {
		ids = append(ids, v.Id)
	}
	return
}

// GetOtherIdsByClassifyNames 查询
func GetOtherIdsByClassifyNames(names []string) (ids []int, err error) {
	var list []*Classify
	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id > 0 AND is_show = 1 AND enabled = 1", names).Scan(&list).Error
	if err != nil {
		return
	}
	for _, v := range list {
		ids = append(ids, v.Id)
	}
	return
}

// GetOtherIdsByClassifyNames 查询
func GetOtherSecondIds(names []string) (ids []int, err error) {
	var list []*Classify
	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("parent_id IN (SELECT id FROM classify WHERE classify_name IN (?) AND parent_id = 0 AND is_show = 1 AND enabled = 1)", names).Scan(&list).Error
	if err != nil {
		return
	}
	for _, v := range list {
		ids = append(ids, v.Id)
	}
	return
}

// GetChildByPid 根据一级分类查找二级分类
func GetChildByPid(pid int) (list []*Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Where("parent_id = ? AND is_show = 1 AND enabled = 1", pid).Scan(&list).Error
	if err == utils.ErrNoRow {
		err = nil
	}
	return
}

// GetClassifyList 获取全部分类
func GetClassifyList() (list []*Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Scan(&list).Error
	return
}

// GetByClassifyIdFirst
// @Description: 根据一级分类id查找一级分类
// @author: Roc
// @datetime 2024-06-24 13:57:59
// @param classifyId int
// @return item *Classify
// @return err error
func GetByClassifyIdFirst(classifyId int) (item *Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Where("id = ? and parent_id = 0", classifyId).First(&item).Error
	if errors.Is(err, utils.ErrNoRow) {
		err = nil
	}

	return
}

// GetListByClassifyIdList
// @Description: 根据ID列表获取所有的分类
// @param idList
// @return list
// @return err
func GetListByClassifyIdList(idList []int) (list []*Classify, err error) {
	err = global.MYSQL["rddp"].Model(Classify{}).Where("id in (?) AND is_show = 1 AND enabled = 1", idList).Find(&list).Error
	return

}