package com.jkcredit.invoice.hub.service.searchInvoiceResult;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.mapper.SearchInvoiceResultMapper;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResult;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResultDto;
import com.jkcredit.invoice.hub.model.dto.searchInvoiceResult.InvoiceRecordDto;
import com.jkcredit.invoice.hub.model.dto.searchInvoiceResult.SearchInvoiceResultDto;
import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.BillRecordPo;
import com.jkcredit.invoice.hub.model.po.searchInvoiceResult.InvoiceRecordPo;
import com.jkcredit.invoice.hub.model.po.searchInvoiceResult.SearchInvoiceResultPo;
import com.jkcredit.invoice.hub.model.vo.record.InvoiceRecordVo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/18 20:52
 * @version: V1.0
 **/
@Slf4j
@Service
public class SearchInvoiceResultServiceImpl extends BaseService implements SearchInvoiceResultService {
    @Resource
    SearchInvoiceResultMapper mapper;

    @Override
    public void saveSearchInvoiceResult(SearchInvoiceResultDto dto) {
        SearchInvoiceResultPo po = new SearchInvoiceResultPo();
        BeanUtil.copyProperties(po, dto);
        po.setCreateTime(new Date());
        try {
            mapper.insert(po);
        } catch (Exception e) {
            log.error("插入发票数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("插入发票数据失败");
        }
    }

    @Override
    public List<InvoiceResult> getSearchInvoiceResult(String num, Long userId) {
        try {
            List<SearchInvoiceResultPo> poList = mapper.getSearchInvoiceResult(num, userId);
            List<InvoiceResult> dtoList = new ArrayList<>();
            for (SearchInvoiceResultPo po : poList) {
                InvoiceResult dto = new InvoiceResult();
                BeanUtil.copyProperties(dto, po);
                dtoList.add(dto);
            }
            return dtoList;
        } catch (Exception e) {
            log.error("查询发票数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("查询发票数据失败");
        }
    }

    @Override
    public IPage<InvoiceRecordVo> getInvoiceRecords(Page page, InvoiceRecordDto dto) {
        try {
            IPage<InvoiceRecordPo> records = mapper.getInvoiceRecords(page, dto);
            return records.convert(this::convert);
        } catch (Exception e) {
            log.error("获取发票详情记录失败,失败原因：{}", e.getMessage());
            throw new ServiceException("获取运单详情记录失败");
        }
    }

    private InvoiceRecordVo convert(InvoiceRecordPo po) {
        InvoiceRecordVo vo = new InvoiceRecordVo();
        BeanUtil.copyProperties(vo, po);
        // 将数据库存储的decimal转double
        return vo;
    }

    @Override
    public List<InvoiceResult> getInvoiceByTransactionId(String transactionId) {
        try {
            List<SearchInvoiceResultPo> poList = mapper.getInvoiceByTransactionId(transactionId);
            List<InvoiceResult> dtoList = new ArrayList<>();
            for (SearchInvoiceResultPo po : poList) {
                InvoiceResult dto = new InvoiceResult();
                BeanUtil.copyProperties(dto, po);
                dtoList.add(dto);
            }
            return dtoList;
        } catch (Exception e) {
            log.error("根据流水号查询发票数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("查询发票数据失败");
        }
    }

    @Override
    public List<InvoiceResult> getInvoiceByInvoiceNum(String invoiceNum, String invoiceCode) {
        try {
            List<SearchInvoiceResultPo> poList = mapper.getInvoiceByInvoiceNum(invoiceNum, invoiceCode);
            List<InvoiceResult> dtoList = new ArrayList<>();
            for (SearchInvoiceResultPo po : poList) {
                InvoiceResult dto = new InvoiceResult();
                BeanUtil.copyProperties(dto, po);
                dtoList.add(dto);
            }
            return dtoList;
        } catch (Exception e) {
            log.error("根据发票号码发票代码查询发票数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("查询发票数据失败");
        }
    }
}
