Commit 597b40dd authored by xuxin's avatar xuxin

fix assign vps bug

parent 20e5d67d
package com.edgec.browserbackend.browser.repository;
import java.util.List;
/**
* @author xuxin
* @date 2020/8/24 14:07
......@@ -7,4 +9,6 @@ package com.edgec.browserbackend.browser.repository;
*/
public interface UserVpsRepositoryCustom {
boolean deleteVpsIdOfUserVpsList(String id, String vpsId);
boolean updateAll(List<String> userIds, List<String> vpsIds);
}
......@@ -8,6 +8,8 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
/**
......@@ -28,6 +30,16 @@ public class UserVpsRepositoryCustomImpl implements UserVpsRepositoryCustom {
update.pull("vpsIdList", vpsId);
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, UserVpsDto.class);
return result.getModifiedCount() >= 1;
}
@Override
public boolean updateAll(List<String> userIds, List<String> vpsIds) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("_id").in(userIds));
Update update = new Update();
update.addToSet("vpsIdList").each(vpsIds);
UpdateResult result = mongoTemplate.updateMulti(basicQuery, update, UserVpsDto.class);
return result.getModifiedCount() == userIds.size();
}
}
......@@ -181,8 +181,7 @@ public class VpsServiceImpl implements VpsService {
Iterable<Vps> vpsIter = vpsRepository.findAllById(assignVpsDto.getVpsIds());
Iterator<Vps> iterator = vpsIter.iterator();
while (iterator.hasNext()) {
List<String> currentIds = new ArrayList<>();
currentIds.addAll(ids);
List<String> currentIds = new ArrayList<>(ids);
Vps next = iterator.next();
// 移除账户列表中的 vps 的创建者 与 父用户
currentIds.remove(next.getOwner1());
......@@ -193,12 +192,10 @@ public class VpsServiceImpl implements VpsService {
);
}
List<UserVpsDto> list = new ArrayList<>();
for (String id : assignVpsDto.getUserIds()) {
UserVpsDto uv = new UserVpsDto(id, assignVpsDto.getVpsIds());
list.add(uv);
boolean b = userVpsRepository.updateAll(assignVpsDto.getUserIds(), assignVpsDto.getVpsIds());
if (!b) {
log.error("批量分配给用户 {} vps类别 {} 失败", assignVpsDto.getUserIds(), assignVpsDto.getVpsIds());
}
userVpsRepository.saveAll(list);
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment