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

<%
    String dataResource = "E7TEST";
    RecordSetDataSource rsd = new RecordSetDataSource(dataResource);

    int totle_count = 0;
    int update_success_count = 0;
    int update_fail_count = 0;
    int update_exp_count = 0;

    int insert_success_count = 0;
    int insert_fail_count = 0;
    int insert_exp_count = 0;

    int unchanged_count = 0;
    int data_error_count = 0;

    List<Map<String,String>> sqlList = new ArrayList<Map<String,String>>();

    RecordSet rs = new RecordSet();
    Map<String,Map<String,String>> E7GroupMap = getE7HrmGroupList2(dataResource);
    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 ";
    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");
                if(!e7jobgroupname.equals(jobgroupname) || !e7jobgroupremark.equals(jobgroupremark)){
                    groupMap.put("e7id",outkey);
                    updateGroupList.add(groupMap);
                }else{
                    unchanged_count++;
                }
            }else{
                insertGroupList.add(groupMap);
            }
        }else{
            insertGroupList.add(groupMap);
        }
    }
    out.println("updateGroupList:"+updateGroupList.size());
    out.println("insertGroupList:"+insertGroupList.size());

    for(Map<String,Object> item : insertGroupList)
    {
        String e9id = Util.null2String(item.get("e9id"));
        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+"' ";
            }
        }
        boolean isTrue2 = false;
        String insert_sql = " insert into hrmjobgroups("+keyString+") values("+valueString+")";
        String newE7Id = insertE7Data(rsd,insert_sql,insert_fail_count);
        out.println("insert_sql:"+insert_sql+" , newE7Id"+newE7Id+"</br>");
        if(StringUtils.isNotBlank(newE7Id)){
            String up_sql = " update hrmjobgroups set outkey="+newE7Id+" where id = "+e9id;
            isTrue2 = rs.executeUpdate(up_sql);
            out.println("up_sql:"+up_sql+" , isTrue2"+isTrue2+"</br>");
            if(isTrue2){
                insert_success_count++;
            }else{
                insert_exp_count++;
            }
        }else{
            insert_exp_count++;
        }
        Map<String,String> sqlMap = new HashMap<String,String>();
        sqlMap.put("zxyj",insert_sql);
        sqlMap.put("dataid",e9id);
        sqlMap.put("zxjg",isTrue2 ? "true" : "false");
        sqlList.add(sqlMap);

    }

    for(Map<String,Object> item : updateGroupList) {
        String e7id = Util.null2String(item.get("e7id"));
        String e9id = Util.null2String(item.get("e9id"));
        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) && !"e9id".equals(key)){
                String value = Util.null2String(entry.getValue());
                valuesStr += valuesStr == "" ? key + "='" + value + "'" : "," + key + "='" + value + "'";
            }
        }
        String update_sql = " update hrmjobgroups set " + valuesStr + " where id = '" + e7id + "' ";
        boolean isTrue = updateE7Data(rsd,update_sql);
        out.println("update_sql:"+update_sql+" , isTrue:"+isTrue+"</br>");
        if(isTrue){
            update_success_count++;
        }else{
            update_fail_count++;
        }

        Map<String,String> sqlMap = new HashMap<String,String>();
        sqlMap.put("zxyj",update_sql);
        sqlMap.put("fbid",e9id);
        sqlMap.put("zxjg",isTrue ? "true" : "false");
        sqlList.add(sqlMap);
    }

    String zwlxtbjg ="职务类型共:"+totle_count+"行数据,数据一致工:"+unchanged_count+"。新增成功:"+insert_success_count+",新增失败:"+insert_fail_count+",新增异常:"+insert_exp_count+"。"+
                    "更新成功:"+update_success_count+",更新失败:"+update_fail_count+",更新异常:"+update_exp_count+"。"+
                    "数据异常(ID在E7中不存在):"+data_error_count ;

    Map<String,String> mainMap = new HashMap<String,String>();
    mainMap.put("zwlxtbjg",zwlxtbjg);
    String mainid = recordSynMainlog("",mainMap);
    if(StringUtils.isNotBlank(mainid)){
        insertLogDetail3(mainid,sqlList);
    }
%>

