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 HrmSynSubcompanyUtil {

    public String SynHrmSubcompanyData(String dataResource,String subcompanyWhere){

        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> E9SubcompanyOutkey = getE9SubcompanyOutkey();
        Map<String,Map<String,String>> E7SubcompanyMap = getE7HrmSubcompanyList2(dataResource);
        bb.writeLog("E9SubcompanyOutkey:"+E9SubcompanyOutkey.size());
        bb.writeLog("E7SubcompanyMap:"+E7SubcompanyMap.size());

        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,subcompanyname,subcompanydesc,supsubcomid,canceled,subcompanycode,outkey,showorder,tlevel " +
                    " from hrmsubcompany \n" +
                    " where (supsubcomid is null or supsubcomid = -1 or supsubcomid = 0) \n" ;
        rs.execute(sql);
        while(rs.next()) {
            totle_count++;

            String e9Id = Util.null2String(rs.getString("id"));
            topList.add(e9Id);

            String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
            String subcompanydesc = Util.null2String(rs.getString("subcompanydesc"));
            String canceled = Util.null2String(rs.getString("canceled"));
            String subcompanycode = Util.null2String(rs.getString("subcompanycode"));
            String supsubcomid = Util.null2String(rs.getString("supsubcomid")).trim();
            String showorder = Util.null2String(rs.getString("showorder"));
            String tlevel = Util.null2String(rs.getString("tlevel")).trim();
            String outkey = Util.null2String(rs.getString("outkey"));  //E7的hrmsubcompany的ID

            Map<String,Object> E9Map = new HashMap<String, Object>();
            E9Map.put("e9id", e9Id);
            E9Map.put("subcompanyname", subcompanyname);
            E9Map.put("subcompanydesc", subcompanydesc);
            E9Map.put("canceled", canceled);
            E9Map.put("showorder", showorder);
            E9Map.put("subcompanycode", subcompanycode);
            E9Map.put("tlevel", tlevel);

            String supsubcomOutKey = "" ;
            if("0".equals(supsubcomid) || "-1".equals(supsubcomid) || StringUtils.isBlank(supsubcomid+"")){
                supsubcomOutKey = "0";
            }else{
                if(E9SubcompanyOutkey.containsKey(supsubcomid)){
                    supsubcomOutKey = E9SubcompanyOutkey.get(supsubcomid);
                }
            }

            if(StringUtils.isNotBlank(outkey)){
                if(E7SubcompanyMap.containsKey(outkey)){
                    if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){
                        Map<String,String> E7SubcompanyInfo = E7SubcompanyMap.get(outkey);
                        String e7subcompanyname = Util.null2String(E7SubcompanyInfo.get("subcompanyname"));
                        String e7subcompanydesc = Util.null2String(E7SubcompanyInfo.get("subcompanydesc"));
                        String e7supsubcomid = Util.null2String(E7SubcompanyInfo.get("supsubcomid"));
                        String e7canceled = Util.null2String(E7SubcompanyInfo.get("canceled"));
                        String e7subcompanycode = Util.null2String(E7SubcompanyInfo.get("subcompanycode"));
                        String e7tlevel = Util.null2String(E7SubcompanyInfo.get("tlevel"));
                        String e7showorder = Util.null2String(E7SubcompanyInfo.get("showorder"));

                        if(!e7subcompanyname.equals(subcompanyname)
                                || !e7subcompanydesc.equals(subcompanydesc)
                                || !e7supsubcomid.equals(supsubcomOutKey)
                                || !e7canceled.equals(canceled)
                                || !e7subcompanycode.equals(subcompanycode)
                                || !e7tlevel.equals(tlevel)
                                || !e7showorder.equals(showorder)
                        ){
                            E9Map.put("supsubcomid", supsubcomOutKey);
                            E9Map.put("e7id", outkey);
                            topUpdateList.add(E9Map);
                        }else{
                            unchanged_count++;
                        }
                    }else{
                        data_error_count++;
                        dataErrorList.add(e9Id);
                    }
                }else{
                    if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){

                        E9Map.put("supsubcomid", supsubcomOutKey);
                        topInsertList.add(E9Map);
                    }else{
                        data_error_count++;
                        dataErrorList.add(e9Id);
                    }
                }
            }else{
                if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){

                    E9Map.put("supsubcomid", supsubcomOutKey);
                    topInsertList.add(E9Map);
                }else{
                    data_error_count++;
                    dataErrorList.add(e9Id);
                }
            }
        }

        bb.writeLog("topUpdateList:"+topUpdateList.size());
        bb.writeLog("topInsertList:"+topInsertList.size());

        for(Map<String,Object> E9Map : topInsertList){

            String e9id = Util.null2String(E9Map.get("e9id"));

            String uuid = UUID.randomUUID().toString().replace("-","").toUpperCase();
            E9Map.put("url",uuid);
            E9Map.put("companyid",1);//默认公司是1

            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 hrmsubcompany("+keyString+") values("+valueString+")";
                bb.writeLog("insert_sql:"+insert_sql+"</br>");
                String newE7Id = insertE7Data(rsd,insert_sql,uuid,insert_fail_count);
                bb.writeLog("newE7Id:"+newE7Id+"</br>");
                if(StringUtils.isNotBlank(newE7Id)){
                    sql = "update hrmsubcompany set outkey="+newE7Id+" where id = "+e9id;
                    isTrue2 = rs.executeUpdate(sql);
                    bb.writeLog("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);
            }catch (Exception e){
                bb.writeLog("e:"+e);
                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 hrmsubcompany set "+valuesStr+" where id = '"+e7id+"' ";
                boolean isTrue = updateE7SubcompanyData(rsd,update_sql);
                bb.writeLog("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",sql);
                sqlMap.put("dataid",update_sql);
                sqlMap.put("zxjg",isTrue ? "true" : "false");
                sqlList.add(sqlMap);
            }catch (Exception e){
                bb.writeLog("e:"+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 subcompanyid : topList){
            if(StringUtils.isNotBlank(subcompanyid)){
                sql =   " select id,subcompanyname,subcompanydesc,supsubcomid,showorder,\n" +
                        " canceled,subcompanycode,outkey,tlevel from hrmsubcompany \n" +
                        " where 1=1 " + subcompanyWhere +
                        " connect by prior  id = supsubcomid \n" +
                        " start with supsubcomid = "+subcompanyid;
                rs.executeQuery(sql);
                while (rs.next()){

                    totle_count++;

                    String e9Id = Util.null2String(rs.getString("id"));
                    String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
                    String subcompanydesc = Util.null2String(rs.getString("subcompanydesc"));
                    String canceled = Util.null2String(rs.getString("canceled"));
                    String subcompanycode = Util.null2String(rs.getString("subcompanycode"));
                    String supsubcomid = Util.null2String(rs.getString("supsubcomid")).trim();
                    String showorder = Util.null2String(rs.getString("showorder")).trim();
                    String outkey = Util.null2String(rs.getString("outkey"));  //E7的hrmsubcompany的ID
                    String tlevel = Util.null2String(rs.getString("tlevel"));

                    Map<String,Object> E9Map = new HashMap<String, Object>();
                    E9Map.put("e9id", e9Id);
                    E9Map.put("subcompanyname", subcompanyname);
                    E9Map.put("subcompanydesc", subcompanydesc);
                    E9Map.put("canceled", canceled);
                    E9Map.put("showorder", showorder);
                    E9Map.put("subcompanycode", subcompanycode);
                    E9Map.put("tlevel", tlevel);

                    String supsubcomOutKey = "" ;
                    if("0".equals(supsubcomid) || "-1".equals(supsubcomid) || StringUtils.isBlank(supsubcomid+"")){
                        supsubcomOutKey = "0";
                    }else{
                        if(E9SubcompanyOutkey.containsKey(supsubcomid)){
                            supsubcomOutKey = E9SubcompanyOutkey.get(supsubcomid);
                        }
                    }

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

                            if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){
                                Map<String,String> E7SubcompanyInfo = E7SubcompanyMap.get(outkey);
                                String e7subcompanyname = Util.null2String(E7SubcompanyInfo.get("subcompanyname"));
                                String e7subcompanydesc = Util.null2String(E7SubcompanyInfo.get("subcompanydesc"));
                                String e7supsubcomid = Util.null2String(E7SubcompanyInfo.get("supsubcomid"));
                                String e7canceled = Util.null2String(E7SubcompanyInfo.get("canceled"));
                                String e7subcompanycode = Util.null2String(E7SubcompanyInfo.get("subcompanycode"));
                                String e7tlevel = Util.null2String(E7SubcompanyInfo.get("tlevel"));
                                String e7showorder = Util.null2String(E7SubcompanyInfo.get("showorder"));

                                if(!e7subcompanyname.equals(subcompanyname)
                                        || !e7subcompanydesc.equals(subcompanydesc)
                                        || !e7supsubcomid.equals(supsubcomOutKey)
                                        || !e7canceled.equals(canceled)
                                        || !e7subcompanycode.equals(subcompanycode)
                                        || !e7tlevel.equals(tlevel)
                                        || !e7showorder.equals(showorder)
                                ){
                                    E9Map.put("supsubcomid", supsubcomOutKey);
                                    E9Map.put("e7id", outkey);
                                    updateList.add(E9Map);
                                }else{
                                    unchanged_count++;
                                }
                            }else{
                                data_error_count++;
                                dataErrorList.add(e9Id);
                            }
                        }else{
//                        data_error_count++;
//                        dataErrorList.add(e9Id);

                            if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){

                                E9Map.put("supsubcomid", supsubcomOutKey);
                                insertList.add(E9Map);
                            }else{
                                data_error_count++;
                                dataErrorList.add(e9Id);
                            }
                        }
                    }else{
                        if(StringUtils.isNotBlank(supsubcomOutKey) && ("0".equals(supsubcomOutKey) || E7SubcompanyMap.containsKey(supsubcomOutKey))){

                            E9Map.put("supsubcomid", supsubcomOutKey);
                            insertList.add(E9Map);
                        }else{
                            data_error_count++;
                            dataErrorList.add(e9Id);
                        }
                    }
                }
            }
        }

        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("url",uuid);
            E9Map.put("companyid",1);//默认公司是1

            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(!"e9id".equals(key) && !"e7id".equals(key)){
                    keyString += keyString == "" ? key : ","+key ;
                    valueString += valueString == "" ? "'"+value+"'" : ",'"+value+"' ";
                }
            }

            try {
                boolean isTrue2 = false;

                String insert_sql = " insert into hrmsubcompany("+keyString+") values("+valueString+")";


                String newE7Id = insertE7Data(rsd,insert_sql,uuid,insert_fail_count);
                bb.writeLog("insert_sql:"+insert_sql+"  "+newE7Id+"</br>");

                if(StringUtils.isNotBlank(newE7Id)){
                    sql = " update hrmsubcompany set outkey="+newE7Id+" where id = "+e9id;
                    isTrue2 = rs.executeUpdate(sql);
                    bb.writeLog("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);
            }catch (Exception e){
                bb.writeLog("e:"+e);
                insert_exp_count++;
            }
        }

        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+"'" ;
                }
            }

            try{
                String update_sql = "update hrmsubcompany set "+valuesStr+" where id = '"+e7id+"' ";
                boolean isTrue = updateE7SubcompanyData(rsd,update_sql);
                bb.writeLog("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);
            }catch (Exception e){
                bb.writeLog("e:"+e);
                update_exp_count++;
            }
        }

        String mainid = "";
        try{
            String fbtbjg = "分部共:"+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("fbtbjg",fbtbjg);
            if(dataErrorList.size() > 0){
                if(dataErrorList.size()  <= 150){
                    String bmycsj = String.join(",", dataErrorList);
                    mainMap.put("fbycsj",bmycsj);
                }else{
                    String bmycsj = String.join(",", dataErrorList.subList(0,150));
                    mainMap.put("fbycsj",bmycsj);
                }
            }
            mainid = recordSynMainlog("",mainMap);
            if(StringUtils.isNotBlank(mainid)){
                insertLogDetail1(mainid,sqlList);
            }
        }catch (Exception e){
            bb.writeLog(e);
        }


        return mainid;
    }


    private String insertE7Data(RecordSetDataSource rsd,String insert_sql,String uuid,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 id from hrmsubcompany where url = '"+uuid+"' ";
            rsd.executeSql(sql);
            if(rsd.next()){
                newE7Id = Util.null2String(rsd.getString("id"));
            }
        }else{
            insert_fail_count++;
        }
        return newE7Id;
    }


    private boolean updateE7SubcompanyData(RecordSetDataSource rsd,String update_sql){
        BaseBean bb = new BaseBean();
        bb.writeLog("update-sql:"+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;
    }

    /***
     *
     * @return
     */
    private List<String> getE7HrmSubcompanyList(String dataResource){
        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7SubcompanyList = new ArrayList<String>();
        Map<String,Map<String,String>> E7SubcompanyMap = new HashMap<String,Map<String,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 Map<String,Map<String,String>> getE7HrmSubcompanyList2(String dataResource){
        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7SubcompanyMap = new HashMap<String,Map<String,String>>();
        String sql = " select id,subcompanyname,subcompanydesc,supsubcomid,canceled,subcompanycode,tlevel,showorder from hrmsubcompany " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            String subcompanyname = Util.null2String(rsd.getString("subcompanyname"));
            String subcompanydesc = Util.null2String(rsd.getString("subcompanydesc"));
            String supsubcomid = Util.null2String(rsd.getString("supsubcomid"));
            String canceled = Util.null2String(rsd.getString("canceled"));

            String subcompanycode = Util.null2String(rsd.getString("subcompanycode"));

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


            Map<String,String> subcompanyMap = new HashMap<>();
            subcompanyMap.put("id",id);
            subcompanyMap.put("subcompanyname",subcompanyname);
            subcompanyMap.put("subcompanydesc",subcompanydesc);
            subcompanyMap.put("supsubcomid",supsubcomid);
            subcompanyMap.put("canceled",canceled);
            subcompanyMap.put("subcompanycode",subcompanycode);
            subcompanyMap.put("tlevel",tlevel);
            subcompanyMap.put("showorder",showorder);
            E7SubcompanyMap.put(id,subcompanyMap);

        }
        return E7SubcompanyMap;
    }


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