package com.sunxung.factoring.service.underlying.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.entity.acceptance.BaseGoods;
import com.sunxung.factoring.entity.underlying.UnderlyingBasicInfo;
import com.sunxung.factoring.entity.underlying.UnderlyingGoodsInfo;
import com.sunxung.factoring.mapper.underlying.UnderlyingGoodsInfoMapper;
import com.sunxung.factoring.service.underlying.UnderlyingBasicInfoService;
import com.sunxung.factoring.service.underlying.UnderlyingGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2021.10.11
 */
@Service
public class UnderlyingGoodsInfoServiceImpl extends ServiceImpl<UnderlyingGoodsInfoMapper, UnderlyingGoodsInfo>
        implements UnderlyingGoodsInfoService {

    @Autowired
    private UnderlyingBasicInfoService underlyingBasicInfoService;

    @Override
    public BigDecimal sumSalesGuaranteeMoney(String businessNumber) {
        List<Long> basicIdList = underlyingBasicInfoService.list(new QueryWrapper<UnderlyingBasicInfo>()
                .eq("business_number", businessNumber)).stream().map(UnderlyingBasicInfo::getId)
                .collect(Collectors.toList());
        if (basicIdList != null && basicIdList.size() > 0) {
            BigDecimal salesGuaranteeMoneySum = list(new QueryWrapper<UnderlyingGoodsInfo>().in("underlying_basic_info_id", basicIdList))
                    .stream().filter(goods -> goods.getSalesGuaranteeMoney() != null)
                    .map(BaseGoods::getSalesGuaranteeMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
            return salesGuaranteeMoneySum;
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal sumPurchGuaranteeMoney(String businessNumber, Long supplierId) {
        List<Long> basicIdList = underlyingBasicInfoService.list(new QueryWrapper<UnderlyingBasicInfo>()
                .eq("business_number", businessNumber)
                .eq("supplier_id", supplierId)).stream().map(UnderlyingBasicInfo::getId)
                .collect(Collectors.toList());
        if (basicIdList != null && basicIdList.size() > 0) {
            BigDecimal purchGuaranteeMoneySum = list(new QueryWrapper<UnderlyingGoodsInfo>().in("underlying_basic_info_id", basicIdList))
                    .stream().filter(goods -> goods.getPurchaseGuaranteeMoney() != null)
                    .map(BaseGoods::getPurchaseGuaranteeMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
            return purchGuaranteeMoneySum;
        }
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal sumSalesTotalPrice(Long underlyingBasicInfoId) {
        BigDecimal salesTotalPriceSum = list(new QueryWrapper<UnderlyingGoodsInfo>().eq("underlying_basic_info_id", underlyingBasicInfoId))
                .stream().filter(goods -> goods.getSalesTotalPrice() != null)
                .map(BaseGoods::getSalesTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        return salesTotalPriceSum;
    }

    @Override
    public BigDecimal sumPurchaseTotalPrice(Long underlyingBasicInfoId) {
        BigDecimal purchaseTotalPriceSum = list(new QueryWrapper<UnderlyingGoodsInfo>().eq("underlying_basic_info_id", underlyingBasicInfoId))
                .stream().filter(goods -> goods.getPurchaseTotalPrice() != null)
                .map(BaseGoods::getPurchaseTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        return purchaseTotalPriceSum;
    }

    @Override
    public List<Long> findAcceptanceIdByBasicId(Long underlyingBasicInfoId) {
        List<UnderlyingGoodsInfo> list = lambdaQuery().eq(UnderlyingGoodsInfo::getUnderlyingBasicInfoId, underlyingBasicInfoId)
                .groupBy(UnderlyingGoodsInfo::getcAcceptanceBasicInfoId).list();
        if (list != null && list.size() > 0) {
            return list.stream().map(UnderlyingGoodsInfo::getcAcceptanceBasicInfoId).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<Long> findSettlementIdByBasicId(Long underlyingBasicInfoId) {
        List<UnderlyingGoodsInfo> list = lambdaQuery().eq(UnderlyingGoodsInfo::getUnderlyingBasicInfoId, underlyingBasicInfoId)
                .groupBy(UnderlyingGoodsInfo::getSettlementBasicInfoId).list();
        if (list != null && list.size() > 0) {
            return list.stream().map(UnderlyingGoodsInfo::getSettlementBasicInfoId).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public Boolean existSameGoods(List<Long> acceptanceBasicInfoIds, List<Long> settlementBasicInfoIds) {

        List<UnderlyingGoodsInfo> goodsInfoList = baseMapper.findByBasicIds(acceptanceBasicInfoIds, settlementBasicInfoIds);
        if (goodsInfoList != null && goodsInfoList.size() > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public List<UnderlyingGoodsInfo> listByAcceptanceId(Long acceptanceBasicInfoId) {
        return baseMapper.listByAcceptanceId(acceptanceBasicInfoId);
    }
}
