package com.engine.htsc.zbrjsq;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 *
 *
 *@description:
 *@author: K1810015
 *@time: 2022-01-13
 *@Remark: 请求获取正版软件的接口
 *
 */
public class ZbrjsqAction {

    @POST
    @Path("/getzbrjsq")
    @Produces({MediaType.TEXT_PLAIN})
    public String getzbrjsq(@Context HttpServletRequest request,
                            @Context HttpServletResponse response){
        PropBean pb=new PropBean();
        String name= Util.null2String(request.getParameter("name"));
        int pageNo= Util.getIntValue(Util.null2String(request.getParameter("pageNo")));
        int pagesize=Util.getIntValue( Util.null2String(request.getParameter("pagesize")));
        String headtoken=Util.null2String(pb.getPropName("zbrjsq.token"));
        String headuserid=Util.null2String(pb.getPropName("zbrjsq.user"));
        String sendurl=Util.null2String(pb.getPropName("zbrjsq.sendurl"));
        JAVATools.SetCmdwritelog("正版软件调用name:"+name);
        JSONObject jsonObject=new JSONObject();
        if(pageNo<0){
            pageNo=0;
        }
        if(pageNo>=1){
            pageNo=pageNo-1;
        }
        if(pagesize<=0){
            pagesize=10;
        }
        pageNo=pageNo*pagesize;//这个不是分页。是查询的开始数量
        jsonObject.put("limit",pagesize);
        jsonObject.put("query",name);
        jsonObject.put("start",pageNo);
        HTUtil htUtil=new HTUtil();
        Map<String, String> headers=new HashMap<>(2);
        headers.put("user",headuserid);
        headers.put("token",headtoken);
        JAVATools.SetCmdwritelog("正版软件调用参数查看：",jsonObject.toJSONString());
        String  responsestr = htUtil.httpPostRaw(sendurl, jsonObject.toJSONString(), headers, null);
        if(StringUtils.isBlank(responsestr)){
            JAVATools.SetCmdwritelog("正版软件调用失败",responsestr);
            JAVATools.SetCmdwritelog("正版软件调用失败参数查看",jsonObject.toJSONString());
            return "";
        }
        JAVATools.SetCmdwritelog("正版软件调用参数回传："+responsestr);

        JSONObject jsonObjectstr = JSONObject.parseObject(responsestr);
        String msg=Util.null2String(jsonObjectstr.get("msg"));
        if(!StringUtils.isBlank(msg)){
            JAVATools.SetCmdwritelog("正版软件调用失败",responsestr);
            return "";
        }
        jsonObjectstr.put("counts",Util.getIntValue(Util.null2String(jsonObjectstr.get("results"))));

        return jsonObjectstr.toJSONString();
    }
}
