瀏覽代碼

Merge branch 'dev/lh' of https://s-20coaj910c.zht2.com/lwh/seo into dev/lwh

lwh 1 周之前
父節點
當前提交
9b7fa3864e

+ 4 - 0
pig-marketing/pig-marketing-biz/pom.xml

@@ -104,6 +104,10 @@
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-data-mongodb</artifactId>
 		</dependency>
+		<dependency>
+			<groupId>com.pig4cloud</groupId>
+			<artifactId>pig-statistics-api</artifactId>
+		</dependency>
 	</dependencies>
 
 	<profiles>

+ 131 - 11
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/service/impl/TcpDataServiceImpl.java

@@ -2,6 +2,7 @@ package com.pig4cloud.pig.marketing.service.impl;
 
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.pig4cloud.pig.common.core.util.R;
 import com.pig4cloud.pig.marketing.api.dto.MktMgmtPushRecordSaveDTO;
 import com.pig4cloud.pig.marketing.api.dto.mongo.PageDeviceInfoDTO;
 import com.pig4cloud.pig.marketing.api.dto.mongo.PageMessageDTO;
@@ -20,6 +21,9 @@ import com.pig4cloud.pig.marketing.config.UserStatisticsConfig;
 import com.pig4cloud.pig.marketing.repository.MessageRepository;
 import com.pig4cloud.pig.marketing.service.MktMgmtPushRecordService;
 import com.pig4cloud.pig.marketing.service.TcpDataService;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatActiveUser;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatNewUser;
