x1ongzhu 6 vuotta sitten
vanhempi
commit
eb59d3aadf

+ 2 - 0
.gitignore

@@ -30,3 +30,5 @@ build/
 
 ### VS Code ###
 .vscode/
+
+application.pid

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 3314
city1.json


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 2970
city2.json


+ 2 - 0
src/main/java/com/izouma/walkchina/WalkChinaApplication.java

@@ -4,9 +4,11 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.ApplicationPidFileWriter;
 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
+import org.springframework.scheduling.annotation.EnableAsync;
 
 @SpringBootApplication
 @EnableJpaAuditing
+@EnableAsync
 public class WalkChinaApplication {
 
     public static void main(String[] args) {

+ 1 - 0
src/main/java/com/izouma/walkchina/dto/MapRegion.java

@@ -10,4 +10,5 @@ import lombok.NoArgsConstructor;
 public class MapRegion {
     private Location northeast;
     private Location southwest;
+    private Location virtualLocation;
 }

+ 3 - 3
src/main/java/com/izouma/walkchina/service/JourneyService.java

@@ -170,13 +170,13 @@ public class JourneyService {
         }
 
         List<Double> extracted = mapService.extractPolyline(journeyStage.getPolyline());
-        int end = mapService.endPoint(journeyStage.getRouteSteps(), journeyStage.getDistance() * progress);
+        Location location = mapService.endPoint(journeyStage.getRouteSteps(), journeyStage.getDistance() * progress, journeyStage.getPolyline());
         BigDecimal coin = Optional.ofNullable(userInfo.getPrice()).orElse(AppConstants.MIN_PRICE).divide(BigDecimal.valueOf(10), RoundingMode.HALF_UP).setScale(2, RoundingMode.HALF_EVEN);
         StageAward stageAward = StageAward.builder()
                                           .userId(userJourney.getUserId())
                                           .stageId(journeyStage.getId())
-                                          .latitude(extracted.get(end - 1))
-                                          .longitude(extracted.get(end))
+                                          .latitude(location.getLatitude())
+                                          .longitude(location.getLongitude())
                                           .journeyId(userJourney.getId())
                                           .coin(coin)
                                           .originCoin(coin)

+ 91 - 4
src/main/java/com/izouma/walkchina/service/MapService.java

@@ -15,6 +15,7 @@ import com.izouma.walkchina.dto.webservice.DirectionResponse;
 import com.izouma.walkchina.dto.webservice.RouteStep;
 import com.izouma.walkchina.exception.ServiceException;
 import com.izouma.walkchina.repo.*;
+import com.izouma.walkchina.utils.LocationUtils;
 import com.izouma.walkchina.utils.SecurityUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -64,6 +65,9 @@ public class MapService {
     }
 
     public Collection<UserMarker> usersInRegion(Long userId, MapRegion mapRegion) {
+        UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
+        double dLat = mapRegion.getVirtualLocation().getLatitude() - userInfo.getLatitude();
+        double dLng = mapRegion.getVirtualLocation().getLongitude() - userInfo.getLongitude();
         List<UserMarker> list = userInfoRepository.findInRegion(userId, mapRegion.getSouthwest().getLatitude(), mapRegion.getSouthwest().getLongitude(),
                                                                 mapRegion.getNortheast().getLongitude(), mapRegion.getNortheast().getLongitude());
         for (UserMarker userMarker : list) {
@@ -94,7 +98,7 @@ public class MapService {
         City destination = Optional.ofNullable(userJourney.getDestination()).orElseThrow(new ServiceException("无记录"));
         UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
         UserMap userMap = UserMap.builder()
-                                 .polyline(minifyPolyline(userJourney.getPolyline()))
+                                 // .polyline(minifyPolyline(userJourney.getPolyline()))
                                  // .polyline(userJourney.getPolyline())
                                  .origin(origin)
                                  .destination(destination)
@@ -105,6 +109,7 @@ public class MapService {
             progressPolyline.addAll(stages.get(i).getPolyline());
         }
         JourneyStage latestStage = stages.get(stages.size() - 1);
+        userMap.setPolyline(minifyPolyline(latestStage.getPolyline()));
         userMap.setDestination(latestStage.getDestination());
         userMap.setNearOrigin(latestStage.getOrigin());
         userMap.setProgress(latestStage.getProgress());
@@ -138,12 +143,18 @@ public class MapService {
         //         break;
         //     }
         // }
-        List<Double> extra = new ArrayList<>();
         for (int i = 0; i <= endPoint(steps, latestStage.getDistance() * latestStage.getProgress()); i++) {
             progressPolyline.add(latestStage.getPolyline().get(i));
         }
 
-        userMap.setProgressPolyline(minifyPolyline(progressPolyline));
+        List<Double> subPolyline = new ArrayList<>();
+        for (int i = 0; i < stages.size() - 1; i++) {
+            subPolyline.addAll(extractPolyline(stages.get(i).getPolyline()));
+        }
+        subPolyline.addAll(subPolyline(steps, latestStage.getDistance() * latestStage.getProgress(), latestStage.getPolyline()));
+        userMap.setProgressPolyline(compressPolyline(subPolyline));
+
+        // userMap.setProgressPolyline(minifyPolyline(progressPolyline));
         // userMap.setProgressPolyline(progressPolyline);
         return userMap;
     }
@@ -170,6 +181,82 @@ public class MapService {
         return steps.get(steps.size() - 1).getPolylineIdx().get(1);
     }
 
+    List<Double> subPolyline(List<RouteStep> steps, double distance, List<Double> polyline) {
+        double d = 0;
+        double sd = 0;
+        int i = 0;
+        for (; i < steps.size(); i++) {
+            if (d + steps.get(i).getDistance() >= distance) {
+                sd = d + steps.get(i).getDistance() - distance;
+                break;
+            }
+            d += steps.get(i).getDistance();
+        }
+
+        List<Double> extracted = extractPolyline(polyline);
+        List<Double> subPolyline = new ArrayList<>();
+        for (int j = 0; j < steps.get(i).getPolylineIdx().get(0); j++) {
+            subPolyline.add(extracted.get(j));
+        }
+
+        int start = steps.get(i).getPolylineIdx().get(0);
+        int end = steps.get(i).getPolylineIdx().get(1);
+        double d1 = 0;
+        for (int j = start; j < end - 2; j += 2) {
+            double lat1 = extracted.get(j);
+            double lng1 = extracted.get(j + 1);
+            subPolyline.add(lat1);
+            subPolyline.add(lng1);
+            double lat2 = extracted.get(j + 2);
+            double lng2 = extracted.get(j + 3);
+            double dd = LocationUtils.getDistance(lat1, lng1, lat2, lng2);
+            if (d1 + dd >= sd) {
+                double p = (d1 + dd) / (d - steps.get(i).getDistance());
+                double lat3 = (lat2 - lat1) * p + lat1;
+                double lng3 = (lng2 - lng1) * p + lng1;
+                subPolyline.add(lat3);
+                subPolyline.add(lng3);
+                return subPolyline;
+            }
+            d1 += dd;
+        }
+        return subPolyline;
+    }
+
+    Location endPoint(List<RouteStep> steps, double distance, List<Double> polyline) {
+        double d = 0;
+        double sd = 0;
+        int i = 0;
+        for (; i < steps.size(); i++) {
+            if (d + steps.get(i).getDistance() >= distance) {
+                sd = d + steps.get(i).getDistance() - distance;
+                break;
+            }
+            d += steps.get(i).getDistance();
+        }
+
+        List<Double> extracted = extractPolyline(polyline);
+
+        int start = steps.get(i).getPolylineIdx().get(0);
+        int end = steps.get(i).getPolylineIdx().get(1);
+        double d1 = 0;
+        for (int j = start; j < end - 2; j += 2) {
+            double lat1 = extracted.get(j);
+            double lng1 = extracted.get(j + 1);
+            double lat2 = extracted.get(j + 2);
+            double lng2 = extracted.get(j + 3);
+            double dd = LocationUtils.getDistance(lat1, lng1, lat2, lng2);
+            if (d1 + dd >= sd) {
+                double p = (d1 + dd) / (d - steps.get(i).getDistance());
+                double lat3 = (lat2 - lat1) * p + lat1;
+                double lng3 = (lng2 - lng1) * p + lng1;
+                return new Location(lat3, lng3);
+            }
+            d1 += dd;
+        }
+        return null;
+    }
+
     List<Double> extractPolyline(List<Double> polyline) {
         List<Double> list = new ArrayList<>(polyline);
         for (int i = 2; i < list.size(); i++) {
@@ -191,7 +278,7 @@ public class MapService {
         List<Double> extracted = extractPolyline(polyline);
         list.add(polyline.get(0));
         list.add(polyline.get(1));
-        for (int i = 2; i < polyline.size(); i += 20) {
+        for (int i = 2; i < polyline.size(); i += 10) {
             double d0 = Math.abs(polyline.get(i));
             double d1 = Math.abs(polyline.get(i + 1));
             if (d0 == 0 && d1 == 0) {

+ 11 - 0
src/main/java/com/izouma/walkchina/service/TeamService.java

@@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Example;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import javax.imageio.ImageIO;
@@ -57,6 +58,8 @@ public class TeamService {
     private RewardRecordRepository rewardRecordRepository;
     @Autowired
     private MessageService         messageService;
+    @Autowired
+    private JourneyService         journeyService;
 
     public void hire(Long userId, Long target) {
         friendInfoService.becomeFriend(userId, target);
@@ -298,4 +301,12 @@ public class TeamService {
                                       .isRead(false)
                                       .build());
     }
+
+    @Async
+    public void updateLeaderJourney(Long userId) {
+        List<UserDTO> leaders = teamMemberRepository.findLeader(userId, LocalDate.now());
+        for (UserDTO userDTO : leaders) {
+            journeyService.updateUserJourney(userDTO.getUserId());
+        }
+    }
 }

+ 5 - 3
src/main/java/com/izouma/walkchina/service/UserInfoService.java

@@ -175,9 +175,11 @@ public class UserInfoService {
 
     public void updateLocation(Long userId, Double latitude, Double longitude) {
         UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
-        userInfo.setLatitude(latitude);
-        userInfo.setLongitude(longitude);
-        userInfoRepository.save(userInfo);
+        if (userInfo.getLatitude() == null) {
+            userInfo.setLatitude(latitude + Math.random() * 0.0003 + 0.0003 * (Math.random() > 0.5 ? 1 : -1));
+            userInfo.setLongitude(longitude + Math.random() * 0.0003 + 0.0003 * (Math.random() > 0.5 ? 1 : -1));
+            userInfoRepository.save(userInfo);
+        }
     }
 
     private void uploadUserMarker(UserInfo userInfo) {

+ 4 - 0
src/main/java/com/izouma/walkchina/service/WalkDataService.java

@@ -36,6 +36,8 @@ public class WalkDataService {
     private UserInfoService         userInfoService;
     @Autowired
     private MessageService          messageService;
+    @Autowired
+    private TeamService             teamService;
 
     public void saveWalkData(Long userId, String encryptedData, String iv) {
         log.info("saveWalkData\nuserId:{}\nencryptedData:{}\niv:{}", userId, encryptedData, iv);
@@ -76,5 +78,7 @@ public class WalkDataService {
                                                              .isEqual(LocalDate.now())
                                                       && wxMaRunStepInfo.getStep() > 0)
                            .findAny().ifPresent(stepInfo -> messageService.sendStepContributeMsg(userInfo, stepInfo.getStep()));
+
+        teamService.updateLeaderJourney(userId);
     }
 }

+ 1 - 1
src/test/java/com/izouma/walkchina/CommonTest.java

@@ -163,7 +163,7 @@ public class CommonTest {
                 getCity(list, jsonObject.optJSONArray("children"));
             } else if (jsonObject.getInt("level") == 3) {
                 String name = jsonObject.getString("name");
-                if (name.endsWith("市")) {
+                if (name.endsWith("市") || name.endsWith("县")) {
                     list.add(jsonObject);
                 }
             }

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä