<%@page import="weaver.conn.RecordSet"%>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%

    String dataResource = "E7TEST";

    int modifier = 1;
    int creater = 1;

    RecordSet rs = new RecordSet();
    List<Map<String,Object>> insertResourceList = new ArrayList<Map<String,Object>>();
    List<Map<String,Object>> updateResourceList = new ArrayList<Map<String,Object>>();

    Map<String,String> E9ResourceMap = getE9ResourceOutkey();
    Map<String,String> E9DepartmentMap = getE9DepartmentOutkey();
    Map<String,String> E9SubcompanyMap = getE9SubcompanyOutkey();
    Map<String,String> E9JobtitleMap =  getE7HrmJobTitlesOutkey();

    out.println("E9ResourceMap:"+E9ResourceMap.size()+"</br>");
    out.println("E9DepartmentMap:"+E9DepartmentMap.size()+"</br>");
    out.println("E9SubcompanyMap:"+E9SubcompanyMap.size()+"</br>");
    out.println("E9JobtitleMap:"+E9JobtitleMap.size()+"</br>");

    List<String> E7DepartmentList = getE7HrmDepartmentList(dataResource);
    List<String> E7SubcompanyList = getE7HrmSubcompanyList(dataResource);
    List<String> E7JobTitleList = getE7HrmJobTitlesList(dataResource);