+import com.pig4cloud.pig.statistics.api.feign.RemoteStatUserDataService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -39,6 +43,8 @@ import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 /**
  * @author: lwh
@@ -63,6 +69,10 @@ import java.util.stream.Collectors;
 		@Autowired
 		private MktMgmtHandPushService mktMgmtHandPushService;
 
+		private final RemoteStatUserDataService remoteStatUserDataService;
+
+		// JSON处理器
+		private final ObjectMapper objectMapper = new ObjectMapper();
 
 	// 定义时间格式器
 	private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@@ -74,10 +84,6 @@ import java.util.stream.Collectors;
 	 */
 	@Override
 	public String saveOrUpdateDevice(SaveDeviceInfoDTO reqDto) {
-		if (reqDto.getClientIp() != null) {
-			reqDto.setClientIp(reqDto.getClientIp().split(":")[0]);
-		}
-
 		// 根据客户端ID查询设备信息
 		Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
 		Device existingDevice = mongoTemplate.findOne(query, Device.class);
@@ -85,7 +91,6 @@ import java.util.stream.Collectors;
 		if (existingDevice == null) {
 			// 无数据时直接新增
 			Device device = new Device();
-			device.setClientIp(reqDto.getClientIp());
 			device.setClientID(reqDto.getClientID());
 			device.setDeviceInfo(reqDto.getDeviceInfo());
 			device.setHeadInfo(reqDto.getHeadInfo());
@@ -94,6 +99,12 @@ import java.util.stream.Collectors;
 			device.setCreateTime(currentTime);
 			device.setUpdateTime(currentTime);
 			Device insert = mongoTemplate.insert(device);
+
+			// 生成新增用户数据并同步
+			syncNewUserData(device);
+			// 生成一条活跃数据并同步
+			syncActiveUserData(device);
+
 			return insert.getId();
 		} else {
 			// 有数据时对比设备信息是否一致
@@ -107,7 +118,6 @@ import java.util.stream.Collectors;
 				if (!StringUtils.isBlank(reqDto.getHeadInfo())) {
 					update.set("headInfo", reqDto.getHeadInfo());
 				}
-				update.set("clientIp", reqDto.getClientIp());
 				// 设置更新时间为当前时间的字符串格式
 				update.set("updateTime", LocalDateTime.now().format(DATE_TIME_FORMATTER));
 
@@ -118,6 +128,114 @@ import java.util.stream.Collectors;
 		return existingDevice.getId();
 	}
 
+	private void syncActiveUserData(Device device) {
+		if (device ==null) {
+			return;
+		}
+		String clientID = device.getClientID();
+		// 通过clientID查询设备详情
+		String deviceInfo = device.getDeviceInfo();
+
+		// 将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", "1.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.saveMktStatActiveUser(activeUser);
+		if (result.getData() == null || !result.getData()) {
+			log.error("add活跃用户记录失败:clientID = {}", clientID);
+		} else {
+			log.debug("add活跃用户记录成功:clientID = {}, appId = {}, channel = {}", clientID, appId, channel);
+		}
+	}
+
+	/**
+	 * 同步新增用户数据
+	 * @param device 设备信息
+	 */
+	private void syncNewUserData(Device device) {
+		String clientID = device.getClientID();
+		// 通过clientID查询设备详情
+		String deviceInfo = device.getDeviceInfo();
+		// 将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("syncNewUserData fail = {}", e.getMessage());
+		}
+
+		// 从JSON对象中提取需要的字段
+		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 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.saveMktStatNewUser(mktStatNewUser);
+		if (result.getData() == null || !result.getData()) {
+			log.error("add新增用户记录失败:clientID = {}", clientID);
+		} else {
+			log.debug("add新增用户记录成功:clientID = {}, appId = {}, channel = {}", clientID, appId, channel);
+		}
+	}
+
+	/**
+	 * 从JSON对象中提取字符串字段
+	 * @param jsonNode JSON节点
+	 * @param fieldName 字段名
+	 * @param defaultValue 默认值
+	 * @return 提取的字符串值
+	 */
+	private String extractStringFromJson(JsonNode jsonNode, String fieldName, String defaultValue) {
+		try {
+			if (jsonNode != null && jsonNode.has(fieldName)) {
+				JsonNode fieldNode = jsonNode.get(fieldName);
+				if (fieldNode != null && !fieldNode.isNull()) {
+					return fieldNode.asText();
+				}
+			}
+		} catch (Exception e) {
+			log.warn("提取JSON字段失败:fieldName = {}, 错误:{}", fieldName, e.getMessage());
+		}
+		return defaultValue;
+	}
+
 	/**
 	 * 根据客户端ID查询设备信息
 	 * @param clientID 客户端ID
@@ -215,13 +333,10 @@ import java.util.stream.Collectors;
 	 */
 	@Override
 	public String saveMessage(SaveTcpMessageDTO reqDto) {
-		if (reqDto.getClientIp() != null) {
-			reqDto.setClientIp(reqDto.getClientIp().split(":")[0]);
-		}
+
 
 		Message message = new Message();
 		message.setClientID(reqDto.getClientID());
-		message.setClientIp(reqDto.getClientIp());
 		message.setMsgData(reqDto.getMsgData());
 		message.setReportTime(LocalDateTime.now().format(DATE_TIME_FORMATTER));
 		Message save = messageRepository.save(message);
@@ -236,9 +351,14 @@ import java.util.stream.Collectors;
 
 			s = mktMgmtPushRecordService.saveRecord(saveRecordDTO);
 			hand = mktMgmtHandPushService.handPush(saveRecordDTO);
+
+			// 根据客户端ID查询设备信息
+			Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
+			Device device = mongoTemplate.findOne(query, Device.class);
+			syncActiveUserData(device);
 		}
 
-		log.info("保存推送记录:{}---{}", s,hand);
+		log.info("保存推送记录:{}", s);
 		return save.getId();
 	}
 

+ 23 - 0
pig-statistics/pig-statistics-api/src/main/java/com/pig4cloud/pig/statistics/api/feign/RemoteStatUserDataService.java

@@ -0,0 +1,23 @@
+package com.pig4cloud.pig.statistics.api.feign;
+
+import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
+import com.pig4cloud.pig.common.core.util.R;
+import com.pig4cloud.pig.common.feign.annotation.NoToken;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatActiveUser;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatNewUser;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+
+@FeignClient(contextId = "remoteStatUserDataService", value = ServiceNameConstants.STATISTICS_SERVICE)
+public interface RemoteStatUserDataService {
+
+	@NoToken
+	@PostMapping("/user/new/save")
+	R<Boolean> saveMktStatNewUser(@RequestBody MktStatNewUser data);
+
+	@NoToken
+	@PostMapping("/user/active/save")
+	R<Boolean> saveMktStatActiveUser(@RequestBody MktStatActiveUser data);
+
+}

