|
@@ -2,6 +2,7 @@ package com.pig4cloud.pig.marketing.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
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.MktMgmtPushRecordSaveDTO;
|
|
import com.pig4cloud.pig.marketing.api.dto.mongo.PageDeviceInfoDTO;
|
|
import com.pig4cloud.pig.marketing.api.dto.mongo.PageDeviceInfoDTO;
|
|
import com.pig4cloud.pig.marketing.api.dto.mongo.PageMessageDTO;
|
|
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.repository.MessageRepository;
|
|
import com.pig4cloud.pig.marketing.service.MktMgmtPushRecordService;
|
|
import com.pig4cloud.pig.marketing.service.MktMgmtPushRecordService;
|
|
import com.pig4cloud.pig.marketing.service.TcpDataService;
|
|
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.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -39,6 +43,8 @@ import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author: lwh
|
|
* @author: lwh
|
|
@@ -63,6 +69,10 @@ import java.util.stream.Collectors;
|
|
@Autowired
|
|
@Autowired
|
|
private MktMgmtHandPushService mktMgmtHandPushService;
|
|
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");
|
|
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
|
|
@Override
|
|
public String saveOrUpdateDevice(SaveDeviceInfoDTO reqDto) {
|
|
public String saveOrUpdateDevice(SaveDeviceInfoDTO reqDto) {
|
|
- if (reqDto.getClientIp() != null) {
|
|
|
|
- reqDto.setClientIp(reqDto.getClientIp().split(":")[0]);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// 根据客户端ID查询设备信息
|
|
// 根据客户端ID查询设备信息
|
|
Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
|
|
Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
|
|
Device existingDevice = mongoTemplate.findOne(query, Device.class);
|
|
Device existingDevice = mongoTemplate.findOne(query, Device.class);
|
|
@@ -85,7 +91,6 @@ import java.util.stream.Collectors;
|
|
if (existingDevice == null) {
|
|
if (existingDevice == null) {
|
|
// 无数据时直接新增
|
|
// 无数据时直接新增
|
|
Device device = new Device();
|
|
Device device = new Device();
|
|
- device.setClientIp(reqDto.getClientIp());
|
|
|
|
device.setClientID(reqDto.getClientID());
|
|
device.setClientID(reqDto.getClientID());
|
|
device.setDeviceInfo(reqDto.getDeviceInfo());
|
|
device.setDeviceInfo(reqDto.getDeviceInfo());
|
|
device.setHeadInfo(reqDto.getHeadInfo());
|
|
device.setHeadInfo(reqDto.getHeadInfo());
|
|
@@ -94,6 +99,12 @@ import java.util.stream.Collectors;
|
|
device.setCreateTime(currentTime);
|
|
device.setCreateTime(currentTime);
|
|
device.setUpdateTime(currentTime);
|
|
device.setUpdateTime(currentTime);
|
|
Device insert = mongoTemplate.insert(device);
|
|
Device insert = mongoTemplate.insert(device);
|
|
|
|
+
|
|
|
|
+ // 生成新增用户数据并同步
|
|
|
|
+ syncNewUserData(device);
|
|
|
|
+ // 生成一条活跃数据并同步
|
|
|
|
+ syncActiveUserData(device);
|
|
|
|
+
|
|
return insert.getId();
|
|
return insert.getId();
|
|
} else {
|
|
} else {
|
|
// 有数据时对比设备信息是否一致
|
|
// 有数据时对比设备信息是否一致
|
|
@@ -107,7 +118,6 @@ import java.util.stream.Collectors;
|
|
if (!StringUtils.isBlank(reqDto.getHeadInfo())) {
|
|
if (!StringUtils.isBlank(reqDto.getHeadInfo())) {
|
|
update.set("headInfo", reqDto.getHeadInfo());
|
|
update.set("headInfo", reqDto.getHeadInfo());
|
|
}
|
|
}
|
|
- update.set("clientIp", reqDto.getClientIp());
|
|
|
|
// 设置更新时间为当前时间的字符串格式
|
|
// 设置更新时间为当前时间的字符串格式
|
|
update.set("updateTime", LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
|
update.set("updateTime", LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
|
|
|
|
|
@@ -118,6 +128,114 @@ import java.util.stream.Collectors;
|
|
return existingDevice.getId();
|
|
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查询设备信息
|
|
* 根据客户端ID查询设备信息
|
|
* @param clientID 客户端ID
|
|
* @param clientID 客户端ID
|
|
@@ -215,13 +333,10 @@ import java.util.stream.Collectors;
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
public String saveMessage(SaveTcpMessageDTO reqDto) {
|
|
public String saveMessage(SaveTcpMessageDTO reqDto) {
|
|
- if (reqDto.getClientIp() != null) {
|
|
|
|
- reqDto.setClientIp(reqDto.getClientIp().split(":")[0]);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
|
|
Message message = new Message();
|
|
Message message = new Message();
|
|
message.setClientID(reqDto.getClientID());
|
|
message.setClientID(reqDto.getClientID());
|
|
- message.setClientIp(reqDto.getClientIp());
|
|
|
|
message.setMsgData(reqDto.getMsgData());
|
|
message.setMsgData(reqDto.getMsgData());
|
|
message.setReportTime(LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
|
message.setReportTime(LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
|
Message save = messageRepository.save(message);
|
|
Message save = messageRepository.save(message);
|
|
@@ -236,9 +351,14 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
s = mktMgmtPushRecordService.saveRecord(saveRecordDTO);
|
|
s = mktMgmtPushRecordService.saveRecord(saveRecordDTO);
|
|
hand = mktMgmtHandPushService.handPush(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();
|
|
return save.getId();
|
|
}
|
|
}
|
|
|
|
|