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

import com.auth.common.model.common.result.PageResult;
import com.auth.model.base.dto.MenuRoleDto;
import com.auth.model.base.vo.MenuRoleVo;
import com.auth.dao.base.entity.MenuRoleEntity;
import com.auth.dao.base.mapper.v1.MenuRoleMapper;
import com.auth.service.base.service.MenuRoleService;
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 MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRoleEntity> implements MenuRoleService {

    /**
     * 系统菜单角色关联 服务实现类
     *
     * @param pageParams 系统菜单角色关联分页查询page对象
     * @param dto        系统菜单角色关联分页查询对象
     * @return 查询分页系统菜单角色关联返回对象
     */
    @Override
    public PageResult<MenuRoleVo> getMenuRolePage(Page<MenuRoleEntity> pageParams, MenuRoleDto dto) {
        IPage<MenuRoleVo> page = baseMapper.selectListByPage(pageParams, dto);

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

    /**
     * 根据id查询系统菜单角色关联详情
     *
     * @param id 主键
     * @return 系统菜单角色关联详情 MenuRoleVo}
     */
    public MenuRoleVo getMenuRoleById(Long id) {
        MenuRoleEntity menuRoleEntity = getById(id);

        MenuRoleVo menuRoleVo = new MenuRoleVo();
        BeanUtils.copyProperties(menuRoleEntity, menuRoleVo);

        return menuRoleVo;
    }

    /**
     * 添加系统菜单角色关联
     *
     * @param dto 系统菜单角色关联添加
     */
    @Override
    public void addMenuRole(MenuRoleDto dto) {
        MenuRoleEntity menuRole = new MenuRoleEntity();
        BeanUtils.copyProperties(dto, menuRole);

        save(menuRole);
    }

    /**
     * 更新系统菜单角色关联
     *
     * @param dto 系统菜单角色关联更新
     */
    @Override
    public void updateMenuRole(MenuRoleDto dto) {
        MenuRoleEntity menuRole = new MenuRoleEntity();
        BeanUtils.copyProperties(dto, menuRole);

        updateById(menuRole);
    }

    /**
     * 删除|批量删除系统菜单角色关联
     *
     * @param ids 删除id列表
     */
    @Override
    public void deleteMenuRole(List<Long> ids) {
        removeByIds(ids);
    }
}