判断缺少的数据
public void saveUserEnterprise(String userId, String enId, String noIds, String lastNoIds) {
//查询新数据 与旧数据多的 添加进去数据库
List<String> add = sysRoleServiceImp.getDiff(lastNoIds,noIds);
if(add!=null && add.size()>0) {
List<WsUserEnterprise> list = new ArrayList<WsUserEnterprise>();
for (String p : add) {
if(oConvertUtils.isNotEmpty(p)) {
WsUserEnterprise rolepms = new WsUserEnterprise(userId,enId, p);
list.add(rolepms);
}
}
this.saveBatch(list);//简单的添加操作
}
//查询 旧数据与新数据的 少的 从数据库中删除
List<String> delete = sysRoleServiceImp.getDiff(noIds,lastNoIds);
if(delete!=null && delete.size()>0) {
for (String noId : delete) {
this.remove(new QueryWrapper<WsUserEnterprise>().lambda().eq(WsUserEnterprise::getUsId, userId).eq(WsUserEnterprise::getEnId, enId).eq(WsUserEnterprise::getNoId, noId));//简单的删除操作
}
}
}
删除没有的数据
/**
* 从diff中找出main中没有的元素
* @param main
* @param diff
* @return
*/
public List<String> getDiff(String main, String diff){
if(oConvertUtils.isEmpty(diff)) {
return null;
}
if(oConvertUtils.isEmpty(main)) {
return Arrays.asList(diff.split(","));
}
String[] mainArr = main.split(",");
String[] diffArr = diff.split(",");
Map<String, Integer> map = new HashMap<>();
for (String string : mainArr) {
map.put(string, 1);
}
List<String> res = new ArrayList<String>();
for (String key : diffArr) {
if(oConvertUtils.isNotEmpty(key) && !map.containsKey(key)) {
res.add(key);
}
}
return res;
}