Quellcode durchsuchen

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

lh vor 1 Woche
Ursprung
Commit
5383178396

+ 91 - 0
pig-marketing/pig-marketing-api/src/main/java/com/pig4cloud/pig/marketing/api/entity/MktStatActiveUser.java

@@ -0,0 +1,91 @@
+package com.pig4cloud.pig.marketing.api.entity;
+
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.time.LocalDateTime;
+
+@Data
+@Schema(description = "活跃用户")
+@EqualsAndHashCode(callSuper = true)
+public class MktStatActiveUser extends Model<MktStatActiveUser> {
+
+	/**
+	 * id
+	 */
+	@TableId(value = "id")
+	private Long id;
+
+	/**
+	 * 用户ID
+	 */
+	@Schema(description = "用户ID")
+	private String userId;
+
+	/**
+	 * 应用ID
+	 */
+	@Schema(description = "应用ID")
+	private String appId;
+
+	/**
+	 * 渠道
+	 */
+	@Schema(description = "渠道")
+	private String channel;
+
+	/**
+	 * 版本
+	 */
+	@Schema(description = "版本")
+	private String version;
+
+	/**
+	 * 统计时间
+	 */
+	@Schema(description = "统计时间")
+	private LocalDateTime statDate;
+
+	/**
+	 * 删除标记
+	 */
+	@TableLogic
+	@TableField(fill = FieldFill.INSERT)
+	@Schema(description = "删除标记,1:已删除,0:正常")
+	private String delFlag;
+
+	/**
+	 * 创建人
+	 */
+	@TableField(fill = FieldFill.INSERT)
+	@Schema(description = "创建人")
+	private String createBy;
+
+	/**
+	 * 修改人
+	 */
+	@TableField(fill = FieldFill.UPDATE)
+	@Schema(description = "修改人")
+	private String updateBy;
+
+	/**
+	 * 创建时间
+	 */
+	@TableField(fill = FieldFill.INSERT)
+	@Schema(description = "创建时间")
+	private LocalDateTime createTime;
+
+	/**
+	 * 更新时间
+	 */
+	@TableField(fill = FieldFill.UPDATE)
+	@Schema(description = "更新时间")
+	private LocalDateTime updateTime;
+}

+ 96 - 0
pig-marketing/pig-marketing-api/src/main/java/com/pig4cloud/pig/marketing/api/entity/MktStatNewUser.java

@@ -0,0 +1,96 @@
+package com.pig4cloud.pig.marketing.api.entity;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.time.LocalDateTime;
+
+@Data
+@Schema(description = "新增用户")
+@EqualsAndHashCode(callSuper = true)
+public class MktStatNewUser extends Model<MktStatNewUser> {
+
+	/**
+	 * id
+	 */
+	@TableId(value = "id")
+	private Long id;
+
+	/**
+	 * 用户ID
+	 */
+	@Schema(description = "用户ID")
+	private String userId;
+
+	/**
+	 * 应用ID
+	 */
+	@Schema(description = "应用ID")
+	private String appId;
+
+	/**
+	 * 渠道
+	 */
+	@Schema(description = "渠道")
+	private String channel;
+
+	/**
+	 * 版本
+	 */
+	@Schema(description = "版本")
+	private String version;
+
+	/**
+	 * 旧版本
+	 */
+	@Schema(description = "旧版本")
+	private String oldVersion;
+
+	/**
+	 * 统计时间
+	 */
+	@Schema(description = "统计时间")
+	private LocalDateTime statDate;
+
+	/**
+	 * 删除标记
+	 */
+	@TableLogic
+	@TableField(fill = FieldFill.INSERT)
+	@Schema(description = "删除标记,1:已删除,0:正常")
+	private String delFlag;
+
+	/**
+	 * 创建人
+	 */
+	@TableField(fill = FieldFill.INSERT)
+	@Schema(description = "创建人")
+	private String createBy;
+
+	/**
+	 * 修改人
+	 */
+	@TableField(fill = FieldFill.UPDATE)
+	@Schema(description = "修改人")
+	private String updateBy;
+
+	/**
+	 * 创建时间
+	 */
+	@TableField(fill = FieldFill.INSERT)
+	@Schema(description = "创建时间")
+	private LocalDateTime createTime;
+
+	/**
+	 * 更新时间
+	 */
+	@TableField(fill = FieldFill.UPDATE)
+	@Schema(description = "更新时间")
+	private LocalDateTime updateTime;
+}

