前阵子做了两个小系统,两个类似的需求场景:
1、给学生一键分配宿舍
2、给需要进入实验室的学生进行权限校验
其实这两个用代码实现后,发现逻辑有点类似,另外,写的过程中可以用反向思维,在可以退出的点及时用return,这样可以使逻辑和代码更清爽
比较绕的可能是两个循环吧,遍历宿舍的同时要去遍历学生数组,并且要判断宿舍和学生的数量,考虑到宿舍数量和学生数量是否冲突的情况
/** * 安排宿舍业务逻辑 * 查询本楼所有未报到的学生-----查询本楼所有未满人的宿舍 * 遍历空宿舍 * 当 该宿舍未满 安排学生进入(学生信息更新宿舍ID,宿舍已入住++) * @param session * @return */ @ResponseBody @GetMapping(value = "/dor/work") public Map<String, Object> work(HttpSession session){ Map<String, Object> map = new HashMap<String, Object>(); //通过session获取当前用户id Integer userId = (Integer) session.getAttribute("userId"); Admin admin = adminMapper.getAdminById(userId); //查询本楼所有未满人的宿舍 Integer buildingId = admin.getBuildingId(); List<Dormitory> dormitories = dormitoryMapper.checkNotNull(buildingId); //查询本楼所有未报到的学生 List<Student> studentList = studentMapper.getByBuildIdNoDor(buildingId); int size = studentList.size(); int count = 0; if (size == 0){ map.put("success",2); map.put("msg","当前没有需要分配宿舍的学生!"); return map; } else { c: for (int i = 0; i < dormitories.size(); i++) { Dormitory dormitory = dormitories.get(i); int existNumber = dormitory.getExistNumber(); int initNumber = dormitory.getInitNumber(); while ((existNumber != initNumber) && (count-1 <= size)){ Student student = studentList.get(count); student.setDormitoryId(dormitory.getDormitoryId()); student.setStudentStatus("在校"); studentMapper.update(student); existNumber++; //学生已安排数更新 count++; dormitory.setExistNumber(existNumber); dormitoryMapper.update(dormitory); if (count+1 >= size){ break c; } } } if (count+1 < size){ map.put("success",3); map.put("msg","宿舍不够,有部分学生没有分配到!"); return map; } map.put("success",1); map.put("msg","当前新生宿舍已分配完毕!"); return map; } }第二个需求场景是学生可能有3种不同的权限,实验室可能存在几种状态,首先判断实验室的状态,再判断权限是否匹配。3种权限对应的可进入实验室的时间不同,根据查询匹配权限来判断是否可进
/** * 开门功能 * @param labId * @return * @throws ParseException */ @ResponseBody @PostMapping(value = "comeIn") public Map<String, Object> goin(@RequestParam(value = "userId" ) Integer userId, @RequestParam(value = "labId" ) Integer labId, @RequestParam(value = "status" ) String status) throws ParseException { Map<String ,Object > map = new HashMap<>(); // Integer userId = (Integer) session.getAttribute("userId"); String date = GetDateUtil.getDate(); Lab lab = labMapper.getById(labId); Integer integer = lab.geteNum(); //查询实验室的信息 LabStatus labStatus = labStatusMapper.getById(labId); Person person = personMapper.getById(userId); if (person == null){ map.put("success",0); map.put("msg","当前没有权限!"); return map; } //获取当前时间 Date nowDayTime = GetDateUtil.getDateTime2(); Boolean tag = false; //判断实验室人数 if(lab.geteNum() >= lab.getmNumber()){ logger.info("comeIn------------>实验室已满人"); map.put("success",0); map.put("msg","实验室已满人!暂时不可进入"); return map; } if (status.equals("开放")){ tag = true; } if (status.equals("维护")){ if (person.getUserType().equals("管理员")){ tag = true; } else { map.put("success",0); map.put("msg","维护中不可进入!"); return map; } } if (status.equals("正常")){ if (person.getUserType().equals("管理员")) { tag = true; } else { //查询条件 用户ID 实验室ID 状态:通过 List<LabPermissions> list = labPermissionsMapper.queryOkById(userId, labId); if (list.size()==0){ //无记录,当前没有权限 logger.info("comeIn------------>用户权限表为空"); map.put("success",0); map.put("msg","当前没有权限!"); return map; } logger.info("comeIn------------>用户权限表不为空"); //存在记录,遍历,确认时间 for (LabPermissions per: list) { String start = sdFormatDay.format(per.getStartDay()) + " " + sdFormat.format(per.getStartTime()); switch (per.getPerType()){ case "临时": logger.info("comeIn------------>per.getPerType().equals(临时)"); if (nowDayTime.before(per.getEndTime())){ tag = true; logger.info("comeIn------------>临时 true"); break; } break; case "长期": logger.info("comeIn------------>per.getPerType().equals(长期)"); Date longDay = sdFormatDayTime.parse(start); if (nowDayTime.before(per.getEndTime()) && nowDayTime.after(longDay)){ tag = true; logger.info("comeIn------------>长期 true"); break; } break; case "特殊": logger.info("comeIn------------>per.getPerType().equals(特殊)"); Date addDateMin = GetDateUtil.addDateMin(start, 20);//可以提前20分钟入场 if (nowDayTime.before(per.getEndTime()) && nowDayTime.after(addDateMin)){ tag = true; logger.info("comeIn------------>特殊 true"); break; } break; } } } } if (!tag){ map.put("success",0); map.put("msg","当前没有权限!"); return map; } map.put("success",1); map.put("msg","门已打开,请进!"); int n = integer + 1; lab.seteNum(n); labMapper.updateeNum(lab); logger.info("comeIn------------>更新实验室当前人数"); LabUseRecord labUseRecord = new LabUseRecord(); labUseRecord.setUserId(userId); labUseRecord.setLabId(labId); labUseRecord.setUseTime(nowDayTime); labUseRecordMapper.save(labUseRecord); logger.info("comeIn------------>保存进入实验室的记录"); return map; }