|
|
@@ -1,12 +1,15 @@
|
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
import com.izouma.nineth.domain.Rice;
|
|
|
+import com.izouma.nineth.domain.RiceOperationRecord;
|
|
|
import com.izouma.nineth.domain.RiceUserWaterDropRecord;
|
|
|
import com.izouma.nineth.domain.User;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.dto.R;
|
|
|
import com.izouma.nineth.dto.RiceDTO;
|
|
|
+import com.izouma.nineth.enums.RiceOperationType;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
+import com.izouma.nineth.repo.RiceOperationRecordRepo;
|
|
|
import com.izouma.nineth.repo.RiceRepo;
|
|
|
import com.izouma.nineth.repo.RiceUserWaterDropRecordRepo;
|
|
|
import com.izouma.nineth.repo.UserRepo;
|
|
|
@@ -35,23 +38,16 @@ public class RiceService {
|
|
|
|
|
|
private static final int MAX_NICKNAME_LENGTH = 6;
|
|
|
private static final String UPDATE_SUCCESS_MSG = "修改成功";
|
|
|
-
|
|
|
- @Autowired
|
|
|
private RiceRepo riceRepo;
|
|
|
-
|
|
|
-
|
|
|
private RiceUserWaterDropRecordRepo riceUserWaterDropRecordRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
+ private RiceOperationRecordRepo riceOperationRecordRepo;
|
|
|
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 Rice getCurrentRiceUser(User authenticatedUser) throws BusinessException {
|
|
|
Long id = authenticatedUser.getId();
|
|
|
Optional<User> byId = userRepo.findByIdAndDelFalse(id);
|
|
|
@@ -64,13 +60,11 @@ public class RiceService {
|
|
|
} else {
|
|
|
throw new BusinessException("用户不存在");
|
|
|
}
|
|
|
-
|
|
|
Optional<Rice> byUserId = riceRepo.findByUserId(id);
|
|
|
if (byUserId.isPresent()) {
|
|
|
Rice rice = byUserId.get();
|
|
|
return rice;
|
|
|
} else {
|
|
|
-
|
|
|
Rice rice = new Rice();
|
|
|
rice.setUserId(id);
|
|
|
rice.setAvatar(avatar);
|
|
|
@@ -86,6 +80,7 @@ public class RiceService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ //修改昵称
|
|
|
public R updateNickName(Long userId, String nickname) {
|
|
|
String trimmedNickname = StringUtils.trim(nickname);
|
|
|
if (trimmedNickname.length() > MAX_NICKNAME_LENGTH) {
|
|
|
@@ -97,14 +92,12 @@ public class RiceService {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 在RiceService中,我们注入了SysConfigService和RiceRepo,这两个类的实现需要您自己完成。
|
|
|
- * 在getCurrentLevel()方法中,我们将原来在@GetMapping注解中的代码全部迁移到了这里,并将返回类型从R<String>改为了String,
|
|
|
- * 因为我们不再需要将查询结果封装为R对象了。最后,我们根据返回结果的不同,抛出了不同的异常,这些异常同样需要您自己定义实现。
|
|
|
+ * 获取当前等级
|
|
|
*
|
|
|
* @param empiricalValue
|
|
|
* @return
|
|
|
*/
|
|
|
- public String getCurrentLevel(Long empiricalValue) {
|
|
|
+ public Map<String, Object> getCurrentLevel(Long empiricalValue) {
|
|
|
String jsonString = sysConfigService.getString("rice_level");
|
|
|
User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
|
Long authId = authenticatedUser.getId();
|
|
|
@@ -116,18 +109,37 @@ public class RiceService {
|
|
|
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
|
|
|
JsonArray jsonArray = jsonReader.readArray();
|
|
|
|
|
|
- String currentLevel = null;
|
|
|
+ Long currentLevel = null;
|
|
|
+ Long currentStart = null;
|
|
|
+ Long nextStart = 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");
|
|
|
+ Long start = Long.valueOf(jsonObject.getInt("start"));
|
|
|
+
|
|
|
+ if (empiricalValue >= start && empiricalValue < (i<jsonArray.size()-1?Long.valueOf(jsonArray.getJsonObject(i+1).getInt("start")):Long.MAX_VALUE)) {
|
|
|
+ String currentLevelStr = jsonObject.getString("name").replace("Lv", "");
|
|
|
+ currentLevel = Long.parseLong(currentLevelStr);
|
|
|
+ currentStart = Long.valueOf(jsonObject.getInt("start"));
|
|
|
+
|
|
|
+ if (i < jsonArray.size() - 1) {
|
|
|
+ JsonObject nextObject = jsonArray.getJsonObject(i + 1);
|
|
|
+ nextStart = nextObject.getJsonNumber("start").longValue();
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (currentLevel != null) {
|
|
|
- return currentLevel;
|
|
|
+ if (currentLevel != null && currentStart != null) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("currentLevel", currentLevel);
|
|
|
+ double levelUpPercentage;
|
|
|
+ if (nextStart != null) {
|
|
|
+ levelUpPercentage = (double)(empiricalValue - currentStart) / (nextStart - currentStart);
|
|
|
+ } else {
|
|
|
+ levelUpPercentage = 1.0;
|
|
|
+ }
|
|
|
+ result.put("levelUpPercentage", levelUpPercentage);
|
|
|
+ return result;
|
|
|
} else {
|
|
|
throw new BusinessException("没有配置对应的等级对应经验值");
|
|
|
}
|
|
|
@@ -137,6 +149,36 @@ public class RiceService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 任务初始化.显示各个任务能否点击
|
|
|
+ *
|
|
|
+ * @return R<?>
|
|
|
+ */
|
|
|
+ public R<?> taskInitialization() {
|
|
|
+ User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
|
+ Long authId = authenticatedUser.getId();
|
|
|
+ Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|
|
|
+ if (byUserId.isPresent()) {
|
|
|
+ Rice rice = byUserId.get();
|
|
|
+ Long lastSignInTime = rice.getLastSignInTime();
|
|
|
+ Long currentTime = System.currentTimeMillis();
|
|
|
+ // 判断上次签到时间是否为空,如果为空,则默认为从未签到过
|
|
|
+ if (lastSignInTime == null) {
|
|
|
+ return R.success("未签到").add("isSignedIn", false).add("exchangeCount", rice.getExchangeCount() == null ? 0 : rice.getExchangeCount()).add("waterDropCount", rice.getWaterDropCount());
|
|
|
+ }
|
|
|
+ // 判断今天是否已经签到过
|
|
|
+ if (DateUtils.isSameDay(new Date(lastSignInTime), new Date(currentTime))) {
|
|
|
+ return R.success("已签到").add("isSignedIn", true).add("exchangeCount", rice.getExchangeCount() == null ? 0 : rice.getExchangeCount()).add("waterDropCount", rice.getWaterDropCount());
|
|
|
+ } else {
|
|
|
+ return R.success("未签到").add("isSignedIn", false).add("exchangeCount", rice.getExchangeCount() == null ? 0 : rice.getExchangeCount()).add("waterDropCount", rice.getWaterDropCount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.error("查询失败");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前用户积分
|
|
|
*/
|
|
|
@@ -214,6 +256,8 @@ public class RiceService {
|
|
|
if (byUserId.isPresent()) {
|
|
|
Rice rice = byUserId.get();
|
|
|
Long waterDropCount = rice.getWaterDropCount();
|
|
|
+ Long beforeWaterDropCount = rice.getWaterDropCount();
|
|
|
+ Long beforeEmpiricalValue = rice.getEmpiricalValue();
|
|
|
// 如果水滴次数为 0,返回不能浇水的提示
|
|
|
if (waterDropCount == 0) {
|
|
|
return R.error("水滴次数已用完,无法浇水").add("can", false);
|
|
|
@@ -222,19 +266,12 @@ public class RiceService {
|
|
|
rice.setWaterDropCount(waterDropCount - 1);
|
|
|
rice.setEmpiricalValue(rice.getEmpiricalValue()+2);
|
|
|
riceRepo.save(rice);
|
|
|
-
|
|
|
- // 记录浇水记录
|
|
|
- RiceUserWaterDropRecord record = new RiceUserWaterDropRecord();
|
|
|
- record.setUserId(authId);
|
|
|
- record.setWateringTime(LocalDateTime.now());
|
|
|
- riceUserWaterDropRecordRepo.save(record);
|
|
|
-
|
|
|
+ createRiceOperationRecord(authId, RiceOperationType.WATER_DROP, 1L,beforeWaterDropCount , rice.getWaterDropCount());
|
|
|
+ createRiceOperationRecord(authId, RiceOperationType.EMPIRICAL_VALUE, 2L,beforeEmpiricalValue , rice.getEmpiricalValue());
|
|
|
return R.success("浇水成功").add("can", true).add("time", waterDropCount - 1);
|
|
|
}
|
|
|
return R.error("浇水失败").add("can", false);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
//根据用户ID和今日时间获取今日浇水次数的方法
|
|
|
public Long getTodayWateringCount(Long userId) {
|
|
|
List<RiceUserWaterDropRecord> records = riceUserWaterDropRecordRepo.findByUserId(userId);
|
|
|
@@ -249,7 +286,7 @@ public class RiceService {
|
|
|
* @param rice
|
|
|
* @return
|
|
|
*/
|
|
|
- public int getWaterDropNeededForLevelUp(Rice rice) {
|
|
|
+ public Long getWaterDropNeededForLevelUp(Rice rice) {
|
|
|
Long currentEmpiricalValue = rice.getEmpiricalValue();
|
|
|
String jsonString = sysConfigService.getString("rice_level");
|
|
|
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
|
|
|
@@ -257,15 +294,15 @@ public class RiceService {
|
|
|
|
|
|
for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
JsonObject jsonObject = jsonArray.getJsonObject(i);
|
|
|
- int start = jsonObject.getInt("start");
|
|
|
- int nextStart = 0;
|
|
|
+ Long start = Long.valueOf(jsonObject.getInt("start"));
|
|
|
+ Long nextStart = 0L;
|
|
|
if (i + 1 < jsonArray.size()) {
|
|
|
JsonObject nextJsonObject = jsonArray.getJsonObject(i + 1);
|
|
|
- nextStart = nextJsonObject.getInt("start");
|
|
|
+ nextStart = Long.valueOf(nextJsonObject.getInt("start"));
|
|
|
}
|
|
|
if (currentEmpiricalValue >= start && currentEmpiricalValue < nextStart) {
|
|
|
- int levelUpEmpiricalValue = (int) (nextStart - currentEmpiricalValue);
|
|
|
- int waterDropNeededForLevelUp = levelUpEmpiricalValue / 2;
|
|
|
+ Long levelUpEmpiricalValue = nextStart - currentEmpiricalValue;
|
|
|
+ Long waterDropNeededForLevelUp = levelUpEmpiricalValue / 2;
|
|
|
return waterDropNeededForLevelUp;
|
|
|
}
|
|
|
}
|
|
|
@@ -281,12 +318,15 @@ public class RiceService {
|
|
|
Rice rice = byUserId.get();
|
|
|
Long lastSignInTime = rice.getLastSignInTime();
|
|
|
Long currentTime = System.currentTimeMillis();
|
|
|
+ Long beforeWaterDropCount = rice.getWaterDropCount();
|
|
|
+ Long beforeEmpiricalValue = rice.getEmpiricalValue();
|
|
|
// 判断上次签到时间是否为空,如果为空,则默认为从未签到过
|
|
|
if (lastSignInTime == null) {
|
|
|
rice.setWaterDropCount(rice.getWaterDropCount() + 1);
|
|
|
rice.setSignCount(rice.getSignCount() + 1);
|
|
|
rice.setLastSignInTime(currentTime);
|
|
|
riceRepo.save(rice);
|
|
|
+ createRiceOperationRecord(userId,RiceOperationType.WATER_DROP, (long) 1,beforeWaterDropCount,rice.getWaterDropCount());
|
|
|
return R.success("签到成功").add("can",true).add("waterDropCount",rice.getWaterDropCount());
|
|
|
}
|
|
|
// 判断今天是否已经签到过
|
|
|
@@ -298,20 +338,23 @@ public class RiceService {
|
|
|
rice.setSignCount(rice.getSignCount() + 1);
|
|
|
rice.setLastSignInTime(currentTime);
|
|
|
riceRepo.save(rice);
|
|
|
+ createRiceOperationRecord(userId,RiceOperationType.WATER_DROP, (long) 1,beforeWaterDropCount,rice.getWaterDropCount());
|
|
|
return R.success("签到成功").add("can",true).add("waterDropCount",rice.getWaterDropCount());
|
|
|
}
|
|
|
return R.error("签到失败").add("can",false);
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Transactional
|
|
|
+
|
|
|
public R<?> exchangeScoreForWaterDrop(User authenticatedUser) {
|
|
|
Long authId = authenticatedUser.getId();
|
|
|
Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|
|
|
if (byUserId.isPresent()) {
|
|
|
Rice rice = byUserId.get();
|
|
|
Long selfScore = rice.getSelfScore();
|
|
|
+ Long beforeSelfScore = rice.getSelfScore();
|
|
|
Integer waterDropCount = Math.toIntExact(rice.getWaterDropCount());
|
|
|
+ Long beforeWaterDropCount = rice.getWaterDropCount();
|
|
|
|
|
|
// 每次兑换需要消耗的积分和最大兑换次数
|
|
|
int exchangeScore = 2;
|
|
|
@@ -324,7 +367,9 @@ public class RiceService {
|
|
|
if (exchangeCount > 0) {
|
|
|
rice.setSelfScore(selfScore - totalScore);
|
|
|
rice.setWaterDropCount((long) (waterDropCount + exchangeCount));
|
|
|
+ rice.setExchangeCount(exchangeCount);
|
|
|
riceRepo.save(rice);
|
|
|
+ createRiceOperationRecord(authId,RiceOperationType.WATER_DROP, (long) exchangeCount,beforeWaterDropCount,rice.getWaterDropCount());
|
|
|
return R.success("兑换成功").add("exchangeCount", exchangeCount).add("waterDropCount", rice.getWaterDropCount());
|
|
|
} else {
|
|
|
return R.error("兑换失败,当前积分不足").add("exchangeCount", 0).add("waterDropCount", rice.getWaterDropCount());
|
|
|
@@ -334,4 +379,15 @@ public class RiceService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private void createRiceOperationRecord(Long userId, RiceOperationType type, Long amount, Long beforeAmount, Long afterAmount) {
|
|
|
+ RiceOperationRecord record = new RiceOperationRecord();
|
|
|
+ record.setUserId(userId);
|
|
|
+ record.setType(type);
|
|
|
+ record.setAmount(amount);
|
|
|
+ record.setBeforeAmount(beforeAmount);
|
|
|
+ record.setAfterAmount(afterAmount);
|
|
|
+ riceOperationRecordRepo.save(record);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|