批量维护关系数据
传入Id以逗号隔开,要求不在里面的删除,原来没有的新增
public void modifyApplication(String groupId, String appIds) { if(StringUtil.isEmpty(groupId)){ throw new RequiredException("组Id不能为空"); } Example example = new Example(GroupApp.class); example.createCriteria() .andEqualTo("groupId", groupId) .andEqualTo("status", 1); List<GroupApp> dbApps = this.groupAppMapper.selectByExample(example); String dbAppIds = dbApps.stream().map(GroupApp::getAppId).collect(Collectors.joining(",")); String[] appIdList = appIds.split(","); List<GroupApp> deleteList = dbApps.stream().filter(p-> !appIds.contains(p.getAppId())).collect(Collectors.toList()); deleteList.forEach(p -> { GroupAppVo item = new GroupAppVo(); item.setGroupId(groupId); item.setAppId(p.getAppId()); this.groupAppService.delete(item); }); for (String appId : appIdList) { if (!dbAppIds.contains(appId)){ GroupAppVo item = new GroupAppVo(); item.setGroupId(groupId); item.setAppId(appId); this.groupAppService.create(item); } } }
签名:删除冗余的代码最开心,找不到删除的代码最痛苦!