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
| public enum LevelEnum {
NORMAL(1, "普通员工"),
GROUP_MANAGER(2, "小组长"),
COMMON_MANAGER(3, "经理"),
BOSS(3, "老板");
private final Integer level;
private final String office;
LevelEnum(Integer level, String office) { this.level = level; this.office = office; }
public Integer getLevel() { return level; }
public String getOffice() { return office; }
@Override public String toString() { return "LevelEnum{" + "level=" + level + ", office='" + office + '\'' + '}'; } }
public class LeaveRequest {
private BaseStaff staff;
private String content;
private String type;
private Integer needDays;
public LeaveRequest(BaseStaff staff, String content, String type, Integer needDays) { this.staff = staff; this.content = content; this.type = type; this.needDays = needDays; }
public BaseStaff getStaff() { return staff; }
public void setStaff(BaseStaff staff) { this.staff = staff; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public Integer getNeedDays() { return needDays; }
public void setNeedDays(Integer needDays) { this.needDays = needDays; }
@Override public String toString() { return "LeaveRequest{" + "content='" + content + '\'' + ", type='" + type + '\'' + ", needDays=" + needDays + '}'; } }
public class LeaveResponse {
public static final Integer DEFAULT = 0;
public static final Integer STATUS_OF_PASS = 1;
public static final Integer STATUS_OF_REFUSE = 2;
private String name;
private Integer resultStatus;
private String resultContent;
public LeaveResponse() { resultStatus = DEFAULT; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getResultStatus() { return resultStatus; }
public void setResultStatus(Integer resultStatus) { this.resultStatus = resultStatus; }
public String getResultContent() { return resultContent; }
public void setResultContent(String resultContent) { this.resultContent = resultContent; }
@Override public String toString() { return "LeaveResponse{" + "name='" + name + '\'' + ", resultStatus=" + resultStatus + ", resultContent='" + resultContent + '\'' + '}'; } }
public abstract class BaseStaff {
private String name;
private LevelEnum level;
private BaseManagerStaff superior;
public BaseStaff(String name, LevelEnum level) { this.name = name; this.level = level; }
public void leave(LeaveRequest request, LeaveResponse leaveResponse) { if (superior != null) { System.out.println(String.format("%s 向 %s 发起请假: %s ", getShowName(), superior.getShowName(), request.toString())); superior.doHandle(request, leaveResponse); } else { throw new RuntimeException(String.format("%s 没有上级,无法审批: %s ", getShowName(), request.toString())); } }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public LevelEnum getLevel() { return level; }
public void setLevel(LevelEnum level) { this.level = level; }
public BaseManagerStaff getSuperior() { return superior; }
public void setSuperior(BaseManagerStaff superior) { this.superior = superior; }
public String getShowName() { return "{" + "name='" + name + '\'' + ", level=" + level + '}'; } }
public abstract class BaseManagerStaff extends BaseStaff {
public BaseManagerStaff(String name, LevelEnum level) { super(name, level); }
public abstract void doHandle(LeaveRequest request, LeaveResponse response);
protected final void approval(LeaveRequest request, LeaveResponse response, Predicate<LeaveRequest> predicate) { response.setName(getName()); if (Objects.isNull(request.getType())) { System.err.println(String.format("当前审批人为 %s>>申请未指定类型, 驳回:%s", getShowName(), request.toString())); response.setResultStatus(LeaveResponse.STATUS_OF_REFUSE); response.setResultContent("申请未指定类型, 驳回"); return; } if (predicate.test(request)) { response.setResultStatus(LeaveResponse.STATUS_OF_PASS); response.setResultContent("同意"); System.out.println(String.format("当前审批人为 %s>>审批通过:%s", getShowName(), request.toString())); } else { BaseManagerStaff managerStaff = getSuperior(); if (managerStaff != null) { System.out.println(String.format("当前审批人为 %s>>得由%s审批:%s", getShowName(), managerStaff.getShowName(), request.toString())); managerStaff.doHandle(request, response); } else { System.out.println(String.format("当前审批人为 %s>>没有上级, 无需审批, 直接通过:%s", getShowName(), request.toString())); response.setResultStatus(LeaveResponse.STATUS_OF_PASS); response.setResultContent("直接同意"); } } }
}
public class NormalStaff extends BaseStaff {
public NormalStaff(String name) { super(name, LevelEnum.NORMAL); }
}
public class GroupManager extends BaseManagerStaff {
public GroupManager(String name) { super(name, LevelEnum.GROUP_MANAGER); }
@Override public void doHandle(LeaveRequest request, LeaveResponse response) { approval(request, response, request1 -> request1.getNeedDays() <= 3); } }
public class CommonManager extends BaseManagerStaff {
public CommonManager(String name) { super(name, LevelEnum.COMMON_MANAGER); }
@Override public void doHandle(LeaveRequest request, LeaveResponse response) { approval(request, response, request1 -> request1.getNeedDays() <= 5); } }
public class BossManager extends BaseManagerStaff {
public BossManager(String name) { super(name, LevelEnum.BOSS); }
@Override public void leave(LeaveRequest request, LeaveResponse leaveResponse) {
System.out.println(String.format("当前审批人为 %s>>没有上级, 无需审批, 直接通过:%s", getShowName(), request.toString())); leaveResponse.setResultStatus(LeaveResponse.STATUS_OF_PASS); leaveResponse.setResultContent("直接同意"); }
@Override public void doHandle(LeaveRequest request, LeaveResponse response) { response.setName(getName()); if (request.getNeedDays() > 10) { System.err.println(String.format("当前审批人为 %s>>申请未指定类型, 驳回:%s", getShowName(), request.toString())); response.setResultStatus(LeaveResponse.STATUS_OF_REFUSE); response.setResultContent("请这么多天, 驳回"); return; } approval(request, response, request1 -> request1.getNeedDays() > 5); }
}
public class LeaveTest {
public static void main(String[] args) {
BossManager bossManager1 = new BossManager("张三"); CommonManager commonManager = new CommonManager("李四"); GroupManager groupManager1 = new GroupManager("王五"); NormalStaff normalStaff1 = new NormalStaff("赵六");
normalStaff1.setSuperior(groupManager1); groupManager1.setSuperior(commonManager); commonManager.setSuperior(bossManager1);
leave(normalStaff1, 1);
leave(groupManager1, 3);
leave(commonManager, 6);
leave(bossManager1, 6);
}
private static void leave(BaseStaff staff, Integer needDays) { LeaveResponse leaveResponse1 = new LeaveResponse(); staff.leave(new LeaveRequest(staff, "国庆请假" + needDays + "天", "事假", needDays), leaveResponse1); System.out.println(staff.getName() + ">>国庆请假" + needDays + "天结果:" + leaveResponse1.toString()); }
}
|