package com.engine.htsc.otherSystemWorkflow;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.util.HttpUtil;
import com.engine.htsc.trip.util.AjaxResult;
import org.apache.commons.lang3.ArrayUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetOtherSystemWorkFlowAction extends BaseBean {


    /**
     * 查询其他系统人员待办
     * @param request
     * @return
     */
    @POST
    @Path("/getOtherSystemWorkFlow")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<List<Map<String, String>>> getPortalTodoData(Map<String, String> request){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        //获取用户
        String userId = Util.null2String(request.get("userId"));
        String type = Util.null2String(request.get("type")).equals("")?"2":Util.null2String(request.get("type"));
        //标题
        String requestName = Util.null2String(request.get("title"));
        //流程编号
        String workFlowNum = Util.null2String(request.get("workFlowNum"));
        //创建人
        String drafterName = Util.null2String(request.get("drafterName"));
        int pageNumber = Integer.parseInt(Util.null2String(request.get("pageNumber")));
        int pageSize = Integer.parseInt(Util.null2String(request.get("pageSize")));

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("title",requestName);
        jsonObject.put("workFlowNum",workFlowNum);
        jsonObject.put("drafterName",drafterName);
        jsonObject.put("pageNumber",pageNumber);
        jsonObject.put("pageSize",pageSize);
        jsonObject.put("userId",userId);
        jsonObject.put("type",type);

        try {
            RecordSet rs = new RecordSet();
            //查询接口
            String urlSql = "select xtmc,qqdz from UF_LCJKDZ where sfqy=0";
            rs.executeQuery(urlSql);
            ArrayList<String> urlList = new ArrayList<>();
            while (rs.next()){
                urlList.add(rs.getString("qqdz"));
            }
            JSONArray jsonArray = new JSONArray();
            for (String url:urlList){
                String responsestr = HttpUtil.postApplicationJson(url, jsonObject.toJSONString());
                if (!"".equals(responsestr)){
                    JSONObject jsonObjectstr = JSONObject.parseObject(responsestr);
                    if(jsonObjectstr!=null&&jsonObject.size()>0){
                        jsonArray.addAll(jsonObjectstr.getJSONArray("data"));
                    }
                }
            }
            /**
             * 分页
             */

            apidatas.put("total",jsonArray.size());
            int min = (pageNumber-1)*pageSize;
            int max = pageNumber*pageSize;
            List dataList = new ArrayList();
            if (max > jsonArray.size()){
                max = jsonArray.size();
            }
            for (int i =min;i<max;i++){
                dataList.add(jsonArray.get(i));
            }
            apidatas.put("data",dataList);

            List responseList = new ArrayList();
            responseList.add(apidatas);

            return AjaxResult.ok(responseList);
        }catch (Exception e){
            writeLog("sync fund status fail, catch error: ", e);
            return AjaxResult.error(e.getMessage());
        }

    }
}
