package manage

import (
	"demo/configs"
	"demo/data/domain"
	"demo/data/domain/vo"
)

func GetGenTableColumnById(id int64) (domain.GenTableColumn, error) {
	var genTableColumn domain.GenTableColumn
	_, err := configs.Engine.Table("gen_table_column").Where("id = ?", id).Get(&genTableColumn)
	if err != nil {
		return genTableColumn, err
	}
	return genTableColumn, nil
}

func GetGenTableColumnListInId(ids []int64) ([]domain.GenTableColumn, error) {
	rows, err := configs.Engine.Table("gen_table_column").In("id", ids).Rows(&domain.GenTableColumn{})
	if err != nil {
		return make([]domain.GenTableColumn, 0), err
	}
	defer rows.Close()
	var list []domain.GenTableColumn
	for rows.Next() {
		var u domain.GenTableColumn
		rows.Scan(&u)
		list = append(list, u)
	}
	return list, nil
}

func GetGenTableColumnList(genTableColumn domain.GenTableColumn, pageNum, pageSize int) (vo.BaseListVo, error) {
	var arr []domain.GenTableColumn
	Count, err := configs.Engine.Table("gen_table_column").Limit(pageSize, (pageNum-1)*pageSize).FindAndCount(&arr, &genTableColumn)
	var vo vo.BaseListVo
	if err != nil {
		return vo, err
	}
	if err != nil {
		return vo, err
	}
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = make([]any, len(arr))
	for i, v := range arr {
		vo.List[i] = v
	}
	vo.Total = int(Count)
	return vo, nil
}

func SaveGenTableColumn(genTableColumn *domain.GenTableColumn) (domain.GenTableColumn, error) {
	_, err := configs.Engine.Table("gen_table_column").Insert(&genTableColumn)
	if err != nil {
		return *genTableColumn, err
	}
	return *genTableColumn, nil
}

func SetGenTableColumn(genTableColumnMap map[string]interface{}, id int64) (domain.GenTableColumn, bool, error) {
	var genTableColumn domain.GenTableColumn
	_, err := configs.Engine.Table("gen_table_column").Where("id = ?", id).Update(genTableColumnMap)
	if err != nil {
		return genTableColumn, false, err
	}
	return genTableColumn, true, nil
}

func DeleteGenTableColumn(id int64) bool {
	var genTableColumn domain.GenTableColumn
	i, err := configs.Engine.Table("gen_table_column").Where("id = ?", id).Delete(&genTableColumn)
	if err != nil {
		return false
	}
	return i > 0
}
