package weaver.interfaces.htsc.GlobatTools;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.wechat.util.Utils;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public  class JAVATools {

    //获取当前的时间(年月日时分秒)
    public static String getDatetime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
        return sdf.format(new Date());
    }
    //end

    //获取所有预算科目
    public static Map<String, String> getAllBudget(){
        String budgetSql = " select distinct t.yskmbm,t.yskmmc,t.sjyskm from uf_kmpz t ";
        Map<String, String> budgetMap = new HashMap<>();
        RecordSet recordSet = new RecordSet();
        try {
            recordSet.execute(budgetSql);
            while (recordSet.next()){
                budgetMap.put(recordSet.getString("yskmbm"), recordSet.getString("yskmmc"));
            }
        }catch (Exception e){
            throw new RuntimeException();
        }
        return budgetMap;
    }

    //数组去重
    public static String disinctrlist(String[] array) {
        if (array == null || array.length < 0) {
            return "";
        }
        String ryvalue = "";
        List<String> listarray = Arrays.asList(array);
        List list = (List) listarray.stream().distinct().collect(Collectors.toList());
        ryvalue = String.join(",", list);
        return ryvalue;
    }

    //获取主表表名
    public static String getMaintable(String reqid) {
        RecordSet rs = new RecordSet();
        String sql = " select abs(t.formid) as formid \n" +
                " from workflow_base t\n" +
                " inner join workflow_requestbase l on l.workflowid = t.id\n" +
                " where l.requestid=" + reqid + "";
        rs.execute(sql);
        String formid = "formtable_main_";

        if (rs.next()) {
            formid = formid + com.weaver.general.Util.getIntValues(rs.getString("formid"));
        }
        return formid;
    }

    public static Map<String,Object> gettablevalue(String requestid){
        if("".equals(requestid)){
            return  null;
        }
        String tablename=getMaintable(requestid);
        if("".equals(tablename)){
            return null;
        }
        String sql="select * from "+ tablename+ " where 1=1 ";
        Map<String,String> map=new HashMap<>();
        map.put("requestid",requestid);
        return GetTableToMap(sql,map);
    }

    public static Map<String,Object> gettablevalue(String tablename,Map<String,String> map){
        if(StringUtils.isBlank(tablename)){
            return  null;
        }

        String sql="select * from "+ tablename+ " where 1=1 ";

        return GetTableToMap(sql,map);
    }

    public static String getrequestname(String requestid){
        if("".equals(requestid)){
            return "";
        }
        Map<String,String> params=new HashMap<>();
        params.put("requestid",requestid);
        String sql="select requestname from workflow_requestbase where 1=1";
        Map<String,Object> retmap= GetTableToMap(sql,params);
        if(retmap==null || retmap.size()<=0){
            return "";
        }
        return Util.null2String(retmap.get("requestname"));
    }

    //获取主表的id
    public static String getMaintableid(String reqid) {
        String tablename = getMaintable(reqid);
        RecordSet rs = new RecordSet();
        String id = "";
        String sql = "Select id from  " + tablename + " where requestid=" + reqid;
        System.out.println("获取主表的sql:" + sql);
        rs.execute(sql);
        if (rs.next()) {
            id = com.weaver.general.Util.null2String(rs.getString("id"));
        }
        return id;
    }

    //获取主表的number
    public static String getmainunnumber(String reqid) {
        RecordSet rs = new RecordSet();
        String unnumber = "";
        String sql = " select t.unnumber as unnumber \n" +
                " from workflow_base t\n" +
                " inner join workflow_requestbase l on l.workflowid = t.id\n" +
                " where   rownum=1 and l.requestid=" + reqid + "";
        rs.execute(sql);


        if (rs.next()) {
            unnumber = com.weaver.general.Util.getIntValues(rs.getString("unnumber"));
        }
        return unnumber;
    }


    //增加方法。根据编号来获取主键id
    public static String GetUnumbertotable(String unnumber) {
        RecordSet rs = new RecordSet();
        String sql = " select abs(t.formid) as formid \n" +
                " from workflow_base t\n" +
                //  " inner join workflow_requestbase l on l.workflowid = t.id\n" +
                " where t.unnumber='" + unnumber + "'";
        rs.execute(sql);
        String formid = "formtable_main_";

        if (rs.next()) {
            formid = formid + com.weaver.general.Util.getIntValues(rs.getString("formid"));
        }
        return formid;
    }

    /**
     * 根据编号和unnumber获取id
     */
    public static String GetUnumbertotableid(String bh, String unnumber) {
        RecordSet rs = new RecordSet();
        String tablename = GetUnumbertotable(unnumber);
        String value = "";
        String sql = "select * from " + tablename + " where lcbh like '%" + bh + "%'";
        rs.execute(sql);
        if (rs.next()) {
            value = Util.null2String(rs.getString("id"));
        }
        return value;

    }


    //获取明细表名
    public static String getdetailtable(String reqid, String detailNo) {
        String tablename = getMaintable(reqid);
        if ("".equals(detailNo)) {
            return "";
        }
        return tablename + "_dt" + detailNo;
    }

    //随机生成uuid
    public static String getuuid() {

        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    //获取用户名称
    public static String getHrmName(String hrmids) {
        String hrmname = "";
        RecordSet rs = new RecordSet();
        if (!hrmids.equals("")) {
            rs.executeQuery("select lastname from hrmresource where id in(" + hrmids + ")");
            while (rs.next()) {
                hrmname = hrmname + "," + Util.null2String(rs.getString("lastname"));
            }
        }
        if (!hrmname.equals("")) {
            hrmname = hrmname.substring(1, hrmname.length());
        }
        return hrmname;
    }
    //获取用户名称
    public static String getHrmtodeptid(String hrmids) {
        String departmentid = "";
        RecordSet rs = new RecordSet();
        if (!hrmids.equals("")) {
            rs.executeQuery("select departmentid from hrmresource where id in(" + hrmids + ")");
            while (rs.next()) {
                departmentid = departmentid + "," + Util.null2String(rs.getString("departmentid"));
            }
        }
        if (!departmentid.equals("")) {
            departmentid = departmentid.substring(1, departmentid.length());
        }
        return departmentid;
    }

    //获取用户名称
    public static String getdeptName(String deptids) {
        String deptname="";
        if("".equals(deptids)){
            return "";
        }
        String sql= "select departmentname from hrmdepartment where id in ("+deptids+")";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if(rs.getCounts()>0){
            if(rs.next()){
                deptname=Util.null2String(rs.getString("departmentname"));
            }
        }
        return deptname;
    }

    //获取用户名称
    public static Map<String,Object> getdeptmap(String deptid) {
        if("".equals(deptid)){
            return null;
        }
        String sql= "select * from hrmdepartment where 1=1 ";
        Map<String,String> params=new HashMap<>();
        params.put("id",deptid);
        return JAVATools.GetTableToMap(sql,params);
    }

    //
    public static Map<String,Object> getfilemap(String fileid){
        if("".equals(fileid)){
            return null;
        }
        String sql=" select doccreaterid as createuserid,docdepartmentid as createdeptid, (select  wm_concat_old(fileid)  from imagefile ifi  join docimagefile dfi on ifi.imagefileid=dfi.imagefileid where dfi.docid =t.id ) as fileid, (select wm_concat_old(imagefilename) from docimagefile where docid=t.id ) as filename from docdetail t where 1=1 ";
        Map<String,String> params=new HashMap<>();
        params.put("id",fileid);
        return JAVATools.GetTableToMap(sql,params);
    }

    public static String getHrmanddeptName(String hrmids) {
        String hrmname = "";
        try {
            RecordSet rs = new RecordSet();
            if (!hrmids.equals("")) {
                rs.executeQuery("select hs.lastname,hd.departmentname from hrmdepartment hd join hrmresource hs on hs.departmentid=hd.id where hs.id in(" + hrmids + ")");
                while (rs.next()) {
                    hrmname = hrmname + "," + Util.null2String(rs.getString("lastname")) + "(" + Util.null2String(rs.getString("departmentname")) + ")";
                }
            }
            if (!hrmname.equals("")) {
                hrmname = hrmname.substring(1, hrmname.length());
            }
        } catch (Exception exo) {
            JAVATools.SetCmdwritelog(exo.getMessage());

        }

        return hrmname;
    }

    //页面获取流程状态
    public static String getRequestStatus(String var1) {
        String var3 = "";
        RecordSet var4 = new RecordSet();
        RecordSet var5 = new RecordSet();
        var4.executeQuery("select isstopped,currentnodetype from workflow_requestbase where requestid =? ", var1);
        if (var4.next()) {
            String var6 = var4.getString("isstopped");
            String var7 = var4.getString("currentnodetype");
            if (var7.equals("3")) {
                var3 = "已办结";
            } else if (var6.equals("1")) {
                var3 = "终止";
            } else {
                var5.executeQuery("select 1 from workflow_requestlog where logtype in ('0','2','i') and requestid =? ", var1);
                if (var5.next()) {
                    var3 = "进行中";
                } else {
                    var3 = "草稿";
                }
            }
        }

        return var3;
    }

    //获取用户名称
    public static User getHrmUser(String hrmlogin) {

        String id = "";
        User user;
        RecordSet rs = new RecordSet();
        if (hrmlogin.equals("")) {
            return null;
        }
        rs.executeQuery("select id from hrmresource where loginid ='" + hrmlogin + "'");
        while (rs.next()) {
            id = Util.null2String(rs.getString("id"));
        }
        if ("".equals(id)) {
            return null;
        }

        try {
            user = new User(Util.getIntValue(id));
        } catch (Exception e) {
            user = null;
        }
        return user;
    }

    //获取工号
    public static String getHrmworkcode(String hrmlogin) {

        String workcode = "";

        RecordSet rs = new RecordSet();
        if (hrmlogin.equals("")) {
            return null;
        }
        rs.executeQuery("select workcode from hrmresource where loginid ='" + hrmlogin + "'");
        while (rs.next()) {
            workcode = Util.null2String(rs.getString("workcode"));
        }
        if ("".equals(hrmlogin)) {
            workcode = hrmlogin;
        }

        return workcode;
    }

    //分别在cmd和basebeanlog日志
    public static void SetCmdwritelog(String title, String Value) {
        writelog("-------------------------" + title + ":start---------------------------");
        writelog(Value);
        //    writelog("--------------------------"+title+":end--------------------------");
    }

    public static void SetCmdwritelog(String Value) {
        writelog(Value);
        //    writelog("--------------------------"+title+":end--------------------------");
    }

    //分别在cmd和basebeanlog日志
    public static void writelog(String Value) {
        BaseBean baseBean = new BaseBean();
        System.out.println(Value);
        baseBean.writeLog(Value);

    }


    /*
    防sql注入，吧参数里面的‘替换成空字符
    * */
    public static String getpreventsql(String value) {
        if ("".equals(value)) {
            return "";
        }
        String valuepar = value.replace("\'", "");
        return valuepar;
    }


    /*
        获取主账号的id
    * */
    public static String getmainhrmid(String value) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(value)) {
            return userid;
        }
        String sql = "select belongto from hrmresource where id = '" + value + "'";
        recordset.execute(sql);
        if (recordset.next()) {
            String belongto = Util.null2String(recordset.getString("belongto"));
            if ("".equals(belongto)) {
                userid = value;
            } else {
                userid = belongto;
            }
        }
        return userid;
    }

    /*
    根据workcode获取用户表的主键id
    * */
    public static String gethrmworkcodetoid(String workcode) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(workcode)) {
            return userid;
        }
        String sql = "select id from hrmresource where workcode='" + workcode + "' and belongto is null";
        recordset.execute(sql);
        if (recordset.next()) {
            String id = Util.null2String(recordset.getString("id"));
            if ("".equals(id)) {
                userid = id;
            } else {
                userid = id;
            }
        }
        return userid;
    }

    public static JSONArray GetHrmDeptWorkCodetoids(List<Map<String, Object>> list) {
        JSONArray jsonArray = new JSONArray();
        if (list == null || list.size() <= 0) {
            return jsonArray;
        }
        String where = "";
        List params = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            String usercodealue = getpreventsql(Util.null2String(map.get("usercode")));
            String deptcodevalue = getpreventsql(Util.null2String(map.get("deptcode")));
            if (!"".equals(usercodealue) && !"".equals(deptcodevalue)) {
                where += where.equals("") ? " (hrm.workcode='" + usercodealue + "' and hdt.departmentcode='" + deptcodevalue + "') " : " or (hrm.workcode='" + usercodealue + "' and hdt.departmentcode='" + deptcodevalue + "') ";
            }
        }
        String sql = "select  hrm.id as userid,hdt.id as  deptid , " +
                "( " +
                "select id from ( select t.* from HrmDepartment t start with t.id= " +
                "( select a.id from HRMDEPARTMENT a,HRMRESOURCE b where a.id=b.DEPARTMENTID and b.id=hdt.id) connect by prior t.supdepid=t.id   ) " +
                "where SUPDEPID is null or SUPDEPID=0 and rownum=1 " +
                ") as jgid " +
                " from hrmresource  hrm left join hrmdepartment hdt  on  hrm.departmentid=hdt.id where  ";
        if ("".equals(where)) {
            return jsonArray;
        }
        sql += where;
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.getCounts() <= 0) {
            return jsonArray;
        }
        while (rs.next()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userid", Util.null2String(rs.getString("userid")));
            jsonObject.put("deptid", Util.null2String(rs.getString("deptid")));
            jsonObject.put("jgid", Util.null2String(rs.getString("jgid")));
            jsonObject.put("sealadmin", sealadmin(Util.null2String(rs.getString("deptid"))));


            jsonArray.add(jsonObject);
        }

        // value=Util.null2String(rs.getString("userids"));
        return jsonArray;

    }


    public static String sealadmin(String bm){
        if("".equals(bm)){
            return "";
        }
        String sql="  select yzgly  from uf_yzgly  where bm='"+bm+"'";
        Map<String, Object> map=JAVATools.GetTableToMap(sql,null);
        if(map==null || map.size()<=0){
            return "";
        }
        return Util.null2String(map.get("yzgly"));
    }

    /*
   根据workcode获取用户表的主键id
   * */
    public static String gethrmworkcodetoidAll(String workcode) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(workcode)) {
            return userid;
        }
        String sql = "select id from hrmresource where workcode in (" + workcode + ") ";
        recordset.execute(sql);
        while (recordset.next()) {
            String id = Util.null2String(recordset.getString("id"));
            if ("".equals(id)) {
                userid = id;
            } else {
                userid = id;
            }
        }
        return userid;
    }

    /*
        根据workcode获取用户表的主键id
        * */
    public static String gethrmworkcodetoid(String workcode, String deptid) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        String sql = "";
        if ("".equals(workcode)) {
            return userid;
        }
        if ("".equals(deptid)) {
            sql = "select id from hrmresource where workcode='" + workcode + "' and belongto is null";
        } else {
            sql = "select id from hrmresource where workcode='" + workcode + "' and departmentid='" + deptid + "' and  status  not in (5,6,7)";

        }
        recordset.execute(sql);
        if (recordset.getCounts() <= 0 && !"".equals(deptid)) {
            sql = "select id from hrmresource where workcode='" + workcode + "' and belongto is null";
            recordset.execute(sql);
        }


        if (recordset.next()) {
            String id = Util.null2String(recordset.getString("id"));
            if ("".equals(id)) {
                userid = id;
            } else {
                userid = id;
            }
        }
        return userid;
    }

    /*
   根据workcode获取用户表的主键id
   * */
    public static String gethrmidtoworkcode(String ids) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(ids)) {
            return userid;
        }
        String sql = "select workcode from hrmresource where id in (" + ids + ") ";
        recordset.execute(sql);
        while (recordset.next()) {
            String uid = Util.null2String(recordset.getString("workcode"));
            if (!"".equals(uid)) {
                userid += userid == "" ? uid : "," + uid;
            }
        }
        return userid;
    }

    /*
   根据用户id获取部门deptid
   * */
    public static String gethrmidtodeptid(String ids) {
        RecordSet recordset = new RecordSet();
        String deptid = "";
        if ("".equals(ids)) {
            return deptid;
        }
        String sql = "select distinct departmentid from hrmresource where id in (" + ids + ") ";
        recordset.execute(sql);
        while (recordset.next()) {
            String uid = Util.null2String(recordset.getString("departmentid"));
            if (!"".equals(uid)) {
                deptid += deptid == "" ? uid : "," + uid;
            }
        }
        return deptid;
    }

    /*
   根据用户id获取部门deptid
   * */
    public static String gethrmidtojgid(String ids) {
        RecordSet recordset = new RecordSet();
        String deptid = "";
        if ("".equals(ids)) {
            return deptid;
        }
        String sql = " select id from ( select t.* from HrmDepartment t start with t.id=( select a.id from HRMDEPARTMENT a,HRMRESOURCE b where a.id=b.DEPARTMENTID and b.id  in (" + ids + "))  connect by prior t.supdepid=t.id) where SUPDEPID is null or SUPDEPID=0 ";
        recordset.execute(sql);
        while (recordset.next()) {
            String uid = Util.null2String(recordset.getString("id"));
            if (!"".equals(uid)) {
                deptid += deptid == "" ? uid : "," + uid;
            }
        }
        return deptid;
    }
    /*
  根据workcode获取用户表的主键id
  * */
    public static String getdepartmentcodetoid(String departmentcode) {
        RecordSet recordset = new RecordSet();
        String deptid = "";
        if ("".equals(departmentcode)) {
            return deptid;
        }
        String sql = " select id from hrmdepartment where departmentcode  ='" + departmentcode + "'";
        recordset.execute(sql);
        if (recordset.next()) {
            String id = Util.null2String(recordset.getString("id"));
            if ("".equals(id)) {
                deptid = id;
            } else {
                deptid = id;
            }
        }
        return deptid;
    }


    /*
        根据loginid获取id
    * */
    public static String getloginidtoid(String value) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(value)) {
            return userid;
        }
        String sql = "select wm_concat_old(id) as id from hrmresource where loginid in (?)";
        recordset.executeQuery(sql, new Object[]{value});
        if (recordset.next()) {
            String belongto = Util.null2String(recordset.getString("id"));
            if ("".equals(belongto)) {
                userid = value;
            } else {
                userid = belongto;
            }
        }
        return userid;
    }

    /*
       根据人员表id获取当前人包括子账号的所有id
   * */
    public static String getAllidandbelongto(String value) {
        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(value)) {
            return userid;
        }
        String sql = "select wm_concat_old(id) as id from hrmresource where id in (" + value + ") or belongto in(" + value + ")";
        recordset.execute(sql);
        if (recordset.next()) {
            String belongto = Util.null2String(recordset.getString("id"));
            if ("".equals(belongto)) {
                userid = value;
            } else {
                userid = belongto;
            }
        }
        return userid;
    }

    public static String GetDetailfield(String tablename, String id, String field) {
        RecordSet rs = new RecordSet();
        String value = "";
        String sql = "select " + field + " from " + tablename + " where id='" + id + "'";
        JAVATools.SetCmdwritelog("获取表的字段值", "sql:" + sql);
        rs.execute(sql);
        if (rs.next()) {
            value = Util.null2String(rs.getString(field));
        }
        return value;
    }

    //插入表
    public static boolean Excetesql(Map<String, String> mapvalue, String tablename) {
        boolean flag = true;
        String dttablename = "";
        String dttablenvalue = "";
        List<String> list = new ArrayList<>();
        JAVATools.SetCmdwritelog("新表插入:", "表名" + tablename);
        for (Map.Entry<String, String> entry : mapvalue.entrySet()) {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            JAVATools.SetCmdwritelog(" key: " + fieldname + " value:" + fieldvalue);
            dttablename += dttablename == "" ? fieldname : "," + fieldname;
            //dttablenvalue += dttablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
            dttablenvalue += dttablenvalue == "" ? "?" : ",?";
            list.add(Lengthmax(fieldvalue.replace("\r\n", "")));
        }

        String sql = "insert into " + tablename + "(" + dttablename + ") values(" + dttablenvalue + ")";
        JAVATools.SetCmdwritelog(" sql: " + sql);
        JAVATools.SetCmdwritelog(" sql: " + list.toString());
        RecordSet rs = new RecordSet();
        RecordSetDataSource rsc = new RecordSetDataSource();
        try {
            rs.executeUpdate(sql, new Object[]{list});
        } catch (Exception exo) {
            flag = false;
            exo.printStackTrace();
            new BaseBean().writeLog("插入明细表失败:" + exo.getMessage());
        }
        return flag;
    }


    //插入表
    public static boolean Updatesql(Map<String, String> mapvalue, String tablename, Map<String, String> map) {
        boolean flag = true;
        String where = "";
        String setvalue = "";
        List<String> list = new ArrayList<>();
        if ("".equals(tablename) || mapvalue == null || mapvalue.size() <= 0 || map == null || map.size() <= 0) {
            return false;
        }
        JAVATools.SetCmdwritelog("修改表:", "表名" + tablename);
        for (Map.Entry<String, String> entry : mapvalue.entrySet()) {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            if (!"".equals(fieldname)) {
                setvalue += setvalue == "" ? fieldname + "=?" : "," + fieldname + "=?";
                list.add(Lengthmax(fieldvalue.replace("\r\n", "")));
            }
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            if (!"".equals(fieldname)) {
                where += where == "" ? fieldname + "=?" : " and " + fieldname + "=?";
                list.add(Lengthmax(fieldvalue.replace("\r\n", "")));
            }
        }

        String sql = "update " + tablename + " set " + setvalue + "  where " + where;
        JAVATools.SetCmdwritelog(" sql: " + sql);
        JAVATools.SetCmdwritelog(" sql: " + list.toString());
        RecordSet rs = new RecordSet();
        RecordSetDataSource rsc = new RecordSetDataSource();
        try {
            rs.executeUpdate(sql, new Object[]{list});
        } catch (Exception exo) {
            flag = false;
            exo.printStackTrace();
            new BaseBean().writeLog("插入明细表失败:" + exo.getMessage());
        }
        return flag;
    }

    //删除操作
    public static boolean deletesql( String tablename, Map<String, String> map) {
        boolean flag = true;
        String where = "";
        String setvalue = "";
        List<String> list = new ArrayList<>();
        if ("".equals(tablename) ||  map == null || map.size() <= 0) {
            return false;
        }
        JAVATools.SetCmdwritelog("删除表:", "表名" + tablename);

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            if (!"".equals(fieldname)) {
                where += where == "" ? fieldname + "=?" : " and " + fieldname + "=?";
                list.add(Lengthmax(fieldvalue.replace("\r\n", "")));
            }
        }

        String sql = "delete from " + tablename +  "  where " + where;
        JAVATools.SetCmdwritelog(" deletesql: " + sql);
        JAVATools.SetCmdwritelog(" deletesql: " + list.toString());
        RecordSet rs = new RecordSet();
        RecordSetDataSource rsc = new RecordSetDataSource();
        try {
            rs.executeUpdate(sql, new Object[]{list});
        } catch (Exception exo) {
            flag = false;
            exo.printStackTrace();
            new BaseBean().writeLog("删除明细表失败:" + exo.getMessage());
        }
        return flag;
    }

    //将查询库转为Map<String,Object>
    public static Map<String, Object> GetTableToMap(String sql, Map<String, String> parmasmap) {
        RecordSet rs = new RecordSet();
        Map<String, Object> map = new HashMap<>();
        String where = "";
        List<String> list = new ArrayList<>();

        if (parmasmap != null && parmasmap.size() > 0) {
            for (String key : parmasmap.keySet()) {
                //   where+=where==""?" key=?":" and key=?";
                where += " and " + key + "=? ";
                list.add(parmasmap.get(key));
            }
        }
        if ("".equals(where)) {
            rs.execute(sql);
        } else {
            rs.executeQuery(sql + where, new Object[]{list});
        }

        if (rs.next()) {
            String[] colnums = rs.getColumnName();
            for (String colnumvalue : colnums) {
                map.put(colnumvalue.toLowerCase(), rs.getString(colnumvalue));
            }

        }
        return map;
    }

    public static List GetTableToListMap(String sql, Map<String, String> parmasmap) {
        RecordSet rs = new RecordSet();
        List<String> list = new ArrayList<>();

        String where = "";
        List<Map<String, Object>> listreturn = new ArrayList<>();

        if (parmasmap != null && parmasmap.size() > 0) {
            for (String key : parmasmap.keySet()) {
                //   where+=where==""?" key=?":" and key=?";
                where += " and " + key + "=? ";
                list.add(parmasmap.get(key));
            }
        }
        if ("".equals(where)) {
            rs.execute(sql);
        } else {
            rs.executeQuery(sql + where, new Object[]{list});
        }

        while (rs.next()) {
            Map<String, Object> map = new HashMap<>();
            String[] colnums = rs.getColumnName();
            for (String colnumvalue : colnums) {
                map.put(colnumvalue.toLowerCase(), rs.getString(colnumvalue));
            }
            listreturn.add(map);

        }
        return listreturn;
    }

    //截取字符串4000以内
    public static String Lengthmax(String value) {
        if ("".equals(value)) return "";
        if (value.length() > 4000) {
            value = value.substring(0, 3999);
        }
        return value;
    }

    public static String lengthsub(String str, int startvalue, int endvalue) {
        String value = "";
        if ("".equals(str)) {
            return "";
        }
        try {
            if (str.length() <= endvalue || str.length() <= startvalue) {
                return str;
            }

            value = str.substring(startvalue, endvalue);
        } catch (Exception exo) {

        }
        return value;
    }


    //oracle本身分页方式。。。

    /**
     * @Params page>0
     * @Params pagesize>0
     */
    public static String SqlPage(String sql, String wherestr, String orderby, int page, int pagesize) {
        if ("".equals(sql) || "".equals(orderby)) {
            return "";
        }
        if ("".equals(page) || page <= 0) {
            page = 1;
        }
        if ("".equals(pagesize) || pagesize <= 0) {
            pagesize = 20;
        }
        String orderbyStr = " order by " + orderby;

 /*       for (String key:orderby.keySet()){
            orderbyStr+=key+" "+orderby.get(key)+",";
        }
        if(!"".equals(orderbyStr)&&orderbyStr.length()>1){
            orderbyStr=orderbyStr.substring(0,orderbyStr.length()-1);
            orderbyStr="order by "+orderbyStr;
        }*/


        String value = "select * from (select ttt.*,ROWNUM AS rowno from (" + sql + orderbyStr + "  " + ") ttt " +

                " where 1=1 " + wherestr + " and rownum<=" + (page * pagesize) + ")   where rowno>" + (page - 1) * pagesize;


        return value;
    }

    //传入用户id获取当前账号下的所有主次账号
    public static String GetZctogid(String ids) {
        String value = "";
        if ("".equals(ids)) {
            return "";
        }
        String sql = "  select wm_concat_old(ttt.id) id " +
                "  from ( " +
                "        select t.id " +
                "          from hrmresource t " +
                "         where workcode in " +
                "               (select t.workcode from hrmresource t where t.id in (" + ids + ") ) and status=1 ) ttt ";
        RecordSet rs = new RecordSet();
        JAVATools.SetCmdwritelog("getHrmUSerAll", sql);
        rs.execute(sql);
        if (rs.getCounts() <= 0) {
            return "";
        }
        if (rs.next()) {
            value = Util.null2String(rs.getString("id"));
        }
        return value;

    }

    public static String getRequestidByRequestMark(String requestMark) {
        RecordSet rs = new RecordSet();
        String requestid = "";
        if (StringUtils.isNotEmpty(requestMark)) {
            String sql = "select requestid from workflow_requestbase where requestMark = '" + requestMark + "'";
            rs.executeQuery(sql);
            if (rs.next()) {
                requestid = Util.null2String(rs.getString("requestid"));
            }
        }
        return requestid;
    }

    public static String getWorkflowidByRequestid(String requestid) {
        RecordSet rs = new RecordSet();
        String workflowid = "";
        if (StringUtils.isNotEmpty(requestid)) {
            String sql = "select workflowid from WORKFLOW_REQUESTBASE where requestid = '" + requestid + "'";
            rs.executeQuery(sql);
            if (rs.next()) {
                workflowid = Util.null2String(rs.getString("workflowid"));
            }
        }
        return workflowid;
    }

    public static String GetBhtorequestid(String bh) {
        RecordSet recordset = new RecordSet();
        String tablename = JAVATools.Getunnumbertotable("yydjb");
        String value = "";
        if ("".equals(bh) || "".equals(tablename)) {
            return value;
        }
        String sql = "select  requestid from " + tablename + " where lcbh like '%" + bh + "%'";
        JAVATools.SetCmdwritelog("获取用印登记表的根据编号获取id的sql:", sql);
        recordset.execute(sql);
        if (recordset.next()) {
            value = Util.null2String(recordset.getString("requestid"));
        }
        return value;
    }

    public static String Getunnumbertotable(String unnumber) {
        String formid = "formtable_main_";
        String value = "";
        RecordSet rs = new RecordSet();
        String sql = " select abs(formid) formid from workflow_base  where unnumber='" + unnumber + "' ";
        JAVATools.SetCmdwritelog("获取用印登记表的sql:", sql);
        rs.execute(sql);
        if (rs.next()) {
            value = Util.null2String(rs.getString("formid"));
            if ("".equals(value)) {
                formid = value;
            } else {
                formid += value;
            }
        }
        return formid;
    }


    //根据人员的工号获取主账号id
    public static String GetWorkcodetoid(String workcode) {

        RecordSet recordset = new RecordSet();
        String userid = "";
        if ("".equals(workcode)) {
            return userid;
        }
        String sql = "    select t.workcode,t.belongto,t.* from hrmresource t " +
                "                 where t.belongto is null and workcode='" + workcode + "' ";
        recordset.execute(sql);
        if (recordset.next()) {
            userid = Util.null2String(recordset.getString("id"));

        }
        return userid;
    }


    //判断具体的某一步是同意
    public static boolean GetWorkflowisagrees(String requestid, String unnumbers) {
        boolean flag = false;
        if ("".equals(requestid) || "".equals(unnumbers)) {
            return flag;
        }
        String tablename = JAVATools.getMaintable(requestid);
        for (String unnumber : unnumbers.split(",")) {
            flag = GetWorkflowisagree(requestid, unnumber);
            if (flag == false) {
                break;
            }
        }
        return flag;
    }

    //判断具体的某一步是同意
    public static boolean GetWorkflowisagree(String requestid, String unnumber) {
        boolean flag = false;
        RecordSet rs = new RecordSet();
        if ("".equals(requestid) || "".equals(unnumber)) {
            return flag;
        }
        String sql = "select logtype as logtype " +
                "  from (select logtype " +
                "          from workflow_requestlog " +
                "         where nodeid in " +
                "               (select id from workflow_nodebase where unnumber = ''{0}'') " +
                "           and requestid = ''{1}'' " +
                "         order by operatedate desc, operatetime desc) " +
                " where rownum = 1 ";
        String sqlreturn = MessageFormat.format(sql, unnumber, requestid);
        rs.execute(sqlreturn);
        if (rs.getCounts() <= 0) {
            return true;
        }
        rs.next();
        String value = rs.getString("logtype");
        if (!"".equals(value) && "3".equals(value)) {
            flag = false;
        } else {
            flag = true;
        }
        return flag;
    }

    //将参数累加
    public static String AddStringparams(String... values) {
        String value = "";
        if ("".equals(values)) {
            return value;
        }
        for (String bodyvalue : values) {
            if (!"".equals(bodyvalue)) {
                value += value == "" ? bodyvalue : "," + bodyvalue;
            }
        }
        return value;
    }

    //返回参数判断
    public static String Apiretrun(String code, String value) {
        JSONObject status = new JSONObject();
        if ("200".equals(code)) {
            status.put("status", code);
            status.put("data", value);
        } else {
            status.put("status", code);
            status.put("errormsg", value);
        }
        return status.toJSONString();
    }

    //返回参数判断
    public static String Apiretrun(String code, boolean value) {
        JSONObject status = new JSONObject();

            status.put("status", code);
            status.put("data", value);

        return status.toJSONString();
    }

    //返回参数判断
    public static String Apiretrun(String code, JSONObject value) {
        JSONObject status = new JSONObject();
        if ("200".equals(code)) {
            status.put("status", code);
            status.put("data", value);
        } else {
            status.put("status", code);
            status.put("errormsg", value);
        }
        return status.toJSONString();
    }

    //返回参数判断
    public static String Apiretrun(String code, JSONArray value) {
        JSONObject status = new JSONObject();
        if ("200".equals(code)) {
            status.put("status", code);
            status.put("data", value);
        } else {
            status.put("status", code);
            status.put("errormsg", value);
        }
        return status.toJSONString();
    }

    //Map转JSON
    public static JSONObject MapToJson(Map<String, String> map) {
        JSONObject jsonObject = new JSONObject();
        for (String key : map.keySet()) {
            jsonObject.put(key, map.get(key));
        }
        return jsonObject;
    }

    //判断。将B数组和A数组里面的人做差处理。
    public static String leavedistinct(String lowstr, String highidstr) {
        String value = "";
        String highstr = WorkCode(highidstr);
        //比较。如果低的和高的比较数据。那么把低的数据删除
        String[] siplitlowstr = lowstr.split(",");
        for (String lowvalue : siplitlowstr) {
            String lowworkcode = gethrmidtoworkcode(lowvalue);
            if (("," + highstr + ",").indexOf("," + lowworkcode + ",") < 0) {
                value += value == "" ? lowvalue : "," + lowvalue;
            }
        }
        return value;
    }

    public static String WorkCode(String userids) {
        String workcodevalue = "";
        if (userids == null && "".equals(userids)) {
            return userids;
        }
        RecordSet rs = new RecordSet();
        String sql = "select distinct workcode from hrmresource where id in (" + userids + ")";
        rs.execute(sql);
        if (rs.getCounts() <= 0) {
            return userids;
        }
        while (rs.next()) {
            String workcode = Util.null2String(rs.getString("workcode"));
            if (!"".equals(workcode)) {
                workcodevalue += workcodevalue == "" ? workcode : "," + workcode;
            }
        }
        return workcodevalue;
    }

    //a否包含的字
    //b为总数
    public static boolean DeptContain(String a, String b) {
        String valuea = "," + a + ",";
        String valueb = "," + b + ",";
        boolean flag = false;
        if (valueb.indexOf(a) >= 0) {
            flag = true;
        }
        return flag;
    }

    //map
    public static Object getMapToClass(Map<String, Object> map, Class classname) {
        try {
            return JSON.parseObject(JSON.toJSONString(map), classname);
            //   return null;
        } catch (Exception e) {
            return null;
        }

    }


    //判断类操作
    public static boolean ismapisnotnull(Map<String, Object> map) {
        boolean flag = true;
        if (map == null || map.size() < 0) {
            flag = false;
        }
        return flag;
    }

    public static boolean islistisnotnull(List list) {
        boolean flag = true;
        if (list == null || list.size() < 0) {
            flag = false;
        }
        return flag;
    }

    public static String Getdeptdirectors(String id) {
        String sql = "select (select deptdirectors from hrmdepartmentdefined where deptid =hd.id) as deptdirectors from hrmdepartment hd where 1=1 ";
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        Map<String, Object> returnmap = GetTableToMap(sql, map);
        if (returnmap == null || returnmap.size() <= 0) {
            return "";
        }
        return Util.null2String(returnmap.get("deptdirectors"));
    }

    //将object类转String
    public static String ConvernullString(Object value) {
        String retvalue = "";
        if (value == null) return "";
        retvalue = Util.null2String(value);
        return retvalue;
    }

    public static boolean isequestString(String a, String b) {
        boolean flag = false;
        if (b.equals(a)) {
            flag = true;
        }
        return flag;
    }

    public static boolean iscontaionString(String a, String b) {

        boolean flag = false;
        if (a.indexOf(b) >= 0) {
            flag = true;
        }
        return flag;

    }


    public static String ListeuqestString(List<String> list, String value) {
        String returnvalue = "";
        if (list == null || list.size() <= 9 || value == "") {
            return returnvalue;
        }

        for (String listvalue : list) {
            if (!value.equals(listvalue)) {
                returnvalue = returnvalue == "" ? listvalue : "," + listvalue;
            }
        }
        return returnvalue;
    }

    public static String[] ArrayCodyAtoB(String[] a, String[] b) {
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
        return b;
    }