+ 16 - 0
pig-statistics/pig-statistics-biz/src/main/java/com/pig4cloud/pig/statistics/controller/UserAnalyseController.java

@@ -5,7 +5,11 @@ import cn.idev.excel.EasyExcel;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.pig4cloud.pig.common.core.exception.BusinessException;
 import com.pig4cloud.pig.common.core.util.R;
+import com.pig4cloud.pig.common.feign.annotation.NoToken;
+import com.pig4cloud.pig.common.security.annotation.Inner;
 import com.pig4cloud.pig.statistics.api.dto.user.*;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatActiveUser;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatNewUser;
 import com.pig4cloud.pig.statistics.api.vo.user.*;
 import com.pig4cloud.pig.statistics.service.UserAnalyseService;
 import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
@@ -45,6 +49,12 @@ public class UserAnalyseController {
 	private final UserAnalyseService userAnalyseService;
 
 /***************************************** 新增用户 *****************************************/
+    @Inner
+	@PostMapping("/new/save")
+	@Operation(summary = "保存新增用户信息")
+	R<Boolean> saveMktStatNewUser(@RequestBody MktStatNewUser data){
+		return R.ok(userAnalyseService.saveMktStatNewUser(data));
+	}
 
 	@PostMapping("/new/trend")
 	@Operation(summary = "查询新增趋势")
@@ -151,6 +161,12 @@ public class UserAnalyseController {
 	}
 
 /***************************************** 活跃用户 *****************************************/
+    @Inner
+	@PostMapping("/active/save")
+	@Operation(summary = "保存活跃用户信息")
+	R<Boolean> saveMktStatActiveUser(@RequestBody MktStatActiveUser data){
+		return R.ok(userAnalyseService.saveMktStatActiveUser(data));
+	}
 
 	@PostMapping("/active/trend")
 	@Operation(summary = "查询活跃趋势")

+ 16 - 0
pig-statistics/pig-statistics-biz/src/main/java/com/pig4cloud/pig/statistics/service/UserAnalyseService.java

@@ -3,6 +3,8 @@ package com.pig4cloud.pig.statistics.service;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.pig4cloud.pig.statistics.api.dto.user.*;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatActiveUser;
+import com.pig4cloud.pig.statistics.api.entity.user.MktStatNewUser;
 import com.pig4cloud.pig.statistics.api.vo.user.*;
 import jakarta.validation.Valid;
 
@@ -150,4 +152,18 @@ public interface UserAnalyseService {
 	 * @return List
 	 */
 	List<String> getChannelList(String appId);
+
+	/**
+	 * 保存新增用戶数据
+	 * @param data
+	 * @return
+	 */
+	Boolean saveMktStatNewUser(MktStatNewUser data);
+
+	/**
+	 * 保存新增用戶数据
+	 * @param data
+	 * @return
+	 */
+	Boolean saveMktStatActiveUser(MktStatActiveUser data);
 }

+ 10 - 0
pig-statistics/pig-statistics-biz/src/main/java/com/pig4cloud/pig/statistics/service/impl/UserAnalyseServiceImpl.java

@@ -1160,6 +1160,16 @@ public class UserAnalyseServiceImpl implements UserAnalyseService {
 		return channels;
 	}
 
+	@Override
+	public Boolean saveMktStatNewUser(MktStatNewUser data) {
+		return newUserMapper.insert(data) == 1;
+	}
+
+	@Override
+	public Boolean saveMktStatActiveUser(MktStatActiveUser data) {
+		return activeUserMapper.insert(data) == 1;
+	}
+
 
 	/************************************** 公用方法 **************************************
 	 * 统计指定时间范围内的新增用户数