|
|
@@ -1,20 +1,177 @@
|
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
import com.izouma.nineth.domain.Rice;
|
|
|
+import com.izouma.nineth.domain.User;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
+import com.izouma.nineth.dto.R;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.repo.RiceRepo;
|
|
|
+import com.izouma.nineth.repo.UserRepo;
|
|
|
import com.izouma.nineth.utils.JpaUtils;
|
|
|
+import com.izouma.nineth.utils.SecurityUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.json.Json;
|
|
|
+import javax.json.JsonArray;
|
|
|
+import javax.json.JsonObject;
|
|
|
+import javax.json.JsonReader;
|
|
|
+import java.io.StringReader;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
public class RiceService {
|
|
|
|
|
|
+ private static final int MAX_NICKNAME_LENGTH = 6;
|
|
|
+ private static final String UPDATE_SUCCESS_MSG = "修改成功";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
private RiceRepo riceRepo;
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserRepo userRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+
|
|
|
public Page<Rice> all(PageQuery pageQuery) {
|
|
|
return riceRepo.findAll(JpaUtils.toSpecification(pageQuery, Rice.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
}
|
|
|
+
|
|
|
+ public Optional<Rice> getCurrentRiceUser(User authenticatedUser) throws BusinessException {
|
|
|
+ Long id = authenticatedUser.getId();
|
|
|
+ Optional<User> byId = userRepo.findByIdAndDelFalse(id);
|
|
|
+ String username = null;
|
|
|
+ String avatar = null;
|
|
|
+ if (byId.isPresent()) {
|
|
|
+ User user = byId.get();
|
|
|
+ username = user.getUsername();
|
|
|
+ avatar = user.getAvatar();
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Optional<Rice> byUserId = riceRepo.findByUserId(id);
|
|
|
+ if (byUserId.isPresent()) {
|
|
|
+ return byUserId;
|
|
|
+ } else {
|
|
|
+ Rice rice = new Rice();
|
|
|
+ rice.setUserId(id);
|
|
|
+ rice.setAvatar(avatar);
|
|
|
+ rice.setNickname(username);
|
|
|
+ rice.setLevel(0L);
|
|
|
+ rice.setWaterDropCount(0L);
|
|
|
+ rice.setSignCount(0L);
|
|
|
+ rice.setSelfScore(0L);
|
|
|
+ rice.setEmpiricalValue(0L);
|
|
|
+ riceRepo.save(rice);
|
|
|
+ return Optional.of(rice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public R updateNickName(Long userId, String nickname) {
|
|
|
+ String trimmedNickname = StringUtils.trim(nickname);
|
|
|
+ if (trimmedNickname.length() > MAX_NICKNAME_LENGTH) {
|
|
|
+ return R.error("昵称不能超过" + MAX_NICKNAME_LENGTH + "个字符");
|
|
|
+ }
|
|
|
+ riceRepo.updateNickName(userId, trimmedNickname);
|
|
|
+ return R.success(UPDATE_SUCCESS_MSG);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在RiceService中,我们注入了SysConfigService和RiceRepo,这两个类的实现需要您自己完成。
|
|
|
+ * 在getCurrentLevel()方法中,我们将原来在@GetMapping注解中的代码全部迁移到了这里,并将返回类型从R<String>改为了String,
|
|
|
+ * 因为我们不再需要将查询结果封装为R对象了。最后,我们根据返回结果的不同,抛出了不同的异常,这些异常同样需要您自己定义实现。
|
|
|
+ * @return
|
|
|
+ * @param empiricalValue
|
|
|
+ */
|
|
|
+ public String getCurrentLevel(Long empiricalValue) {
|
|
|
+ String jsonString = sysConfigService.getString("rice_level");
|
|
|
+ User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
|
+ Long authId = authenticatedUser.getId();
|
|
|
+ Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|
|
|
+ if (byUserId.isPresent()) {
|
|
|
+ Rice rice = byUserId.get();
|
|
|
+ empiricalValue= rice.getEmpiricalValue();
|
|
|
+
|
|
|
+ JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
|
|
|
+ JsonArray jsonArray = jsonReader.readArray();
|
|
|
+
|
|
|
+ String currentLevel = null;
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
+ JsonObject jsonObject = jsonArray.getJsonObject(i);
|
|
|
+ int start = jsonObject.getInt("start");
|
|
|
+
|
|
|
+ if (empiricalValue >= start) {
|
|
|
+ currentLevel = jsonObject.getString("name");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (currentLevel != null) {
|
|
|
+ return currentLevel;
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("没有配置对应的等级对应经验值");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("用户不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前用户积分
|
|
|
+ */
|
|
|
+ public R<String> getCurrentScore() {
|
|
|
+ User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
|
+ Long authId = authenticatedUser.getId();
|
|
|
+ Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|
|
|
+ if (byUserId.isPresent()) {
|
|
|
+ Rice rice = byUserId.get();
|
|
|
+ Long selfScore = rice.getSelfScore();
|
|
|
+ return R.success("获取到当前用户积分").add("selfScore",selfScore);
|
|
|
+ }
|
|
|
+ return R.error("获取失败");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //在RiceService中添加一个方法用于更新所有用户的排名。
|
|
|
+ public void updateScoreRank() {
|
|
|
+ List<Rice> allRices = riceRepo.findAll();
|
|
|
+ allRices.sort(Comparator.comparing(Rice::getSelfScore).reversed());
|
|
|
+ for (int i = 0; i < allRices.size(); i++) {
|
|
|
+ Rice rice = allRices.get(i);
|
|
|
+ rice.setScoreRank(i + 1);
|
|
|
+ riceRepo.save(rice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public List<Map<String, Object>> getTop100() {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|