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.underlying.UnderlyingAccountsReceivableInfo;
import com.sunxung.factoring.mapper.underlying.UnderlyingAccountsReceivableInfoMapper;
import com.sunxung.factoring.service.underlying.UnderlyingAccountsReceivableInfoService;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2021.10.11
 */
@Service
public class UnderlyingAccountsReceivableInfoServiceImpl extends ServiceImpl<UnderlyingAccountsReceivableInfoMapper, UnderlyingAccountsReceivableInfo>
        implements UnderlyingAccountsReceivableInfoService {


    @Override
    public List<UnderlyingAccountsReceivableInfo> findByBasicId(Long basicId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("underlying_basic_info_id", basicId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BigDecimal sumNetAmount(Long basicId) {
        BigDecimal netAmountSum = list(new QueryWrapper<UnderlyingAccountsReceivableInfo>().eq("underlying_basic_info_id", basicId))
                .stream().filter(a -> a.getNetAccountsReceivableAmount() != null)
                .map(UnderlyingAccountsReceivableInfo::getNetAccountsReceivableAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        return netAmountSum;
    }
}
