Browse Source

2019/08/27

x1ongzhu 6 years ago
parent
commit
ba27c07960

+ 24 - 0
src/main/java/com/izouma/walkchina/event/SendJoinAwardEvent.java

@@ -0,0 +1,24 @@
+package com.izouma.walkchina.event;
+
+import org.springframework.context.ApplicationEvent;
+
+import java.math.BigDecimal;
+
+public class SendJoinAwardEvent extends ApplicationEvent {
+    private Long       userId;
+    private BigDecimal coin;
+
+    public SendJoinAwardEvent(Object source, Long userId, BigDecimal coin) {
+        super(source);
+        this.userId = userId;
+        this.coin = coin;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public BigDecimal getCoin() {
+        return coin;
+    }
+}

+ 23 - 0
src/main/java/com/izouma/walkchina/event/SendStepContributeMsgEvent.java

@@ -0,0 +1,23 @@
+package com.izouma.walkchina.event;
+
+import com.izouma.walkchina.domain.UserInfo;
+import org.springframework.context.ApplicationEvent;
+
+public class SendStepContributeMsgEvent extends ApplicationEvent {
+    private UserInfo userInfo;
+    private Integer  steps;
+
+    public SendStepContributeMsgEvent(Object source, UserInfo userInfo, Integer steps) {
+        super(source);
+        this.userInfo = userInfo;
+        this.steps = steps;
+    }
+
+    public UserInfo getUserInfo() {
+        return userInfo;
+    }
+
+    public Integer getSteps() {
+        return steps;
+    }
+}

+ 9 - 0
src/main/java/com/izouma/walkchina/event/UpdateLevelEvent.java

@@ -0,0 +1,9 @@
+package com.izouma.walkchina.event;
+
+import org.springframework.context.ApplicationEvent;
+
+public class UpdateLevelEvent extends ApplicationEvent {
+    public UpdateLevelEvent(Object source) {
+        super(source);
+    }
+}

+ 9 - 0
src/main/java/com/izouma/walkchina/event/UpdatePriceEvent.java

@@ -0,0 +1,9 @@
+package com.izouma.walkchina.event;
+
+import org.springframework.context.ApplicationEvent;
+
+public class UpdatePriceEvent extends ApplicationEvent {
+    public UpdatePriceEvent(Object source) {
+        super(source);
+    }
+}

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

@@ -7,10 +7,14 @@ import com.izouma.walkchina.dto.JourneyStageStat;
 import com.izouma.walkchina.dto.Location;
 import com.izouma.walkchina.dto.webservice.DirectionResponse;
 import com.izouma.walkchina.dto.webservice.MapRoute;
+import com.izouma.walkchina.event.SendJoinAwardEvent;
+import com.izouma.walkchina.event.UpdateLevelEvent;
 import com.izouma.walkchina.exception.ServiceException;
 import com.izouma.walkchina.repo.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.EventListener;
 import org.springframework.data.domain.Example;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
@@ -50,6 +54,8 @@ public class JourneyService {
     private CoinService            coinService;
     @Autowired
     private UserInfoService        userInfoService;
+    @Autowired
+    private ApplicationContext     applicationContext;
 
     public boolean hasJourney(Long userId) {
         return userJourneyRepository.findByUserId(userId) != null;
@@ -158,7 +164,10 @@ public class JourneyService {
     }
 
     @Async
-    public void sendJoinAward(Long userId, BigDecimal coin) {
+    @EventListener
+    public void sendJoinAward(SendJoinAwardEvent event) {
+        Long userId = event.getUserId();
+        BigDecimal coin = event.getCoin();
         UserInfo userInfo = userInfoRepository.findById(userId).orElseThrow(new ServiceException("用户不存在"));
         UserJourney userJourney = userJourneyRepository.findByUserId(userId);
         JourneyStage journeyStage = journeyStageRepository.findFirstByUserIdAndJourneyIdOrderByCreatedAtDesc(userId, userJourney
@@ -292,7 +301,6 @@ public class JourneyService {
         LocalDateTime finishAt = LocalDateTime.now();
         if (latestStage.getFinishAt() == null) {
             userInfo.setWalkCities(Optional.ofNullable(userInfo.getWalkCities()).orElse(0) + 1);
-            userInfoService.updateUserLevel(userInfo);
 
             latestStage.setFinishAt(finishAt);
             journeyStageRepository.save(latestStage);
@@ -317,6 +325,7 @@ public class JourneyService {
                                                      .destination(latestStage.getDestination())
                                                      .totalSteps(mySteps + teamSteps)
                                                      .build();
+        applicationContext.publishEvent(new UpdateLevelEvent(userInfo));
         return stageStat;
     }
 }

+ 6 - 1
src/main/java/com/izouma/walkchina/service/MessageService.java

@@ -5,10 +5,12 @@ import com.izouma.walkchina.constant.Strings;
 import com.izouma.walkchina.domain.Message;
 import com.izouma.walkchina.domain.UserInfo;
 import com.izouma.walkchina.dto.UserDTO;
+import com.izouma.walkchina.event.SendStepContributeMsgEvent;
 import com.izouma.walkchina.repo.MessageRepository;
 import com.izouma.walkchina.repo.TeamMemberRepository;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.event.EventListener;
 import org.springframework.data.domain.*;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
@@ -39,7 +41,10 @@ public class MessageService {
     }
 
     @Async
-    public void sendStepContributeMsg(UserInfo userInfo, Integer steps) {
+    @EventListener
+    public void sendStepContributeMsg(SendStepContributeMsgEvent sendStepContributeMsgEvent) {
+        UserInfo userInfo = sendStepContributeMsgEvent.getUserInfo();
+        Integer steps = sendStepContributeMsgEvent.getSteps();
         List<UserDTO> leaders = teamMemberRepository.findLeader(userInfo.getId(), LocalDate.now());
         for (UserDTO leader : leaders) {
             Message msg1 = messageRepository.findFirstByUserIdAndSecondUserIdAndTypeOrderByCreatedAtDesc(leader.getUserId(),

+ 8 - 7
src/main/java/com/izouma/walkchina/service/TeamService.java

@@ -8,12 +8,15 @@ import com.izouma.walkchina.constant.AppConstants;
 import com.izouma.walkchina.constant.Strings;
 import com.izouma.walkchina.domain.*;
 import com.izouma.walkchina.dto.UserDTO;
+import com.izouma.walkchina.event.SendStepContributeMsgEvent;
+import com.izouma.walkchina.event.UpdatePriceEvent;
 import com.izouma.walkchina.exception.ServiceException;
 import com.izouma.walkchina.repo.*;
 import com.izouma.walkchina.utils.ImageUtils;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
 import org.springframework.data.domain.Example;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
@@ -53,13 +56,11 @@ public class TeamService {
     @Autowired
     private FormIdService          formIdService;
     @Autowired
-    private UserInfoService        userInfoService;
-    @Autowired
     private RewardRecordRepository rewardRecordRepository;
     @Autowired
-    private MessageService         messageService;
-    @Autowired
     private JourneyService         journeyService;
+    @Autowired
+    private ApplicationContext     applicationContext;
 
     public void hire(Long userId, Long target) {
         friendInfoService.becomeFriend(userId, target);
@@ -125,7 +126,7 @@ public class TeamService {
                                              .build();
         teamMemberRepository.save(newTeamMember);
 
-        userInfoService.updateUserPrice(targetUserInfo);
+        applicationContext.publishEvent(new UpdatePriceEvent(targetUserInfo));
         messageRepository.save(Message.builder()
                                       .userId(target)
                                       .content(String.format(Strings.MSG_HIRED, userInfo.getNickname(), targetUserInfo.getPrice()))
@@ -143,7 +144,7 @@ public class TeamService {
 
         WalkData walkData = walkDataRepository.findByUserIdAndDate(target, LocalDate.now());
         if (walkData != null) {
-            messageService.sendStepContributeMsg(targetUserInfo, Math.toIntExact(walkData.getSteps()));
+            applicationContext.publishEvent(new SendStepContributeMsgEvent(null, userInfo, Math.toIntExact(walkData.getSteps())));
         }
     }
 
@@ -316,7 +317,7 @@ public class TeamService {
                                              .build();
         teamMemberRepository.save(newTeamMember);
 
-        userInfoService.updateUserPrice(targetUserInfo);
+        applicationContext.publishEvent(new UpdatePriceEvent(targetUserInfo));
 
         messageRepository.save(Message.builder()
                                       .userId(userId)

+ 15 - 6
src/main/java/com/izouma/walkchina/service/UserInfoService.java

@@ -9,6 +9,9 @@ import com.izouma.walkchina.domain.MonthWalkData;
 import com.izouma.walkchina.domain.UserInfo;
 import com.izouma.walkchina.dto.RankInfo;
 import com.izouma.walkchina.dto.UserWalkStats;
+import com.izouma.walkchina.event.SendJoinAwardEvent;
+import com.izouma.walkchina.event.UpdateLevelEvent;
+import com.izouma.walkchina.event.UpdatePriceEvent;
 import com.izouma.walkchina.exception.ServiceException;
 import com.izouma.walkchina.repo.*;
 import com.izouma.walkchina.security.AuthorityName;
@@ -18,6 +21,9 @@ import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
 
@@ -53,9 +59,7 @@ public class UserInfoService {
     @Autowired
     private AuthorityRepository     authorityRepository;
     @Autowired
-    private CoinService             coinService;
-    @Autowired
-    private JourneyService          journeyService;
+    private ApplicationContext      applicationContext;
 
     public UserInfo registerByUserPwd(String username, String password) {
         UserInfo userInfo = userInfoRepository.findByUsername(username);
@@ -242,7 +246,10 @@ public class UserInfoService {
     }
 
 
-    public void updateUserPrice(UserInfo userInfo) {
+    @Async
+    @EventListener
+    public void updateUserPrice(UpdatePriceEvent event) {
+        UserInfo userInfo = (UserInfo) event.getSource();
         MonthWalkData monthWalkData = monthWalkDataRepository.findByUserId(userInfo.getId()).orElse(null);
         BigDecimal price = BigDecimal.ZERO;
         if (monthWalkData != null) {
@@ -266,11 +273,13 @@ public class UserInfoService {
         userInfo.setPrice(price);
         userInfoRepository.save(userInfo);
         if (userInfo.getIsNew() != null && userInfo.getIsNew()) {
-            journeyService.sendJoinAward(userInfo.getId(), price);
+            applicationContext.publishEvent(new SendJoinAwardEvent(null, userInfo.getId(), price));
         }
     }
 
-    public void updateUserLevel(UserInfo userInfo) {
+    @EventListener
+    public void updateUserLevel(UpdateLevelEvent event) {
+        UserInfo userInfo = (UserInfo) event.getSource();
         int walkCities = Optional.ofNullable(userInfo.getWalkCities()).orElse(0);
         int level;
         if (walkCities < 4) {

+ 7 - 6
src/main/java/com/izouma/walkchina/service/WalkDataService.java

@@ -5,12 +5,15 @@ import cn.binarywang.wx.miniapp.bean.WxMaRunStepInfo;
 import com.izouma.walkchina.domain.MonthWalkData;
 import com.izouma.walkchina.domain.UserInfo;
 import com.izouma.walkchina.domain.WalkData;
+import com.izouma.walkchina.event.SendStepContributeMsgEvent;
+import com.izouma.walkchina.event.UpdatePriceEvent;
 import com.izouma.walkchina.exception.ServiceException;
 import com.izouma.walkchina.repo.MonthWalkDataRepository;
 import com.izouma.walkchina.repo.UserInfoRepository;
 import com.izouma.walkchina.repo.WalkDataRepository;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Service;
 
 import java.text.SimpleDateFormat;
@@ -33,11 +36,9 @@ public class WalkDataService {
     @Autowired
     private MonthWalkDataRepository monthWalkDataRepository;
     @Autowired
-    private UserInfoService         userInfoService;
-    @Autowired
-    private MessageService          messageService;
-    @Autowired
     private TeamService             teamService;
+    @Autowired
+    private ApplicationContext      applicationContext;
 
     public void saveWalkData(Long userId, String encryptedData, String iv) {
         log.info("saveWalkData\nuserId:{}\nencryptedData:{}\niv:{}", userId, encryptedData, iv);
@@ -68,7 +69,7 @@ public class WalkDataService {
                                          .build();
             monthWalkData.setSteps(steps);
             monthWalkDataRepository.save(monthWalkData);
-            userInfoService.updateUserPrice(userInfo);
+            applicationContext.publishEvent(new UpdatePriceEvent(userInfo));
         }
 
         wxMaRunStepInfoList.stream()
@@ -77,7 +78,7 @@ public class WalkDataService {
                                                              .toLocalDate()
                                                              .isEqual(LocalDate.now())
                                                       && wxMaRunStepInfo.getStep() > 0)
-                           .findAny().ifPresent(stepInfo -> messageService.sendStepContributeMsg(userInfo, stepInfo.getStep()));
+                           .findAny().ifPresent(stepInfo -> applicationContext.publishEvent(new SendStepContributeMsgEvent(null, userInfo, stepInfo.getStep())));
 
         teamService.updateLeaderJourney(userId);
     }