Pārlūkot izejas kodu

营销系统-保存用户新增和活跃数据

lh 1 nedēļu atpakaļ
vecāks
revīzija
a3da92fe34

+ 90 - 1
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/service/impl/TcpDataServiceImpl.java

@@ -122,12 +122,101 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 				update.set("updateTime", LocalDateTime.now().format(DATE_TIME_FORMATTER));
 
 				mongoTemplate.updateFirst(query, update, Device.class);
+
+				syncNewUserData(existingDevice, reqDto.getDeviceInfo());
+				synUpdateUserData(existingDevice, reqDto.getDeviceInfo());
+
 				return existingDevice.getId();
 			}
 		}
 		return existingDevice.getId();
 	}
 
+	private void synUpdateUserData(Device device, String deviceInfo) {
+		if (device ==null) {
+			return;
+		}
+		String clientID = device.getClientID();
+
+		// 将deviceInfo从JSON字符串转换为JSON对象
+		JsonNode deviceInfoJson = null;
+		try {
+			if (deviceInfo != null && !deviceInfo.trim().isEmpty()) {
+				String subDeviceInfo = deviceInfo.trim().substring(deviceInfo.indexOf("{"));
+				deviceInfoJson = objectMapper.readTree(subDeviceInfo);
+			}
+		} catch (Exception e) {
+			log.error("syncActiveUserData fail = {}", e.getMessage());
+			return;
+		}
+
+		// 从JSON对象中提取需要的字段
+		String bundle = extractStringFromJson(deviceInfoJson, "bundle", "");
+		String appId = extractStringFromJson(deviceInfoJson, "appId", bundle);
+		String channel = extractStringFromJson(deviceInfoJson, "channel", "应用市场");
+		String version = extractStringFromJson(deviceInfoJson, "version", "0.0.0");
+
+		// 同步新增用户数据 -> mysql
+		// 构建mkt_stat_new_user对象
+		MktStatActiveUser activeUser = new MktStatActiveUser();
+		activeUser.setUserId(clientID);
+		activeUser.setAppId(appId);
+		activeUser.setChannel(channel);
+		activeUser.setVersion(version);
+		activeUser.setStatDate(LocalDateTime.now());
+
+		R<Boolean> result = remoteStatUserDataService.updateMktStatActiveUser(activeUser);
+		if (result.getData() == null || !result.getData()) {
+			log.error("update活跃用户记录失败:clientID = {}", clientID);
+		} else {
+			log.debug("update活跃用户记录成功:clientID = {}, appId = {}, channel = {}", clientID, appId, channel);
+		}
+	}
+
+	private void syncNewUserData(Device device, String deviceInfo) {
+		if (device ==null) {
+			return;
+		}
+
+		String clientID = device.getClientID();
+
+		// 将deviceInfo从JSON字符串转换为JSON对象
+		JsonNode deviceInfoJson = null;
+		try {
+			if (deviceInfo != null && !deviceInfo.trim().isEmpty()) {
+				String subDeviceInfo = deviceInfo.trim().substring(deviceInfo.indexOf("{"));
+				deviceInfoJson = objectMapper.readTree(subDeviceInfo);
+			}
+		} catch (Exception e) {
+			log.error("syncActiveUserData fail = {}", e.getMessage());
+			return;
+		}
+
+		// 从JSON对象中提取需要的字段
+		String bundle = extractStringFromJson(deviceInfoJson, "bundle", "");
+		String appId = extractStringFromJson(deviceInfoJson, "appId", bundle);
+		String channel = extractStringFromJson(deviceInfoJson, "channel", "应用市场");
+		String version = extractStringFromJson(deviceInfoJson, "version", "0.0.0");
+		String oldVersion = extractStringFromJson(deviceInfoJson, "oldVersion", "");
+
+		// 同步新增用户数据 -> mysql
+		// 构建mkt_stat_new_user对象
+		MktStatNewUser mktStatNewUser = new MktStatNewUser();
+		mktStatNewUser.setUserId(clientID);
+		mktStatNewUser.setAppId(appId);
+		mktStatNewUser.setChannel(channel);
+		mktStatNewUser.setVersion(version);
+		mktStatNewUser.setOldVersion(oldVersion);
+		mktStatNewUser.setStatDate(LocalDateTime.now());
+
+		R<Boolean> result = remoteStatUserDataService.updateMktStatNewUser(mktStatNewUser);
+		if (result.getData() == null || !result.getData()) {
+			log.error("update新增用户记录失败:clientID = {}", clientID);
+		} else {
+			log.debug("update新增用户记录成功:clientID = {}, appId = {}, channel = {}", clientID, appId, channel);
+		}
+	}
+
 	private void syncActiveUserData(Device device) {
 		if (device ==null) {
 			return;
@@ -194,7 +283,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 		String bundle = extractStringFromJson(deviceInfoJson, "bundle", "");
 		String appId = extractStringFromJson(deviceInfoJson, "appId", bundle);
 		String channel = extractStringFromJson(deviceInfoJson, "channel", "应用市场");
-		String version = extractStringFromJson(deviceInfoJson, "version", "1.0.0");
+		String version = extractStringFromJson(deviceInfoJson, "version", "0.0.0");
 		String oldVersion = extractStringFromJson(deviceInfoJson, "oldVersion", "");
 
 		// 同步新增用户数据 -> mysql