<%@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="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="java.text.SimpleDateFormat" %>
<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);
    RecordSet rs = new RecordSet();

    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<String> dataErrorList = new ArrayList<String>();
    List<Map<String,String>> sqlList = new ArrayList<Map<String,String>>();


    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 * from hrmjobactivities where id in(select h.jobactivityid from hrmjobtitles h)";
    out.println("e9--sql:"+sql+"</br>");
    rs.execute(sql);
    while(rs.next()){
        totle_count++;

        String e9id = 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("e9id",e9id);
        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)
                    ){
                        groupMap.put("jobgroupid",E9GroupOutkey);
                        groupMap.put("e7id",outkey);
                        updateGroupList.add(groupMap);
                    }else{
                        unchanged_count++;
                    }
                }else{
                    insertGroupList.add(groupMap);
                }
            }else{
                data_error_count++;
                dataErrorList.add(e9id);
            }
        }else{
            if(E7GroupMap.contains(E9GroupOutkey)){
                insertGroupList.add(groupMap);
            }else{
                data_error_count++;
                dataErrorList.add(e9id);
            }
        }
    }
    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(!"e9id".equals(key) && !"e7id".equals(key)){
                keyString += keyString == "" ? key : ","+key ;
                valueString += valueString == "" ? "'"+value+"'" : ",'"+value+"' ";
            }

        }
        boolean isTrue2 = false;
        String insert_sql = " insert into hrmjobactivities("+keyString+") values("+valueString+")";
        String newE7Id = insertE7Data(rsd,insert_sql,insert_fail_count);
        out.println("newE7Id:"+newE7Id+"</br>");
        if(StringUtils.isNotBlank(newE7Id)){
            sql = "update hrmjobactivities set outkey="+newE7Id+" where id = "+e9id;
            isTrue2 = rs.executeUpdate(sql);
            out.println("sql:"+sql+"  "+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(!"e9id".equals(key) && !"e7id".equals(key)){
                String value = Util.null2String(entry.getValue());
                valuesStr += valuesStr == "" ? key + "='" + value + "'" : "," + key + "='" + value + "'";
            }
        }
        String update_sql = "update hrmjobactivities set " + valuesStr + " where id = '" + e7id + "' ";
        boolean isTrue = updateE7Data(rsd,update_sql);
        out.println("update_sql:"+update_sql+" "+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("dataid",e9id);
        sqlMap.put("zxjg",isTrue ? "true" : "false");
        sqlList.add(sqlMap);
    }

    String zztbjg = "职责共:"+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("zztbjg",zztbjg);
    if(dataErrorList.size() > 0){
        String zzycsj = String.join(",", dataErrorList);
        mainMap.put("zzycsj",zzycsj);
    }
    String mainid =  recordSynMainlog("",mainMap);
    out.println("mainid:"+mainid);
    if(StringUtils.isNotBlank(mainid)){
        insertLogDetail4(mainid,sqlList);
    }

%>

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


    public String insertE7Data(RecordSetDataSource rsd,String insert_sql,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 hrmjobactivities ";
            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){
        BaseBean bb = new BaseBean();
        bb.writeLog("update-sql:"+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
     */
    public void insertLogDetail4(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_dt4(mainid,zzid,zxyj,zxjg) values("+mainid+",'"+dataid+"','"+zxyj+"','"+zxjg+"')" ;
            bb.writeLog("sql:"+sql);
            boolean istrue = rs.executeUpdate(sql);
            bb.writeLog("istrue:"+istrue);
        }
    }

%>




