<%@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();
    Map<String,String> E9SubcompanyOutkey = getE9SubcompanyOutkey();
//    List<String> E7SubcompanyList = getE7HrmSubcompanyList(dataResource);
    Map<String,Map<String,String>> E7SubcompanyMap = getE7HrmSubcompanyList2(dataResource);

    List<Map<String,Object>> topInsertList = new ArrayList<Map<String,Object>>();
    List<Map<String,Object>> topUpdateList = new ArrayList<Map<String,Object>>();

    List<String> topList = new ArrayList<>();
    String sql =" select id,subcompanyname,subcompanydesc,supsubcomid,canceled,subcompanycode,outkey,showorder " +
                " from hrmsubcompany \n" +
                " where (supsubcomid is null or supsubcomid = -1 or supsubcomid = 0) \n" ;
    out.println("e9-sql:"+sql+"</br>");
    rs.execute(sql);
    while(rs.next()) {
        String e9Id = Util.null2String(rs.getString("id"));
        topList.add(e9Id);

        String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
        String subcompanydesc = Util.null2String(rs.getString("subcompanydesc"));
        String canceled = Util.null2String(rs.getString("canceled"));
        String subcompanycode = Util.null2String(rs.getString("subcompanycode"));
        String supsubcomid = Util.null2String(rs.getString("supsubcomid")).trim();
        int showorder = Util.getIntValue(rs.getString("showorder"));
        String outkey = Util.null2String(rs.getString("outkey"));  //E7的hrmsubcompany的ID

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

        Map<String,Object> E9Map = new HashMap<String, Object>();
        E9Map.put("subcompanyname", subcompanyname);
        if (StringUtils.isNotBlank(subcompanydesc)) {
            E9Map.put("subcompanydesc", subcompanydesc);
        }
        if (StringUtils.isNotBlank(canceled)) {
            E9Map.put("canceled", canceled);
        }
        if (showorder > -1) {
            E9Map.put("showorder", showorder);
        }
        if(StringUtils.isNotBlank(subcompanycode)){
            E9Map.put("subcompanycode", subcompanycode);
        }

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

        out.println("0".equals(supsubcomid) || "-1".equals(supsubcomid) || StringUtils.isBlank(supsubcomid+""));

        String supsubcomOutKey = "" ;
        if("0".equals(supsubcomid) || "-1".equals(supsubcomid) || StringUtils.isBlank(supsubcomid+"")){
            supsubcomOutKey = "0";
        }else{
            if(E9SubcompanyOutkey.containsKey(supsubcomid)){
                supsubcomOutKey = E9SubcompanyOutkey.get(supsubcomid);
            }
        }
        out.println("supsubcomOutKey:"+supsubcomOutKey+"</br>");


        if(StringUtils.isNotBlank(outkey)){
            if(E7SubcompanyMap.containsKey(outkey)){

                if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){
                    out.println("更新--outkey:"+outkey+"</br>");

                    Map<String,String> E7SubcompanyInfo = E7SubcompanyMap.get(outkey);
                    String e7subcompanyname = Util.null2String(E7SubcompanyInfo.get("subcompanyname"));
                    String e7subcompanydesc = Util.null2String(E7SubcompanyInfo.get("subcompanydesc"));
                    String e7supsubcomid = Util.null2String(E7SubcompanyInfo.get("supsubcomid"));
                    String e7canceled = Util.null2String(E7SubcompanyInfo.get("canceled"));
                    String e7subcompanycode = Util.null2String(E7SubcompanyInfo.get("subcompanycode"));

                    if(!e7subcompanyname.equals(subcompanyname)
                        || !e7subcompanydesc.equals(subcompanydesc)
                        || !e7supsubcomid.equals(supsubcomOutKey)
                        || !e7canceled.equals(canceled)
                        || !e7subcompanycode.equals(subcompanycode)
                    ){
                        E9Map.put("supsubcomid", supsubcomOutKey);
                        E9Map.put("e7id", outkey);
                        topUpdateList.add(E9Map);
                    }else{
                        out.println("=====outkey:"+outkey+"没有变化的数据"+"</br>");
                    }
                }else{
                    out.println("=====outkey:"+outkey+"的supsubcom"+supsubcomOutKey+"在E7中不存在"+"</br>");
                }
            }else{
                out.println("=====outkey:"+outkey+" 在E7中不存在"+"</br>");
            }
        }else{
            if(StringUtils.isNotBlank(supsubcomOutKey) && E7SubcompanyMap.containsKey(supsubcomOutKey)){
                out.println("新增--outkey:"+outkey+"</br>");
                E9Map.put("supsubcomid", supsubcomOutKey);
                topInsertList.add(E9Map);
            }else{
                out.println("=====outkey:"+outkey+"的supsubcom"+supsubcomOutKey+"在E7中不存在"+"</br>");
            }
        }

