package manage

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

func GetBackRoleMenuById(id int64) (domain.BackRoleMenu, error) {
	var backRoleMenu domain.BackRoleMenu
	_, err := configs.Engine.Table("back_role_menu").Where("id = ?", id).Get(&backRoleMenu)
	if err != nil {
		return backRoleMenu, err
	}
	return backRoleMenu, nil
}

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

func GetBackRoleMenuList(backRoleMenu domain.BackRoleMenu, pageNum, pageSize int) (vo.BaseListVo, error) {
	var arr []domain.BackRoleMenu
	Count, err := configs.Engine.Table("back_role_menu").Limit(pageSize, (pageNum-1)*pageSize).FindAndCount(&arr, &backRoleMenu)
	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 SaveBackRoleMenu(backRoleMenu *domain.BackRoleMenu) (domain.BackRoleMenu, error) {
	_, err := configs.Engine.Table("back_role_menu").Insert(&backRoleMenu)
	if err != nil {
		return *backRoleMenu, err
	}
	return *backRoleMenu, nil
}

func SetBackRoleMenu(backRoleMenuMap map[string]interface{}, id int64) (domain.BackRoleMenu, bool, error) {
	var backRoleMenu domain.BackRoleMenu
	_, err := configs.Engine.Table("back_role_menu").Where("id = ?", id).Update(backRoleMenuMap)
	if err != nil {
		return backRoleMenu, false, err
	}
	return backRoleMenu, true, nil
}

func DeleteBackRoleMenu(id int64) bool {
	var backRoleMenu domain.BackRoleMenu
	i, err := configs.Engine.Table("back_role_menu").Where("id = ?", id).Delete(&backRoleMenu)
	if err != nil {
		return false
	}
	return i > 0
}
