package com.jwipc.supplier.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.supplier.convert.SupplierContactsConvert;
import com.jwipc.supplier.dto.SupplierContactsDTO;
import com.jwipc.supplier.entity.Supplier;
import com.jwipc.supplier.entity.SupplierContacts;
import com.jwipc.supplier.entity.SupplierPurchaseCategoryVersion;
import com.jwipc.supplier.logic.SupplierContactLogic;
import com.jwipc.supplier.model.param.ContactQuery;
import com.jwipc.supplier.service.ISupplierContactsService;
import com.jwipc.supplier.service.ISupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class SupplierContactLogicImpl implements SupplierContactLogic {
    private final ISupplierContactsService supplierContactsService;
    private final ISupplierService supplierService;
    private final SupplierContactsConvert contactsConvert;

    @Override
    public PageView getList(ContactQuery contactQuery) {
        final Page<SupplierContacts> page = PageUtil.toPage(contactQuery);
        final String type = contactQuery.getType();
        final Supplier supplier = supplierService.getOne(
                new LambdaQueryWrapper<Supplier>().eq(Supplier::getSupplierCode, contactQuery.getSupplierCode()));
        if (Objects.isNull(supplier)) {
            return PageUtil.getPV(Lists.newArrayList(), 0);
        }
        final LambdaQueryWrapper<SupplierContacts> queryWrapper = new LambdaQueryWrapper<SupplierContacts>().eq(
                SupplierContacts::getSupplierId, supplier.getId());
        if (StrUtil.isNotEmpty(type)) {
            final List<String> strings = StrUtil.split(type, ",");
            queryWrapper.in(SupplierContacts::getContactsType, strings);
        }
        final List<SupplierContacts> supplierContacts = supplierContactsService.list(page, queryWrapper);
        final List<SupplierContactsDTO> supplierContactsDTOS = contactsConvert.entities2DTOs(supplierContacts);
        return PageUtil.getPV(supplierContactsDTOS, page.getTotal());
    }

    @Override
    public SupplierContactsDTO getOne(ContactQuery contactQuery) {
        final Supplier supplier = supplierService.getOne(
                new LambdaQueryWrapper<Supplier>().eq(Supplier::getSupplierCode, contactQuery.getSupplierCode()));
        if (Objects.isNull(supplier)) {
            return null;
        }
        final String type = contactQuery.getType();
        final LambdaQueryWrapper<SupplierContacts> queryWrapper = new LambdaQueryWrapper<SupplierContacts>().eq(
                SupplierContacts::getSupplierId, supplier.getId());
        if (StrUtil.isNotEmpty(type)) {
            queryWrapper.eq(SupplierContacts::getContactsType, type);
        }
        final SupplierContacts supplierContacts = supplierContactsService.getOne(queryWrapper, false);
        return contactsConvert.entity2DTO(supplierContacts);
    }

    @Override
    public List<SupplierContactsDTO> getListBySupplierCodeAndType(String supplierCode, String type) {
        final Supplier supplier = supplierService.getOne(
                new LambdaQueryWrapper<Supplier>().eq(Supplier::getSupplierCode, supplierCode));
        if (Objects.isNull(supplier)) {
            return null;
        }
        final LambdaQueryWrapper<SupplierContacts> queryWrapper = new LambdaQueryWrapper<SupplierContacts>().eq(
                SupplierContacts::getSupplierId, supplier.getId());
        if (StrUtil.isNotEmpty(type)) {
            queryWrapper.eq(SupplierContacts::getContactsType, type);
        }
         List<SupplierContacts> supplierContacts = supplierContactsService.list(queryWrapper);
        //根据姓名去重
        supplierContacts = supplierContacts.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SupplierContacts::getContactsName))), ArrayList::new));
        return contactsConvert.entities2DTOs(supplierContacts);
    }
}