<%@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" %>
<%@ page import="weaver.general.BaseBean" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%
    String dataResource = "E7TEST";

    RecordSet rs = new RecordSet();
    List<Map<String,Object>> insertTitleList = new ArrayList<Map<String,Object>>();
    List<Map<String,Object>> updateTitleList = new ArrayList<Map<String,Object>>();

    Map<String,String> E9JobActivitiesMap = getE9HrmJobActivitiesList();
    Map<String,String> E9DepartmentMap = getE9DepartmentOutkey();
    List<String> E7HrmactivitiesList = getE7HrmActivitiesList(dataResource);

    out.println("=====E9JobActivitiesMap:"+E9JobActivitiesMap.size()+"</br>");
    out.println("=====E9DepartmentMap:"+E9DepartmentMap.size()+""+"</br>");
    out.println("=====E7HrmactivitiesList:"+E7HrmactivitiesList.size()+""+"</br>");

    Map<String,Map<String,String>> E7JobTitlesMap = getE7HrmTitlesList2(dataResource);
    out.println("=====E7JobTitlesMap:"+E7JobTitlesMap.size()+""+"</br>");
    String sql =" select t.id,t.jobtitlemark,t.jobtitlename,t.jobactivityid,t.jobdepartmentid,t.outkey,t.jobtitlecode \n" +
                " from hrmjobtitles t\n" +
                " where t.jobdepartmentid is not null\n" ;

    out.println("e9--sql:"+sql+"</br>");
    rs.execute(sql);
    while(rs.next()) {

        String e9id = Util.null2String(rs.getString("id"));

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


        String jobtitlename = Util.null2String(rs.getString("jobtitlename")).trim();
        String jobtitlemark = Util.null2String(rs.getString("jobtitlemark")).trim();
        String jobactivityid = Util.null2String(rs.getString("jobactivityid"));
        String jobdepartmentid = Util.null2String(rs.getString("jobdepartmentid"));
        String outkey = Util.null2String(rs.getString("outkey"));
        String jobtitlecode = Util.null2String(rs.getString("jobtitlecode"));

        Map<String,Object> E9Map = new HashMap<String,Object>();
        E9Map.put("e9id", e9id);
        E9Map.put("jobtitlename", jobtitlename);
        E9Map.put("jobtitlemark", jobtitlemark);
        E9Map.put("jobtitlecode", jobtitlecode);

        String jobactivityOutkey = "";
        if(StringUtils.isNotBlank(jobactivityid)){
            if(E9JobActivitiesMap.containsKey(jobactivityid)){
                jobactivityOutkey = E9JobActivitiesMap.get(jobactivityid);
            }
        }

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

        if(StringUtils.isNotBlank(outkey)){
            if(E7JobTitlesMap.containsKey(outkey)){
                if(StringUtils.isNotBlank(jobactivityOutkey) && E7HrmactivitiesList.contains(jobactivityOutkey)){
                    if(StringUtils.isNotBlank(departmentOutkey)){
                        Map<String,String> jobTitlesMap = E7JobTitlesMap.get(outkey);
                        String e7jobtitlemark = Util.null2String(jobTitlesMap.get("e7jobtitlemark")).trim();
                        String e7jobtitlename = Util.null2String(jobTitlesMap.get("e7jobtitlename")).trim();
                        String e7jobactivityid = Util.null2String(jobTitlesMap.get("e7jobactivityid")).trim();
                        String e7jobdepartmentid = Util.null2String(jobTitlesMap.get("e7jobdepartmentid")).trim();
                        String e7jobtitlecode = Util.null2String(jobTitlesMap.get("e7jobtitlecode")).trim();

                        out.println("e7id:"+outkey+",e7jobtitlemark:"+e7jobtitlemark+" e7jobtitlename:"+e7jobtitlename+" e7jobactivityid:"+e7jobactivityid+" e7jobdepartmentid:"+e7jobdepartmentid+" e7jobtitlecode:"+e7jobtitlecode+"</br>" );
                        out.println("e9id:"+e9id+",jobtitlemark:"+jobtitlemark+" jobtitlename:"+jobtitlename+" jobactivityid:"+jobactivityOutkey+" jobdepartmentid:"+departmentOutkey +" jobtitlecode:"+jobtitlecode+"</br>");

                        if(!e7jobtitlemark.equals(jobtitlemark)
                                || !e7jobtitlename.equals(jobtitlename)
                                || !e7jobactivityid.equals(jobactivityOutkey)
                                || !e7jobdepartmentid.equals(departmentOutkey)
                                || !e7jobtitlecode.equals(jobtitlecode)
                        ){
                            out.println("=====outkey:"+outkey+"更新,"+e9id+"</br>");
                            E9Map.put("e7id", outkey);
                            E9Map.put("jobdepartmentid", departmentOutkey);
                            E9Map.put("jobactivityid", jobactivityOutkey);
                            updateTitleList.add(E9Map);
                        }else{
                            out.println("=====outkey:"+outkey+","+e9id+"没有变化的数据"+"</br>");
                        }
                    }else{
                        out.println("=====outkey:"+outkey+","+e9id+"的activity:"+jobactivityOutkey+"不在E7里</br>");
                    }
                }else{
                    out.println("=====outkey:"+outkey+","+e9id+",departmentid 为空</br>");
                }
            }else{
                if(StringUtils.isNotBlank(jobactivityOutkey) && E7HrmactivitiesList.contains(jobactivityOutkey)){
                    out.println("=====outkey:"+outkey+"新增,"+e9id+"</br>");
                    E9Map.put("jobdepartmentid", departmentOutkey);
                    E9Map.put("jobactivityid", jobactivityOutkey);
                    insertTitleList.add(E9Map);
                }else{
                    out.println("=====outkey:"+outkey+","+e9id+",activity:"+jobactivityOutkey+"不在E7里</br>");
                }
            }
        }else{
            if(StringUtils.isNotBlank(jobactivityOutkey) && E7HrmactivitiesList.contains(jobactivityOutkey)){
                out.println("=====outkey:"+outkey+"新增,"+e9id+"</br>");
                E9Map.put("jobdepartmentid", departmentOutkey);
                E9Map.put("jobactivityid", jobactivityOutkey);
                insertTitleList.add(E9Map);
            }else{
                out.println("=====outkey:"+outkey+","+e9id+",activity:"+jobactivityOutkey+"不在E7里</br>");
            }
        }
    }

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

    for(Map<String,Object> item : insertTitleList)
    {
        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());
            if(!"e7id".equals(key) && !"e9id".equals(key)){
                keyString += keyString == "" ? key : ","+key ;
                valueString += valueString == "" ? "'"+value+"'" : ",'"+value+"' ";
            }
        }
        String insert_sql = " insert into hrmjobtitles("+keyString+") values("+valueString+")";
        out.println("insert_sql:"+insert_sql+"</br>");
    }

    for(Map<String,Object> item : updateTitleList) {
        String e7id = Util.null2String(item.get("e7id"));
        String valuesStr = "";
        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 hrmjobtitles set " + valuesStr + " where id = '" + e7id + "' ";
        out.println("sql:"+sql+"</br>");
    }

