package manage

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

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

func GetListRoleAuthority(roleAuthority domain.RoleAuthority, pageNum, pageSize int) (vo.BaseListVo, error) {
	rows, err := configs.Engine.Limit(pageSize, (pageNum-1)*pageSize).Desc("id").Rows(&roleAuthority)
	var vo vo.BaseListVo
	if err != nil {
		return vo, err
	}
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = make([]any, 0)
	for rows.Next() {
		var u domain.RoleAuthority
		rows.Scan(&u)
		vo.List = append(vo.List, u)
	}
	//查询数量
	count, err := configs.Engine.Count(&roleAuthority)
	vo.Total = int(count)
	return vo, nil
}

func SaveRoleAuthority(roleAuthority *domain.RoleAuthority) (domain.RoleAuthority, error) {
	_, err := configs.Engine.Insert(&roleAuthority)
	if err != nil {
		return *roleAuthority, err
	}
	return *roleAuthority, nil
}

func SetRoleAuthority(roleAuthorityMap map[string]interface{}, id int) (domain.RoleAuthority, bool, error) {
	var roleAuthority domain.RoleAuthority
	_, err := configs.Engine.Table("role_authority").Where("authority_id = ?", id).Update(roleAuthorityMap)
	if err != nil {
		return roleAuthority, false, err
	}
	return roleAuthority, true, nil
}
