批量维护关系数据

传入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);
            }
        }
    }

 

posted @ 2020-05-22 13:55  Season2009  阅读(143)  评论(0编辑  收藏  举报