//        if(StringUtils.isNotBlank(supsubcomOutKey)){
//            E9Map.put("supsubcomid", supsubcomOutKey);
//
//            if(StringUtils.isNotBlank(outkey)){
//                E9Map.put("e7id", outkey);
//                topUpdateList.add(E9Map);
//            }else{
//                topInsertList.add(E9Map);
//            }
//        }
    }

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

    for(Map<String,Object> E9Map : topInsertList){
        String uuid = UUID.randomUUID().toString().replace("-","").toUpperCase();
        E9Map.put("modifier",modifier);
        E9Map.put("creater",creater);
        E9Map.put("UUID",uuid);
        E9Map.put("companyid",1);//默认公司是1

        String keyString = "";
        String valueString = "";
        Iterator<Map.Entry<String, Object>> it = E9Map.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 hrmsubcompany(modified,created,"+keyString+") values(systimestamp,systimestamp,"+valueString+")";
        out.println("sql:"+sql+"</br>");
    }


    for(Map<String,Object> E9Map : topUpdateList){
        String e7id = Util.null2String(E9Map.get("e7id"));
        String valuesStr = " modified=systimestamp,modifier=1";
        Iterator<Map.Entry<String, Object>> it = E9Map.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 hrmsubcompany set "+valuesStr+" where id = '"+e7id+"' ";
        out.println("sql:"+sql+"</br>");

    }

    List<Map<String,Object>> insertList = new ArrayList<Map<String,Object>>();
    List<Map<String,Object>> updateList = new ArrayList<Map<String,Object>>();

    for(String subcompanyid : topList){
        if(StringUtils.isNotBlank(subcompanyid)){
            sql = " select id,subcompanyname,subcompanydesc,supsubcomid,showorder,\n" +
                    " canceled,subcompanycode,outkey from hrmsubcompany \n" +
                    " connect by prior  id = supsubcomid \n" +
                    " start with supsubcomid = "+subcompanyid;

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

            rs.executeQuery(sql);
            while (rs.next()){
                String e9Id = Util.null2String(rs.getString("id"));
                String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
                String subcompanydesc = Util.null2String(rs.getString("subcompanydesc"));
                String canceled = Util.null2String(rs.getString("canceled"));
                String subcompanycode = Util.null2String(rs.getString("subcompanycode"));
                String supsubcomid = Util.null2String(rs.getString("supsubcomid")).trim();
                int showorder = Util.getIntValue(rs.getString("showorder"));
                String outkey = Util.null2String(rs.getString("outkey"));  //E7的hrmsubcompany的ID

                Map<String,Object> E9Map = new HashMap<String, Object>();
                E9Map.put("subcompanyname", subcompanyname);
                if (StringUtils.isNotBlank(subcompanydesc)) {
                    E9Map.put("subcompanydesc", subcompanydesc);
                }
                if (StringUtils.isNotBlank(canceled)) {
                    E9Map.put("canceled", canceled);
                }
                if (showorder > -1) {
                    E9Map.put("showorder", showorder);
                }
                if(StringUtils.isNotBlank(subcompanycode)){
                    E9Map.put("subcompanycode", subcompanycode);
                }

                out.println("supsubcomid:"+supsubcomid+"</br>");
                String supsubcomOutKey = "" ;
                if("0".equals(supsubcomid) || "-1".equals(supsubcomid) || StringUtils.isBlank(supsubcomid+"")){
                    supsubcomOutKey = "0";
                }else{
                    if(E9SubcompanyOutkey.containsKey(supsubcomid)){
                        supsubcomOutKey = E9SubcompanyOutkey.get(supsubcomid);
                    }
                }

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

                if(StringUtils.isNotBlank(outkey)){
                    if(E7SubcompanyMap.containsKey(outkey)){

                        if(StringUtils.isNotBlank(supsubcomOutKey) && E7SubcompanyMap.containsKey(supsubcomOutKey)){


                            Map<String,String> E7SubcompanyInfo = E7SubcompanyMap.get(outkey);
                            String e7subcompanyname = Util.null2String(E7SubcompanyInfo.get("subcompanyname"));
                            String e7subcompanydesc = Util.null2String(E7SubcompanyInfo.get("subcompanydesc"));
                            String e7supsubcomid = Util.null2String(E7SubcompanyInfo.get("supsubcomid"));
                            String e7canceled = Util.null2String(E7SubcompanyInfo.get("canceled"));
                            String e7subcompanycode = Util.null2String(E7SubcompanyInfo.get("subcompanycode"));

                            out.println("e7subcompanyname:"+e7subcompanyname+" e7subcompanydesc:"+e7subcompanydesc+" e7supsubcomid:"+e7supsubcomid+" e7canceled:"+e7canceled +" e7subcompanycode:"+e7subcompanycode+"</br>");
                            out.println("subcompanyname:"+subcompanyname+" subcompanydesc:"+subcompanydesc+" supsubcomid:"+supsubcomOutKey+" canceled:"+canceled +" subcompanycode:"+subcompanycode+"</br>");

                            if(!e7subcompanyname.equals(subcompanyname)
                                    || !e7subcompanydesc.equals(subcompanydesc)
                                    || !e7supsubcomid.equals(supsubcomOutKey)
                                    || !e7canceled.equals(canceled)
                                    || !e7subcompanycode.equals(subcompanycode)
                            ){

                                out.println("更新--outkey:"+outkey+"</br>");
                                E9Map.put("supsubcomid", supsubcomOutKey);
                                E9Map.put("e7id", outkey);
                                updateList.add(E9Map);
                            }else{
                                out.println("=====outkey:"+outkey+"没有变化的数据"+"</br>");
                            }
                        }else{
                            out.println("=====outkey:"+outkey+"的supsubcom"+supsubcomOutKey+"在E7中不存在"+"</br>");
                        }
                    }else{
                        out.println("=====outkey:"+outkey+" 在E7中不存在"+"</br>");
                    }
                }else{
                    if(StringUtils.isNotBlank(supsubcomOutKey) && E7SubcompanyMap.containsKey(supsubcomOutKey)){
                        out.println("新增--outkey:"+outkey+"</br>");
                        E9Map.put("supsubcomid", supsubcomOutKey);
                        insertList.add(E9Map);
                    }else{
                        out.println("=====outkey:"+outkey+"的supsubcom"+supsubcomOutKey+"在E7中不存在"+"</br>");
                    }
                }
            }
        }
    }

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

    for(Map<String,Object> E9Map : insertList){
        String uuid = UUID.randomUUID().toString().replace("-","").toUpperCase();
        E9Map.put("modifier",modifier);
        E9Map.put("creater",creater);
        E9Map.put("UUID",uuid);
        E9Map.put("companyid",1);//默认公司是1

        String keyString = "";
        String valueString = "";
        Iterator<Map.Entry<String, Object>> it = E9Map.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 hrmsubcompany(modified,created,"+keyString+") values(systimestamp,systimestamp,"+valueString+")";
        out.println("sql:"+sql+"</br>");
    }


    for(Map<String,Object> E9Map : updateList){
        String e7id = Util.null2String(E9Map.get("e7id"));
        String valuesStr = " modified=systimestamp,modifier=1";
        Iterator<Map.Entry<String, Object>> it = E9Map.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 hrmsubcompany 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;
    }

    /***
     *
     * @return
     */
    public List<String> getE7HrmSubcompanyList(String dataResource){
        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7SubcompanyList = new ArrayList<String>();
        Map<String,Map<String,String>> E7SubcompanyMap = new HashMap<String,Map<String,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 Map<String,Map<String,String>> getE7HrmSubcompanyList2(String dataResource){
        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7SubcompanyMap = new HashMap<String,Map<String,String>>();
        String sql = " select id,subcompanyname,subcompanydesc,supsubcomid,canceled,subcompanycode from hrmsubcompany " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            String subcompanyname = Util.null2String(rsd.getString("subcompanyname"));
            String subcompanydesc = Util.null2String(rsd.getString("subcompanydesc"));
            String supsubcomid = Util.null2String(rsd.getString("supsubcomid"));
            String canceled = Util.null2String(rsd.getString("canceled"));
            String subcompanycode = Util.null2String(rsd.getString("subcompanycode"));

            Map<String,String> subcompanyMap = new HashMap<>();
            subcompanyMap.put("id",id);
            subcompanyMap.put("subcompanyname",subcompanyname);
            subcompanyMap.put("subcompanydesc",subcompanydesc);
            subcompanyMap.put("supsubcomid",supsubcomid);
            subcompanyMap.put("canceled",canceled);
            subcompanyMap.put("subcompanycode",subcompanycode);
            E7SubcompanyMap.put(id,subcompanyMap);

        }
        return E7SubcompanyMap;
    }
%>




