package com.sunxung.factoring.service.settlement.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.settlement.SettlementGoodsInfo;
import com.sunxung.factoring.mapper.settlement.SettlementGoodsInfoMapper;
import com.sunxung.factoring.service.settlement.SettlementGoodsInfoService;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2021.09.15
 */
@Service
public class SettlementGoodsInfoServiceImpl extends ServiceImpl<SettlementGoodsInfoMapper, SettlementGoodsInfo> implements SettlementGoodsInfoService {

    @Override
    public List<SettlementGoodsInfo> findByBasicIds(List<Long> basicInfoIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("settlement_basic_info_id", basicInfoIds);
        return baseMapper.selectList(queryWrapper);
    }

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

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