package com.codvision.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.auth.entity.SysRoleMenu;
import com.codvision.auth.entity.vo.SysRoleMenuVO;
import com.codvision.auth.mapper.SysRoleMenuMapper;
import com.codvision.auth.service.ISysRoleMenuService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 系统角色菜单权限 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenuMapper, SysRoleMenu, SysRoleMenuVO> implements ISysRoleMenuService {

    @Override
    public SysRoleMenuVO getDetail(Long id) {
        SysRoleMenuVO vo = new SysRoleMenuVO();
        SysRoleMenu po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<SysRoleMenuVO> getPageResult(QueryPage<SysRoleMenu> queryPage) {
        QueryWrapper<SysRoleMenu> queryWrapper = queryPage.getWrapper();
        IPage<SysRoleMenuVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<SysRoleMenuVO> listAll(SysRoleMenu queryBean) {
        QueryWrapper<SysRoleMenu> wrapper = PageUtil.initWrapper(queryBean);
        List<SysRoleMenu> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysRoleMenuVO.class);
    }

    @Override
    public boolean save(SysRoleMenu sysRoleMenu) {
        Assert.notNull(sysRoleMenu, "系统角色菜单权限不能为空");
        getBaseMapper().insert(sysRoleMenu);
        return true;
    }

    @Override
    public boolean updateById(SysRoleMenu sysRoleMenu) {
        Assert.notNull(sysRoleMenu, "系统角色菜单权限不能为空");
        getBaseMapper().updateById(sysRoleMenu);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        SysRoleMenu sysRoleMenu = getById(id);
        Assert.notNull(sysRoleMenu, "系统角色菜单权限不存在");
        getBaseMapper().deleteById(id);
        return true;
    }
}
