/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.base.mapper.UserdeptMapper;
import com.tofly.base.service.UserdeptService;
import com.tofly.pojo.Userdept;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 用户部门表
 *
 * @author JF
 * @date Wed Sep 18 00:00:00 CST 2024
 */
@Service("userdeptService")
public class UserdeptServiceImpl extends ServiceImpl<UserdeptMapper, Userdept> implements UserdeptService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateUserDept(List<Userdept> userdeptList) {
        if (CollUtil.isEmpty(userdeptList)) {
            return true;
        }
        List<Long> userIdList = userdeptList.stream().map(Userdept::getUserId).collect(Collectors.toList());
        this.remove(new QueryWrapper<Userdept>().lambda().in(Userdept::getUserId, userIdList));
        userdeptList.forEach(this::save);
        return true;
    }
}
