package com.seeyon.apps.src_hxinvoice.controller;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_hxinvoice.service.IInvoiceService;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.util.annotation.NeedlessCheckLogin;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;

public class PushOrderInfoAndInvoiceInfoController extends BaseController {

    private IInvoiceService invoiceServiceImpl;

    public IInvoiceService getInvoiceService() {
        if (this.invoiceServiceImpl == null) {
            this.invoiceServiceImpl = ((IInvoiceService) AppContext.getBean("invoiceServiceImpl"));
        }
        return invoiceServiceImpl;
    }
    public void setInvoiceService(IInvoiceService invoiceServiceImpl) {
        this.invoiceServiceImpl = invoiceServiceImpl;
    }

    @NeedlessCheckLogin
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        ConfigVo configVo = getYdctLeaseBillConfig();
        //            设置返回值对象
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        BufferedReader reader = request.getReader();
        String line;
        StringBuilder requestBody = new StringBuilder();
        while((line = reader.readLine())!=null){
            requestBody.append(line);
        }
        String data = requestBody.toString();
        String decodedParam = URLDecoder.decode(data, "UTF-8");
        String[] datas =  decodedParam.split("&");
        JSONObject jsonObject = new JSONObject();
        for(int i = 0 ; i < datas.length ; i ++ ){
            String[] params = datas[i].split("=");
            if(params.length>1){
                jsonObject.put(params[0],params[1]);
            }
        }
        JSONObject res = null;
        try {
//            res = getInvoiceService().PushOrderInfoAndInvoiceInfoService(jsonObject);
            res = new JSONObject();
        } catch (Exception e) {
            e.printStackTrace();
        }

        render(response, res.toString());
        return null;
    }

    /**
     * 给前台渲染json数据
     * @param response
     * @param text
     */
    private void render(HttpServletResponse response, String text) {
        response.setContentType("application/json;charset=UTF-8");
        try {
            response.setContentLength(text.getBytes("UTF-8").length);
            response.getWriter().write(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
