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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.entity.ledger.LedgerFundGoodsAssign;
import com.sunxung.factoring.mapper.ledger.LedgerFundGoodsAssignMapper;
import com.sunxung.factoring.service.ledger.LedgerFundGoodsAssignService;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2021.11.11
 */
@Service
public class LedgerFundGoodsAssignServiceImpl extends ServiceImpl<LedgerFundGoodsAssignMapper, LedgerFundGoodsAssign>
        implements LedgerFundGoodsAssignService {

    @Override
    public List<LedgerFundGoodsAssign> findByBasicId(Long basicId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ledger_account_basic_info_id", basicId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BigDecimal calculateAssignAmount(Long sopAccountsReceivableId, Long billingFactoringBusinessLoanRecordId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sop_accounts_receivable_id", sopAccountsReceivableId);
        queryWrapper.eq("billing_factoring_business_loan_record_id", billingFactoringBusinessLoanRecordId);
        queryWrapper.eq("is_submit", true);
        List<LedgerFundGoodsAssign> list = baseMapper.selectList(queryWrapper);

        BigDecimal assignAmount = BigDecimal.ZERO;
        if (list != null && list.size() > 0) {
            for (LedgerFundGoodsAssign ledgerFundGoodsAssign : list) {
                if (ledgerFundGoodsAssign.getCurrentAssignAmount() != null) {
                    assignAmount = assignAmount.add(ledgerFundGoodsAssign.getCurrentAssignAmount());
                }
            }
        }
        return assignAmount;
    }

    @Override
    public BigDecimal calculateAssignAmount(String settlementNumber) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("settlement_number", settlementNumber);
        List<LedgerFundGoodsAssign> list = baseMapper.selectList(queryWrapper);

        BigDecimal assignAmount = BigDecimal.ZERO;
        if (list != null && list.size() > 0) {
            for (LedgerFundGoodsAssign ledgerFundGoodsAssign : list) {
                if (ledgerFundGoodsAssign.getCurrentAssignAmount() != null) {
                    assignAmount = assignAmount.add(ledgerFundGoodsAssign.getCurrentAssignAmount());
                }
            }
        }
        return assignAmount;
    }
}
