<%@page import="weaver.conn.RecordSet"%>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.conn.ConnStatement" %>
<%@ page import="oracle.sql.CLOB" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%

    long start = System.currentTimeMillis();
    out.println("开始时间:"+start);
    String billid = request.getParameter("billid");
    out.println("billid:"+billid+"</br>");
    ResourceComInfo resourceComInfo = null;
    try{
        resourceComInfo = new ResourceComInfo();

        if(StringUtils.isNotBlank(billid)){
            RecordSet rs = new RecordSet();
            out.println("getDBType:"+rs.getDBType()+"</br>");
            if(("oracle").equals(rs.getDBType())){
                String ksfw = "";
                ConnStatement statement = null;
                try{
                    statement = new ConnStatement();
                    String sql = "select ksfw from uf_ks_kjb where id = " + billid;
                    statement.setStatementSql(sql, false);
                    statement.executeQuery();
                    if (statement.next()) {
                        CLOB clob = statement.getClob("ksfw");
                        String readline = "";
                        StringBuffer clobStrBuff = new StringBuffer("");
                        BufferedReader clobin = new BufferedReader(clob.getCharacterStream());
                        while ((readline = clobin.readLine()) != null)
                            clobStrBuff = clobStrBuff.append(readline);
                        clobin.close();
                        ksfw = clobStrBuff.toString();
                    }
                }catch(Exception e){
                    out.println("获取文档内容元素内容异常:"+e);
                }finally {
                    statement.close();
                }

                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);
                }

                out.println("userMap:"+userMap.size()+"</br>");

                out.println("ksfw:"+ksfw+"</br>");

                if(StringUtils.isNotBlank(ksfw)){
                    sql = " delete from uf_ks_kjb_dt2 where mainid = "+billid ;
                    out.println("sql:"+sql+"</br>");
                    boolean isTrue = rs.executeUpdate(sql);
                    out.println("isTrue:"+isTrue+"</br>");
                    if(isTrue){
                        String[] userids = ksfw.split(",");

                        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");
                                }
                            }


                            out.println("userid:"+userid+"</br>");
                            out.println("deptid:"+deptid+"</br>");
                            List<String> userList = new ArrayList<String>();
                            userList.add(billid);
                            userList.add(userid);
                            userList.add(deptid);

                            userList.add(zgs);
                            userList.add(fgs);
                            userList.add(sgs);

                            userList.add("0");
                            batchList.add(userList);
                        }
                        if(batchList.size()>0){
                            sql = " insert into uf_ks_kjb_dt2(mainid,ks,ksssbm,zt,kssszgs,ksssfgs,kssssgs) values(?,?,?,?,?,?,?)";
                            isTrue = rs.executeBatchSql(sql,batchList);
                            out.println("isTrue2:"+isTrue+"</br>");
                        }
                    }
                }
            }

//            else if("mysql".equalsIgnoreCase(rs.getDBType())){
//                String ksfw = "";
//                try{
//                    String sql = "select ksfw from uf_ks_kjb where id = " + billid;
//                    rs.execute(sql);
//                    if (rs.next()) {
//                        ksfw = Util.null2String(rs.getString("ksfw"));
//                    }
//                }catch(Exception e){
//                    out.println("获取文档内容元素内容异常:"+e);
//                }finally {
//                }
//
//                out.println("ksfw:"+ksfw+"</br>");
//
//                if(StringUtils.isNotBlank(ksfw)){
//                    String sql = " delete from uf_ks_kjb_dt2 where mainid = "+billid ;
//                    out.println("sql:"+sql+"</br>");
//                    boolean isTrue = rs.executeUpdate(sql);
//                    out.println("isTrue:"+isTrue+"</br>");
//                    if(isTrue){
//                        String[] userids = ksfw.split(",");
//
//                        List<List> batchList = new ArrayList<List>(); //存放每条数据更新时需要的参数
//                        for(int i=0;i<userids.length;i++){
//                            String userid = userids[i];
//                            String deptid = "" ;
//                            if(StringUtils.isNotBlank(userid)){
//                                deptid = resourceComInfo.getDepartmentID(userid);
//                            }
//                            out.println("userid:"+userid+"</br>");
//                            out.println("deptid:"+deptid+"</br>");
//                            List<Object> userList = new ArrayList<>();
//                            userList.add(billid);
//                            userList.add(userid);
//                            userList.add(deptid);
//                            userList.add("0");
//                            batchList.add(userList);
//                        }
//                        if(batchList.size()>0){
//                            sql = " insert into uf_ks_kjb_dt2(mainid,ks,ksssbm,zt) values(?,?,?,?)";
//                            isTrue = rs.executeBatchSql(sql,batchList);
//                            out.println("isTrue2:"+isTrue+"</br>");
//                        }
//                    }
//                }
//            }


        }
    }catch (Exception e){
        out.println("e:"+e+"</br>");
    }

    long end = System.currentTimeMillis();
    out.println("结束时间:"+end);

    out.println("结束时间:"+(end-start));

%>




