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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.invoicemanage.InvoiceGoodsInfo;
import com.sunxung.factoring.entity.invoicemanage.vo.SaleInvoiceGoodsAssignVo;
import com.sunxung.factoring.entity.invoicemanage.vo.SaleInvoiceGoodsSearch;
import com.sunxung.factoring.mapper.invoicemanage.InvoiceGoodsInfoMapper;
import com.sunxung.factoring.service.invoicemanage.InvoiceGoodsInfoService;
import org.springframework.stereotype.Service;

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

/**
 * @Description : 销售发票货物信息service实现类
 * @Author : JiaYf
 * @Date: 2021-10-18 19:17
 */
@Service
public class InvoiceGoodsInfoServiceImpl extends ServiceImpl<InvoiceGoodsInfoMapper, InvoiceGoodsInfo>
        implements InvoiceGoodsInfoService {

//    @Autowired
//    private MortgageInformationService mortgageInformationService;

    @Override
    public GridPage<SaleInvoiceGoodsAssignVo> findAllUnAssignedGoods(SaleInvoiceGoodsSearch search) {

        Page<SaleInvoiceGoodsAssignVo> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<SaleInvoiceGoodsAssignVo> list = baseMapper.findAllUnAssignedGoods(search);
        if (list != null && list.size() > 0) {
            list.stream().forEach(s -> {
                //判断货物是否做底层
                if (s.getMortgage()) {
//                    UnderlyingBasicInfo mortgageInformation = mortgageInformationService.findByAcceptanceBusinessGoodsId(s.getAcceptanceBusinessGoodsId());
//                    s.setMortgageInformationId(mortgageInformation.getId());
                }
            });
        }
        return new GridPage<>(search.getRows(), search.getPage(), page.getTotal(), list);
    }

    @Override
    public BigDecimal calculateInvoicedQuantity(Long acceptanceBusinessGoodsId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("acceptance_business_goods_id", acceptanceBusinessGoodsId);
        queryWrapper.select("IFNULL(sum(IFNULL(open_quantity,0)),0) as openQuantity ");
        InvoiceGoodsInfo invoiceGoods = baseMapper.selectOne(queryWrapper);
        return invoiceGoods.getOpenQuantity();
    }

    @Override
    public List<InvoiceGoodsInfo> findListGoods(Long invoiceBasicInfoId) {
        return baseMapper.findListGoods(invoiceBasicInfoId);
    }
}
