<%@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);
    int id = -1;
    try{
        String sql = " update SequenceIndex set currentid = (select currentid  from SequenceIndex where indexdesc='resourceid'\n" +
                    " )+1 where indexdesc='resourceid' ";
        boolean isTrue = rsd.executeSql(sql);
        out.println("isTrue:"+isTrue);
        if(isTrue){
            sql = " select currentid  from SequenceIndex where indexdesc='resourceid' ";
            rsd.execute(sql);
            if(rsd.next()){
                id = Util.getIntValue(Util.null2String(rsd.getString("currentid")));
            }
        }
    }catch (Exception e){
        out.println(e);
    }

    out.println("id:"+id);

%>


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


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


    public Map<String,String> getE9ResourceOutkey(){

        Map<String,String> E9ResourceMap = new HashMap<String,String>();
        RecordSet rs = new RecordSet();
        String sql =" select id,outkey from hrmresource ";
        rs.executeQuery(sql);
        while(rs.next()) {
            String id = Util.null2String(rs.getString("id"));
            String outkey = Util.null2String(rs.getString("outkey"));
            E9ResourceMap.put(id,outkey);
        }

        return E9ResourceMap;
    }





    public Map<String,String> getE9HrmJobTitlesOutkey(){

        RecordSet rs = new RecordSet();

        Map<String,String> E9JobtitleMap = new HashMap<String,String>();
        String sql = " select id,outkey from hrmjobtitles " ;
        rs.execute(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString("id"));
            String outkey = Util.null2String(rs.getString("outkey"));
            E9JobtitleMap.put(id,outkey);
        }
        return E9JobtitleMap;
    }


    public List<String> getE7HrmJobTitlesList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7JobtitlesList = new ArrayList<String>();
        String sql = " select id from hrmjobtitles " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7JobtitlesList.add(id);
        }
        return E7JobtitlesList;
    }


    public List<String> getE7HrmDepartmentList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7DepartmentList = new ArrayList<String>();
        String sql = " select id from hrmdepartment " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7DepartmentList.add(id);
        }
        return E7DepartmentList;
    }


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


    public List<String> getE7HrmResourceList(String dataResource){

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        List<String> E7ResourceList = new ArrayList<String>();
        String sql = " select id from hrmresource " ;
        rsd.execute(sql);
        while (rsd.next()){
            String id = Util.null2String(rsd.getString("id"));
            E7ResourceList.add(id);
        }
        return E7ResourceList;
    }


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

        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        Map<String,Map<String,String>> E7ResourceMap = new HashMap<String,Map<String,String>>();
        String sql =" select id,workcode,status,dsporder,mobile,jobtitle,password,lastname,loginid,systemlanguage," +
                    " sex,birthday,lastmoddate,createdate,jobactivitydesc,departmentid," +
                    " subcompanyid1,seclevel,accounttype,belongto,managerid,telephone,email,enddate,startdate  " +
                    " from hrmresource " ;
        rsd.execute(sql);
        while (rsd.next()){
            String e7id = Util.null2String(rsd.getString("id")).trim();
            String e7workcode = Util.null2String(rsd.getString("workcode")).trim();
            String e7status = Util.null2String(rsd.getString("status")).trim();
            String e7dsporder = Util.null2String(rsd.getString("dsporder")).trim();
            String e7mobile = Util.null2String(rsd.getString("mobile")).trim();
            String e7jobtitle = Util.null2String(rsd.getString("jobtitle")).trim();
            String e7password = Util.null2String(rsd.getString("password")).trim();
            String e7lastname = Util.null2String(rsd.getString("lastname")).trim();
            String e7loginid = Util.null2String(rsd.getString("loginid")).trim();
            String e7systemlanguage = Util.null2String(rsd.getString("systemlanguage")).trim();
            String e7sex = Util.null2String(rsd.getString("sex")).trim();
            String e7birthday = Util.null2String(rsd.getString("birthday")).trim();
            String e7lastmoddate = Util.null2String(rsd.getString("lastmoddate")).trim();
            String e7createdate = Util.null2String(rsd.getString("createdate")).trim();
            String e7jobactivitydesc = Util.null2String(rsd.getString("jobactivitydesc")).trim();
            String e7departmentid = Util.null2String(rsd.getString("departmentid")).trim();
            String e7subcompanyid1 = Util.null2String(rsd.getString("subcompanyid1")).trim();
            String e7seclevel = Util.null2String(rsd.getString("seclevel")).trim();
            String e7accounttype = Util.null2String(rsd.getString("accounttype")).trim();
            String e7belongto = Util.null2String(rsd.getString("belongto")).trim();
            String e7managerid = Util.null2String(rsd.getString("managerid")).trim();
            String e7telephone = Util.null2String(rsd.getString("telephone")).trim();
            String e7email = Util.null2String(rsd.getString("email")).trim();
            String e7startdate = Util.null2String(rsd.getString("startdate")).trim();
            String e7enddate = Util.null2String(rsd.getString("enddate")).trim();


            if(StringUtils.isBlank(e7systemlanguage)){
                e7systemlanguage = "7";
            }

            if("-1".equals(e7accounttype) || StringUtils.isBlank(e7accounttype)){
                e7accounttype = "0";
            }

            if("-1".equals(e7belongto) || "0".equals(e7belongto)){
                e7belongto = "";
            }

            if("0".equals(e7managerid)){
                e7managerid = "";
            }

            Map<String,String> resourceMap = new HashMap<String,String>();
            resourceMap.put("e7lastname",e7lastname);
            resourceMap.put("e7loginid",e7loginid);
            resourceMap.put("e7password",e7password);
            resourceMap.put("e7sex",e7sex);
            resourceMap.put("e7birthday",e7birthday);
            resourceMap.put("e7mobile",e7mobile);
            resourceMap.put("e7jobtitle",e7jobtitle);
            resourceMap.put("e7departmentid",e7departmentid);
            resourceMap.put("e7subcompanyid1",e7subcompanyid1);
            resourceMap.put("e7seclevel",e7seclevel);
            resourceMap.put("e7workcode",e7workcode);
            resourceMap.put("e7status",e7status);
            resourceMap.put("e7dsporder",e7dsporder);
            resourceMap.put("e7systemlanguage",e7systemlanguage);
            resourceMap.put("e7accounttype",e7accounttype);
            resourceMap.put("e7belongto",e7belongto);
            resourceMap.put("e7managerid",e7managerid);
            resourceMap.put("e7telephone",e7telephone);
            resourceMap.put("e7email",e7email);
            resourceMap.put("e7lastmoddate",e7lastmoddate);
            resourceMap.put("e7createdate",e7createdate);
            resourceMap.put("e7jobactivitydesc",e7jobactivitydesc);
            resourceMap.put("e7startdate",e7startdate);
            resourceMap.put("e7enddate",e7enddate);

            E7ResourceMap.put(e7id,resourceMap);
        }
        return E7ResourceMap;
    }



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


    public String insertE7HrmResourceData(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 hrmresource where textfield1 = '"+uuid+"' ";
            rsd.executeSql(sql);
            if(rsd.next()){
                newE7Id = Util.null2String(rsd.getString("id"));
            }
        }else{
            insert_fail_count++;
        }
        return newE7Id;
    }


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


    public int getE7HrmResourceId(RecordSetDataSource rsd){
        int id = -1;
        try{
            rsd.executeProc("HRMRESOURCEMAXID_GET", "");
            rsd.next();
            id = rsd.getInt(1);
        }catch (Exception e){

        }
        return id;
    }


    /***
     *
     * @param dt1List
     * @param mainid
     */
    private void insertLogDetail6(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_dt6(mainid,ryid,zxyj,zxjg) values("+mainid+",'"+dataid+"','"+zxyj+"','"+zxjg+"')" ;
            bb.writeLog("sql:"+sql);
            boolean istrue = rs.executeUpdate(sql);
            bb.writeLog("istrue:"+istrue);
        }
    }

%>



