|
@@ -4,6 +4,7 @@ import com.izouma.nineth.domain.Rice;
|
|
|
import com.izouma.nineth.domain.User;
|
|
import com.izouma.nineth.domain.User;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.dto.R;
|
|
import com.izouma.nineth.dto.R;
|
|
|
|
|
+import com.izouma.nineth.dto.RiceDTO;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.repo.RiceRepo;
|
|
import com.izouma.nineth.repo.RiceRepo;
|
|
|
import com.izouma.nineth.repo.UserRepo;
|
|
import com.izouma.nineth.repo.UserRepo;
|
|
@@ -11,6 +12,7 @@ import com.izouma.nineth.utils.JpaUtils;
|
|
|
import com.izouma.nineth.utils.SecurityUtils;
|
|
import com.izouma.nineth.utils.SecurityUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -62,8 +64,8 @@ public class RiceService {
|
|
|
Rice rice = byUserId.get();
|
|
Rice rice = byUserId.get();
|
|
|
return rice;
|
|
return rice;
|
|
|
} else {
|
|
} else {
|
|
|
- Rice rice = byUserId.get();
|
|
|
|
|
-// Rice rice = new Rice();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ Rice rice = new Rice();
|
|
|
rice.setUserId(id);
|
|
rice.setUserId(id);
|
|
|
rice.setAvatar(avatar);
|
|
rice.setAvatar(avatar);
|
|
|
rice.setNickname(nickname);
|
|
rice.setNickname(nickname);
|
|
@@ -150,31 +152,55 @@ public class RiceService {
|
|
|
allRices.sort(Comparator.comparing(Rice::getSelfScore).reversed());
|
|
allRices.sort(Comparator.comparing(Rice::getSelfScore).reversed());
|
|
|
for (int i = 0; i < allRices.size(); i++) {
|
|
for (int i = 0; i < allRices.size(); i++) {
|
|
|
Rice rice = allRices.get(i);
|
|
Rice rice = allRices.get(i);
|
|
|
- rice.setScoreRank(i + 1);
|
|
|
|
|
|
|
+// rice.setScoreRank(i + 1);
|
|
|
riceRepo.save(rice);
|
|
riceRepo.save(rice);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- public List<Map<String, Object>> getTop100() {
|
|
|
|
|
|
|
+ public List<RiceDTO> getTop100(Long userId) {
|
|
|
List<Rice> top100Rices = riceRepo.findTop100ByOrderBySelfScoreDesc();
|
|
List<Rice> top100Rices = riceRepo.findTop100ByOrderBySelfScoreDesc();
|
|
|
- List<Map<String, Object>> result = new ArrayList<>();
|
|
|
|
|
- for (Rice rice : top100Rices) {
|
|
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
|
|
- map.put("avatarUrl", rice.getAvatar());
|
|
|
|
|
- map.put("nickName", rice.getNickname());
|
|
|
|
|
- map.put("level", rice.getLevel());
|
|
|
|
|
- map.put("scoreRank", rice.getScoreRank());
|
|
|
|
|
- map.put("selfScore", rice.getSelfScore());
|
|
|
|
|
- result.add(map);
|
|
|
|
|
|
|
+ List<RiceDTO> result = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 计算自己的排名
|
|
|
|
|
+ int selfRank = 0;
|
|
|
|
|
+ for (int i = 0; i < top100Rices.size(); i++) {
|
|
|
|
|
+ if (top100Rices.get(i).getId().equals(userId)) {
|
|
|
|
|
+ selfRank = i + 1;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < top100Rices.size(); i++) {
|
|
|
|
|
+ Rice rice = top100Rices.get(i);
|
|
|
|
|
+ RiceDTO riceDTO = new RiceDTO();
|
|
|
|
|
+ BeanUtils.copyProperties(rice, riceDTO);
|
|
|
|
|
+ riceDTO.setScoreRank(i + 1);
|
|
|
|
|
+ // 判断是否上榜
|
|
|
|
|
+ if (i < 100) {
|
|
|
|
|
+ riceDTO.setOnTop100(true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ riceDTO.setOnTop100(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 如果是自己,则设置自己的头像、昵称、等级和离上榜还差多少名
|
|
|
|
|
+ if (rice.getId().equals(userId)) {
|
|
|
|
|
+ riceDTO.setAvatar(rice.getAvatar());
|
|
|
|
|
+ riceDTO.setNickname(rice.getNickname());
|
|
|
|
|
+ riceDTO.setLevel(rice.getLevel());
|
|
|
|
|
+ if (i < 100) {
|
|
|
|
|
+ riceDTO.setRankGap(0);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ riceDTO.setRankGap(i + 1 - 100);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ result.add(riceDTO);
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
//浇水
|
|
//浇水
|
|
|
public R<? extends Object> watering() {
|
|
public R<? extends Object> watering() {
|
|
|
- // 获取当前用户 ID,这里假设是 1
|
|
|
|
|
|
|
+ // 获取当前用户 ID,
|
|
|
User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
|
Long authId = authenticatedUser.getId();
|
|
Long authId = authenticatedUser.getId();
|
|
|
Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|
|
Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|