package weaver.interfaces.zjrb.job;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.SimpleDateFormat;
import java.util.*;

public class HrmSynDepartmentUtil {

    public void SynHrmDepartmentData(String dataResource,String mainid,String departmentwhere){

        BaseBean bb = new BaseBean();
        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,String> subCompanyOutkeyMap = getE9SubcompanyOutkey();
        Map<String,String> departmentOutkeyMap = getE9DepartmentOutkey();
        Map<String,Map<String,String>> E7DepartmentMap = getE7DepartmentData(dataResource);
        List<String> E7SubcompanyList = getE7HrmSubcompanyList(dataResource);

        bb.writeLog("subCompanyOutkeyMap:"+subCompanyOutkeyMap.size()+"</br>");
        bb.writeLog("departmentOutkeyMap:"+departmentOutkeyMap.size()+"</br>");
        bb.writeLog("E7DepartmentMap:"+E7DepartmentMap.size()+"</br>");
        bb.writeLog("E7SubcompanyList:"+E7SubcompanyList.size()+"</br>");

        List<Map<String,Object>> topInsertList = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> topUpdateList = new ArrayList<Map<String,Object>>();
        List<String> topList = new ArrayList<>();
        String sql =" select id,departmentmark,departmentname,subcompanyid1,nvl(supdepid,0) as supdepid,allsupdepid,canceled,departmentcode,outkey,tlevel,showorderoftree,showorder\n" +
                " from hrmdepartment \n" +
                " where nvl(supdepid,0) = 0  \n" ;
        rs.execute(sql);
        while(rs.next()) {
            totle_count++;
            String e9id = Util.null2String(rs.getString("id"));
            topList.add(e9id);

            String departmentmark = Util.null2String(rs.getString("departmentmark"));
            String departmentname = Util.null2String(rs.getString("departmentname"));
            String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
            String supdepid = Util.null2String(rs.getString("supdepid"));
            String allsupdepid = Util.null2String(rs.getString("allsupdepid"));
            String canceled = Util.null2String(rs.getString("canceled"));
            String departmentcode = Util.null2String(rs.getString("departmentcode"));
            String outkey = Util.null2String(rs.getString("outkey"));  //E7的hrmdepartment的ID
            String tlevel = Util.null2String(rs.getString("tlevel"));
            String showorder = Util.null2String(rs.getString("showorder"));

            Map<String,Object> E9Map = new HashMap<String, Object>();
            E9Map.put("e9id", e9id);
            E9Map.put("departmentname", departmentname);
            E9Map.put("departmentmark", departmentmark);
            E9Map.put("departmentcode", departmentcode);
            E9Map.put("tlevel", tlevel);
            E9Map.put("showorder", showorder);
            E9Map.put("canceled", canceled);

            String subcompanyOutKey = "" ;
            if(subCompanyOutkeyMap.containsKey(subcompanyid1)){
                subcompanyOutKey = subCompanyOutkeyMap.get(subcompanyid1);
            }

            String supdepidOutKey = "";
            if(StringUtils.isNotBlank(supdepid)){
                if("0".equals(supdepid) || "-1".equals(supdepid) || StringUtils.isBlank(supdepid)){
                    supdepidOutKey = "0";
                }else{
                    if(departmentOutkeyMap.containsKey(supdepid)){
                        supdepidOutKey = departmentOutkeyMap.get(supdepid);
                    }
                }
            }

            String allsupdepidOutKey = "";
            if(StringUtils.isNotBlank(allsupdepid)){
                String[] supdepids = allsupdepid.split(",");
                for(int i=0;i<supdepids.length;i++){
                    if(StringUtils.isNotBlank(supdepids[i])){
                        if(departmentOutkeyMap.containsKey(supdepids[i])){
                            String supdepid_OutKey = departmentOutkeyMap.get(supdepids[i]);
                            allsupdepidOutKey += StringUtils.isBlank(allsupdepidOutKey) ? supdepid_OutKey : ","+supdepid_OutKey ;
                        }
                    }
                }
            }

            if(StringUtils.isNotBlank(outkey)){
                if(E7DepartmentMap.containsKey(outkey)){
                    if(StringUtils.isNotBlank(subcompanyOutKey) && E7SubcompanyList.contains(subcompanyOutKey) ) {
                        if (StringUtils.isNotBlank(supdepidOutKey) && ("0".equals(supdepidOutKey) || E7DepartmentMap.containsKey(supdepidOutKey))){

                            Map<String,String> e7DeptMap = E7DepartmentMap.get(outkey);
                            String e7departmentmark = Util.null2String(e7DeptMap.get("e7departmentmark"));
                            String e7departmentname = Util.null2String(e7DeptMap.get("e7departmentname"));
                            String e7subcompanyid1 = Util.null2String(e7DeptMap.get("e7subcompanyid1"));
                            String e7supdepid = Util.null2String(e7DeptMap.get("e7supdepid"));
                            String e7allsupdepid = Util.null2String(e7DeptMap.get("e7allsupdepid"));
                            String e7canceled = Util.null2String(e7DeptMap.get("e7canceled"));
                            String e7departmentcode = Util.null2String(e7DeptMap.get("e7departmentcode"));
                            String e7tlevel = Util.null2String(e7DeptMap.get("e7tlevel"));
                            String e7showorder = Util.null2String(e7DeptMap.get("e7showorder"));

                            if(!e7departmentmark.equals(departmentmark)
                                    || !e7departmentname.equals(departmentname)
                                    || !e7subcompanyid1.equals(subcompanyOutKey)
                                    || !e7supdepid.equals(supdepidOutKey)
                                    || !e7canceled.equals(canceled)
                                    || !e7departmentcode.equals(departmentcode)
                                    || !e7allsupdepid.equals(allsupdepidOutKey)
                                    || !e7tlevel.equals(tlevel)
                                    || !e7showorder.equals(showorder)
                            ){

                                E9Map.put("subcompanyid1", subcompanyOutKey);
                                E9Map.put("supdepid", supdepidOutKey);
                                E9Map.put("e7id", outkey);
                                topUpdateList.add(E9Map);
                            }else{
                                unchanged_count++;
                            }
                        }else{
                            dataErrorList.add(e9id);
                            data_error_count++;
                        }
                    }else{
                        dataErrorList.add(e9id);
                        data_error_count++;
                    }
                }else{
                    if(StringUtils.isNotBlank(subcompanyOutKey) && E7SubcompanyList.contains(subcompanyOutKey) ) {
                        if (StringUtils.isNotBlank(supdepidOutKey) && ("0".equals(supdepidOutKey) || E7DepartmentMap.containsKey(supdepidOutKey))){
                            E9Map.put("subcompanyid1", subcompanyOutKey);
                            E9Map.put("supdepid", supdepidOutKey);
                            topInsertList.add(E9Map);
                        }else{
                            dataErrorList.add(e9id);
                            data_error_count++;
                        }
                    }else{
                        dataErrorList.add(e9id);
                        data_error_count++;
                    }
                }
            }else{

                if(StringUtils.isNotBlank(subcompanyOutKey) && E7SubcompanyList.contains(subcompanyOutKey) ) {
                    if (StringUtils.isNotBlank(supdepidOutKey) && ("0".equals(supdepidOutKey) || E7DepartmentMap.containsKey(supdepidOutKey))){

                        E9Map.put("subcompanyid1", subcompanyOutKey);
                        E9Map.put("supdepid", supdepidOutKey);
                        topInsertList.add(E9Map);
                    }else{
                        dataErrorList.add(e9id);
                        data_error_count++;
                    }
                }else{
                    dataErrorList.add(e9id);
                    data_error_count++;
                }
            }
        }

        bb.writeLog("topUpdateList:"+topUpdateList.size()+"</br>");
        bb.writeLog("topInsertList:"+topInsertList.size()+"</br>");

        for(Map<String,Object> E9Map : topInsertList){
            String e9id = Util.null2String(E9Map.get("e9id"));

            String uuid = UUID.randomUUID().toString().replace("-","").toUpperCase();
            E9Map.put("uuid",uuid);

            String keyString = "";
            String valueString = "";
            Iterator<Map.Entry<String, Object>> it = E9Map.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+"' ";
                }
            }
            try{
                boolean isTrue2 = false ;
                String insert_sql = " insert into hrmdepartment("+keyString+") values("+valueString+")";

                String newE7Id =  insertE7DepartmentData(rsd,insert_sql,uuid,insert_fail_count);
                bb.writeLog("insert_sql:"+insert_sql+"   newE7Id:"+newE7Id+"</br>");
                if(StringUtils.isNotBlank(newE7Id)){
                    String up_sql = " update hrmdepartment set outkey="+newE7Id+" where id = "+e9id;
                    isTrue2 = rs.executeUpdate(up_sql);
                    bb.writeLog("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);

            }catch (Exception e){
                bb.writeLog("");
                insert_exp_count++;
            }
        }

        for(Map<String,Object> E9Map : topUpdateList){
            String e7id = Util.null2String(E9Map.get("e7id"));
            String e9id = Util.null2String(E9Map.get("e9id"));

            String valuesStr = "";
            Iterator<Map.Entry<String, Object>> it = E9Map.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+"'" ;
                }
            }
            try{
                String update_sql = " update hrmdepartment set "+valuesStr+" where id = '"+e7id+"' ";
                boolean isTrue = updateE7DepartmentData(rsd,update_sql);
                bb.writeLog("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("dataid",e9id);
                sqlMap.put("zxjg",isTrue ? "true" : "false");
                sqlList.add(sqlMap);

            }catch (Exception e){
                update_exp_count++;
            }
        }
        List<Map<String,Object>> insertList = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> updateList = new ArrayList<Map<String,Object>>();
        for(String departmentid : topList){
            if(StringUtils.isNotBlank(departmentid)){
                sql =   " select id,departmentmark,departmentname,subcompanyid1,nvl(supdepid,0) supdepid,allsupdepid,canceled,departmentcode,outkey,tlevel,showorderoftree,showorder \n" +
                        " from hrmdepartment  \n" +
                        " where 1=1 " + departmentwhere +
                        " connect by prior id = supdepid \n" +
                        " start with supdepid = "+departmentid;

                rs.executeQuery(sql);
                while (rs.next()){
                    totle_count++;

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

                    String departmentmark = Util.null2String(rs.getString("departmentmark"));
                    String departmentname = Util.null2String(rs.getString("departmentname"));
                    String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
                    String supdepid = Util.null2String(rs.getString("supdepid"));
                    String allsupdepid = Util.null2String(rs.getString("allsupdepid"));
                    String canceled = Util.null2String(rs.getString("canceled"));
                    String departmentcode = Util.null2String(rs.getString("departmentcode"));
                    String outkey = Util.null2String(rs.getString("outkey"));  //E7的hrmsubcompany的ID

                    String tlevel = Util.null2String(rs.getString("tlevel"));
                    String showorder = Util.null2String(rs.getString("showorder"));

                    Map<String,Object> E9Map = new HashMap<String, Object>();
                    E9Map.put("e9id", e9id);
                    E9Map.put("departmentname", departmentname);
                    E9Map.put("departmentmark", departmentmark);
                    E9Map.put("departmentcode", departmentcode);
                    E9Map.put("tlevel", tlevel);
                    E9Map.put("showorder", showorder);
                    E9Map.put("canceled", canceled);

                    String subcompanyOutKey = "" ;
                    if(subCompanyOutkeyMap.containsKey(subcompanyid1)){
                        subcompanyOutKey = subCompanyOutkeyMap.get(subcompanyid1);
                    }

                    String supdepidOutKey = "";
                    if(StringUtils.isNotBlank(supdepid)){
                        if("0".equals(supdepid) || "-1".equals(supdepid) || StringUtils.isBlank(supdepid)){
                            supdepidOutKey = "0";
                        }else{
                            if(departmentOutkeyMap.containsKey(supdepid)){
                                supdepidOutKey = departmentOutkeyMap.get(supdepid);
                            }
                        }
                    }else{
                        supdepidOutKey = "0";
                    }

                    String allsupdepidOutKey = "";
                    if(StringUtils.isNotBlank(allsupdepid)){
                        String[] supdepids = allsupdepid.split(",");
                        for(int i=0;i<supdepids.length;i++){
                            if(StringUtils.isNotBlank(supdepids[i])){
                                if(departmentOutkeyMap.containsKey(supdepids[i])){
                                    String supdepid_OutKey = departmentOutkeyMap.get(supdepids[i]);
                                    allsupdepidOutKey += StringUtils.isBlank(allsupdepidOutKey) ? supdepid_OutKey : ","+supdepid_OutKey ;
                                }
                            }
                        }
                    }

                    if(StringUtils.isNotBlank(outkey)){
                        if(E7DepartmentMap.containsKey(outkey)){
                            if(StringUtils.isNotBlank(subcompanyOutKey) && E7SubcompanyList.contains(subcompanyOutKey) ) {
                                if (StringUtils.isNotBlank(supdepidOutKey) && ("0".equals(supdepidOutKey) || E7DepartmentMap.containsKey(supdepidOutKey))){

                                    Map<String,String> e7DeptMap = E7DepartmentMap.get(outkey);
                                    String e7departmentmark = Util.null2String(e7DeptMap.get("e7departmentmark"));
                                    String e7departmentname = Util.null2String(e7DeptMap.get("e7departmentname"));
                                    String e7subcompanyid1 = Util.null2String(e7DeptMap.get("e7subcompanyid1"));
                                    String e7supdepid = Util.null2String(e7DeptMap.get("e7supdepid"));
                                    String e7allsupdepid = Util.null2String(e7DeptMap.get("e7allsupdepid"));
                                    String e7canceled = Util.null2String(e7DeptMap.get("e7canceled"));
                                    String e7departmentcode = Util.null2String(e7DeptMap.get("e7departmentcode"));
                                    String e7tlevel = Util.null2String(e7DeptMap.get("e7tlevel"));
                                    String e7showorder = Util.null2String(e7DeptMap.get("e7showorder"));

                                    if(!e7departmentmark.equals(departmentmark)
                                            || !e7departmentname.equals(departmentname)
                                            || !e7subcompanyid1.equals(subcompanyOutKey)
                                            || !e7supdepid.equals(supdepidOutKey)
                                            || !e7canceled.equals(canceled)
                                            || !e7departmentcode.equals(departmentcode)
                                            || !e7allsupdepid.equals(allsupdepidOutKey)
                                            || !e7tlevel.equals(tlevel)
                                            || !e7showorder.equals(showorder)
                                    ){

                                        E9Map.put("subcompanyid1", subcompanyOutKey);
                                        E9Map.put("supdepid", supdepidOutKey);
                                        E9Map.put("e7id", outkey);
                                        updateList.add(E9Map);
                                    }else{
                                        unchanged_count++;
                                    }
                                }else{
                                    dataErrorList.add(e9id);
                                    data_error_count++;
                                }
                            }else{
                                dataErrorList.add(e9id);
                                data_error_count++;
                            }
                        }else{
                            if(StringUtils.isNotBlank(subcompanyOutKey) && E7SubcompanyList.contains(subcompanyOutKey) ) {
                                if (StringUtils.isNotBlank(supdepidOutKey) && ("0".equals(supdepidOutKey) || E7DepartmentMap.containsKey(supdepidOutKey))){
                                    E9Map.put("subcompanyid1", subcompanyOutKey);
                                    E9Map.put("supdepid", supdepidOutKey);
                                    insertList.add(E9Map);
                                }else{
                                    dataErrorList.add(e9id);
                                    data_error_count++;
                                }
                            }else{
                                dataErrorList.add(e9id);
                                data_error_count++;
                            }
                        }
                    }else{

                        if(StringUtils.isNotBlank(subcompanyOutKey) && E7SubcompanyList.contains(subcompanyOutKey) ) {
                            if (StringUtils.isNotBlank(supdepidOutKey) && ("0".equals(supdepidOutKey) || E7DepartmentMap.containsKey(supdepidOutKey))){

                                E9Map.put("subcompanyid1", subcompanyOutKey);
                                E9Map.put("supdepid", supdepidOutKey);
                                insertList.add(E9Map);
                            }else{
                                dataErrorList.add(e9id);
                                data_error_count++;
                            }
                        }else{
                            dataErrorList.add(e9id);
                            data_error_count++;
                        }
                    }
                }
            }
        }