//    List<String> E7ResourceList = getE7HrmResourceList();

    Map<String,Map<String,String>> E7ResourceMap = getE7HrmResourceMap(dataResource);
    out.println("E7ResourceMap:"+E7ResourceMap.size()+"</br>");
    out.println("E7DepartmentList:"+E7DepartmentList.size()+"</br>");
    out.println("E7SubcompanyList:"+E7SubcompanyList.size()+"</br>");
    out.println("E7JobTitleList:"+E7JobTitleList.size()+"</br>");

    String sql =" select id,workcode,status,dsporder,mobile,jobtitle,password,lastname,loginid,systemlanguage," +
                " sex,birthday,departmentid," +
                " subcompanyid1,seclevel,accounttype,belongto,managerid,outkey " +
                " from hrmresource " +
                " connect by prior id = managerid \n" +
                " start with managerid is null";

    bb.writeLog("E9--sql:"+sql+"</br>");
    rs.executeQuery(sql);
    while(rs.next()) {
        String hrmid = Util.null2String(rs.getString("id"));
        String workcode = Util.null2String(rs.getString("workcode")).trim();
        String status = Util.null2String(rs.getString("status")).trim();
        String dsporder = Util.null2String(rs.getString("dsporder")).trim();
        String mobile = Util.null2String(rs.getString("mobile")).trim();
        String jobtitle = Util.null2String(rs.getString("jobtitle")).trim();
        String password = Util.null2String(rs.getString("password")).trim();
        String lastname = Util.null2String(rs.getString("lastname")).trim();
        String loginid = Util.null2String(rs.getString("loginid")).trim();
        String systemlanguage = Util.null2String(rs.getString("systemlanguage")).trim();
        String sex = Util.null2String(rs.getString("sex")).trim();
        String birthday = Util.null2String(rs.getString("birthday")).trim();
        String lastmoddate = Util.null2String(rs.getString("lastmoddate")).trim();
        String createdate = Util.null2String(rs.getString("createdate")).trim();
        String jobactivitydesc = Util.null2String(rs.getString("jobactivitydesc")).trim();
        String departmentid = Util.null2String(rs.getString("departmentid")).trim();
        String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1")).trim();
        String seclevel = Util.null2String(rs.getString("seclevel")).trim();
        String accounttype = Util.null2String(rs.getString("accounttype")).trim();
        String belongto = Util.null2String(rs.getString("belongto")).trim();
        String managerid = Util.null2String(rs.getString("managerid")).trim();
        String outkey = Util.null2String(rs.getString("outkey")).trim();

        if(StringUtils.isBlank(systemlanguage)){
            systemlanguage = "7";
        }
        if("-1".equals(accounttype) || StringUtils.isBlank(accounttype)){
            accounttype = "0";
        }
        if("-1".equals(belongto) || "0".equals(belongto)){
            belongto = "";
        }
        if("0".equals(managerid)){
            managerid = "";
        }


        Map<String,Object> ResourceMap = new HashMap<String,Object>();
        ResourceMap.put("workcode", workcode);
        ResourceMap.put("status", status);
        ResourceMap.put("dsporder", dsporder);
        ResourceMap.put("mobile", mobile);
        ResourceMap.put("password", password);
        ResourceMap.put("lastname", lastname);
        ResourceMap.put("loginid", loginid);
        ResourceMap.put("systemlanguage", systemlanguage);
        ResourceMap.put("sex", sex);
        ResourceMap.put("birthday", birthday);
        ResourceMap.put("lastmoddate", lastmoddate);
        ResourceMap.put("createdate", createdate);
        ResourceMap.put("jobactivitydesc", jobactivitydesc);
        ResourceMap.put("seclevel", seclevel);
        ResourceMap.put("accounttype", accounttype);

        String managerOutkey = "";
        if(StringUtils.isNotBlank(managerid)){
            if(E9ResourceMap.containsKey(managerid)){
                managerOutkey = E9ResourceMap.get(managerid);
            }
        }

        String belongtoOutkey = "";
        if(StringUtils.isNotBlank(belongto)){
            if(E9ResourceMap.containsKey(belongto)){
                belongtoOutkey = E9ResourceMap.get(belongto);
            }
        }

        String departmentOutkey = "";
        if(StringUtils.isNotBlank(departmentid+"")){
            if(E9DepartmentMap.containsKey(departmentid)){
                departmentOutkey = E9DepartmentMap.get(departmentid);
            }
        }

        String subcompanyOutkey = "";
        if(StringUtils.isNotBlank(subcompanyid1)){
            if(E9SubcompanyMap.containsKey(subcompanyid1)){
                subcompanyOutkey = E9SubcompanyMap.get(subcompanyid1);
            }
        }

        String jobtitleOutkey = "";
        if(StringUtils.isNotBlank(jobtitle)){
            if(E9JobtitleMap.containsKey(jobtitle)){
                jobtitleOutkey = E9JobtitleMap.get(jobtitle);
            }
        }

        if(StringUtils.isNotBlank(outkey)){
            if(E7ResourceMap.containsKey(outkey)){
                if(StringUtils.isNotBlank(departmentOutkey) && E7DepartmentList.contains(departmentOutkey)){
                    if(StringUtils.isNotBlank(subcompanyOutkey) && E7SubcompanyList.contains(subcompanyOutkey)){
                        if(StringUtils.isNotBlank(jobtitleOutkey) && E7JobTitleList.contains(jobtitleOutkey)){

                            Map<String,String> resourceMap = E7ResourceMap.get(outkey);

                            String e7lastname = Util.null2String(resourceMap.get("e7lastname"));
                            String e7loginid = Util.null2String(resourceMap.get("e7loginid"));
                            String e7password = Util.null2String(resourceMap.get("e7password"));
                            String e7sex = Util.null2String(resourceMap.get("e7sex"));
                            String e7birthday = Util.null2String(resourceMap.get("e7birthday"));
                            String e7mobile = Util.null2String(resourceMap.get("e7mobile"));
                            String e7jobtitle = Util.null2String(resourceMap.get("e7jobtitle"));
                            String e7departmentid = Util.null2String(resourceMap.get("e7departmentid"));
                            String e7subcompanyid1 = Util.null2String(resourceMap.get("e7subcompanyid1"));
                            String e7seclevel = Util.null2String(resourceMap.get("e7seclevel"));
                            String e7workcode = Util.null2String(resourceMap.get("e7workcode"));
                            String e7status = Util.null2String(resourceMap.get("e7status"));
                            String e7dsporder = Util.null2String(resourceMap.get("e7dsporder"));
                            String e7systemlanguage = Util.null2String(resourceMap.get("e7systemlanguage"));
                            String e7accounttype = Util.null2String(resourceMap.get("e7accounttype"));
                            String e7belongto = Util.null2String(resourceMap.get("e7belongto"));
                            String e7managerid = Util.null2String(resourceMap.get("e7managerid"));

                            out.println("lastname:"+lastname+" loginid:"+loginid+" password:"+password+" sex:"+sex+" birthday:"+birthday+" mobile:"+mobile+" jobtitle:"+jobtitleOutkey+" departmentid:"+departmentOutkey+" subcompanyid1:"+subcompanyOutkey+" seclevel:"+seclevel+" workcode:"+workcode+" status:"+status +" dsporder:"+dsporder+" systemlanguage:"+systemlanguage+" accounttype:"+accounttype+" belongto:"+belongtoOutkey+" managerid:"+managerOutkey+"</br>");
                            out.println("e7lastname:"+e7lastname+" e7loginid:"+e7loginid+" e7password:"+e7password+" e7sex:"+e7sex+" e7birthday:"+e7birthday+" e7mobile:"+e7mobile+" e7jobtitle:"+e7jobtitle+" e7departmentid:"+e7departmentid+" e7subcompanyid1:"+e7subcompanyid1+" e7seclevel:"+e7seclevel+" e7workcode:"+e7workcode+" e7status:"+e7status +" e7dsporder:"+e7dsporder+" e7systemlanguage:"+e7systemlanguage+" e7accounttype:"+e7accounttype+" e7belongto:"+e7belongto+" e7managerid:"+e7managerid+"</br>");

                            if(!e7lastname.equals(lastname)
                                    || !e7loginid.equals(loginid)
//                                    || !e7password.equals(password)
                                    || !e7sex.equals(sex)
                                    || !e7birthday.equals(birthday)
                                    || !e7mobile.equals(mobile)
                                    || !e7jobtitle.equals(jobtitleOutkey)
                                    || !e7departmentid.equals(departmentOutkey)
                                    || !e7subcompanyid1.equals(subcompanyOutkey)
                                    || !e7seclevel.equals(seclevel)
                                    || !e7workcode.equals(workcode)
                                    || !e7status.equals(status)
                                    || !e7dsporder.equals(dsporder)
                                    || !e7systemlanguage.equals(systemlanguage)
                                    || !e7accounttype.equals(accounttype)
                                    || !e7belongto.equals(belongtoOutkey)
                                    || !e7managerid.equals(managerOutkey)
                            ){
                                out.println("更新--outkey:"+outkey+"</br>");
                                ResourceMap.put("e7id", outkey);
                                ResourceMap.put("jobtitle", jobtitleOutkey);
                                ResourceMap.put("belongto", belongtoOutkey);
                                ResourceMap.put("managerid", managerOutkey);
                                ResourceMap.put("departmentid", departmentOutkey);
                                ResourceMap.put("subcompanyid1", subcompanyOutkey);
                                updateResourceList.add(ResourceMap);
                            }else{
                                out.println("=====outkey:"+outkey+"没有变化的数据"+"</br>");
                            }
                        }else{
                            out.println("=====outkey:"+outkey+"的"+jobtitleOutkey+"在E7中不存在"+"</br>");
                        }
                    }else{
                        out.println("=====outkey:"+outkey+"的"+subcompanyOutkey+"在E7中不存在"+"</br>");
                    }
                }else{
                    out.println("=====outkey:"+outkey+"的"+departmentOutkey+"在E7中不存在"+"</br>");
                }
            }else{
                out.println("=====outkey:"+outkey+"在E7中不存在"+"</br>");
            }
        }else{
            if(StringUtils.isNotBlank(departmentOutkey) && E7DepartmentList.contains(departmentOutkey)){
                if(StringUtils.isNotBlank(subcompanyOutkey) && E7SubcompanyList.contains(subcompanyOutkey)){
                    if(StringUtils.isNotBlank(jobtitleOutkey) && E7JobTitleList.contains(jobtitleOutkey)){
                        out.println("新增</br>");
                        ResourceMap.put("jobtitle", jobtitleOutkey);
                        ResourceMap.put("belongto", belongtoOutkey);
                        ResourceMap.put("managerid", managerOutkey);
                        ResourceMap.put("departmentid", departmentOutkey);
                        ResourceMap.put("subcompanyid1", subcompanyOutkey);
                        insertResourceList.add(ResourceMap);
                    }else{
                        out.println("=====outkey:"+outkey+"的"+jobtitleOutkey+"在E7中不存在"+"</br>");
                    }
                }else{
                    out.println("=====outkey:"+outkey+"的"+subcompanyOutkey+"在E7中不存在"+"</br>");
                }
            }else{
                out.println("=====outkey:"+outkey+"的"+departmentOutkey+"在E7中不存在"+"</br>");
            }
        }
    }

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

    for(int i=0;i<insertResourceList.size();i++) {

        Map<String, Object> item = insertResourceList.get(i);
        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        item.put("modifier", modifier);
        item.put("creater", creater);
        item.put("UUID", uuid);

//        rs.executeProc("HRMRESOURCEMAXID_GET", "");
//        rs.next();
//        int id = rs.getInt(1);
//        item.put("id", id);

        String keyString = "";
        String valueString = "";
        Iterator<Map.Entry<String, Object>> it = item.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = Util.null2String(entry.getKey());
            String value = Util.null2String(entry.getValue());
            keyString += keyString == "" ? key : ","+key ;
            valueString += valueString == "" ? "'"+value+"'" : ",'"+value+"' ";
        }
        sql = " insert into hrmresource(modified,created,"+keyString+") values(systimestamp,systimestamp,"+valueString+")";
        out.println("sql:" + sql+"</br>");
    }

    for(int i=0;i<updateResourceList.size();i++) {
        Map<String, Object> item = updateResourceList.get(i);
        String e7id = Util.null2String(item.get("e7id"));
        String valuesStr = " modified=systimestamp,modifier=1";
        Iterator<Map.Entry<String, Object>> it = item.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = Util.null2String(entry.getKey());
            if(!"e7id".equals(key)){
                String value = Util.null2String(entry.getValue());
                valuesStr += valuesStr == "" ? key+"='"+value+"'" : ","+key+"='"+value+"'" ;
            }
        }

        sql = "update hrmresource set " + valuesStr + " where id = "+e7id+"";
        out.println("sql:" + sql+"</br>");

    }