%>

<%!

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


    /***
     *
     * @return
     */
    public List<String> getE7HrmActivitiesList(String dataResource){

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


    public List<String> getE7HrmTitlesList(String dataResource){

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


    public Map<String,String> getE9HrmJobActivitiesList(){

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


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

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7JobtitlesMap = new HashMap<String,Map<String,String>>();
        String sql = " select id,jobtitlemark,jobtitlename,jobactivityid,jobdepartmentid,jobtitlecode from hrmjobtitles " ;
        rsd.execute(sql);
        while (rsd.next()){
            String e7id = Util.null2String(rsd.getString("id"));
            String e7jobtitlemark = Util.null2String(rsd.getString("jobtitlemark"));
            String e7jobtitlename = Util.null2String(rsd.getString("jobtitlename"));
            String e7jobactivityid = Util.null2String(rsd.getString("jobactivityid"));
            String e7jobdepartmentid = Util.null2String(rsd.getString("jobdepartmentid"));
            String e7jobtitlecode = Util.null2String(rsd.getString("jobtitlecode"));

            Map<String,String> jobtitlesMap = new HashMap<String,String>();
            jobtitlesMap.put("e7id",e7id);
            jobtitlesMap.put("e7jobtitlemark",e7jobtitlemark);
            jobtitlesMap.put("e7jobtitlename",e7jobtitlename);
            jobtitlesMap.put("e7jobactivityid",e7jobactivityid);
            jobtitlesMap.put("e7jobdepartmentid",e7jobdepartmentid);
            jobtitlesMap.put("e7jobtitlecode",e7jobtitlecode);
            E7JobtitlesMap.put(e7id,jobtitlesMap);
        }
        return E7JobtitlesMap;
    }


    public String insertE7Data(RecordSetDataSource rsd,String insert_sql,String uuid,int insert_fail_count){
        String newE7Id = "";
        BaseBean bb = new BaseBean();
        bb.writeLog("insert-sql:"+insert_sql);
        boolean isTrue = rsd.execute(insert_sql);
        if(isTrue){
            String sql = " select max(id) as id from hrmjobtitles ";
            rsd.executeSql(sql);
            if(rsd.next()){
                newE7Id = Util.null2String(rsd.getString("id"));
            }
        }else{
            insert_fail_count++;
        }
        return newE7Id;
    }


    public boolean updateE7SubcompanyData(RecordSetDataSource rsd,String update_sql){
        BaseBean bb = new BaseBean();
        bb.writeLog("update-sql:"+update_sql);
        boolean isTrue = rsd.execute(update_sql);
        return isTrue;
    }

%>




