四、核心代码
4.1 查询单条个人习惯
@RequestMapping(value = "/getMySettings", method = RequestMethod.GET) @ApiOperation(value = "查询单条个人习惯") public Result<MySettings> getMySettings(){ User currUser = securityUtil.getCurrUser(); MySettings mySettings = iMySettingsService.getById(currUser.getId()); if(mySettings == null) { mySettings = MySettingsUtils.getMySettings(currUser); iMySettingsService.saveOrUpdate(mySettings); } return new ResultUtil<MySettings>().setData(mySettings); }
4.2 查询我的室友
@RequestMapping(value = "/getMyFriend", method = RequestMethod.GET) @ApiOperation(value = "查询我的室友") public Result<List<MySettings>> getMyFriend(){ User currUser = securityUtil.getCurrUser(); MySettings mySettings = iMySettingsService.getById(currUser.getId()); if(mySettings == null) { return ResultUtil.error("没有你的配置"); } QueryWrapper<MySettings> qw = new QueryWrapper<>(); qw.eq("dorm_id",mySettings.getDormId()); return new ResultUtil<List<MySettings>>().setData(iMySettingsService.list(qw)); }
4.3 查询宿舍
@RequestMapping(value = "/getByPage", method = RequestMethod.GET) @ApiOperation(value = "查询宿舍") public Result<IPage<Dorm>> getByPage(@ModelAttribute Dorm dorm ,@ModelAttribute PageVo page){ QueryWrapper<Dorm> qw = new QueryWrapper<>(); if(!ZwzNullUtils.isNull(dorm.getHouse())) { qw.like("house",dorm.getHouse()); } if(!ZwzNullUtils.isNull(dorm.getRoom())) { qw.like("room",dorm.getRoom()); } if(!ZwzNullUtils.isNull(dorm.getType())) { qw.like("type",dorm.getType()); } IPage<Dorm> data = iDormService.page(PageUtil.initMpPage(page),qw); return new ResultUtil<IPage<Dorm>>().setData(data); }
4.4 查询指定性别全部宿舍
@RequestMapping(value = "/getMyAll", method = RequestMethod.GET) @ApiOperation(value = "查询指定性别全部宿舍") public Result<List<Dorm>> getMyAll(){ User currUser = securityUtil.getCurrUser(); QueryWrapper<Dorm> qw = new QueryWrapper<>(); if(Objects.equals("女",currUser.getSex())) { qw.eq("type","女生宿舍"); } else { qw.eq("type","男生宿舍"); } return new ResultUtil<List<Dorm>>().setData(iDormService.list(qw)); }
4.5 初次分配宿舍
@RequestMapping(value = "/createDorm", method = RequestMethod.GET) @ApiOperation(value = "初次分配宿舍") public Result<Object> createDorm(){ User currUser = securityUtil.getCurrUser(); MySettings mySettings = iMySettingsService.getById(currUser.getId()); if(mySettings == null) { return ResultUtil.error("未初始化"); } if(!ZwzNullUtils.isNull(mySettings.getDormId())) { return ResultUtil.error("您的宿舍已分配!"); } QueryWrapper<DormChange> qw2 = new QueryWrapper<>(); qw2.eq("user_id",currUser.getId()); qw2.eq("status","未审核"); if(iDormChangeService.count(qw2) > 0L) { return ResultUtil.error("您已申请,请等待审核!"); } // 确认学生的性别 String sex = mySettings.getSex(); if(!Objects.equals("女",sex)) { sex = "男"; } // 查询所有宿舍 List<Dorm> dormList = iDormService.list(); // 次优解,空宿舍 Dorm ans2 = null; // 最优解,有人且符合条件的宿舍 Dorm ans1 = null; // 最优解符合的条件数 Integer ansCount = -1; for (Dorm d : dormList) { if(Objects.equals("男",sex) && Objects.equals("女生宿舍",d.getType()) || Objects.equals("女",sex) && Objects.equals("男生宿舍",d.getType())) { // 宿舍性别过滤 continue; } // 查询宿舍内的学生 QueryWrapper<MySettings> sQw = new QueryWrapper<>(); sQw.eq("dorm_id",d.getId()); List<MySettings> settingsList = iMySettingsService.list(sQw); if(settingsList.size() > 1) { // 设定两人满员,过滤该宿舍 } else if(settingsList.size() < 1) { // 找到次优解 if(ans2 == null) { ans2 = d; } } else { // 宿舍内满足条件的数量 Integer maxCount = 0; MySettings se = settingsList.get(0); if(se == null) { continue; } // 计算和室友关系的匹配指标 if(Objects.equals(se.getSetting1(),mySettings.getSetting1())) { maxCount ++; } if(Objects.equals(se.getSetting2(),mySettings.getSetting2())) { maxCount ++; } if(Objects.equals(se.getSetting3(),mySettings.getSetting3())) { maxCount ++; } if(Objects.equals(se.getSetting4(),mySettings.getSetting4())) { maxCount ++; } if(Objects.equals(se.getSetting5(),mySettings.getSetting5())) { maxCount ++; } if(Objects.equals(se.getSetting6(),mySettings.getSetting6())) { maxCount ++; } if(Objects.equals(se.getSetting7(),mySettings.getSetting7())) { maxCount ++; } if(Objects.equals(se.getSetting8(),mySettings.getSetting8())) { maxCount ++; } if(maxCount > ansCount) { // 如果满足最优条件 ansCount = maxCount; ans1 = d; } } } if(ans1 != null) { DormChange dc = MySettingsUtils.createDormChange(mySettings, ans1); iDormChangeService.saveOrUpdate(dc); } else if(ans2 != null) { DormChange dc = MySettingsUtils.createDormChange(mySettings, ans2); iDormChangeService.saveOrUpdate(dc); } else { return ResultUtil.error("没有合适的宿舍"); } return ResultUtil.success(); }
五、免责说明
- 本项目仅供个人学习使用,商用授权请联系博主,否则后果自负。
- 博主拥有本软件构建后的应用系统全部内容所有权及独立的知识产权,拥有最终解释权。
- 如有问题,欢迎在仓库 Issue 留言,看到后会第一时间回复,相关意见会酌情考虑,但没有一定被采纳的承诺或保证。
下载本系统代码或使用本系统的用户,必须同意以下内容,否则请勿下载!
- 出于自愿而使用/开发本软件,了解使用本软件的风险,且同意自己承担使用本软件的风险。
- 利用本软件构建的网站的任何信息内容以及导致的任何版权纠纷和法律争议及后果和博主无关,博主对此不承担任何责任。
- 在任何情况下,对于因使用或无法使用本软件而导致的任何难以合理预估的损失(包括但不仅限于商业利润损失、业务中断与业务信息丢失),博主概不承担任何责任。
- 必须了解使用本软件的风险,博主不承诺提供一对一的技术支持、使用担保,也不承担任何因本软件而产生的难以预料的问题的相关责任。