%>


<%!

    public Map<String,String> getE9SubcompanyOutkey(){

        RecordSet rs = new RecordSet();
        Map<String,String> outkeyMap = new HashMap<String,String>();
        String sql = " select id,outkey from hrmsubcompany " ;
        rs.executeQuery(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString("id"));
            String outkey = Util.null2String(rs.getString("outkey"));
            outkeyMap.put(id,outkey);
        }
        return outkeyMap;
    }


    public Map<String,String> getE9DepartmentOutkey(){

        RecordSet rs = new RecordSet();
        Map<String,String> outkeyMap = new HashMap<String,String>();
        String sql = " select id,outkey from hrmdepartment " ;
        rs.executeQuery(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString("id"));
            String outkey = Util.null2String(rs.getString("outkey"));
            outkeyMap.put(id,outkey);
        }
        return outkeyMap;
    }


    public Map<String,String> getE9ResourceOutkey(){

        Map<String,String> E9ResourceMap = new HashMap<String,String>();
        RecordSet rs = new RecordSet();
        String sql =" select id,outkey from hrmresource ";
        rs.executeQuery(sql);
        while(rs.next()) {
            String id = Util.null2String(rs.getString("id"));
            String outkey = Util.null2String(rs.getString("outkey"));
            E9ResourceMap.put(id,outkey);
        }

        return E9ResourceMap;
    }





    public Map<String,String> getE7HrmJobTitlesOutkey(){

        RecordSet rs = new RecordSet();

        Map<String,String> E9JobtitleMap = new HashMap<String,String>();
        String sql = " select id,outkey from hrmjobtitles " ;
        rs.execute(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString("id"));
            String outkey = Util.null2String(rs.getString("outkey"));
            E9JobtitleMap.put(id,outkey);
        }
        return E9JobtitleMap;
    }


    public List<String> getE7HrmJobTitlesList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7JobtitlesList = new ArrayList<String>();
        String sql = " select id from hrmjobtitles " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7JobtitlesList.add(id);
        }
        return E7JobtitlesList;
    }


    public List<String> getE7HrmDepartmentList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7DepartmentList = new ArrayList<String>();
        String sql = " select id from hrmdepartment " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7DepartmentList.add(id);
        }
        return E7DepartmentList;
    }


    public List<String> getE7HrmSubcompanyList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7SubcompanyList = new ArrayList<String>();
        String sql = " select id from hrmsubcompany " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7SubcompanyList.add(id);
        }
        return E7SubcompanyList;
    }


    public List<String> getE7HrmResourceList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7ResourceList = new ArrayList<String>();
        String sql = " select id from hrmresource " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7ResourceList.add(id);
        }
        return E7ResourceList;
    }


    public Map<String,Map<String,String>> getE7HrmResourceMap(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7ResourceMap = new HashMap<String,Map<String,String>>();
        String sql =" select id,workcode,status,dsporder,mobile,jobtitle,password,lastname,loginid,systemlanguage," +
                    " sex,birthday,lastmoddate,createdate,jobactivitydesc,departmentid," +
                    " subcompanyid1,seclevel,accounttype,belongto,managerid " +
                    " from hrmresource " ;
        rsd.execute(sql);
        while (rsd.next()){
            String e7id = Util.null2String(rsd.getString("id")).trim();
            String e7lastname = Util.null2String(rsd.getString("lastname")).trim();
            String e7loginid = Util.null2String(rsd.getString("loginid")).trim();
            String e7password = Util.null2String(rsd.getString("password")).trim();
            String e7sex = Util.null2String(rsd.getString("sex")).trim();
            String e7birthday = Util.null2String(rsd.getString("birthday")).trim();
            String e7mobile = Util.null2String(rsd.getString("mobile")).trim();
            String e7jobtitle = Util.null2String(rsd.getString("jobtitle")).trim();
            String e7departmentid = Util.null2String(rsd.getString("departmentid")).trim();
            String e7subcompanyid1 = Util.null2String(rsd.getString("subcompanyid1")).trim();
            String e7seclevel = Util.null2String(rsd.getString("seclevel")).trim();
            String e7workcode = Util.null2String(rsd.getString("workcode")).trim();
            String e7status = Util.null2String(rsd.getString("status")).trim();
            String e7dsporder = Util.null2String(rsd.getString("dsporder")).trim();

            String e7systemlanguage = Util.null2String(rsd.getString("systemlanguage")).trim();
            if(StringUtils.isBlank(e7systemlanguage)){
                e7systemlanguage = "7";
            }
            String e7accounttype = Util.null2String(rsd.getString("accounttype")).trim();
            if("-1".equals(e7accounttype) || StringUtils.isBlank(e7accounttype)){
                e7accounttype = "0";
            }
            String e7belongto = Util.null2String(rsd.getString("belongto")).trim();
            if("-1".equals(e7belongto) || "0".equals(e7belongto)){
                e7belongto = "";
            }
            String e7managerid = Util.null2String(rsd.getString("managerid")).trim();
            if("0".equals(e7managerid)){
                e7managerid = "";
            }

            Map<String,String> resourceMap = new HashMap<String,String>();
            resourceMap.put("e7lastname",e7lastname);
            resourceMap.put("e7loginid",e7loginid);
            resourceMap.put("e7password",e7password);
            resourceMap.put("e7sex",e7sex);

            resourceMap.put("e7birthday",e7birthday);
            resourceMap.put("e7mobile",e7mobile);
            resourceMap.put("e7jobtitle",e7jobtitle);
            resourceMap.put("e7departmentid",e7departmentid);
            resourceMap.put("e7subcompanyid1",e7subcompanyid1);
            resourceMap.put("e7seclevel",e7seclevel);
            resourceMap.put("e7workcode",e7workcode);
            resourceMap.put("e7status",e7status);

            resourceMap.put("e7dsporder",e7dsporder);
            resourceMap.put("e7systemlanguage",e7systemlanguage);
            resourceMap.put("e7accounttype",e7accounttype);
            resourceMap.put("e7belongto",e7belongto);
            resourceMap.put("e7managerid",e7managerid);

            E7ResourceMap.put(e7id,resourceMap);
        }
        return E7ResourceMap;
    }



%>



