package com.jwipc.sys.client.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.jwipc.sys.client.SupplierEmailClient;
import com.jwipc.sys.client.convert.SupplierEmailClientConvert;
import com.jwipc.sys.client.dto.SupplierEmailCDTO;
import com.jwipc.sys.entity.SupplierEmail;
import com.jwipc.sys.service.ISupplierEmailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class SupplierEmailClientImpl implements SupplierEmailClient {
    private final ISupplierEmailService supplierEmailService;
    private final SupplierEmailClientConvert supplierEmailClientConvert;

    @Override
    public List<SupplierEmailCDTO> getSupplierEmails(Collection<? extends Serializable> supplierCodes) {
        if (CollUtil.isEmpty(supplierCodes)) {
            return Lists.newArrayList();
        }
        final List<SupplierEmail> list = supplierEmailService.list(
                new LambdaQueryWrapper<SupplierEmail>().in(SupplierEmail::getSupplierCode, supplierCodes));
        List<SupplierEmail> uniqueList = list.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SupplierEmail::getSupplierCode))),
                        collection -> new ArrayList<>(collection)
                )).stream().toList();
        return supplierEmailClientConvert.covert2(uniqueList);
    }
}
