package com.engine.htsc.seal.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.htsc.seal.entity.HttpParamsJSON;


import com.engine.htsc.seal.service.OtherSealService;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.Context;
import javax.ws.rs.core.MediaType;
import java.util.*;
import java.util.stream.Collectors;

public class OtherSeal {

    @POST
    @Path("/getSealnumbertoid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getsealnumbertoid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String values=Util.null2String(request.getParameter("values"));
        if(values==null||values.equals("")){
            return JAVATools.Apiretrun("400","参数有误");
        }
        return  JAVATools.Apiretrun("200",getnumbertoid(values));

    }


    private String getnumbertoid(String ids){
        String value="";
        String wherein= Arrays.stream(ids.split(",")).map(item->"\'"+item+"\'").distinct().collect(Collectors.joining(","));
        String sql="select  wm_concat_old(id) as idsum from uf_yinzhang where yzbh in ("+wherein+")";
        Map<String,Object> map=JAVATools.GetTableToMap(sql,null);
        if(map==null||map.size()<=0){
            return  value;
        }
        value= Util.null2String(map.get("idsum"));
        return  value;
    }

    @POST
    @Path("/getHrmworkcodetoid")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.TEXT_PLAIN})
    public String getHrmworkcodetoid(HttpParamsJSON httpparamsjson){
        JSONArray jsonArray=httpparamsjson.getValue();
      //  String values=Util.null2String(request.getParameter("values"));
        if(jsonArray==null||jsonArray.size()<=0){
            return JAVATools.Apiretrun("400","参数有误");
        }
        return  JAVATools.Apiretrun("200",getworkcodetoid(jsonArray));
    }


    @POST
    @Path("/getsealinfo")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.TEXT_PLAIN})
    public String getsealinfo(@Context HttpServletRequest req, @Context HttpServletResponse rep){

        Map<String,Object> Paramsmap= ParamUtil.request2Map(req);
        JSONArray jsonObject=new OtherSealService().getothertype(Paramsmap);
        if(jsonObject==null){
            return JAVATools.Apiretrun("400","参数有误，请联系管理员");
        }else{
            return JAVATools.Apiretrun("200",jsonObject);
        }
    }


    private JSONArray getworkcodetoid(JSONArray jsonArray){
        String value="";
        List<Map<String,Object>> list=new ArrayList<>();

        for (int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            if(jsonObject!=null&&jsonObject.size()==2){
                String usercode=Util.null2String(jsonObject.get("usercode"));
                String deptcode=Util.null2String(jsonObject.get("deptcode"));
                if(!"".equals(usercode) &&!"".equals(deptcode)){
                    Map<String,Object> map=new HashMap<>();
                    map.put("usercode",usercode);
                    map.put("deptcode",deptcode);
                    list.add(map);
                }
            }
        }

        if(list.size()<=0){
            return new JSONArray();
        }
        JSONArray jsonArray1=   JAVATools.GetHrmDeptWorkCodetoids(list);


        return  jsonArray1;
    }

}
