package com.kingdee.eas.custom.sendmessage.callback;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.Context;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.cp.eip.sso.qrcode.QrTokenOpenIdManager;
import com.kingdee.eas.cp.eip.sso.util.JSONUtil;
import com.kingdee.eas.cp.eip.sso.web.qrcode.ResponseWriteUtil;
import com.kingdee.eas.cp.eip.sso.web.qrcode.YZJCallbackServlet;
import com.kingdee.eas.custom.sendmessage.utils.GetMessageConfig;
import com.kingdee.shr.api.OSFWSClient;
import com.kingdee.util.StringUtils;
import com.kingdee.util.Uuid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 接口回调osf
 * description: CallBackToOSFServlet <br>
 * date: 2025/2/27 16:22 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */

public class CallBackToOSFServlet extends HttpServlet {
    private static final long serialVersionUID = 1426489082732035281L;
    private static final Logger logger = LoggerFactory.getLogger(CallBackToOSFServlet.class);

    public CallBackToOSFServlet() {
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String time = req.getHeader("X-*-Time");
            String nonce = req.getHeader("X-*-Nonce");
            String sign = req.getHeader("X-*-Sign");
            System.out.println("CallBackToOSFServlet");
            logger.error("CallBackToOSFServlet  start--------------------------------------------------------------------");
            logger.error("CallBackToOSFServlet  time----------------->" + time);
            logger.error("CallBackToOSFServlet  nonce----------------->" + nonce);
            logger.error("CallBackToOSFServlet  sign----------------->" + sign);
            String data = req.getParameter("data");
            // 读取请求体
            BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream()));
            StringBuilder jsonInput = new StringBuilder();
            String jsonPart;
            while ((jsonPart = reader.readLine()) != null) {
                jsonInput.append(jsonPart);
            }

            logger.error("CallBackToOSFServlet  data----------------->" + data);
            Properties prop = GetMessageConfig.getProp();
            String shrAddr = prop.getProperty("shrAddr");
            OSFWSClient client = new OSFWSClient();
            Map<String, Object> map = new HashMap<String, Object>();
            if(jsonInput.length()>0){
                map.put("data", jsonInput.toString());
            }else {
                map.put("data", data);
            }

            String response = client.proceedOSF(shrAddr, "CallBackToOSF", map);
            if (response.indexOf("success") > 0) {
                Map<String, Object> result = new HashMap<String, Object>();
                result.put("code", 200);
                result.put("mes", "\u6210\u529f\uff01");
                result.put("requestId", null);
                result.put("data", response);
                ResponseWriteUtil.writeObjectData(resp, result, "JSON");
            } else {
                JSONObject object = JSON.parseObject(response);
                Map<String, Object> result = new HashMap<String, Object>();
                result.put("code", 400);
                result.put("mes", object.get("msg"));
                result.put("requestId", null);
                result.put("data", response);
                ResponseWriteUtil.writeObjectData(resp, result, "JSON");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            logger.error("CallBackToOSFServlet  end-----------------");
        }
    }

    private Context parseRestUrl(String dataCenter, String language) throws ServletException {

        Context ctx = null;
        try {
            ctx = QrTokenOpenIdManager.getContext(dataCenter, language);
            return ctx;
        } catch (Exception var10) {
            logger.error(var10.getMessage());
            throw new ServletException(var10.getCause());
        }
    }
}
