package com.engine.tjyh.xc.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.tjyh.xc.service.XcService;
import com.engine.tjyh.xc.service.impl.XcServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 *  接受携程数据
 * @Description
 * @Author matrix
 * @Date 2023/6/15 10:05
 **/
@Slf4j
public class AcceptXcDataAction {

    public XcService getService(){
        return (XcService) ServiceUtil.getService(XcServiceImpl.class);

    }

    @POST
    @Path("/sendData")
    @Produces(MediaType.TEXT_PLAIN)
    public String sendData(@Context HttpServletRequest request, @Context HttpServletResponse response){
        log.info("测试-------------------------");
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = br.readLine())!=null){
                    sb.append(line);
            }
            String input = null2String(sb.toString());
            if(!"".equals(input)) {
                if(isJsonValid(input)) {
                    JSONObject jo1 = JSON.parseObject(input);
                    apidatas.putAll(this.getService().sendData(jo1));
                }else{
                    apidatas.put("errno", -1);
                    apidatas.put("errmsg", "输入参数不是json格式");
                }
            }else {
                apidatas.put("errno", -1);
                apidatas.put("errmsg", "输入参数为空!");
            }
        } catch (Exception e) {
            apidatas.put("errno", -1);
            apidatas.put("errmsg", "catch exception : " + e.getMessage());
        }
        log.info("测试-------------------------"+JSON.toJSONString(apidatas));
        return JSONObject.toJSONString(apidatas);
    }


    @POST
    @Path("/sendData2")
    @Produces(MediaType.TEXT_PLAIN)
    public String sendData2(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = br.readLine())!=null){
                sb.append(line);
            }
            String input = null2String(sb.toString());
            if(!"".equals(input)) {
                if(isJsonValid(input)) {
                    JSONObject jo1 = JSON.parseObject(input);
                    apidatas.putAll(this.getService().sendData2(jo1));
                }else{
                    apidatas.put("errno", -1);
                    apidatas.put("errmsg", "输入参数不是json格式");
                }
            }else {
                apidatas.put("errno", -1);
                apidatas.put("errmsg", "输入参数为空!");
            }
        } catch (Exception e) {
            apidatas.put("errno", -1);
            apidatas.put("errmsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }



    public static String null2String(String s) {
        return s == null ? "" : s;
    }

    /**
     * 判断JSON字符串是否合法
     *
     * @param jsonStr JSON字符串
     * @return true：合法；false：不合法
     */
    public static boolean isJsonValid(String jsonStr) {
        try {
            JSON.parse(jsonStr);
            return true;
        } catch (JSONException e) {
            return false;
        }
    }

}
