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

import com.auth.common.model.common.result.PageResult;
import com.auth.model.base.dto.RoleDataScopeDto;
import com.auth.model.base.vo.RoleDataScopeVo;
import com.auth.dao.base.entity.RoleDataScopeEntity;
import com.auth.dao.base.mapper.v1.RoleDataScopeMapper;
import com.auth.service.base.service.RoleDataScopeService;
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 RoleDataScopeServiceImpl extends ServiceImpl<RoleDataScopeMapper, RoleDataScopeEntity> implements RoleDataScopeService {

    /**
     * 系统角色数据权限范围 服务实现类
     *
     * @param pageParams 系统角色数据权限范围分页查询page对象
     * @param dto        系统角色数据权限范围分页查询对象
     * @return 查询分页系统角色数据权限范围返回对象
     */
    @Override
    public PageResult<RoleDataScopeVo> getRoleDataScopePage(Page<RoleDataScopeEntity> pageParams, RoleDataScopeDto dto) {
        IPage<RoleDataScopeVo> page = baseMapper.selectListByPage(pageParams, dto);

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

    /**
     * 根据id查询系统角色数据权限范围详情
     *
     * @param id 主键
     * @return 系统角色数据权限范围详情 RoleDataScopeVo}
     */
    public RoleDataScopeVo getRoleDataScopeById(Long id) {
        RoleDataScopeEntity roleDataScopeEntity = getById(id);

        RoleDataScopeVo roleDataScopeVo = new RoleDataScopeVo();
        BeanUtils.copyProperties(roleDataScopeEntity, roleDataScopeVo);

        return roleDataScopeVo;
    }

    /**
     * 添加系统角色数据权限范围
     *
     * @param dto 系统角色数据权限范围添加
     */
    @Override
    public void addRoleDataScope(RoleDataScopeDto dto) {
        RoleDataScopeEntity roleDataScope = new RoleDataScopeEntity();
        BeanUtils.copyProperties(dto, roleDataScope);

        save(roleDataScope);
    }

    /**
     * 更新系统角色数据权限范围
     *
     * @param dto 系统角色数据权限范围更新
     */
    @Override
    public void updateRoleDataScope(RoleDataScopeDto dto) {
        RoleDataScopeEntity roleDataScope = new RoleDataScopeEntity();
        BeanUtils.copyProperties(dto, roleDataScope);

        updateById(roleDataScope);
    }

    /**
     * 删除|批量删除系统角色数据权限范围
     *
     * @param ids 删除id列表
     */
    @Override
    public void deleteRoleDataScope(List<Long> ids) {
        removeByIds(ids);
    }
}