+ 11 - 0
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/mapper/MktStatActiveUserDataMapper.java

@@ -0,0 +1,11 @@
+package com.pig4cloud.pig.marketing.mapper;
+
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.pig4cloud.pig.marketing.api.entity.MktStatActiveUser;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface MktStatActiveUserDataMapper extends BaseMapper<MktStatActiveUser> {
+
+}

+ 10 - 0
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/mapper/MktStatNewUserDataMapper.java

@@ -0,0 +1,10 @@
+package com.pig4cloud.pig.marketing.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.pig4cloud.pig.marketing.api.entity.MktStatNewUser;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface MktStatNewUserDataMapper extends BaseMapper<MktStatNewUser> {
+
+}

+ 132 - 0
pig-marketing/pig-marketing-biz/src/main/java/com/pig4cloud/pig/marketing/service/impl/TcpDataServiceImpl.java

@@ -7,6 +7,8 @@ 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.SaveDeviceInfoDTO;
 import com.pig4cloud.pig.marketing.api.dto.mongo.SaveTcpMessageDTO;
+import com.pig4cloud.pig.marketing.api.entity.MktStatActiveUser;
+import com.pig4cloud.pig.marketing.api.entity.MktStatNewUser;
 import com.pig4cloud.pig.marketing.api.entity.mongo.Device;
 import com.pig4cloud.pig.marketing.api.entity.mongo.Message;
 import com.pig4cloud.pig.marketing.api.service.MktMgmtHandPushService;
@@ -14,6 +16,8 @@ import com.pig4cloud.pig.marketing.api.vo.mongo.OnlineUserVO;
 import com.pig4cloud.pig.marketing.api.vo.mongo.PageDeviceInfoVO;
 import com.pig4cloud.pig.marketing.api.vo.mongo.UserStatisticsVO;
 import com.pig4cloud.pig.marketing.config.UserStatisticsConfig;
+import com.pig4cloud.pig.marketing.mapper.MktStatActiveUserDataMapper;
+import com.pig4cloud.pig.marketing.mapper.MktStatNewUserDataMapper;
 import com.pig4cloud.pig.marketing.repository.MessageRepository;
 import com.pig4cloud.pig.marketing.service.MktMgmtPushRecordService;
 import com.pig4cloud.pig.marketing.service.TcpDataService;
@@ -35,6 +39,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
@@ -59,6 +65,13 @@ import java.util.stream.Collectors;
 		@Autowired
 		private MktMgmtHandPushService mktMgmtHandPushService;
 
+		@Autowired
+		private MktStatNewUserDataMapper mktStatNewUserDataMapper;
+		@Autowired
+		private MktStatActiveUserDataMapper mktStatActiveUserDataMapper;
+
+		// JSON处理器
+		private final ObjectMapper objectMapper = new ObjectMapper();
 
 	// 定义时间格式器
 	private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@@ -85,6 +98,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 {
 			// 有数据时对比设备信息是否一致
@@ -108,6 +127,112 @@ 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());
+		int inserted = mktStatActiveUserDataMapper.insert(activeUser);
+		if (inserted != 1) {
+			log.warn("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());
+		int inserted = mktStatNewUserDataMapper.insert(mktStatNewUser);
+		if (inserted != 1) {
+			log.warn("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
@@ -205,6 +330,8 @@ import java.util.stream.Collectors;
 	 */
 	@Override
 	public String saveMessage(SaveTcpMessageDTO reqDto) {
+
+
 		Message message = new Message();
 		message.setClientID(reqDto.getClientID());
 		message.setMsgData(reqDto.getMsgData());
@@ -221,6 +348,11 @@ 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);