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

    RecordSet rs = new RecordSet();

    Map<String,Map<String,String>> E7GroupMap = getE7HrmGroupList2(dataResource);
    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 id,jobgroupname,jobgroupremark,outkey from hrmjobgroups ";
    out.println("e9--sql:"+sql+"</br>");
    rs.execute(sql);
    while(rs.next()){
        String e9id = Util.null2String(rs.getString("id"));
        String jobgroupname = Util.null2String(rs.getString("jobgroupname"));
        String jobgroupremark = Util.null2String(rs.getString("jobgroupremark"));
        String outkey = Util.null2String(rs.getString("outkey"));

        Map<String,Object> groupMap = new HashMap<String,Object>();
        groupMap.put("e9id",e9id);
        groupMap.put("jobgroupname",jobgroupname);
        groupMap.put("jobgroupremark",jobgroupremark);

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

                Map<String,String> e7Map = E7GroupMap.get(outkey);
                String e7jobgroupname = e7Map.get("e7jobgroupname");
                String e7jobgroupremark = e7Map.get("e7jobgroupremark");
                out.println("e7jobgroupname:"+e7jobgroupname+" e7jobgroupremark:"+e7jobgroupremark+"</br>");
                out.println("jobgroupname:"+jobgroupname+" jobgroupremark:"+jobgroupremark+" outkey:"+outkey+"</br>");
                if(!e7jobgroupname.equals(jobgroupname)
                || !e7jobgroupremark.equals(jobgroupremark)){
                    out.println("outkey:"+outkey+"更新</br>");
                    groupMap.put("e7id",outkey);
                    updateGroupList.add(groupMap);
                }else{
                    out.println("=====outkey:"+outkey+"没有变化的数据"+"</br>");
                }
            }else{
                out.println("outkey:"+outkey+"不在E7里</br>");
                insertGroupList.add(groupMap);
            }
        }else{
            out.println("新增</br>");
            insertGroupList.add(groupMap);
        }
    }
    out.println("updateGroupList:"+updateGroupList.size());
    out.println("insertGroupList:"+insertGroupList.size());

    for(Map<String,Object> item : insertGroupList)
    {
        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+"' ";
            }
        }
        sql = " insert into hrmjobgroups("+keyString+") values("+valueString+")";
        out.println("sql:"+sql+"</br>");
    }

    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) && !"e9id".equals(key)){
                String value = Util.null2String(entry.getValue());
                valuesStr += valuesStr == "" ? key + "='" + value + "'" : "," + key + "='" + value + "'";
            }
        }
        sql = "update hrmjobgroups set " + valuesStr + " where id = '" + e7id + "' ";
        out.println("sql:"+sql+"</br>");
    }
%>

<%!
    public List<String> getE7HrmGroupList(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;
    }

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

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7GroupMap = new HashMap<String,Map<String,String>>();
        String sql = " select id,jobgroupname,jobgroupremark from hrmjobgroups" ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            String jobgroupname = Util.null2String(rsd.getString("jobgroupname"));
            String jobgroupremark = Util.null2String(rsd.getString("jobgroupremark"));

            Map<String,String> groupMap = new HashMap<String,String>();
            groupMap.put("e7jobgroupname",jobgroupname);
            groupMap.put("e7jobgroupremark",jobgroupremark);
            E7GroupMap.put(id,groupMap);
        }
        return E7GroupMap;
    }

%>




