<%@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" %>

<%
    int modifier = 1;
    int creater = 1;

    String dataResource = "E7TEST";

    RecordSet rs = new RecordSet();
//    List<String> E7ActivityList = getE7HrmActivitiesList();

    Map<String,Map<String,String>> E7ActivitiesMap  = getE7HrmActivitiesList2(dataResource) ;
    Map<String,String> E9GroupMap = getE9HrmGroupData();
    List<String> E7GroupMap = getE7HrmGroupData(dataResource);

    out.println("E7ActivitiesMap:"+E7ActivitiesMap.size()+"</br>");
    out.println("E9GroupMap:"+E9GroupMap.size()+"</br>");
    out.println("E7GroupMap:"+E7GroupMap.size()+"</br>");

    List<Map<String,Object>> insertGroupList = new ArrayList<Map<String,Object>>();
    List<Map<String,Object>> updateGroupList = new ArrayList<Map<String,Object>>();
    String sql ="  select k.*\n" +
                " from (\n" +
                " select rank() over(PARTITION BY outkey ORDER BY id DESC) as xh,id,jobactivitymark,jobactivityname,jobgroupid,outkey \n" +
                " from hrmjobactivities\n" +
                " ) k where k.xh = 1";
    out.println("e9--sql:"+sql+"</br>");
    rs.execute(sql);
    while(rs.next()){
//        String dataId = Util.null2String(rs.getString("id"));
        String jobactivitymark = Util.null2String(rs.getString("jobactivitymark"));
        String jobactivityname = Util.null2String(rs.getString("jobactivityname"));
        String jobgroupid = Util.null2String(rs.getString("jobgroupid"));
        String outkey = Util.null2String(rs.getString("outkey"));

        Map<String,Object> groupMap = new HashMap<String,Object>();
        groupMap.put("jobactivitymark",jobactivitymark);
        groupMap.put("jobactivityname",jobactivityname);
        String E9GroupOutkey = "";
        if(StringUtils.isNotBlank(jobgroupid)){
            if(E9GroupMap.containsKey(jobgroupid)){
                E9GroupOutkey = E9GroupMap.get(jobgroupid);
            }
        }
        if(StringUtils.isNotBlank(outkey)){
            if(E7GroupMap.contains(E9GroupOutkey)){
                if(E7ActivitiesMap.containsKey(outkey)){
                    Map<String,String> activitiesMap = E7ActivitiesMap.get(outkey);

                    String e7jobactivitymark = Util.null2String(activitiesMap.get("e7jobactivitymark"));
                    String e7jobactivityname = Util.null2String(activitiesMap.get("e7jobactivityname"));
                    String e7jobgroupid = Util.null2String(activitiesMap.get("e7jobgroupid"));

                    out.println("e7jobactivitymark:"+e7jobactivitymark+" e7jobactivityname:"+e7jobactivityname+" e7jobgroupid:"+e7jobgroupid+"</br>");
                    out.println("jobactivitymark:"+jobactivitymark+" jobactivityname:"+jobactivityname+" jobgroupid:"+E9GroupOutkey+"</br>");

                    if(!e7jobactivitymark.equals(jobactivitymark)
                    || !e7jobactivityname.equals(jobactivityname)
                    || !e7jobgroupid.equals(E9GroupOutkey)
                    ){
                        out.println("=====outkey:"+outkey+"更新"+"</br>");
                        groupMap.put("jobgroupid",E9GroupOutkey);
                        groupMap.put("e7id",outkey);
                        updateGroupList.add(groupMap);
                    }else{
                        out.println("=====outkey:"+outkey+"没有变化的数据"+"</br>");
                    }
                }else{
                    out.println("=====outkey:"+outkey+"不在e7里"+"</br>");
                }
            }else{
                out.println("=====outkey:"+outkey+"的Group:"+E9GroupOutkey+"不在E7里</br>");
            }
        }else{
            if(E7GroupMap.contains(E9GroupOutkey)){
                out.println("====新增"+"</br>");
                insertGroupList.add(groupMap);
            }else{
                out.println("=====outkey:"+outkey+"的Group:"+E9GroupOutkey+"不在E7里</br>");
            }
        }
    }
    out.println("updateGroupList:"+updateGroupList.size());

    out.println("insertGroupList:"+insertGroupList.size());

    for(Map<String,Object> item : insertGroupList)
    {
        String uuid = UUID.randomUUID().toString().replace("-","").toUpperCase();
        item.put("modifier",modifier);
        item.put("creater",creater);
        item.put("UUID",uuid);

        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 hrmjobactivities(modified,created,"+keyString+") values(systimestamp,systimestamp,"+valueString+")";
        out.println("sql:"+sql);
    }

    for(Map<String,Object> item : updateGroupList) {
        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 hrmjobactivities set " + valuesStr + " where id = '" + e7id + "' ";
        out.println("sql:"+sql);
    }
%>

<%!
    public List<String> getE7HrmActivitiesList(String dataResource){

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


    public List<String> getE7HrmGroupData(String dataResource){

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

    /***
     *
     * @return
     */
    public Map<String,String> getE9HrmGroupData(){

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


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

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7ActivitiesMap = new HashMap<String,Map<String,String>>();
        String sql =" select id,jobactivitymark,jobactivityname,jobgroupid \n" +
                    " from hrmjobactivities \n" ;

        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            String e7jobactivitymark = Util.null2String(rsd.getString("jobactivitymark"));
            String e7jobactivityname = Util.null2String(rsd.getString("jobactivityname"));
            String e7jobgroupid = Util.null2String(rsd.getString("jobgroupid"));

            Map<String,String> activitiesMap = new HashMap<String,String>();
            activitiesMap.put("e7jobactivitymark",e7jobactivitymark);
            activitiesMap.put("e7jobactivityname",e7jobactivityname);
            activitiesMap.put("e7jobgroupid",e7jobgroupid);
            E7ActivitiesMap.put(id,activitiesMap);
        }
        return E7ActivitiesMap;
    }

%>