//更新操作

    public static String addreloadString(String returnvalue, String value) {
        if (value == null || "".equals(value)) {
            return returnvalue;
        }
        if (returnvalue == null || "".equals(returnvalue)) {
            return value;
        }
        String workolds = WorkCode(returnvalue);
        String workvalue = gethrmidtoworkcode(value);
        if (workolds.indexOf(workvalue) < 0) {
            returnvalue += returnvalue == "" ? value : "," + value;
        }
        return returnvalue;
    }

    public static String addidinreloadString(String ids, String value) {
        if (value == null || "".equals(value)) {
            return ids;
        }
        if (ids == null || "".equals(ids)) {
            return value;
        }

        if (ids.indexOf(value) < 0) {
            ids += ids == "" ? value : "," + value;
        }
        return ids;
    }


    public List<Map<String, Object>> getDistinctList(List<Map<String, Object>> listMap, String keyName) {
        Iterator<Map<String, Object>> iter = listMap.iterator();
        List<String> listTemp = new ArrayList<>();
        while (iter.hasNext()) {
            Map item = iter.next();
            if (!item.containsKey(keyName) || null == item.get(keyName) || "".equals(item.get(keyName).toString())) {
                iter.remove();
            } else {
                String str = item.get(keyName).toString();
                if (listTemp.contains(str)) {
                    iter.remove();
                } else {
                    listTemp.add(item.get(keyName).toString());
                }
            }
        }
        return listMap;
    }


    public static String Gethrmlastname(String id) {
        RecordSet rs = new RecordSet();
        String value = "";
        String where = "";
        if ("".equals(id)) {
            return value;
        }
        for (String parmasvalue : id.split(",")) {
            where += where == "" ? "'" + parmasvalue + "'" : ",'" + parmasvalue + "'";
        }
        if (!"".equals(where)) {
            String sql = "select  wm_concat_old(lastname) as lastname from hrmresource where id in (" + where + ")";
            rs.execute(sql);
            rs.next();
            if (rs.getCounts() > 0) {
                value = rs.getString("lastname");
            }
        }
        return value;
    }

    public static String getidlastname(String ids) {
        RecordSet rs = new RecordSet();
        String userid = "";
        if ("".equals(ids)) {
            return userid;
        }
        String sql = "select workcode,lastname from hrmresource where id in ( " + ids + ")";
        //System.out.println(sql);
        rs.execute(sql);
        while (rs.next()) {
            String workcode = Util.null2String(rs.getString("workcode"));
            String lastname = Util.null2String(rs.getString("lastname"));
            String value = lastname + "(" + workcode + ")";
            if (!"".equals(value)) {
                userid += userid == "" ? value : "," + value;
            }
        }
        return userid;
    }

    public static String AddStrToUser(String value, User user) {
        String valueret = "";
        if ("".equals(value)) {
            return valueret;
        }
        if (user != null && !"".equals(user.getUID()) && value.indexOf(Util.null2String(user.getUID())) < 0) {
            valueret += value + "," + user.getUID();
        } else {
            valueret = value;
        }
        return valueret;
    }


    public static String getnodeunnumbertoid(String unnumber) {
        boolean flag = false;
        RecordSet rs = new RecordSet();
        if ("".equals(unnumber)) {
            return "";
        }

        String sql = "select id from workflow_nodebase where 1=1 ";
        Map<String, String> map = new HashMap<>();
        map.put("unnumber", unnumber);
        Map<String, Object> retrunmap = GetTableToMap(sql, map);
        if (retrunmap == null || retrunmap.size() <= 0) {
            return "";
        }

        return Util.null2String(retrunmap.get("id"));
    }

    public  static String getsupdepid(String id){
        if("".equals(id)){
            return "";
        }
        String sql="  select supdepid from hrmdepartment where id=?";
        Map<String,Object> map=DBTools.getsqltomap(sql,id);
        if(map==null && map.size()<=0){
            return "";
        }
        return Util.null2String(map.get("supdepid"));
    }



    /**
     * 如果是部门有值返回部门，否则返回机构
     * */
    public static String gethrmdeptjg(String userid){
        if(StringUtils.isBlank(userid)){
            return  "";
        }
       String deptname="";
        deptname=gethrmtodeptname(userid);
        if(!StringUtils.isBlank(deptname)){
            return  deptname;
        }
        deptname=gethrmtojg(userid);
        if(!StringUtils.isBlank(deptname)){
            return deptname;
        }
        return deptname;

    }

    /**
     * 根据用户id获取部门名称
     * */
    public static String gethrmtodeptname(String userid){
        if(StringUtils.isBlank(userid)){
            return  "";
        }
        String sql="select (select departmentname from HrmDepartment where id=hr.departmentid ) as departmentname from hrmresource hr where  1=1 ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("hr.id",userid);
        Map<String,Object> map=GetTableToMap(sql,wheremap);
        if(map==null || map.size()<=0){
            return "";
        }
        return Utils.null2String(map.get("departmentname"));
    }
    /**
     * 根据用户id获取机构名称
     * */
      public static String gethrmtojg(String userid){
        if(StringUtils.isBlank(userid)){
            return  "";
        }
        String sql="select departmentname " +
                "  from (select t.* " +
                "          from HrmDepartment t " +
                "         start with t.id = (select a.id " +
                "                              from HRMDEPARTMENT a, HRMRESOURCE b " +
                "                             where a.id = b.DEPARTMENTID " +
                "                               and b.id = '"+userid+"') " +
                "        connect by prior t.supdepid = t.id) " +
                " where SUPDEPID is null " +
                "    or SUPDEPID = 0 ";
        Map<String,Object> map=GetTableToMap(sql,null);
        if(map==null || map.size()<=0){
            return "";
        }
        return Utils.null2String(map.get("departmentname"));
    }

}
