package com.engine.htsc.ReimbursementFlow.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.htsc.ReimbursementFlow.Esb.Dao.UfzbbxesblocalDao;
import com.engine.htsc.ReimbursementFlow.Esb.ThreadAsync.Ufzbbxesblocalinsert;
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.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class ItToListgetAll  {

    @POST
    @Path("/getOldAllList")
    @Produces({MediaType.TEXT_PLAIN})
    public String  getOldAllList(@Context HttpServletRequest request,
                                      @Context HttpServletResponse response){
        PropBean pb = new PropBean();
        HTUtil htUtil=new HTUtil();
        String jsonvalue="";
        String badge= Util.null2String(request.getParameter("badge"));
        String title= Util.null2String(request.getParameter("title"));
        String workFlowNumber= Util.null2String(request.getParameter("workFlowNumber"));
        String drafterNamestr= Util.null2String(request.getParameter("drafterName"));
        String workFlowNumstr= Util.null2String(request.getParameter("workFlowNum"));

        String workflowkey=Util.null2String(request.getParameter("workflowkey"));

        int pageNo= Util.getIntValue(Util.null2String(request.getParameter("pageNo")));
        int pagesize=Util.getIntValue( Util.null2String(request.getParameter("pagesize")));
    if("".equals(badge)||"".equals(badge)){
          return "";

        }
        if(pageNo<0){
            pageNo=0;
        }
        if(pageNo>=1){
            pageNo=pageNo-1;
        }
        if(pagesize<=0){
            pagesize=20;
        }

        JSONObject permissionObj=new JSONObject();
        if(!"".equals(title.toString().trim())) {
            permissionObj.put("title",title);
        }
        if(!"".equals(workFlowNumber.toString().trim())) {
            permissionObj.put("workFlowNumber",workFlowNumber);
        }
        if(!"".equals(workFlowNumstr.toString().trim())) {
            permissionObj.put("workFlowNum",workFlowNumstr);
        }
        if(!"".equals(drafterNamestr.toString().trim())) {
            permissionObj.put("drafterName",drafterNamestr);
        }

        if(!"".equals(workflowkey.toString().trim())) {

           String[] keysiplit= workflowkey.split("\\^");
            if(keysiplit.length==3){
                permissionObj.put("workFlowNumber",keysiplit[0]);
                permissionObj.put("workFlowName",keysiplit[1]);
                permissionObj.put("workFlowNum",keysiplit[2]);

            } if(keysiplit.length==2){
                permissionObj.put("workFlowNumber",keysiplit[0]);
                permissionObj.put("workFlowName",keysiplit[1]);


            }else if(keysiplit.length==1){
                permissionObj.put("workFlowNumber",keysiplit[0]);
            }

        }

        JSONObject jsonObject=new JSONObject();

        jsonObject.put("badge",badge);
        jsonObject.put("start",pageNo);
        jsonObject.put("count",pagesize);
        jsonObject.put("permissionObj",permissionObj);
        String urllistrl= pb.getPropName("url_Itgetall");
        Map<String,String> handermap= htUtil.getAppHeader();
        JAVATools.SetCmdwritelog("url："+urllistrl+"params:"+jsonObject.toJSONString());
        String  responsestr = htUtil.httpPostRaw(urllistrl, jsonObject.toJSONString(), null, null);
        if(!"".equals(responsestr)){
            JSONObject jsonObjectstr = JSONObject.parseObject(responsestr);
            if(jsonObjectstr!=null&&jsonObject.size()>0){
                if(jsonObjectstr.get("data")!=null && ((JSONObject)jsonObjectstr.get("data") ).get("totalCount") !=null){

                    JSONArray jsondetaillist= jsonObjectstr.getJSONObject("data").getJSONArray("dataList");
                        jsondetaillist.stream().forEach((item)->{
                                    String workFlowName= Util.null2String(((JSONObject)item).get("workFlowName"));
                                    String workFlowNumberstr=Util.null2String(((JSONObject)item).get("workFlowNumber"));
                                    String workFlowNum=Util.null2String(((JSONObject)item).get("workFlowNum"));
                                    ((JSONObject)item).put("workflowkey",workFlowNumberstr+"^"+workFlowName+"^"+workFlowNum);
                        });


                     jsonObjectstr.put("counts",((JSONObject)jsonObjectstr.get("data") ).get("totalCount")) ;
                     jsonObjectstr.put("data",jsondetaillist);
                }
                responsestr=JSON.toJSONString( jsonObjectstr,SerializerFeature.WriteMapNullValue);
            }

        }
         JAVATools.SetCmdwritelog("差旅付款接口回写",responsestr);
        return responsestr;
    }


}
