package com.dxhy.core.job.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dxhy.core.job.dao.*;
import com.dxhy.core.job.entity.TDxHttpLog;
import com.dxhy.core.job.entity.TDxInvoiceLogJobEntity;
import com.dxhy.core.job.entity.TDxRecordInvoiceDetailJobEntity;
import com.dxhy.core.job.entity.TDxRecordInvoiceJobEntity;
import com.dxhy.core.job.pojo.GatherData;
import com.dxhy.core.job.pojo.InvoiceDetailInfo;
import com.dxhy.core.job.pojo.InvoiceInfo;
import com.dxhy.core.job.service.TDxRecordInvoiceService;
import com.dxhy.core.job.service.TDxRecordInvoiceStatisticsService;
import com.dxhy.core.job.utils.TransitionUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 功能描述
 *
 * @author 陈刚扬
 * @date 2020/10/30
 */

@Service("TDxRecordInvoiceService")
@Slf4j
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")

public class TDxRecordInvoiceServiceImpl implements TDxRecordInvoiceService {

    @Resource
    private TDxHttpLogJobDao tDxHttpLogDao;
    @Resource
    private TDxInvoiceLogJobDao tDxInvoiceLogDao;
    @Resource
    private TDxRecordInvoiceJobDao tDxRecordInvoiceDao;
    @Resource
    private TDxRecordInvoiceDetailJobDao tDxRecordInvoiceDetailDao;
    @Resource
    private TDxRecordInvoiceStatisticsJobDao tDxRecordInvoiceStatisticsDao;
    @Resource
    private TDxRecordInvoiceStatisticsService tDxRecordInvoiceStatisticsService;

    @Override
    public void parseInvoice(Long httpLogId, GatherData gather) {
        // 增值税专用发票
        TDxHttpLog tDxHttpLog = tDxHttpLogDao.selectHttpLogById(httpLogId);

        List<InvoiceInfo> invoiceList = (List<InvoiceInfo>)gather.getInvoices();
        String total = invoiceList.size() + "";
        for (InvoiceInfo invoiceInfo : invoiceList) {
            try {
                // 构建底账表（解析接口返回的信息）
                TDxRecordInvoiceJobEntity recordInvoices = TransitionUtils.getInvoiceInfo(invoiceInfo);
                String invoiceCode = invoiceInfo.getInvoiceCode();
                String invoiceNo = invoiceInfo.getInvoiceNo();
                TDxRecordInvoiceJobEntity invoice = tDxRecordInvoiceDao.findInvoiceByCodeAndNo(invoiceCode, invoiceNo);
                recordInvoices.setUuid(invoiceCode + invoiceNo);
                List<InvoiceDetailInfo> detailList = invoiceInfo.getDetailList();
                if (detailList != null && detailList.size() > 0) {
                    tDxRecordInvoiceDetailDao.delDetail(invoiceCode + invoiceNo);
                    for (InvoiceDetailInfo invoiceDetailInfo : detailList) {
                        TDxRecordInvoiceDetailJobEntity detail =
                            TransitionUtils.getInvoiceDetailInfo(invoiceDetailInfo);
                        detail.setUuid(invoiceCode + invoiceNo);
                        detail.setInvoiceCode(invoiceCode);
                        detail.setInvoiceNo(invoiceNo);
                        tDxRecordInvoiceDetailDao.insertRecordInvoiceDetail(detail);
                    }
                    tDxRecordInvoiceStatisticsDao.delStatistics(invoiceCode, invoiceNo);
                    // 税率统计表插入
                    tDxRecordInvoiceStatisticsService.countTaxRate(detailList, invoiceCode, invoiceNo);
                    int countDetail = tDxRecordInvoiceDetailDao.getDetailCount(invoiceCode + invoiceNo);
                    int countTaxRate = tDxRecordInvoiceStatisticsDao.getStatisticsCount(invoiceCode, invoiceNo);

                    if (countDetail > 0 && countTaxRate > 0) {
                        recordInvoices.setDetailYesorno("1");
                    } else {
                        recordInvoices.setDetailYesorno("0");
                    }

                }
                // 判断底账是否存在 存在更新 不存在保存
                if (invoice == null) {
                    tDxRecordInvoiceDao.insertRecordInvoice(recordInvoices);
                } else {
                    if (!invoice.getInvoiceStatus().equals(invoiceInfo.getInvoiceStatus())) {
                        recordInvoices.setStatusUpdateDate(new Date());
                    }
                    tDxRecordInvoiceDao.updateRecordInvoice(recordInvoices);
                }
                // 保存发票关联请求日志id
                TDxInvoiceLogJobEntity tDxInvoiceLog = new TDxInvoiceLogJobEntity();
                tDxInvoiceLog.setInvoiceCode(invoiceCode);
                tDxInvoiceLog.setInvoiceNo(invoiceNo);
                tDxInvoiceLog.setLogId(httpLogId + "");
                tDxInvoiceLogDao.insertInvoiceLog(tDxInvoiceLog);
            } catch (ParseException e) {
                e.printStackTrace();
                tDxHttpLog.setStatus("2");
                tDxHttpLog.setTotal(total);
                tDxHttpLogDao.updateHttoLogById(tDxHttpLog);
            }
        }
        tDxHttpLog.setStatus("1");
        tDxHttpLog.setTotal(total);
        tDxHttpLogDao.updateHttoLogById(tDxHttpLog);
    }
}