<%!


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


    public String insertE7Data(RecordSetDataSource rsd,String insert_sql,int insert_fail_count){
        String newE7Id = "";
        boolean isTrue = rsd.execute(insert_sql);
        if(isTrue){
            String sql = " select max(id) as id from hrmjobgroups ";
            rsd.executeSql(sql);
            if(rsd.next()){
                newE7Id = Util.null2String(rsd.getString("id"));
            }
        }else{
            insert_fail_count++;
        }
        return newE7Id;
    }


    public boolean updateE7Data(RecordSetDataSource rsd,String update_sql){
        boolean isTrue = rsd.execute(update_sql);
        return isTrue;
    }

    private String recordSynMainlog(String id,Map<String,String> mainMap){
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String dataid = "" ;
        String uf_table = "uf_zzjgtbrz";
        String modedatacreater = "1";
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");//设置日期格式
        String modedatacreatedate = sdfDate.format(new Date());
        String modedatacreatetime = sdfTime.format(new Date());
        String modedatacreatertype = "0";

        String formmodeid = "" ;
        String sql =" select k.id from modeinfo k  \n" +
                " inner join workflow_bill l on formid = l.id\n" +
                " where l.tablename = '"+uf_table+"' " ;
        rs.execute(sql);
        if(rs.next()){
            formmodeid = Util.null2String(rs.getString("id"));
        }

        if(StringUtils.isBlank(id)){

            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            String uuid = UUID.randomUUID().toString();
            mainMap.put("uuid",uuid);
            mainMap.put("formmodeid",formmodeid);
            mainMap.put("modedatacreater",modedatacreater);
            mainMap.put("modedatacreatertype",modedatacreatertype);
            mainMap.put("modedatacreatedate",modedatacreatedate);
            mainMap.put("modedatacreatetime",modedatacreatetime);

            String tablename = "" ;
            String tablenvalue = "" ;
            for (Map.Entry<String, String> entry : mainMap.entrySet())
            {
                String fieldname = Util.null2String(entry.getKey()).trim();
                String fieldvalue = Util.null2String(entry.getValue()).replace("'", "''").trim();
                tablename += tablename==""? fieldname :","+fieldname ;
                tablenvalue += tablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
            }
            if(tablename !="" && tablenvalue !="")
            {
                sql = " insert into "+uf_table+"("+tablename+") values ("+ tablenvalue +")";
                boolean boo = rs.execute(sql);
                bb.writeLog("insert-istrue:"+boo);
                if(boo)
                {

                    sql = " select * from "+uf_table+" where uuid='"+uuid+"' " ;
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    if(rs.next()){
                        dataid = Util.null2String(rs.getString("id"));
                    }
                    bb.writeLog("dataid:"+dataid);
                    if(!"".equals(dataid))
                    {
                        modeRightInfo.editModeDataShare(Integer.valueOf(modedatacreater), Integer.parseInt(formmodeid), Integer.parseInt(dataid));
                    }
                }
            }
        }else{
            dataid = id ;
            String valuesStr = "";
            Iterator<Map.Entry<String, String>> it = mainMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                String key = Util.null2String(entry.getKey());
                String value = Util.null2String(entry.getValue());
                valuesStr += valuesStr == "" ? key+"='"+value+"'" : ","+key+"='"+value+"'" ;
            }

            if(StringUtils.isNotBlank(valuesStr)){
                sql = "update "+uf_table+" set " + valuesStr + " where id = "+id+"";
                boolean isTrue = rs.execute(sql);
                bb.writeLog("update-istrue:"+isTrue);
            }
        }
        return dataid;
    }

    /***
     *
     * @param dt1List
     * @param mainid
     */
    private void insertLogDetail3(String mainid, List<Map<String,String>> dt1List){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        for(int i=0;i<dt1List.size();i++){
            Map<String,String> dt1Map = dt1List.get(i);
            String dataid = dt1Map.get("dataid");
            String zxyj = dt1Map.get("zxyj");
            if(StringUtils.isNotBlank(zxyj) && zxyj.contains("'")){
                zxyj = zxyj.replace("'", "''");
            }
            String zxjg = dt1Map.get("zxjg");

            String sql = " insert into uf_zzjgtbrz_dt3(mainid,zwlxid,zxyj,zxjg) values("+mainid+",'"+dataid+"','"+zxyj+"','"+zxjg+"')" ;
            bb.writeLog("sql:"+sql);
            boolean istrue = rs.executeUpdate(sql);
            bb.writeLog("istrue:"+istrue);
        }
    }



%>




