package com.jwipc.finance.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.exception.DaoException;
import com.jwipc.finance.entity.InvoicedApply;
import com.jwipc.finance.entity.InvoicedApplyPa;
import com.jwipc.finance.logic.InvoicedApplyPaLogic;
import com.jwipc.finance.service.IInvoicedApplyPaService;
import com.jwipc.finance.service.IInvoicedApplyService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shenyang.ou
 * @date 2024/3/6 11:29
 * @desc
 */
@Service
@RequiredArgsConstructor
public class InvoicedApplyPaLogicImpl implements InvoicedApplyPaLogic {
    private final IInvoicedApplyPaService invoicedApplyPaService;
    private final IInvoicedApplyService invoicedApplyService;
    /**
     * 根据采购申请明细IDS获取明细信息
     *
     * @param invoiceApplyId
     * @return 结果
     */
    @Override
    public List<InvoicedApplyPa> findPaaDetailByInvoiceApplyId(Long invoiceApplyId) {
        return invoicedApplyPaService.list(Wrappers.lambdaQuery(InvoicedApplyPa.class).eq(InvoicedApplyPa::getInvoiceApplyId,invoiceApplyId));
    }

    /**
     * 开票申请单校验预付款是否被使用
     *
     * @param selectList     预付款信息
     * @param invoiceApplyId
     */
    @Override
    public void checkAdvanceUse(List<InvoicedApplyPa> selectList, Long invoiceApplyId) {
        for (InvoicedApplyPa invoicedApplyPa : selectList){
            //2024-12-17预付款冲账逻辑校验修改
            // surplusReversalMoney剩余可冲账金额
            // strikePrice 本次冲账金额
            invoicedApplyPa.getSurplusReversalMoney();
            invoicedApplyPa.getStrikePrice();
            if(invoicedApplyPa.getSurplusReversalMoney().compareTo(invoicedApplyPa.getStrikePrice()) < 0){
                throw new DaoException(StrUtil.format("预付款凭证号{}，行号{}，冲减金额能大于剩余可冲减金额！",invoicedApplyPa.getApplyNo(),invoicedApplyPa.getApplyItemNo()));
            }


         /*   List<InvoicedApplyPa> pas = invoicedApplyPaService.list(Wrappers.lambdaQuery(InvoicedApplyPa.class)
                    .eq(InvoicedApplyPa::getApplyNo,invoicedApplyPa.getApplyNo())
                    .eq(InvoicedApplyPa::getApplyItemNo,invoicedApplyPa.getApplyItemNo())
                    .ne(invoiceApplyId!=null,InvoicedApplyPa::getInvoiceApplyId,invoiceApplyId)
                    .eq(InvoicedApplyPa::getDeleteFlag,0));
            if (CollectionUtils.isNotEmpty(pas)){
                InvoicedApply invoicedApply = invoicedApplyService.getById(pas.get(0).getInvoiceApplyId());
                throw new DaoException(StrUtil.format("预付款凭证号{}，行号{}，已被开票申请单号{}使用，请重新选择",invoicedApplyPa.getApplyNo(),invoicedApplyPa.getApplyItemNo(),invoicedApply.getInvoiceApplyNo()));
            }*/
        }
    }
}
