package com.dxhy.core.rabbitmq;

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

import javax.annotation.Resource;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dxhy.core.entity.InvoiceLogEntity;
import com.dxhy.core.entity.SysCustomsLog;
import com.dxhy.core.entity.SysLogEntity;
import com.dxhy.core.entity.TDxRecordInvoice;
import com.dxhy.core.model.openservice.SenderInfo;
import com.dxhy.core.service.dataupload.TdxScanCustomsLogService;
import com.dxhy.core.service.export.IExcelExportService;
import com.dxhy.core.service.log.CoreCustomsLogService;
import com.dxhy.core.service.log.CoreInvoiceLogService;
import com.dxhy.core.service.log.SysLogService;
import com.dxhy.core.util.PushIntefaceUtil;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author user
 */
@Component
@Slf4j
public class Receiver {
    @Resource
    private IExcelExportService excelExportService;

    @Resource
    private SysLogService sysLogService;

    @Resource
    private CoreInvoiceLogService coreInvoiceLogService;

    @Resource
    private CoreCustomsLogService coreCustomsLogService;

    @Resource
    private TdxScanCustomsLogService scanCustomsLogService;

    /**
     * 导出消费者
     *
     * @param message
     *            导出数据
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_EXCEL_APPLY:TO_EXCEL_APPLY}")
    public void handleMessage(String message) {
        try {
            String body = Base64.decodeStr(message);
            log.debug("导出消费者1接收的消息为" + body);
            excelExportService.exportExcel(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 日志请求mq
     *
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_MESSAGE_LOG:TO_MESSAGE_LOG}")
    public void processMessageLog(String logs) {
        log.debug("日志消费者开始,时间：{}, 日志报文：{}", DateUtil.now(), logs);
        List<SysLogEntity> logList = new ArrayList<>();
        try {
            SysLogEntity logEntity = JSON.parseObject(Base64.decodeStr(logs), SysLogEntity.class);
            logList.add(logEntity);
            sysLogService.saveLog(logList);
            log.debug("日志消费者结束,时间：{}", DateUtil.now());
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("异常：{}", e.getMessage());
        }

    }

    /**
     * 发票操作日志消费者
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_INVOICE_LOG:TO_INVOICE_LOG}")
    public void processMessageInvoiceLog(String logs) {
        log.debug("发票日志消费者开始： 时间：" + DateUtil.now() + "           发票日志报文：" + logs);
        try {
            List<InvoiceLogEntity> logList = JSON.parseArray(Base64.decodeStr(logs), InvoiceLogEntity.class);
            coreInvoiceLogService.saveLog(logList);
            log.debug("日志消费者结束： 时间：" + DateUtil.now());
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("异常--" + e.getMessage());
        }
    }

    /**
     * 缴款书操作日志消费者
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_CUSTOMS_LOG:TO_CUSTOMS_LOG}")
    public void processMessageCustomsLog(String logs) {
        log.debug("缴款书日志消费者开始： 时间：" + DateUtil.now() + "           缴款书日志报文：" + logs);
        try {
            List<SysCustomsLog> logList = JSON.parseArray(Base64.decodeStr(logs), SysCustomsLog.class);
            coreCustomsLogService.saveBatch(logList);
            log.debug("日志消费者结束： 时间：" + DateUtil.now());
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("异常--" + e.getMessage());
        }
    }

    /**
     * 艺龙推送发票状态消费者
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_INVOICE_STATUS:TO_INVOICE_STATUS}")
    public void processMessageInvoiceStatus(String status) {
        log.debug("推送发票状态消费者开始： 时间：" + DateUtil.now() + "           推送发票状态报文：" + status);
        try {
            TDxRecordInvoice record = JSON.parseObject(Base64.decodeStr(status), TDxRecordInvoice.class);
            String message = PushIntefaceUtil.get(record);
            log.debug("--" + message);
            log.debug("日志消费者结束： 时间：" + DateUtil.now());
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("异常--" + e.getMessage());
        }
    }

    @RabbitHandler
    @RabbitListener(queues = "${TO_CUSTOMS_OCR:TO_CUSTOMS_OCR}")
    public void toCustomsOcr(String sendInfo) {
        log.debug("缴款书OCR识别消费者开始： 时间：" + DateUtil.now() + "           缴款书OCR识别报文：" + sendInfo);
        try {
            SenderInfo info = JSON.parseObject(Base64.decodeStr(sendInfo), SenderInfo.class);
            scanCustomsLogService.getOcrData(info.getPch(), info.getScanId(), info.getEncodeImg());
            log.debug("日志消费者结束： 时间：" + DateUtil.now());
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("异常--" + e.getMessage());
        }
    }
}
