package com.jkcredit.invoice.service.selfcar.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.SelfCarInvoiceMapper;
import com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice;
import com.jkcredit.invoice.service.selfcar.SelfCarInvoiceService;
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;

/**
 * 自有车发票服务实现类
 * @author mumuxigua
 */
@Service
public class SelfCarInvoiceServiceImpl extends ServiceImpl<SelfCarInvoiceMapper, SelfCarInvoice> implements SelfCarInvoiceService {
    @Autowired
    SelfCarInvoiceMapper selfCarInvoiceMapper;

    @Override
    public IPage<List<SelfCarInvoice>> findByPageAndInvoice(Page page, SelfCarInvoice selfCarInvoice) {
        return selfCarInvoiceMapper.selectAllByPage(page, selfCarInvoice);
    }


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

        List<SelfCarInvoice> selfCarInvoicesTmp = new ArrayList<SelfCarInvoice>();
        for (SelfCarInvoice selfCarInvoice : selfCarInvoices) {
            if (StringUtils.isNotEmpty(selfCarInvoice.getCardId()) && StringUtils.isEmpty(selfCarInvoice.getTradeId())) {
                List<SelfCarInvoice> cars = selfCarInvoiceMapper.selectByCardId(selfCarInvoice.getCardId());
                selfCarInvoicesTmp.addAll(cars);
            } else if (StringUtils.isEmpty(selfCarInvoice.getCardId()) && StringUtils.isNotEmpty(selfCarInvoice.getTradeId())) {
                List<SelfCarInvoice> cars = selfCarInvoiceMapper.selectByTrade(selfCarInvoice.getTradeId());
                selfCarInvoicesTmp.addAll(cars);
            } else {
                return new RespR<>("失败", "0");
            }

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