Переглянути джерело

fix 公众号取关、关注状态更新

zoujiajian 3 роки тому
батько
коміт
29e0034367

+ 16 - 10
netflix-service/src/main/java/com/cyksj/service/gzh/factory/EventMsgService.java

@@ -21,6 +21,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 
+import java.util.Optional;
+
 /*
  *项目名: netflix
  *文件名: EventMsgService
@@ -113,11 +115,7 @@ public class EventMsgService implements GzhMsgService {
 				user.setUnionid(info.getUnionid());
 				userMapper.updateById(user);
 			}
-			UserGzhRelation userGzhRelation = userGzhRelationMapper.selectOne(Wrappers.lambdaQuery(UserGzhRelation.class)
-					.eq(UserGzhRelation::getOpenId, msgEvent.getFromUserName()).last("limit 1"));
-			if (userGzhRelation == null) {
-				saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
-			}
+			saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
 		}
 		//场景值扫描公众号二维码 登录
 		if (StrUtil.isNotEmpty(sceneKey)) {
@@ -127,13 +125,17 @@ public class EventMsgService implements GzhMsgService {
 	}
 
 	private void saveGzhUserRelation(MsgEvent msgEvent, Long userId, String openId, String unionid) {
-		UserGzhRelation userGzhRelation = new UserGzhRelation();
-		userGzhRelation.setUserId(userId);
-
 		WxApp wxApp = wxAppMapper.selectOne(Wrappers.lambdaQuery(WxApp.class)
 				.eq(WxApp::getGzhOriginalId, msgEvent.getToUserName()).last("limit 1"));
 		if (wxApp != null) {
-			userGzhRelation.setWxAppId(wxApp.getId());
+			Long wxAppId = wxApp.getId();
+			UserGzhRelation userGzhRelation = userGzhRelationMapper.selectOne(Wrappers.lambdaQuery(UserGzhRelation.class)
+					.eq(UserGzhRelation::getWxAppId, wxAppId)
+					.eq(UserGzhRelation::getOpenId, msgEvent.getFromUserName()).last("limit 1"));
+			userGzhRelation = Optional.ofNullable(userGzhRelation).orElseGet(() -> new UserGzhRelation());
+
+			userGzhRelation.setUserId(userId);
+			userGzhRelation.setWxAppId(wxAppId);
 			userGzhRelation.setIsSubscribe(true);
 			userGzhRelation.setEvent(msgEvent.getEvent());
 			userGzhRelation.setOpenId(openId);
@@ -142,7 +144,11 @@ public class EventMsgService implements GzhMsgService {
 				userGzhRelation.setIsQrScene(true);
 			}
 			try {
-				userGzhRelationMapper.insert(userGzhRelation);
+				if (userGzhRelation.getId() != null) {
+					userGzhRelationMapper.insert(userGzhRelation);
+					return;
+				}
+				userGzhRelationMapper.updateById(userGzhRelation);
 			} catch (DuplicateKeyException e) {
 
 			}