package com.dxhy.erp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.dxhy.erp.entity.alltax.*;
import com.dxhy.erp.service.AllTaxesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author luxiaoxu
 * @Description 全税接口入口
 */
@RestController
@RequestMapping("/rest/alltaxes")
@Slf4j
public class AllTaxesController {

    @Autowired
    private AllTaxesService allTaxesService;

    /**
     * 核心线程池大小
     */
    private int corePoolSize = 10;
    /**
     * 最大线程池大小
     */

    private int maximumPoolSize = 20;

    @ResponseBody
    @RequestMapping(value = "/apply", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    public String apply(HttpServletResponse response, HttpServletRequest request) throws IOException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(1), new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "全税进项接口申请-" + r.hashCode());
                }
            });

        String returnResult = null;
        String params = IOUtils.toString(request.getInputStream(), "UTF-8");
        log.info("线程{},进项汇总通知请求报文:{}", Thread.currentThread().getId(), params);
        try {
            JSONObject jsonObject = JSONObject.parseObject(params);
            TaxesRequestInfo req = JSONObject.toJavaObject(jsonObject, TaxesRequestInfo.class);
            returnResult = allTaxesService.getInvoice(params, req);
            executor.submit(new ToServer(req));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
        }
        return returnResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getSign", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    public String getSign(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String params = IOUtils.toString(request.getInputStream(), "UTF-8");
        JSONObject jsonObject = JSONObject.parseObject(params);
        TaxesRequestSign req = JSONObject.toJavaObject(jsonObject, TaxesRequestSign.class);
        log.info("线程{},进项汇总获取完成标识请求报文:{}", Thread.currentThread().getId(), params);
        return allTaxesService.getInvoiceSign(req);
    }

    @ResponseBody
    @RequestMapping(value = "/getInvoiceInfo", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    public String getInvoiceInfo(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String params = IOUtils.toString(request.getInputStream(), "UTF-8");
        log.info("线程{},进项获取关键字发票信息请求报文:{}", Thread.currentThread().getId(), params);
        // ReqInvoiceInfo req = JSON.parseObject(params, ReqInvoiceInfo.class);
        ReqInvoiceInfo reqList = JSON.parseObject(params, new TypeReference<ReqInvoiceInfo>() {});
        return allTaxesService.getInvoiceByKey(reqList);

    }

    @ResponseBody
    @RequestMapping(value = "/getInvoiceCount", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    public String getInvoiceCount(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String params = IOUtils.toString(request.getInputStream(), "UTF-8");
        log.info("线程{},进项获取汇总数据请求报文:{}", Thread.currentThread().getId(), params);
        List<ReqCountInfo> reqList = JSON.parseObject(params, new TypeReference<ArrayList<ReqCountInfo>>() {});
        return allTaxesService.getInvoiceCount(reqList);
    }

}
