JourneyService.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.izouma.walkchina.service;
  2. import com.izouma.walkchina.constant.AppConstants;
  3. import com.izouma.walkchina.domain.*;
  4. import com.izouma.walkchina.dto.Location;
  5. import com.izouma.walkchina.constant.Strings;
  6. import com.izouma.walkchina.dto.webservice.DirectionResponse;
  7. import com.izouma.walkchina.dto.webservice.MapRoute;
  8. import com.izouma.walkchina.exception.ServiceException;
  9. import com.izouma.walkchina.repo.*;
  10. import org.joda.time.Days;
  11. import org.joda.time.LocalDate;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import java.math.BigDecimal;
  15. import java.util.Date;
  16. import java.util.List;
  17. import java.util.Optional;
  18. @Service
  19. public class JourneyService {
  20. @Autowired
  21. private UserJourneyRepository userJourneyRepository;
  22. @Autowired
  23. private JourneyStageRepository journeyStageRepository;
  24. @Autowired
  25. private WalkDataRepository walkDataRepository;
  26. @Autowired
  27. private TeamMemberRepository teamMemberRepository;
  28. @Autowired
  29. private MessageRepository messageRepository;
  30. @Autowired
  31. private CityRepository cityRepository;
  32. @Autowired
  33. private MapService mapService;
  34. public boolean hasJourney(Long userId) {
  35. return userJourneyRepository.findByUserId(userId) != null;
  36. }
  37. public void newJourney(Long userId, Long originId, Long destinationId) {
  38. City origin = cityRepository.findById(originId).orElse(null);
  39. if (origin == null) {
  40. throw new ServiceException("起始地不存在");
  41. }
  42. City destination = cityRepository.findById(destinationId).orElse(null);
  43. if (destination == null) {
  44. throw new ServiceException("目的地不存在");
  45. }
  46. DirectionResponse directionResponse = mapService.direction(new Location(origin.getLatitude(), origin.getLongitude()),
  47. new Location(destination.getLatitude(), destination.getLongitude()));
  48. if (directionResponse.getStatus() != 0) {
  49. throw new ServiceException(directionResponse.getMessage());
  50. }
  51. UserJourney userJourney = UserJourney.builder()
  52. .userId(userId)
  53. .originId(originId)
  54. .destinationId(destinationId)
  55. .distance(directionResponse.getResult().getRoutes().get(0).getDistance())
  56. .polyline(directionResponse.getResult().getRoutes().get(0).getPolyline())
  57. .build();
  58. userJourney = userJourneyRepository.save(userJourney);
  59. int distance = directionResponse.getResult().getRoutes().get(0).getDistance();
  60. JourneyStage journeyStage = JourneyStage.builder()
  61. .userId(userId)
  62. .journeyId(userJourney.getId())
  63. .originId(originId)
  64. .destinationId(destinationId)
  65. .polyline(directionResponse.getResult().getRoutes().get(0).getPolyline())
  66. .routeSteps(directionResponse.getResult().getRoutes().get(0).getSteps())
  67. .distance(directionResponse.getResult().getRoutes().get(0).getDistance())
  68. .progress(.0)
  69. .steps(Math.round(distance / AppConstants.STEP_TO_DISTANCE_RATE))
  70. .award(BigDecimal.valueOf(Math.round(distance * AppConstants.DISTANCE_TO_COIN_RATE)))
  71. .build();
  72. journeyStage = journeyStageRepository.save(journeyStage);
  73. }
  74. public void newDestination(Long userId, Long id) {
  75. City destination = cityRepository.findById(id).orElse(null);
  76. if (destination == null) {
  77. throw new ServiceException("目的地不存在");
  78. }
  79. UserJourney userJourney = userJourneyRepository.findByUserId(userId);
  80. JourneyStage journeyStage = journeyStageRepository.findByUserIdAndJourneyIdOrderByIdDesc(userId, userJourney.getId());
  81. DirectionResponse directionResponse = mapService.direction(new Location(journeyStage.getDestination().getLatitude(), journeyStage.getDestination().getLongitude()),
  82. new Location(destination.getLatitude(), destination.getLongitude()));
  83. if (directionResponse.getStatus() != 0) {
  84. throw new ServiceException(directionResponse.getMessage());
  85. }
  86. MapRoute route = directionResponse.getResult().getRoutes().get(0);
  87. JourneyStage newStage = JourneyStage.builder()
  88. .userId(userId)
  89. .journeyId(userJourney.getId())
  90. .originId(journeyStage.getDestination().getId())
  91. .destinationId(destination.getId())
  92. .distance(route.getDistance())
  93. .progress(.0)
  94. .polyline(route.getPolyline())
  95. .routeSteps(route.getSteps())
  96. .build();
  97. newStage = journeyStageRepository.save(newStage);
  98. List<Double> polyline = mapService.extractPolyline(userJourney.getPolyline());
  99. route.getPolyline().set(0, route.getPolyline().get(0) * 1000000 - polyline.get(polyline.size() - 2) * 1000000);
  100. route.getPolyline().set(1, route.getPolyline().get(1) * 1000000 - polyline.get(polyline.size() - 1) * 1000000);
  101. userJourney.getPolyline().addAll(route.getPolyline());
  102. userJourneyRepository.save(userJourney);
  103. }
  104. public void updateUserJourney(Long userId, Date date) {
  105. UserJourney userJourney = userJourneyRepository.findByUserId(userId);
  106. if (userJourney == null) {
  107. throw new ServiceException("无数据");
  108. }
  109. List<JourneyStage> stageList = journeyStageRepository.findAllByJourneyIdOrderById(userJourney.getId());
  110. if (stageList.size() == 0) {
  111. throw new ServiceException("无数据");
  112. }
  113. JourneyStage latestStage = stageList.get(stageList.size() - 1);
  114. Long steps = 0L;
  115. WalkData todayWalkData = walkDataRepository.findByUserIdAndDate(userId, date);
  116. steps += walkDataRepository.sumTeamWalkSteps(userId, latestStage.getCreatedAt(),
  117. Optional.of(latestStage.getFinishAt()).orElse(new Date()))
  118. .orElse(0);
  119. latestStage.setSteps(steps);
  120. double progress = steps * AppConstants.STEP_TO_DISTANCE_RATE / latestStage.getDistance();
  121. progress = progress > 1 ? 1 : progress;
  122. latestStage.setProgress(progress);
  123. journeyStageRepository.save(latestStage);
  124. if (progress == 1) {
  125. Message msg = Message.builder()
  126. .userId(userId)
  127. .content(String.format(Strings.MSG_JOURNEY_PROGRESS,
  128. latestStage.getOrigin().getName(),
  129. latestStage.getDestination().getName(),
  130. (todayWalkData != null ? todayWalkData.getSteps() : 0),
  131. calcDays(latestStage.getCreatedAt(), new Date()),
  132. steps,
  133. Math.round(progress * 100)))
  134. .build();
  135. messageRepository.save(msg);
  136. }
  137. }
  138. private int calcDays(Date start, Date end) {
  139. return Days.daysBetween(LocalDate.fromDateFields(start), LocalDate.fromDateFields(end)).getDays();
  140. }
  141. }