package com.storlead.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.storlead.system.mapper.RoleMenuMapper;
import com.storlead.system.pojo.entity.RoleMenuEntity;
import com.storlead.system.service.IRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @Author scott
 * @since 2018-12-21
 */
@Slf4j
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuEntity> implements IRoleMenuService {

    @Override
    public void updateRoleMenu(Long roleId, Set<Long> menuIds,Integer isApp) {
        if (Objects.nonNull(roleId) && !CollectionUtils.isEmpty(menuIds)) {
            if (Objects.isNull(isApp)) {
                isApp = 0;
            }
            Integer finalIsApp = isApp;
            this.removeByMap(Map.of("role_id", roleId,"is_app", finalIsApp));
            Set<RoleMenuEntity> collect = menuIds.stream().map(menuId -> new RoleMenuEntity(roleId, menuId,finalIsApp)).collect(Collectors.toSet());
            this.saveBatch(collect);
        }
    }

}
