package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.iscs.domain.IsBlacklist;
import com.ktg.iscs.domain.vo.user.WhiteUserVO;
import com.ktg.iscs.mapper.IsBlacklistMapper;
import com.ktg.iscs.service.IIsBlacklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 黑名单关联Service业务层处理
 *
 * @author cgj
 * @date 2025-01-10
 */
@Service
public class IsBlacklistServiceImpl extends ServiceImpl<IsBlacklistMapper, IsBlacklist> implements IIsBlacklistService {

    @Autowired
    private IsBlacklistMapper isBlacklistMapper;

    @Override
    public Page<IsBlacklist> getIsBlacklistPage(Page<IsBlacklist> page, IsBlacklist isBlacklist) {
        return isBlacklistMapper.getIsBlacklistPage(page, isBlacklist);
    }

    @Override
    public Page<WhiteUserVO> getWhiteUserPage(Page page, WhiteUserVO dto) {
        return isBlacklistMapper.getWhiteUserPage(page, dto);
    }

    @Override
    public Boolean insertIsBlacklist(List<IsBlacklist> isBlacklist) {
        Assert.isFalse(isBlacklist.isEmpty(), "请选择人员！");
        // 开始新增黑名单
        List<Long> newUserIds = isBlacklist.stream().map(IsBlacklist::getUserId).collect(Collectors.toList());
        // 开始检查当前人员是不是已经在给名单
        List<IsBlacklist> list = list(Wrappers.<IsBlacklist>lambdaQuery().in(IsBlacklist::getUserId, newUserIds));
        // 防止多地操作导致的重复黑名单，去除已经在黑名单之中的
        if (!list.isEmpty()) {
            isBlacklist.removeIf(o -> list.stream().map(IsBlacklist::getUserId).collect(Collectors.toSet()).contains(o.getUserId()));
        }
        // 开始拉黑
        // saveBatch(isBlacklist);
        for (IsBlacklist blacklist : isBlacklist) {
            save(blacklist);
        }
        return true;
    }

}
