package com.mx.cneeds.server.user.service.impl;

import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.pager.Query;
import com.mx.cneeds.server.dao.SysRoleFuncDao;
import com.mx.cneeds.server.entity.SysRoleFuncEntity;
import com.mx.cneeds.server.user.service.SysRoleFuncService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysRoleFuncService")
public class SysRoleFuncServiceImpl extends ServiceImpl<SysRoleFuncDao, SysRoleFuncEntity> implements SysRoleFuncService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        //先删除角色与菜单关系
        deleteBatch(new Long[]{roleId});

        if(menuIdList.size() == 0){
            return ;
        }

        //保存角色与菜单关系
        for(Long funcId : menuIdList){
            SysRoleFuncEntity sysRoleFuncEntity = new SysRoleFuncEntity();
            sysRoleFuncEntity.setFuncId(funcId);
            sysRoleFuncEntity.setRoleId(roleId);

            this.save(sysRoleFuncEntity);
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleFuncEntity> page = this.page(
                new Query<SysRoleFuncEntity>().getPage(params),
                new QueryWrapper<SysRoleFuncEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public int deleteBatch(Long[] roleIds){
        return baseMapper.deleteBatch(roleIds);
    }

    @Override
    public List<Long> queryFuncId(Long roleId) {
        return baseMapper.queryFuncId(roleId);
    }

}
