package com.yunyan.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyan.common.core.domain.entity.UserDept;
import com.yunyan.system.mapper.UserDeptMapper;
import com.yunyan.system.service.IUserDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户与部门关联Service业务层处理
 *
 * @author yunyan
 * @date 2023-06-06
 */
@Service
public class UserDeptServiceImpl extends ServiceImpl<UserDeptMapper, UserDept> implements IUserDeptService
{
    @Autowired
    private UserDeptMapper userDeptMapper;

    /**
     * 查询用户与部门关联列表
     *
     * @param userDept 用户与部门关联
     * @return 用户与部门关联
     */
    @Override
    public List<UserDept> selectUserDeptList(UserDept userDept)
    {
        return userDeptMapper.selectUserDeptList(userDept);
    }

    @Override
    public List<Long> getDeptIdList(Long userId) {
        List<UserDept> list = this.lambdaQuery().eq(UserDept::getUserId, userId).list();
        List<Long> resList = list.stream().map(UserDept::getDeptId).collect(Collectors.toList());
        return resList;
    }

    @Override
    public int deleteUserDeptByUserId(Long userId) {
        boolean res = this.lambdaUpdate().eq(UserDept::getUserId, userId).remove();
        return res ? 1 : 0;
    }
}
