package com.jkcredit.invoice.service.nocar.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.mapper.binvoce.BillInvoiceMapper;
import com.jkcredit.invoice.model.entity.invoice.BillInvoice;
import com.jkcredit.invoice.service.nocar.NocarInvoiceService;
import com.jkcredit.invoice.util.RespR;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 无车发票服务实现类
 * @author mumuxigua
 */
@Service
public class NocarInvoiceServiceImpl extends ServiceImpl<BillInvoiceMapper, BillInvoice> implements NocarInvoiceService {
    @Autowired
    BillInvoiceMapper billInvoiceMapper;

    @Override
    public IPage<List<BillInvoice>> findByPageAndInvoice(Page page, BillInvoice billInvoice) {
        return billInvoiceMapper.selectAllByPage(page, billInvoice);
    }

    @Override
    public IPage<List<BillInvoice>> findByPageAndInvoiceHis(Page page, BillInvoice billInvoice) {
        return billInvoiceMapper.selectAllByPageHis(page, billInvoice);
    }

    @Override
    public Map<String, String> findInvoiceStatics(BillInvoice billInvoice) {
        return billInvoiceMapper.findInvoiceStatics(billInvoice);
    }

    @Override
    public RespR findImportNocarInvoices(List<BillInvoice> billInvoices) {
        if (billInvoices == null || 0 == billInvoices.size()) {
            return new RespR<>("失败", "0");
        }

        List<BillInvoice> billInvoicesTmp = new ArrayList<BillInvoice>();
        for (BillInvoice billInvoice : billInvoices) {
            if (StringUtils.isNotEmpty(billInvoice.getWaybillNum()) && StringUtils.isEmpty(billInvoice.getInvoiceNum())
                    && StringUtils.isEmpty(billInvoice.getInvoiceCode())) {
                List<BillInvoice> bills = billInvoiceMapper.selectByBillNum(billInvoice.getWaybillNum());
                billInvoicesTmp.addAll(bills);
            } else if (StringUtils.isNotEmpty(billInvoice.getWaybillNum()) && StringUtils.isNotEmpty(billInvoice.getInvoiceNum())
                    && StringUtils.isNotEmpty(billInvoice.getInvoiceCode())) {
                BillInvoice bill = billInvoiceMapper.selectAllByPageByBillInvoice(billInvoice);
                billInvoicesTmp.add(bill);
            } else if (StringUtils.isEmpty(billInvoice.getWaybillNum()) && StringUtils.isNotEmpty(billInvoice.getInvoiceNum())
                    && StringUtils.isNotEmpty(billInvoice.getInvoiceCode())) {
                BillInvoice bill = billInvoiceMapper.selectAllByPageByBillInvoice(billInvoice);
                billInvoicesTmp.add(bill);
            } else {
                return new RespR<>("失败", "0");
            }

        }
        return new RespR<>(billInvoicesTmp, "1");
    }
}
