package com.dxhy.erp.controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
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;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.dxhy.erp.entity.alltax.ReqSaleCountInfo;
import com.dxhy.erp.entity.alltax.TaxesSaleRequestInfo;
import com.dxhy.erp.entity.alltax.TaxesSaleRequestSign;
import com.dxhy.erp.entity.alltax.ToSaleServer;
import com.dxhy.erp.service.AllTaxesSaleService;

import lombok.extern.slf4j.Slf4j;

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

    @Resource
    private AllTaxesSaleService allTaxesSaleService;

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

    private int maximumPoolSize = 20;

    @ResponseBody
    @RequestMapping(value = "/applySale", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    public String applySale(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(), StandardCharsets.UTF_8);
        log.info("线程{},进项汇总通知请求报文:{}", Thread.currentThread().getId(), params);
        try {
            JSONObject jsonObject = JSONObject.parseObject(params);
            TaxesSaleRequestInfo req = JSONObject.toJavaObject(jsonObject, TaxesSaleRequestInfo.class);
            returnResult = allTaxesSaleService.getInvoiceSale(params, req);
            if ("1".equals(req.getInformType())) {
                executor.submit(new ToSaleServer(req));
            }
            if ("2".equals(req.getInformType())) {
                allTaxesSaleService.countSale(req);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnResult;
    }

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

    @ResponseBody
    @RequestMapping(value = "/getInvoiceSaleCount", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    public String getInvoiceCount(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String params = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
        log.info("线程{},进项获取汇总数据请求报文:{}", Thread.currentThread().getId(), params);
        JSONObject json = JSONObject.parseObject(params);
        String informType = json.getString("informType");
        List<ReqSaleCountInfo> reqList =
            JSON.parseObject(json.getString("param"), new TypeReference<ArrayList<ReqSaleCountInfo>>() {});
        if ("1".equals(informType)) {
            return allTaxesSaleService.getInvoiceSaleTaxRateCount(reqList);
        }
        if ("2".equals(informType)) {
            return allTaxesSaleService.getInvoiceSaleItemCount(reqList);
        }
        return "";

    }

}
