package com.mosty.common.core.business.service.impl;

import cn.hutool.core.stream.CollectorUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.core.business.entity.SysUserDept;
import com.mosty.common.core.business.service.SysUserDeptService;
import com.mosty.common.core.business.mapper.SysUserDeptMapper;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author li
 * @description 针对表【sys_user_dept(用户和部门关联表)】的数据库操作Service实现
 * @createDate 2022-02-12 16:53:21
 */
@Service
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptMapper, SysUserDept>
        implements SysUserDeptService {

    @Override
    public List<SysUserDept> listByDeptId(Long deptId) {
        List<SysUserDept> list = this.list(new LambdaQueryWrapper<SysUserDept>()
                .eq(SysUserDept::getDeptId, deptId));
        return list;
    }

    @Override
    public SysUserDept getByDeptIdAndUserId(Long deptId, Long userId) {
        SysUserDept one = this.getOne(new LambdaQueryWrapper<SysUserDept>()
                .eq(SysUserDept::getDeptId, deptId).eq(SysUserDept::getUserId, userId));
        return one;
    }

    @Override
    public List<Long> listByUserId(Long userId) {
        List<SysUserDept> list = this.list(new LambdaQueryWrapper<SysUserDept>()
                .eq(SysUserDept::getUserId, userId));
        if (!CollectionUtils.isEmpty(list)) {
            return list.stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<SysUserDept> listByDeptIds(List<String> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            List<SysUserDept> list = this.list(new LambdaQueryWrapper<SysUserDept>()
                    .in(SysUserDept::getDeptId, ids));
            return list;
        }
        return new ArrayList<>();
    }
}