        bb.writeLog("insertList:"+insertList.size()+"</br>");
        bb.writeLog("updateList:"+updateList.size()+"</br>");

        for(Map<String,Object> E9Map : insertList){
            String e9id = Util.null2String(E9Map.get("e9id"));
            String uuid = UUID.randomUUID().toString().replace("-","").toUpperCase();
            E9Map.put("uuid",uuid);

            String keyString = "";
            String valueString = "";
            Iterator<Map.Entry<String, Object>> it = E9Map.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 hrmdepartment("+keyString+") values("+valueString+")";
            String newE7Id =  insertE7DepartmentData(rsd,insert_sql,uuid,insert_fail_count);
            bb.writeLog("insert_sql:"+insert_sql+"   newE7Id:"+newE7Id+"</br>");
            if(StringUtils.isNotBlank(newE7Id)){
                String up_sql = " update hrmdepartment set outkey="+newE7Id+" where id = "+e9id;
                isTrue2 = rs.executeUpdate(up_sql);
                bb.writeLog("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> E9Map : updateList){
            String e7id = Util.null2String(E9Map.get("e7id"));
            String e9id = Util.null2String(E9Map.get("e9id"));

            String valuesStr = "";
            Iterator<Map.Entry<String, Object>> it = E9Map.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 hrmdepartment set "+valuesStr+" where id = '"+e7id+"' ";

            boolean isTrue = updateE7DepartmentData(rsd,update_sql);
            bb.writeLog("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("dataid",e9id);
            sqlMap.put("zxjg",isTrue ? "true" : "false");
            sqlList.add(sqlMap);
        }

        try{
            String bmtbjg = "部门共:"+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("bmtbjg",bmtbjg);
            if(dataErrorList.size() > 0 ){

                if(dataErrorList.size()  <= 150){
                    String bmycsj = String.join(",", dataErrorList);
                    mainMap.put("bmycsj",bmycsj);
                }else{
                    String bmycsj = String.join(",", dataErrorList.subList(0,150));
                    mainMap.put("bmycsj",bmycsj);
                }

            }

            recordSynMainlog(mainid,mainMap);
            if(StringUtils.isNotBlank(mainid)){
                insertLogDetail2(mainid,sqlList);
            }
        }catch (Exception e){
            bb.writeLog(e);
        }

    }


    private String insertE7DepartmentData(RecordSetDataSource rsd,String insert_sql,String uuid,int insert_fail_count){
        String newE7Id = "";
        boolean isTrue = rsd.execute(insert_sql);
        if(isTrue){
            String sql = " select id from hrmdepartment where uuid = '"+uuid+"' ";
            rsd.executeSql(sql);
            if(rsd.next()){
                newE7Id = Util.null2String(rsd.getString("id"));
            }
        }else{
            insert_fail_count++;
        }
        return newE7Id;
    }


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

    private Map<String,String> getE9SubcompanyOutkey(){

        RecordSet rs = new RecordSet();
        Map<String,String> outkeyMap = new HashMap<String,String>();
        String sql = " select id,outkey from hrmsubcompany " ;
        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;
    }


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



    private Map<String,Map<String,String>> getE7DepartmentData(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7DepartmentMap = new HashMap<String,Map<String,String>>();
        String sql = " select id,departmentmark,departmentname,subcompanyid1,supdepid,allsupdepid,canceled,departmentcode,tlevel,showorder from hrmdepartment " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            String e7departmentmark = Util.null2String(rsd.getString("departmentmark"));
            String e7departmentname = Util.null2String(rsd.getString("departmentname"));
            String e7subcompanyid1 = Util.null2String(rsd.getString("subcompanyid1"));
            String e7supdepid = Util.null2String(rsd.getString("supdepid"));
            String e7allsupdepid = Util.null2String(rsd.getString("allsupdepid"));
            String e7canceled = Util.null2String(rsd.getString("canceled"));
            String e7departmentcode = Util.null2String(rsd.getString("departmentcode"));
            String e7tlevel = Util.null2String(rsd.getString("tlevel"));
            String e7showorder = Util.null2String(rsd.getString("showorder"));

            Map<String,String> deptMap= new HashMap<String,String>();
            deptMap.put("e7departmentmark",e7departmentmark);
            deptMap.put("e7departmentname",e7departmentname);
            deptMap.put("e7subcompanyid1",e7subcompanyid1);
            deptMap.put("e7supdepid",e7supdepid);
            deptMap.put("e7allsupdepid",e7allsupdepid);
            deptMap.put("e7canceled",e7canceled);
            deptMap.put("e7departmentcode",e7departmentcode);
            deptMap.put("e7tlevel",e7tlevel);
            deptMap.put("e7showorder",e7showorder);

            E7DepartmentMap.put(id,deptMap);
        }
        return E7DepartmentMap;
    }

    /***
     *
     * @return
     */
    private List<String> getE7HrmSubcompanyList(String dataResource){
        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7SubcompanyList = new ArrayList<String>();
        String sql = " select id from hrmsubcompany " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7SubcompanyList.add(id);
        }
        return E7SubcompanyList;
    }

    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 insertLogDetail2(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_dt2(mainid,bmid,zxyj,zxjg) values("+mainid+",'"+dataid+"','"+zxyj+"','"+zxjg+"')" ;
            bb.writeLog("sql:"+sql);
            boolean istrue = rs.executeUpdate(sql);
            bb.writeLog("istrue:"+istrue);
        }
    }

}
