|
@@ -11,6 +11,7 @@ import com.izouma.walkchina.exception.ServiceException;
|
|
|
import com.izouma.walkchina.repo.*;
|
|
import com.izouma.walkchina.repo.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.domain.Example;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
@@ -90,6 +91,19 @@ public class JourneyService {
|
|
|
.build();
|
|
.build();
|
|
|
journeyStageRepository.save(journeyStage);
|
|
journeyStageRepository.save(journeyStage);
|
|
|
|
|
|
|
|
|
|
+ stageAwardRepository.save(StageAward.builder()
|
|
|
|
|
+ .userId(userId)
|
|
|
|
|
+ .journeyId(userJourney.getId())
|
|
|
|
|
+ .stageId(journeyStage.getId())
|
|
|
|
|
+ .latitude(destination.getLatitude())
|
|
|
|
|
+ .longitude(destination.getLongitude())
|
|
|
|
|
+ .needProgress(1D)
|
|
|
|
|
+ .needSteps(journeyStage.getSteps())
|
|
|
|
|
+ .coin(BigDecimal.ZERO)
|
|
|
|
|
+ .arrival(true)
|
|
|
|
|
+ .received(false)
|
|
|
|
|
+ .build());
|
|
|
|
|
+
|
|
|
userInfo.setWalkCities(Optional.ofNullable(userInfo.getWalkCities()).orElse(0) + 1);
|
|
userInfo.setWalkCities(Optional.ofNullable(userInfo.getWalkCities()).orElse(0) + 1);
|
|
|
userInfoRepository.save(userInfo);
|
|
userInfoRepository.save(userInfo);
|
|
|
}
|
|
}
|
|
@@ -97,8 +111,8 @@ public class JourneyService {
|
|
|
public void newDestination(Long userId, Long id) {
|
|
public void newDestination(Long userId, Long id) {
|
|
|
City destination = cityRepository.findById(id).orElseThrow(new ServiceException("目的地不存在"));
|
|
City destination = cityRepository.findById(id).orElseThrow(new ServiceException("目的地不存在"));
|
|
|
UserJourney userJourney = userJourneyRepository.findByUserId(userId);
|
|
UserJourney userJourney = userJourneyRepository.findByUserId(userId);
|
|
|
- JourneyStage journeyStage = journeyStageRepository.findByUserIdAndJourneyIdOrderByIdDesc(userId, userJourney
|
|
|
|
|
- .getId());
|
|
|
|
|
|
|
+ JourneyStage journeyStage = journeyStageRepository.findByUserIdAndJourneyIdOrderByCreatedAtDesc(userId, userJourney
|
|
|
|
|
+ .getId()).stream().findFirst().orElseThrow(new ServiceException("无记录"));
|
|
|
DirectionResponse directionResponse = mapService.direction(new Location(journeyStage.getDestination()
|
|
DirectionResponse directionResponse = mapService.direction(new Location(journeyStage.getDestination()
|
|
|
.getLatitude(), journeyStage
|
|
.getLatitude(), journeyStage
|
|
|
.getDestination().getLongitude()),
|
|
.getDestination().getLongitude()),
|
|
@@ -127,6 +141,19 @@ public class JourneyService {
|
|
|
newPolyline.set(1, newPolyline.get(1) * 1000000 - polyline.get(polyline.size() - 1) * 1000000);
|
|
newPolyline.set(1, newPolyline.get(1) * 1000000 - polyline.get(polyline.size() - 1) * 1000000);
|
|
|
userJourney.getPolyline().addAll(newPolyline);
|
|
userJourney.getPolyline().addAll(newPolyline);
|
|
|
userJourneyRepository.save(userJourney);
|
|
userJourneyRepository.save(userJourney);
|
|
|
|
|
+
|
|
|
|
|
+ stageAwardRepository.save(StageAward.builder()
|
|
|
|
|
+ .userId(userId)
|
|
|
|
|
+ .journeyId(userJourney.getId())
|
|
|
|
|
+ .stageId(newStage.getId())
|
|
|
|
|
+ .latitude(destination.getLatitude())
|
|
|
|
|
+ .longitude(destination.getLongitude())
|
|
|
|
|
+ .needProgress(1D)
|
|
|
|
|
+ .needSteps(newStage.getSteps())
|
|
|
|
|
+ .coin(BigDecimal.ZERO)
|
|
|
|
|
+ .arrival(true)
|
|
|
|
|
+ .received(false)
|
|
|
|
|
+ .build());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void createStageAward(UserJourney userJourney, JourneyStage journeyStage, Long needSteps) {
|
|
private void createStageAward(UserJourney userJourney, JourneyStage journeyStage, Long needSteps) {
|
|
@@ -152,6 +179,7 @@ public class JourneyService {
|
|
|
.longitude(extracted.get(end))
|
|
.longitude(extracted.get(end))
|
|
|
.journeyId(userJourney.getId())
|
|
.journeyId(userJourney.getId())
|
|
|
.coin(coin)
|
|
.coin(coin)
|
|
|
|
|
+ .originCoin(coin)
|
|
|
.needProgress(progress)
|
|
.needProgress(progress)
|
|
|
.needSteps((long) (progress * journeyStage
|
|
.needSteps((long) (progress * journeyStage
|
|
|
.getDistance() / AppConstants.STEP_TO_DISTANCE_RATE))
|
|
.getDistance() / AppConstants.STEP_TO_DISTANCE_RATE))
|
|
@@ -187,7 +215,7 @@ public class JourneyService {
|
|
|
latestStage.setSteps((long) (latestStage.getDistance() / AppConstants.STEP_TO_DISTANCE_RATE));
|
|
latestStage.setSteps((long) (latestStage.getDistance() / AppConstants.STEP_TO_DISTANCE_RATE));
|
|
|
latestStage.setCurrentSteps(steps);
|
|
latestStage.setCurrentSteps(steps);
|
|
|
double progress = steps * AppConstants.STEP_TO_DISTANCE_RATE / latestStage.getDistance();
|
|
double progress = steps * AppConstants.STEP_TO_DISTANCE_RATE / latestStage.getDistance();
|
|
|
- progress = progress > 1 ? 1 : (progress < 0 ? 0 : progress);
|
|
|
|
|
|
|
+ progress = Math.max(0, Math.min(1, progress));
|
|
|
latestStage.setProgress(progress);
|
|
latestStage.setProgress(progress);
|
|
|
journeyStageRepository.save(latestStage);
|
|
journeyStageRepository.save(latestStage);
|
|
|
|
|
|
|
@@ -208,12 +236,30 @@ public class JourneyService {
|
|
|
messageRepository.save(msg);
|
|
messageRepository.save(msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- createStageAward(userJourney, latestStage, steps + AppConstants.STAGE_AWARD_STEPS);
|
|
|
|
|
|
|
+ if (progress == 1) {
|
|
|
|
|
+ StageAward example = StageAward.builder()
|
|
|
|
|
+ .stageId(latestStage.getId())
|
|
|
|
|
+ .journeyId(latestStage.getJourneyId())
|
|
|
|
|
+ .arrival(true)
|
|
|
|
|
+ .received(false)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ StageAward stageAward = stageAwardRepository.findAll(Example.of(example)).stream().findFirst().orElse(null);
|
|
|
|
|
+ if (stageAward != null) {
|
|
|
|
|
+ if (Optional.ofNullable(stageAward.getCoin()).orElse(BigDecimal.ZERO).compareTo(BigDecimal.ZERO) == 0) {
|
|
|
|
|
+ UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
|
|
|
|
|
+ stageAward.setCoin(userInfo.getPrice());
|
|
|
|
|
+ stageAward.setOriginCoin(userInfo.getPrice());
|
|
|
|
|
+ stageAwardRepository.save(stageAward);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ createStageAward(userJourney, latestStage, steps + AppConstants.STAGE_AWARD_STEPS);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public JourneyStageStat finishJourneyStage(Long userId) {
|
|
public JourneyStageStat finishJourneyStage(Long userId) {
|
|
|
UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
|
|
UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
|
|
|
- JourneyStage latestStage = journeyStageRepository.findUserLatest(userId)
|
|
|
|
|
|
|
+ JourneyStage latestStage = journeyStageRepository.findUserLatest(userId).stream().findFirst()
|
|
|
.orElseThrow(new ServiceException("无数据"));
|
|
.orElseThrow(new ServiceException("无数据"));
|
|
|
if (latestStage.getProgress() < 1) {
|
|
if (latestStage.getProgress() < 1) {
|
|
|
throw new ServiceException("当前阶段未完成");
|
|
throw new ServiceException("当前阶段未完成");
|
|
@@ -221,9 +267,6 @@ public class JourneyService {
|
|
|
|
|
|
|
|
LocalDateTime finishAt = LocalDateTime.now();
|
|
LocalDateTime finishAt = LocalDateTime.now();
|
|
|
if (latestStage.getFinishAt() == null) {
|
|
if (latestStage.getFinishAt() == null) {
|
|
|
- coinService.balanceChange(userInfo, latestStage
|
|
|
|
|
- .getAward(), AppConstants.CoinRecordType.ARRIVAL, Strings.REMARK_ARRIVAL,
|
|
|
|
|
- String.format(Strings.MSG_ARRIVAL, latestStage.getDestination().getName(), latestStage.getAward()));
|
|
|
|
|
userInfo.setWalkCities(Optional.ofNullable(userInfo.getWalkCities()).orElse(0) + 1);
|
|
userInfo.setWalkCities(Optional.ofNullable(userInfo.getWalkCities()).orElse(0) + 1);
|
|
|
userInfoService.updateUserLevel(userInfo);
|
|
userInfoService.updateUserLevel(userInfo);
|
|
|
|
|
|
|
@@ -234,8 +277,14 @@ public class JourneyService {
|
|
|
}
|
|
}
|
|
|
Long mySteps = walkDataRepository.sumUserWalkSteps(userId, latestStage.getCreatedAt(), finishAt).orElse(0L);
|
|
Long mySteps = walkDataRepository.sumUserWalkSteps(userId, latestStage.getCreatedAt(), finishAt).orElse(0L);
|
|
|
Long teamSteps = walkDataRepository.sumTeamWalkSteps(userId, latestStage.getCreatedAt(), finishAt).orElse(0L);
|
|
Long teamSteps = walkDataRepository.sumTeamWalkSteps(userId, latestStage.getCreatedAt(), finishAt).orElse(0L);
|
|
|
|
|
+ StageAward example = StageAward.builder()
|
|
|
|
|
+ .stageId(latestStage.getId())
|
|
|
|
|
+ .journeyId(latestStage.getJourneyId())
|
|
|
|
|
+ .arrival(true)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ StageAward stageAward = stageAwardRepository.findAll(Example.of(example)).stream().findFirst().orElseThrow(new ServiceException(""));
|
|
|
JourneyStageStat stageStat = JourneyStageStat.builder()
|
|
JourneyStageStat stageStat = JourneyStageStat.builder()
|
|
|
- .award(latestStage.getAward())
|
|
|
|
|
|
|
+ .award(stageAward.getOriginCoin())
|
|
|
.mySteps(mySteps)
|
|
.mySteps(mySteps)
|
|
|
.teamSteps(teamSteps)
|
|
.teamSteps(teamSteps)
|
|
|
.days((int) DAYS
|
|
.days((int) DAYS
|