package com.jwipc.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.jwipc.finance.entity.InvoicedApplyDetail;
import com.jwipc.finance.mapper.InvoicedApplyDetailMapper;
import com.jwipc.finance.model.po.CreatePcaDetailPO;
import com.jwipc.finance.model.po.InvoicedApplyItemPO;
import com.jwipc.finance.service.IInvoicedApplyDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 对账模块-开票申请明细 服务实现类
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-03-04
 */
@Service
public class InvoicedApplyDetailServiceImpl extends ServiceImpl<InvoicedApplyDetailMapper, InvoicedApplyDetail> implements IInvoicedApplyDetailService {

    /**
     * 获取已经存在开票申请中的对账单明细ID
     *
     * @param supplierCode 供应商编码
     * @param companyCode  公司编码
     * @return 结果
     */
    @Override
    public List<Long> findExistAsdIdByQuery(String supplierCode, String companyCode) {
        return baseMapper.findExistAsdIdByQuery(supplierCode,companyCode);
    }

    /**
     * 提供采购确认时获取开票申请明细数据
     *
     * @param iaId 开票申请单ID
     * @return 结果
     */
    @Override
    public List<InvoicedApplyItemPO> providePurchaseConfirm(Long iaId) {
        return baseMapper.providePurchaseConfirm(iaId);
    }

    /**
     * 根据开票申请ID获取开票申请明细
     *
     * @param iaId     开票申请单ID
     * @param iaIdList 开票申请单ID集合
     * @return 结果
     */
    @Override
    public List<InvoicedApplyDetail> findByInvoiceApplyId(Long iaId, List<Long> iaIdList) {
        if(null == iaId && CollectionUtils.isEmpty(iaIdList)){
            return Lists.newArrayList();
        }

        if(null != iaId && CollectionUtils.isEmpty(iaIdList)){
            iaIdList.add(iaId);
            return baseMapper.findByInvoiceApplyId(iaIdList);
        }

        if(null == iaId && CollectionUtils.isNotEmpty(iaIdList)){
            return baseMapper.findByInvoiceApplyId(iaIdList);
        }

        if(null != iaId && CollectionUtils.isNotEmpty(iaIdList)){
            iaIdList.add(iaId);
        }
        return baseMapper.findByInvoiceApplyId(iaIdList);
    }

    /**
     * 根据明细ID获取数据
     *
     * @param iadIdList 开票申请明细ID
     * @return 结果
     */
    @Override
    public List<InvoicedApplyDetail> findByIdList(List<Long> iadIdList) {
        return baseMapper.selectList(new LambdaQueryWrapper<InvoicedApplyDetail>().in(InvoicedApplyDetail::getId, iadIdList));
    }

    /**
     * 查询贷项凭证数量
     *
     * @param invoiceNo
     * @return
     */
    @Override
    public List<InvoicedApplyDetail> findByInvoiceNo(String invoiceNo) {
         return baseMapper.findByInvoiceNo(invoiceNo);
    }

    @Override
    public List<CreatePcaDetailPO> queryCodApplyInvoiceById(Long id) {
        return baseMapper.queryCodApplyInvoiceById(id);
    }

    @Override
    public List<CreatePcaDetailPO> queryCodApplyInvoiceByAccountStatementNo(String accountStatementNo) {
        return baseMapper.queryCodApplyInvoiceByAccountStatementNo(accountStatementNo);
    }

    @Override
    public List<CreatePcaDetailPO> queryCodApplyInvoiceListByAccountStatementNo(String accountStatementNo,String createTime) {
        return baseMapper.queryCodApplyInvoiceListByAccountStatementNo(accountStatementNo,createTime);
    }
}
