|
|
@@ -1,6 +1,7 @@
|
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.alibaba.excel.util.CollectionUtils;
|
|
|
import com.izouma.nineth.config.Constants;
|
|
|
import com.izouma.nineth.domain.Asset;
|
|
|
import com.izouma.nineth.domain.MetaItem;
|
|
|
@@ -15,7 +16,9 @@ import com.izouma.nineth.repo.SpaceObjectsInfoRepo;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.transaction.Transactional;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -28,35 +31,32 @@ public class MetaPlayerInfoService {
|
|
|
|
|
|
private MetaItemRepo metaItemRepo;
|
|
|
|
|
|
+ @Transactional
|
|
|
public MetaRestResult<SpaceObjectsInfo> metaSpaceInfo(Long userId, Long spaceId) {
|
|
|
+ merge(userId);
|
|
|
SpaceObjectsInfo spaceObjectsInfo = spaceObjectsInfoRepo.findBySpaceIdAndDel(spaceId, false);
|
|
|
if (Objects.isNull(spaceObjectsInfo)) {
|
|
|
return MetaRestResult.returnError(String.format("不存在空间id:%S的空间信息", spaceId));
|
|
|
}
|
|
|
spaceObjectsInfo.setPersonal(spaceObjectsInfo.getUserId().equals(userId));
|
|
|
// 如果是查询个人空间物品信息,统计用户剩余物品信息
|
|
|
- try {
|
|
|
- if (spaceObjectsInfo.isPersonal()) {
|
|
|
- List<SpaceObjectsInfo> spaceObjectsInfos = spaceObjectsInfoRepo.findAllByUserIdAndDel(userId, false);
|
|
|
- spaceObjectsInfo.setBuildingList(buildingList(userId, spaceObjectsInfos));
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- return MetaRestResult.returnError(e.getMessage());
|
|
|
+ if (spaceObjectsInfo.isPersonal()) {
|
|
|
+ List<SpaceObjectsInfo> spaceObjectsInfos = spaceObjectsInfoRepo.findAllByUserIdAndDel(userId, false);
|
|
|
+ spaceObjectsInfo.setBuildingList(buildingList(userId, spaceObjectsInfos));
|
|
|
}
|
|
|
return MetaRestResult.returnSuccess(spaceObjectsInfo);
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
public MetaRestResult<SpaceObjectsInfo> personalMetaSpaceInfo(Long userId) {
|
|
|
+ merge(userId);
|
|
|
SpaceObjectsInfo spaceObjectsInfo = new SpaceObjectsInfo();
|
|
|
- try {
|
|
|
- List<SpaceObjectsInfo> spaceObjectsInfos = spaceObjectsInfoRepo.findAllByUserIdAndDel(userId, false);
|
|
|
- spaceObjectsInfo.setBuildingList(buildingList(userId, spaceObjectsInfos));
|
|
|
- } catch (Exception e) {
|
|
|
- return MetaRestResult.returnError(e.getMessage());
|
|
|
- }
|
|
|
+ List<SpaceObjectsInfo> spaceObjectsInfos = spaceObjectsInfoRepo.findAllByUserIdAndDel(userId, false);
|
|
|
+ spaceObjectsInfo.setBuildingList(buildingList(userId, spaceObjectsInfos));
|
|
|
return MetaRestResult.returnSuccess(spaceObjectsInfo);
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
public MetaRestResult<SpaceObjectsInfo> updateMetaPersonalSpace(SpaceObjectsInfo spaceObjectsInfo) {
|
|
|
if (Objects.isNull(spaceObjectsInfo.getId())) {
|
|
|
return MetaRestResult.returnError("参数不合法,缺少空间物品信息记录id");
|
|
|
@@ -65,6 +65,7 @@ public class MetaPlayerInfoService {
|
|
|
if (Objects.isNull(dbSpaceObjectsInfo)) {
|
|
|
return MetaRestResult.returnError(String.format("操作失败:不存在id[%S]的空间物品数据", spaceObjectsInfo.getSpaceId()));
|
|
|
}
|
|
|
+ merge(dbSpaceObjectsInfo.getUserId());
|
|
|
List<BuildingPosDTO> buildingPosList = spaceObjectsInfo.getBuildingPosList();
|
|
|
if (CollectionUtil.isEmpty(buildingPosList)) {
|
|
|
dbSpaceObjectsInfo.setBuildingPosList(null);
|
|
|
@@ -84,9 +85,6 @@ public class MetaPlayerInfoService {
|
|
|
List<BuildingDTO> buildingDTOS = buildingList(dbSpaceObjectsInfo.getUserId(), spaceObjectsInfos);
|
|
|
// 查询用户所有已经编辑过的物品id
|
|
|
Set<Long> keys = map.keySet();
|
|
|
- if (!check(keys, buildingDTOS)) {
|
|
|
- return MetaRestResult.returnError("当前用户物品数据异常:用户未持有相关藏品,个人空间中有已编辑藏品信息");
|
|
|
- }
|
|
|
// 比较个人空间拥有的各物品最大数量
|
|
|
try {
|
|
|
keys.forEach(key -> {
|
|
|
@@ -106,60 +104,23 @@ public class MetaPlayerInfoService {
|
|
|
}
|
|
|
|
|
|
private List<BuildingDTO> buildingList(Long userId, List<SpaceObjectsInfo> spaceObjectsInfos) {
|
|
|
- List<BuildingDTO> buildingList = new ArrayList<>();
|
|
|
- List<MetaItem> metaItems = metaItemRepo.findAllByDel(false);
|
|
|
- // 统计该用户所有物品信息
|
|
|
- metaItems.forEach(metaItem -> {
|
|
|
- List<Asset> assets = assetRepo.findAllByUserIdAndStatusInAndNameLike(userId, Constants.META_NORMAL_STATUS, "%" + metaItem.getName() + "%");
|
|
|
- if (CollectionUtil.isNotEmpty(assets)) {
|
|
|
- buildingList.add(new BuildingDTO(metaItem.getId(), metaItem.getName(), assets.size()));
|
|
|
- }
|
|
|
- });
|
|
|
+ List<BuildingDTO> buildingList = countAll(userId);
|
|
|
// 如果该该用户没有空间信息直接返回 buildingList
|
|
|
- if (CollectionUtil.isEmpty(spaceObjectsInfos)) {
|
|
|
- return buildingList;
|
|
|
- }
|
|
|
- // 统计该用户已经编辑过的物品信息
|
|
|
- Map<Long, Integer> map = new HashMap<>();
|
|
|
- spaceObjectsInfos.forEach(spaceObjectsInfo -> {
|
|
|
- if (CollectionUtil.isNotEmpty(spaceObjectsInfo.getBuildingPosList())) {
|
|
|
- spaceObjectsInfo.getBuildingPosList().forEach(buildingPosDTO -> {
|
|
|
- Long key = buildingPosDTO.getItemId();
|
|
|
- if (map.containsKey(key)) {
|
|
|
- map.put(key, map.get(key) + 1);
|
|
|
- } else {
|
|
|
- map.put(key, 1);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- if (CollectionUtil.isEmpty(buildingList)) {
|
|
|
- // 如过buildingList为空,且用户有编辑过的物品返回错误
|
|
|
- if (CollectionUtil.isNotEmpty(map)) {
|
|
|
- throw new BusinessException("当前用户物品数据异常:用户未持有相关藏品,个人空间中有已编辑藏品信息");
|
|
|
- }
|
|
|
- // 如过buildingList为空,且用户没有编辑过物品 正常返回buildingList
|
|
|
+ if (CollectionUtil.isEmpty(spaceObjectsInfos) || CollectionUtil.isEmpty(buildingList)) {
|
|
|
return buildingList;
|
|
|
}
|
|
|
+ HashMap<Long, Integer> map = countEdit(spaceObjectsInfos);
|
|
|
// 如过buildingList不为为空,且用户没有编辑过物品,返回buildingList
|
|
|
if (CollectionUtil.isEmpty(map)) {
|
|
|
return buildingList;
|
|
|
}
|
|
|
// 查询用户所有已经编辑过的物品id
|
|
|
Set<Long> keys = map.keySet();
|
|
|
- if (!check(keys, buildingList)) {
|
|
|
- throw new BusinessException("当前用户物品数据异常:用户未持有相关藏品,个人空间中有已编辑藏品信息");
|
|
|
- }
|
|
|
// 查询剩余未编辑物品信息
|
|
|
buildingList.forEach(buildingDTO -> {
|
|
|
keys.forEach(key -> {
|
|
|
if (buildingDTO.getItemId().equals(key)) {
|
|
|
- int newCount = buildingDTO.getCount() - map.get(key);
|
|
|
- // 如果用户剩余物品数量为负数,返回失败
|
|
|
- if (newCount < 0) {
|
|
|
- throw new BusinessException("当前用户物品数据异常:个人空间中编辑存在物品编辑数量大于持有量");
|
|
|
- }
|
|
|
- buildingDTO.setCount(newCount);
|
|
|
+ buildingDTO.setCount(buildingDTO.getCount() - map.get(key));
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
@@ -167,10 +128,106 @@ public class MetaPlayerInfoService {
|
|
|
return buildingList;
|
|
|
}
|
|
|
|
|
|
- public boolean check(Set<Long> keys, List<BuildingDTO> buildingList) {
|
|
|
+ @Transactional
|
|
|
+ public void merge(Long userId) {
|
|
|
+ List<SpaceObjectsInfo> spaceObjectsInfos = spaceObjectsInfoRepo.findAllByUserIdAndDel(userId, false);
|
|
|
+ // 统计该用户藏品室内所有物品信息
|
|
|
+ List<BuildingDTO> buildingList = countAll(userId);
|
|
|
+ // 用户空间没有物品编辑信息 直接退出
|
|
|
+ if (CollectionUtils.isEmpty(spaceObjectsInfos)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 所有物品信息为空 删除已经编辑的物品信息
|
|
|
+ if (CollectionUtil.isEmpty(buildingList)) {
|
|
|
+ spaceObjectsInfoRepo.updateByUserId(userId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 统计该用户已经编辑过的物品信息
|
|
|
+ HashMap<Long, Integer> map = countEdit(spaceObjectsInfos);
|
|
|
+ Set<Long> keys = map.keySet();
|
|
|
// 查询用户拥有的所有的物品id
|
|
|
List<Long> all = buildingList.stream().map(BuildingDTO::getItemId).collect(Collectors.toList());
|
|
|
- // 如果用户编辑过未拥有的物品返回错误
|
|
|
- return new HashSet<>(all).containsAll(keys);
|
|
|
+ spaceObjectsInfos.forEach(spaceObjectsInfo -> {
|
|
|
+ List<BuildingPosDTO> newBuildingPosList = new ArrayList<>();
|
|
|
+ List<BuildingPosDTO> buildingPosList = spaceObjectsInfo.getBuildingPosList();
|
|
|
+ if (CollectionUtil.isNotEmpty(buildingPosList)) {
|
|
|
+ buildingPosList.forEach(buildingPosDTO -> {
|
|
|
+ if (all.contains(buildingPosDTO.getItemId())) {
|
|
|
+ newBuildingPosList.add(buildingPosDTO);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ spaceObjectsInfo.setBuildingPosList(newBuildingPosList);
|
|
|
+ });
|
|
|
+ // 查询剩余未编辑物品信息
|
|
|
+ buildingList.forEach(buildingDTO -> {
|
|
|
+ keys.forEach(key -> {
|
|
|
+ if (buildingDTO.getItemId().equals(key)) {
|
|
|
+ AtomicInteger newCount = new AtomicInteger(buildingDTO.getCount() - map.get(key));
|
|
|
+ // 如果用户剩余物品数量为负数,返回失败
|
|
|
+ if (newCount.get() < 0) {
|
|
|
+ spaceObjectsInfos.forEach(spaceObjectsInfo -> {
|
|
|
+ if (newCount.get() < 0) {
|
|
|
+ List<BuildingPosDTO> newBuildingPosList = new ArrayList<>(spaceObjectsInfo.getBuildingPosList());
|
|
|
+ List<BuildingPosDTO> buildingPosList = spaceObjectsInfo.getBuildingPosList();
|
|
|
+ if (CollectionUtil.isNotEmpty(buildingPosList)) {
|
|
|
+ buildingPosList.forEach(buildingPosDTO -> {
|
|
|
+ if (newCount.get() < 0) {
|
|
|
+ if (key.equals(buildingDTO.getItemId())) {
|
|
|
+ newBuildingPosList.remove(buildingPosDTO);
|
|
|
+ newCount.set(newCount.get() + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ spaceObjectsInfo.setBuildingPosList(newBuildingPosList);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ spaceObjectsInfoRepo.saveAll(spaceObjectsInfos);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计该用户已经编辑过的物品信息
|
|
|
+ *
|
|
|
+ * @param spaceObjectsInfos 空间物品信息
|
|
|
+ * @return 统计后已经编辑过的物品信息
|
|
|
+ */
|
|
|
+ private HashMap<Long, Integer> countEdit(List<SpaceObjectsInfo> spaceObjectsInfos) {
|
|
|
+ HashMap<Long, Integer> map = new HashMap<>();
|
|
|
+ spaceObjectsInfos.forEach(spaceObjectsInfo -> {
|
|
|
+ if (CollectionUtil.isNotEmpty(spaceObjectsInfo.getBuildingPosList())) {
|
|
|
+ spaceObjectsInfo.getBuildingPosList().forEach(buildingPosDTO -> {
|
|
|
+ Long key = buildingPosDTO.getItemId();
|
|
|
+ if (map.containsKey(key)) {
|
|
|
+ map.put(key, map.get(key) + 1);
|
|
|
+ } else {
|
|
|
+ map.put(key, 1);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计该用户所有物品信息
|
|
|
+ *
|
|
|
+ * @param userId 用户id
|
|
|
+ * @return 所有物品信息
|
|
|
+ */
|
|
|
+ private List<BuildingDTO> countAll(Long userId) {
|
|
|
+ List<BuildingDTO> buildingList = new ArrayList<>();
|
|
|
+ List<MetaItem> metaItems = metaItemRepo.findAllByDel(false);
|
|
|
+ metaItems.forEach(metaItem -> {
|
|
|
+ List<Asset> assets = assetRepo.findAllByUserIdAndStatusInAndNameLike(userId, Constants.META_NORMAL_STATUS, "%" + metaItem.getName() + "%");
|
|
|
+ if (CollectionUtil.isNotEmpty(assets)) {
|
|
|
+ buildingList.add(new BuildingDTO(metaItem.getId(), metaItem.getName(), assets.size()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return buildingList;
|
|
|
}
|
|
|
}
|