package manage

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

func GetManageUserById(id int64) (domain.ManageUser, error) {
	var manageUser domain.ManageUser
	_, err := configs.Engine.Table("manage_user").Where("id = ?", id).Get(&manageUser)
	if err != nil {
		return manageUser, err
	}
	return manageUser, nil
}

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

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

func SetManageUser(manageUserMap map[string]interface{}, id int64) (domain.ManageUser, bool, error) {
	var manageUser domain.ManageUser
	_, err := configs.Engine.Table("manage_user").Where("id = ?", id).Update(manageUserMap)
	if err != nil {
		return manageUser, false, err
	}
	return manageUser, true, nil
}

func DeleteManageUser(id int64) bool {
	var manageUser domain.ManageUser
	i, err := configs.Engine.Table("manage_user").Where("id = ?", id).Delete(&manageUser)
	if err != nil {
		return false
	}
	return i > 0
}
