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

import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.constant.ExceptionMessage;
import com.jkcredit.invoice.hub.mapper.SearchInvoiceMapper;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResultDto;
import com.jkcredit.invoice.hub.model.dto.searchInvoice.SearchInvoiceDto;
import com.jkcredit.invoice.hub.model.po.searchInvoice.SearchInvoicePo;
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.Date;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/18 15:45
 * @version: V1.0
 **/
@Slf4j
@Service
public class SearchInvoiceServiceImpl extends BaseService implements SearchInvoiceService {
    @Resource
    SearchInvoiceMapper mapper;

    @Override
    public void saveSearchInvoice(InvoiceResultDto dto) {
        SearchInvoicePo po = new SearchInvoicePo();
        BeanUtil.copyProperties(po, dto);
        po.setCreateTime(new Date());
        try {
            mapper.insert(po);
        } catch (Exception e) {
            log.error("新增运单查询发票外层数据失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增运单查询发票外层数据失败");
        }
    }

    @Override
    public SearchInvoiceDto getSearchInvoiceByNum(String num) {
        try {
            SearchInvoicePo po = mapper.getSearchInvoiceByNum(num);
            if (po != null) {
                SearchInvoiceDto dto = new SearchInvoiceDto();
                BeanUtil.copyProperties(dto, po);
                return dto;
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("运单发票查询失败，失败原因：{}", e.getMessage());
            throw new ServiceException("运单发票查询失败");
        }
    }
}
