WorkerReportUtil.jsp 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
<%@ page import="weaver.general.Util"%>
<%@ page import="java.util.Map.Entry"%>
<%@ page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%! 


	/**
	 * 
	 * 得到人员sql
	 * @param hrmids
	 * @param subcompanyids
	 * @param departmentids
	 * @param cpyincludesub
	 * @param deptincludesub
	 * @param iNextNum
	 * @param ipageset
	 * @return
	 */
	public String getPageHrmSql(String hrmids, String subcompanyids, String departmentids, String cpyincludesub, String deptincludesub, int iNextNum, int ipageset, int ipagesize){
	    StringBuffer pageHrmids = new StringBuffer();
	    weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
	    //是否是sql server数据库
	    boolean isSqlServer = rs.getDBType().equals("sqlserver");
	    int iTotal = 0;
	    String ids = "";
	    String backfields = null;
	    String fromSql = null;
	    String sqlWhere = null;
	    String[] orderbyFields = new String[]{"id"};
	    String order = "asc";
	    
	    //如果没有查询条件,则查询所有人的数据
	    if(isEmpty(hrmids) && isEmpty(subcompanyids) && isEmpty(departmentids)){
	        backfields = "*";
	        fromSql = " from HrmResource";
	        sqlWhere = " where 1=1 and status<=3 ";
	    //如果人员id不为空,则查询指定人员的数据
	    }else if(!isEmpty(hrmids)){
	        backfields = "*";
	        fromSql = " from HrmResource";
	        sqlWhere = " where id in("+hrmids+") and status<=3 ";
	    //如果部门id不为空,则查询指定部门的数据
	    }else if(!isEmpty(departmentids)){
	        ids = getDeptIds(departmentids,deptincludesub);
	        //如果所查询的部门id为空则返回0
	        if(isEmpty(ids)){
	            return "";
	        }
	        backfields = "*";
	        fromSql = " from HrmResource";
	        sqlWhere = " where departmentid in("+ids+") and status<=3 ";
	    //如果分部id不为空,则查询指定分部的数据
	    }else if(!isEmpty(subcompanyids)){
	        ids = getCpyIds(subcompanyids,cpyincludesub);
	        //如果所查询的分部id为空则返回0
	        if(isEmpty(ids)){
	            return "";
	        }
	        backfields = "*";
	        fromSql = " from HrmResource";
	        sqlWhere = " where subcompanyid1 in("+ids+")  and status<=3 ";
	    }
	    String sql = null;
	    if(isSqlServer){
	        sql = getPageSql(backfields,fromSql, sqlWhere, orderbyFields, order, iNextNum, ipageset);
	    }else if("mysql".equals(rs.getDBType())){
	    	if(ipagesize>=10000){
	    		sql = "select "+backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order);
	    	}else{
	    		sql = "select A.* from (select "+
		                backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order) + 
		                " ) A limit " + (iNextNum-ipagesize)+","+ipagesize;
	    	}
	    }else{
	    	if(ipagesize>=10000){
	    		sql = "select "+backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order);
	    	}else{
	    		sql = "select * from (select A.*,rownum rn from (select "+
		                backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order) + 
		                " ) A where rownum<=" + iNextNum +" ) B where rn>" + (iNextNum-ipagesize);
	    	}
	    }
	    rs.execute(sql);
	    while(rs.next()){
	        pageHrmids.append(","+rs.getString("id"));
	    }
	    return sql;
	}
	/**
	 * 
	 * 得到人员id
	 * @param hrmids
	 * @param subcompanyids
	 * @param departmentids
	 * @param cpyincludesub
	 * @param deptincludesub
	 * @param iNextNum
	 * @param ipageset
	 * @return
	 */
	public String getPageHrmids(String hrmids, String subcompanyids, String departmentids, String cpyincludesub, String deptincludesub, int iNextNum, int ipageset, int ipagesize){
	    StringBuffer pageHrmids = new StringBuffer();
	    weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
	    //是否是sql server数据库
	    boolean isSqlServer = rs.getDBType().equals("sqlserver");
	    int iTotal = 0;
	    String ids = "";
	    String backfields = null;
	    String fromSql = null;
	    String sqlWhere = null;
	    String[] orderbyFields = new String[]{"id"};
	    String order = "asc";
	    
	    //如果没有查询条件,则查询所有人的数据
	    if(isEmpty(hrmids) && isEmpty(subcompanyids) && isEmpty(departmentids)){
	        backfields = "id";
	        fromSql = " from HrmResource";
	        sqlWhere = " where 1=1 and status<=3 ";
	    //如果人员id不为空,则查询指定人员的数据
	    }else if(!isEmpty(hrmids)){
	        backfields = "id";
	        fromSql = " from HrmResource";
	        sqlWhere = " where id in("+hrmids+") and status<=3 ";
	    //如果部门id不为空,则查询指定部门的数据
	    }else if(!isEmpty(departmentids)){
	        ids = getDeptIds(departmentids,deptincludesub);
	        //如果所查询的部门id为空则返回0
	        if(isEmpty(ids)){
	            return "";
	        }
	        backfields = "id";
	        fromSql = " from HrmResource";
	        sqlWhere = " where departmentid in("+ids+") and status<=3 ";
	    //如果分部id不为空,则查询指定分部的数据
	    }else if(!isEmpty(subcompanyids)){
	        ids = getCpyIds(subcompanyids,cpyincludesub);
	        //如果所查询的分部id为空则返回0
	        if(isEmpty(ids)){
	            return "";
	        }
	        rs.executeSql("select count(1) from HrmResource where subcompanyid1 in("+ids+") and status<=3 ");
	        backfields = "id";
	        fromSql = " from HrmResource";
	        sqlWhere = " where subcompanyid1 in("+ids+")  and status<=3 ";
	    }
	    String sql = null;
	    if(isSqlServer){
	        sql = getPageSql(backfields,fromSql, sqlWhere, orderbyFields, order, iNextNum, ipageset);
	    }else if("mysql".equals(rs.getDBType())){
	    	if(ipagesize>=10000){
	    		sql = "select "+ backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order);  
	    	}else{
	    		sql = "select A.* from (select "+
	                    backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order) + 
	                    " ) A limit " + (iNextNum-ipagesize)+","+ipagesize;  
	    	}
	    }else{
	    	if(ipagesize>=10000){
	    		sql = "select "+ backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order);  
	    	}else{
	    		sql = "select * from (select A.*,rownum rn from (select "+
	                    backfields + fromSql + sqlWhere + getOrderSql(orderbyFields,order) + 
	                    " ) A where rownum<=" + iNextNum +" ) B where rn>" + (iNextNum-ipagesize);  
	    	}
	    }
	    rs.execute(sql);
	    while(rs.next()){
	        pageHrmids.append(","+rs.getString("id"));
	    }
	    return pageHrmids.length()>0?pageHrmids.substring(1):"";
	}
	
	
	/**
	 * 得到分页sql
	 * @param backfields
	 * @param fromSql
	 * @param sqlWhere
	 * @param orderbyFields
	 * @param order
	 * @return
	 */
	public String getPageSql(String backfields,String fromSql,String sqlWhere,String[] orderbyFields,String order,int maxNum,int pageset){
	    String orderby1 = getOrderSql(orderbyFields,order);
	    String orderby2 = getOrderSql(orderbyFields,"asc".equalsIgnoreCase(order)?"desc":"asc");
	    String orderby3 = getOrderSql(orderbyFields,order);
	    
	    String sql = "select top " + pageset +" A.* "
	                 + "from (" 
	                 +         "select top " + maxNum + " " + backfields 
	                 +" " +     fromSql 
	                 +" " +     sqlWhere  
	                 +" " +     orderby3 
	                 +" " +   ") A "+
	                   orderby2;
	    sql = "select top " + pageset +" B.* from (" + sql + ") B "+orderby1;
	    if(pageset>=10000){
	    	sql = "select " + backfields 
            	+" " +     fromSql 
            	+" " +     sqlWhere  
            	+" " +     orderby3;
	    }
	    	
	    return sql;
	}
	
	/**
	 * 得到排序sql
	 * @param orderbyFields 排序字段
	 * @param order 排序规则("desc"/"asc")
	 * @return
	 */
	private String getOrderSql(String[] orderbyFields, String order) {
	    StringBuffer sb = new StringBuffer(" ORDER BY ");
	    for (int i = 0; i < orderbyFields.length; i++) {
	        if(i == 0){
	            sb.append(orderbyFields[i] + " " + order + " ");
	        }else{
	            sb.append("," + orderbyFields[i] + " " + order + " ");
	        }
	    }
	    return sb.toString();
	}

    /**
     * 
     * 得到数据总数
     * @param hrmids
     * @param subcompanyids
     * @param departmentids
     * @param cpyincludesub
     * @param deptincludesub
     * @return
     */
    public int getTotalRecord(String hrmids, String subcompanyids, String departmentids, String cpyincludesub, String deptincludesub){
        weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
        int iTotal = 0;
        String ids = "";
        //如果没有查询条件,则查询所有人的数据
        if(isEmpty(hrmids) && isEmpty(subcompanyids) && isEmpty(departmentids)){
            rs.executeSql("select count(1) from HrmResource WHERE status<=3");
            if(rs.next()){
                iTotal = rs.getInt(1);
            }
        //如果人员id不为空,则查询指定人员的数据
        }else if(!isEmpty(hrmids)){
            rs.executeSql("select count(1) from HrmResource where id in("+hrmids+") and status<=3");
            if(rs.next()){
                iTotal = rs.getInt(1);
            }
        //如果部门id不为空,则查询指定部门的数据
        }else if(!isEmpty(departmentids)){
            ids = getDeptIds(departmentids,deptincludesub);
            //如果所查询的部门id为空则返回0
            if(isEmpty(ids)){
                return 0;
            }
            rs.executeSql("select count(1) from HrmResource where departmentid in("+ids+") AND status<=3");
            if(rs.next()){
                iTotal = rs.getInt(1);
            }
        //如果分部id不为空,则查询指定分部的数据
        }else if(!isEmpty(subcompanyids)){
            ids = getCpyIds(subcompanyids,cpyincludesub);
            //如果所查询的分部id为空则返回0
            if(isEmpty(ids)){
                return 0;
            }
            rs.executeSql("select count(1) from HrmResource where subcompanyid1 in("+ids+") AND status<=3");
            if(rs.next()){
                iTotal = rs.getInt(1);
            }
        }
        return iTotal;
    }
    
    /**
     * 得到所查询的分部id
     * @param departmentids 分部id
     * @param deptincludesub 是否包含子分部
     * @return
     */
    public String getCpyIds(String subcompanyids,String cpyincludesub){
        weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
        //是否是sql server数据库
        boolean isSqlServer = rs.getDBType().equals("sqlserver");
        String ids = "";
        //如果仅子分部
        if("2".equals(cpyincludesub)){
            rs.executeSql("SELECT id "+
                          "FROM   HrmSubCompany "+
                          "WHERE  supsubcomid IN("+subcompanyids+") ");
            StringBuffer sb = new StringBuffer();
            while(rs.next()){
                sb.append("," + rs.getInt(1));
            }
            if(sb.length()>0){
                ids = sb.substring(1);
            }
        //如果含子分部
        }else if("3".equals(cpyincludesub)){
            //递归查询本分部和子分部的id
            String sql = null;
            if(isSqlServer){
	            sql = "SELECT id FROM GetSubcpyIdsTreeWithSelf ('"+subcompanyids+"')";
            }else if("mysql".equals(rs.getDBType())){
            	sql = "SELECT t.id FROM (SELECT @id idlist, @lv:=@lv+1 lv,"
            		+"(SELECT @id:=GROUP_CONCAT(id SEPARATOR ',') FROM HrmSubCompany d WHERE (d.canceled IS NULL OR d.canceled!=1) AND FIND_IN_SET(supsubcomid,@id)) sub"
            		+" FROM HrmSubCompany,(SELECT @id:='"+subcompanyids+"',@lv:=0) vars "
            		+" WHERE @id IS NOT NULL AND (canceled IS NULL OR canceled!=1)) tl,HrmSubCompany t"
            	    +" WHERE FIND_IN_SET(t.id,tl.idlist) AND (canceled IS NULL OR canceled!=1)";
            }else{
            	
                /* sql = "WITH mycte(id) "+
                        "     AS (SELECT id "+
                        "         FROM   HrmSubCompany "+
                        "         WHERE  1=1 "+
                        "               and id in("+subcompanyids+")"+
                        "               AND (canceled IS NULL  "+
                        "                   OR  canceled!=1) "+
                        "         UNION ALL "+
                        "         SELECT hrm.id "+
                        "         FROM   HrmSubCompany hrm, "+
                        "                mycte "+
                        "         WHERE  hrm.supsubcomid = mycte.id "+
                        "                AND (canceled IS NULL  "+
                        "                   OR  canceled!=1)) "+
                        " SELECT DISTINCT * "+
                        " FROM   mycte "; */
                
                sql = "select distinct id"+
                		" from HrmSubCompany where (canceled IS NULL OR canceled!=1)"+
                		" connect by prior id=supsubcomid"+
                		" start with"+
                		" supsubcomid in ("+subcompanyids+")";
            }
            rs.execute(sql);
            StringBuffer sb = new StringBuffer();
            sb.append(subcompanyids);
            while(rs.next()){
                sb.append("," + rs.getInt(1));
            }
            ids = sb.toString();
            /* if(sb.length()>0){
                ids = sb.substring(1);
            } */
        //如果仅本分部
        }else{
            ids = subcompanyids;
        }
        return ids;
    }
    
    /**
     * 得到所查询的部门id
     * @param departmentids 部门id
     * @param deptincludesub 是否包含子部门
     * @return
     */
    public String getDeptIds(String departmentids,String deptincludesub){
        weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
        //是否是sql server数据库
        boolean isSqlServer = rs.getDBType().equals("sqlserver");
        String ids = "";
        //如果仅子部门
        if("2".equals(deptincludesub)){
            rs.executeSql("SELECT id "+
                          "FROM   HrmDepartment "+
                          "WHERE  supdepid IN("+departmentids+") ");
            StringBuffer sb = new StringBuffer();
            while(rs.next()){
                sb.append("," + rs.getInt(1));
            }
            if(sb.length()>0){
                ids = sb.substring(1);
            }
        //如果含子部门
        }else if("3".equals(deptincludesub)){
            //递归查询本部门和子部门的id
            String sql = null;
            if(isSqlServer){
	            sql = "SELECT id FROM GetDeptIdsTreeWithSelf ('"+departmentids+"')";
            }else if("mysql".equals(rs.getDBType())){
            	sql = "SELECT t.id FROM (SELECT @id idlist, @lv:=@lv+1 lv,"
                		+"(SELECT @id:=GROUP_CONCAT(id SEPARATOR ',') FROM HrmDepartment d WHERE (d.canceled IS NULL OR d.canceled!=1) AND FIND_IN_SET(supdepid,@id)) sub"
                		+" FROM HrmDepartment,(SELECT @id:='"+departmentids+"',@lv:=0) vars "
                		+" WHERE @id IS NOT NULL AND (canceled IS NULL OR canceled!=1)) tl,HrmDepartment t"
                	    +" WHERE FIND_IN_SET(t.id,tl.idlist) AND (canceled IS NULL OR canceled!=1)";
            }else{
                /* sql = "WITH mycte(id) "+
                        "     AS (SELECT id "+
                        "         FROM   HrmDepartment "+
                        "         WHERE  1=1 "+
                        "               and id in("+departmentids+")"+
                        "               AND (canceled IS NULL  "+
                        "                   OR  canceled!=1) "+
                        "         UNION ALL "+
                        "         SELECT hrm.id "+
                        "         FROM   HrmDepartment hrm, "+
                        "                mycte "+
                        "         WHERE  hrm.supdepid = mycte.id "+
                        "                AND (hrm.canceled IS NULL  "+
                        "                   OR  hrm.canceled!=1)) "+
                        " SELECT DISTINCT * "+
                        " FROM   mycte "; */
                
                sql = "select distinct id"+
                		" from HrmDepartment where (canceled IS NULL OR canceled!=1)"+
                		" connect by prior id=supdepid"+
                		" start with"+
                		" supdepid in ("+departmentids+")";
            }
            rs.execute(sql);
            StringBuffer sb = new StringBuffer();
            sb.append(departmentids);
            while(rs.next()){
                sb.append("," + rs.getInt(1));
            }
            ids = sb.toString();
            /* if(sb.length()>0){
                ids = sb.substring(1);
            } */
        //如果仅本部门
        }else{
            ids = departmentids;
        }
        return ids;
    }

    /**
     * null转空字符串
     * @param str
     * @return
     */
    public static String null2String(String str) {
        return null2String(str, "");
    }
    /**
     * null转换为指定字符串
     * @param str 检测字符串
     * @param nullValue 替换字符串
     * @return
     */
    public static String null2String(String str, String nullValue) {
        return (str == null || "".equals(str)) ? nullValue : str;
    }
    /**
     * 字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        return str==null || "".equals(str.trim());
    }
%>