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

import com.auth.common.model.common.result.PageResult;
import com.auth.model.base.dto.PermissionDto;
import com.auth.model.base.vo.PermissionVo;
import com.auth.dao.base.entity.PermissionEntity;
import com.auth.dao.base.mapper.v1.PermissionMapper;
import com.auth.service.base.service.PermissionService;
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 PermissionServiceImpl extends ServiceImpl<PermissionMapper, PermissionEntity> implements PermissionService {

    /**
     * 系统权限表 服务实现类
     *
     * @param pageParams 系统权限表分页查询page对象
     * @param dto        系统权限表分页查询对象
     * @return 查询分页系统权限表返回对象
     */
    @Override
    public PageResult<PermissionVo> getPermissionPage(Page<PermissionEntity> pageParams, PermissionDto dto) {
        IPage<PermissionVo> page = baseMapper.selectListByPage(pageParams, dto);

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

    /**
     * 根据id查询系统权限表详情
     *
     * @param id 主键
     * @return 系统权限表详情 PermissionVo}
     */
    public PermissionVo getPermissionById(Long id) {
        PermissionEntity permissionEntity = getById(id);

        PermissionVo permissionVo = new PermissionVo();
        BeanUtils.copyProperties(permissionEntity, permissionVo);

        return permissionVo;
    }

    /**
     * 添加系统权限表
     *
     * @param dto 系统权限表添加
     */
    @Override
    public void addPermission(PermissionDto dto) {
        PermissionEntity permission = new PermissionEntity();
        BeanUtils.copyProperties(dto, permission);

        save(permission);
    }

    /**
     * 更新系统权限表
     *
     * @param dto 系统权限表更新
     */
    @Override
    public void updatePermission(PermissionDto dto) {
        PermissionEntity permission = new PermissionEntity();
        BeanUtils.copyProperties(dto, permission);

        updateById(permission);
    }

    /**
     * 删除|批量删除系统权限表
     *
     * @param ids 删除id列表
     */
    @Override
    public void deletePermission(List<Long> ids) {
        removeByIds(ids);
    }
}