package com.chushang.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.system.entity.po.SysUserPost;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author by zhaowenyuan create 2022/8/19 11:49
 */
public interface ISysUserPostService extends IService<SysUserPost> {
    @Transactional
    default void saveOrUpdate(Integer userId, Integer[] roleIdList){
        //先删除用户与岗位
        this.remove(new LambdaQueryWrapper<SysUserPost>()
                .eq(SysUserPost::getUserId, userId));

        if(roleIdList == null || roleIdList.length == 0){
            return ;
        }

        List<SysUserPost> collect = Arrays.stream(roleIdList).map(s -> {
            SysUserPost sysUserRoleEntity = new SysUserPost();
            sysUserRoleEntity.setUserId(userId);
            sysUserRoleEntity.setPostId(s);
            return sysUserRoleEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }
}
