package com.dxhy.erp.service;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.erp.dao.InvoiceElectronDao;
import com.dxhy.erp.dao.SysDeptDao;
import com.dxhy.erp.entity.InvoiceElectron;
import com.dxhy.erp.utils.MyThread;
import com.dxhy.oss.service.FileService;

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

/**
 * @author dxhy
 */
@Service("electronService")
@Slf4j
public class ElectronService {
    @Value("${pro.yl.gfTaxNo:}")
    private String yLgfTaxNo;
    @Value("${pro.yl.userName:}")
    private String qsName;
    @Value("${pro.yl.userAccount:}")
    private String userAccount;
    @Value("${pro.yl.company:}")
    private String company;
    @Value("${ftp.connection.tempPath:}")
    private String tempPath;

    @Resource
    private InvoiceElectronDao invoiceElectronDao;

    @Resource
    private SysDeptDao sysDeptDao;
    @Resource
    private FileService fileService;

    public Map<String, String> electronEvent(String content) {
        String msg = Base64.decodeStr(content);
        log.info("解密报文：" + msg);
        Map<String, String> requestMao = JSONObject.parseObject(msg, Map.class);
        requestMao.put("company", company);
        requestMao.put("userAccount", userAccount);
        requestMao.put("userName", qsName);
        // 另起线程处理图片
        MyThread myThread = new MyThread(requestMao, tempPath, fileService, yLgfTaxNo);
        myThread.start();
        Map<String, String> map = new HashMap<>(16);
        map.put("returnCode", "0000");
        map.put("returnMessage", "接收成功");
        map.put("request_id", requestMao.get("request_id"));
        return map;
    }

    public List<Map<String, String>> getScanMsg(Map<String, Object> requestMap) {
        SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, String>> returnList = new ArrayList<>();
        List<Map<String, String>> idList = (List<Map<String, String>>)requestMap.get("data");
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        for (Map<String, String> idMap : idList) {
            Map<String, String> map = new HashMap<>(16);
            InvoiceElectron invoiceElectron = invoiceElectronDao.selectByRequestId(idMap.get("request_id"));
            if (invoiceElectron == null) {
                map.put("returnCode", "0009");
                map.put("returnMessage", "所查发票不存在");
                map.put("invoice_no", "");
                map.put("invoice_code", "");
                map.put("invoice_date", "");
                map.put("invoice_amount", "");
                map.put("tax_amount", "");
                map.put("total_amount", "");
                map.put("check_code", "");
                map.put("image_path", "");
                map.put("request_id", idMap.get("request_id"));
            } else {
                String checkReturnCode = invoiceElectron.getCheckReturnCode();
                if ("1".equals(invoiceElectron.getQsStatus())) {
                    map.put("returnCode", "0001");
                } else {
                    map.put("returnCode", invoiceElectron.getCheckReturnCode());
                }
                map.put("returnMessage", invoiceElectron.getNotes());
                map.put("invoice_no", invoiceElectron.getInvoiceNo());
                map.put("invoice_code", invoiceElectron.getInvoiceCode());
                Date invoiceDate = invoiceElectron.getInvoiceDate();
                map.put("invoice_date", invoiceDate == null ? "" : aDate.format(invoiceElectron.getInvoiceDate()));
                map.put("invoice_amount", invoiceElectron.getInvoiceAmount().toString());
                map.put("tax_amount", invoiceElectron.getTaxAmount().toString());
                map.put("total_amount", invoiceElectron.getTotalAmount().toString());
                map.put("check_code", invoiceElectron.getCheckCode());
                map.put("image_path", "/api/erp/elector/getpdf?scanId=" + invoiceElectron.getScanId());
                map.put("request_id", idMap.get("request_id"));
            }
            returnList.add(map);
        }
        return returnList;
    }
}
