package com.changfa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changfa.entity.TRoleMenu;
import com.changfa.mapper.TRoleMenuMapper;
import com.changfa.service.ITRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单表 服务实现类
 * </p>
 *
 * @author wwz
 * @since 2022-06-24
 */
@Service
public class TRoleMenuServiceImpl extends ServiceImpl<TRoleMenuMapper, TRoleMenu> implements ITRoleMenuService {

    @Override
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean assignPermissions(Integer roleId, List<Integer> menuIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        if(this.baseMapper.selectCount(queryWrapper)>0){
            this.baseMapper.delete(queryWrapper);
        }
        List<TRoleMenu> list = new ArrayList<>();
        menuIds.stream().forEach(item->{
            TRoleMenu roleMenu = new TRoleMenu();
            roleMenu.setMenuId(item);
            roleMenu.setRoleId(roleId);
            roleMenu.fillCreated();
            list.add(roleMenu);
        });
        return this.saveBatch(list);
    }
}
