package com.auth.service.base.service.impl;

import com.auth.common.model.common.result.PageResult;
import com.auth.model.base.dto.UserDto;
import com.auth.model.base.vo.UserVo;
import com.auth.dao.base.entity.UserEntity;
import com.auth.dao.base.mapper.v1.UserMapper;
import com.auth.service.base.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author Bunny
 * @since 2025-07-17 23:06:38
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    /**
     * 用户信息 服务实现类
     *
     * @param pageParams 用户信息分页查询page对象
     * @param dto        用户信息分页查询对象
     * @return 查询分页用户信息返回对象
     */
    @Override
    public PageResult<UserVo> getUserPage(Page<UserEntity> pageParams, UserDto dto) {
        IPage<UserVo> page = baseMapper.selectListByPage(pageParams, dto);

        return PageResult.<UserVo>builder()
                .list(page.getRecords())
                .pageNo(page.getCurrent())
                .pageSize(page.getSize())
                .total(page.getTotal())
                .build();
    }

    /**
     * 根据id查询用户信息详情
     *
     * @param id 主键
     * @return 用户信息详情 UserVo}
     */
    public UserVo getUserById(Long id) {
        UserEntity userEntity = getById(id);

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userEntity, userVo);

        return userVo;
    }

    /**
     * 添加用户信息
     *
     * @param dto 用户信息添加
     */
    @Override
    public void addUser(UserDto dto) {
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(dto, user);

        save(user);
    }

    /**
     * 更新用户信息
     *
     * @param dto 用户信息更新
     */
    @Override
    public void updateUser(UserDto dto) {
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(dto, user);

        updateById(user);
    }

    /**
     * 删除|批量删除用户信息
     *
     * @param ids 删除id列表
     */
    @Override
    public void deleteUser(List<Long> ids) {
        removeByIds(ids);
    }
}