<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
    boolean isTrue2 = false;
    BaseBean bb = new BaseBean();
    RecordSet rs = new RecordSet();
    String kjid = request.getParameter("kjid");
    String ksfw = request.getParameter("userids");
    ResourceComInfo resourceComInfo = null;
    try{
        resourceComInfo = new ResourceComInfo();

        if(StringUtils.isNotBlank(kjid)){
            Map<String, Map<String,String>> userMap = new HashMap<String,Map<String,String>>();
            String sql = " select id,zgs,fgs,sgs from picc_hrm_org_path_view";
            rs.executeQuery(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String zgs = Util.null2String(rs.getString("zgs"));
                String fgs = Util.null2String(rs.getString("fgs"));
                String sgs = Util.null2String(rs.getString("sgs"));
                Map<String,String> gsMap = new HashMap<String,String>();
                gsMap.put("zgs",zgs);
                gsMap.put("fgs",fgs);
                gsMap.put("sgs",sgs);
                userMap.put(id,gsMap);
            }
            if(StringUtils.isNotBlank(ksfw)){
                sql = " delete from uf_ks_kjb_dt2 where mainid = "+kjid ;
                boolean isTrue = rs.executeUpdate(sql);
                if(isTrue){
                    String[] userids = ksfw.split(",");
                    bb.writeLog("userids:"+userids.length);
                    List<List> batchList = new ArrayList<>(); //存放每条数据更新时需要的参数
                    for(int i=0;i<userids.length;i++){
                        String userid = userids[i];
                        String deptid = "" ;
                        if(StringUtils.isNotBlank(userid)){
                            deptid = resourceComInfo.getDepartmentID(userid);
                        }

                        String zgs = "";
                        String fgs = "";
                        String sgs = "";
                        if(userMap.containsKey(userid)){
                            Map<String,String> gsMap =userMap.get(userid);
                            if(gsMap.containsKey("zgs")){
                                zgs = gsMap.get("zgs");
                            }
                            if(gsMap.containsKey("fgs")){
                                fgs = gsMap.get("fgs");
                            }
                            if(gsMap.containsKey("sgs")){
                                sgs = gsMap.get("sgs");
                            }
                        }

                        List<String> userList = new ArrayList<String>();
                        userList.add(kjid);
                        userList.add(userid);
                        userList.add(deptid);
                        userList.add("0");
                        userList.add(zgs);
                        userList.add(fgs);
                        userList.add(sgs);
                        batchList.add(userList);
                    }

                    bb.writeLog("batchList:"+batchList.size());
                    if(batchList.size()>0){
                        sql = " insert into uf_ks_kjb_dt2(mainid,ks,ksssbm,zt,kssszgs,ksssfgs,kssssgs) values(?,?,?,?,?,?,?)";
                        isTrue2 = rs.executeBatchSql(sql,batchList);
                        bb.writeLog("isTrue2:"+isTrue2);
                    }
                }
            }
        }

    }catch (Exception e){

    }

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("isTrue2",isTrue2);
%>

<%=jsonObject.toJSONString() %>