Commit a3415806 by zhouhaibao

[+]新增周报月报推送配置以及周报月报推送任务

1 parent 4b29ff7a
...@@ -67,5 +67,20 @@ ...@@ -67,5 +67,20 @@
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>5.8.38</version> <version>5.8.38</version>
</dependency> </dependency>
<dependency>
<groupId>cn.com.glsx.entmicro</groupId>
<artifactId>rpc-scrm-entmicro-api</artifactId>
<version>3.25.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.com.glsx</groupId>
<artifactId>rpc-scrm-wechat-api</artifactId>
<version>4.54.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.com.glsx.entmicro</groupId>
<artifactId>rpc-scrm-entmicro-api</artifactId>
<version>5.3.0-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.job;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import cn.com.glsx.dj.peanalysis.service.ReportSubscribeService;
import cn.com.glsx.dj.peanalysis.service.WeComMessageService;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Slf4j
@ElasticJobConf(name = "MonthlyReportPushJob", cron = "0 0 9 1 * ?", description = "每月1日上午9:00推送展业月报", overwrite = false)
public class MonthlyReportPushJob implements SimpleJob {
@Resource
private ReportSubscribeService reportSubscribeService;
@Resource
private WeComMessageService weComMessageService;
@Override
public void execute(ShardingContext shardingContext) {
log.info("开始推送展业月报");
try {
// 获取所有已订阅月报的用户
List<DjReportSubscribeConfig> subscribers = reportSubscribeService.getMonthlySubscribers();
if (subscribers == null || subscribers.isEmpty()) {
log.info("没有用户订阅展业月报");
return;
}
// 计算上月的开始和结束日期
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
Date startDate = calendar.getTime();
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date endDate = calendar.getTime();
// 获取年份和月份
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1
// 批量发送月报通知
boolean success = weComMessageService.sendBatchMonthlyReportNotice(subscribers, startDate, endDate, year, month);
if (success) {
log.info("批量推送展业月报成功,订阅用户数: {}", subscribers.size());
} else {
log.error("批量推送展业月报失败");
}
} catch (Exception e) {
log.error("推送展业月报时发生异常", e);
}
log.info("推送展业月报完成");
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.job;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import cn.com.glsx.dj.peanalysis.service.ReportSubscribeService;
import cn.com.glsx.dj.peanalysis.service.WeComMessageService;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Slf4j
@ElasticJobConf(name = "WeeklyReportPushJob", cron = "0 0 9 ? * MON", description = "每周一上午9:00推送展业周报", overwrite = false)
public class WeeklyReportPushJob implements SimpleJob {
@Resource
private ReportSubscribeService reportSubscribeService;
@Resource
private WeComMessageService weComMessageService;
@Override
public void execute(ShardingContext shardingContext) {
log.info("开始推送展业周报");
try {
// 获取所有已订阅周报的用户
List<DjReportSubscribeConfig> subscribers = reportSubscribeService.getWeeklySubscribers();
if (subscribers == null || subscribers.isEmpty()) {
log.info("没有用户订阅展业周报");
return;
}
// 计算上周的开始和结束日期
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.add(Calendar.WEEK_OF_YEAR, -1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date startDate = calendar.getTime();
calendar.add(Calendar.DAY_OF_WEEK, 6);
Date endDate = calendar.getTime();
// 获取年份和周数
int year = calendar.get(Calendar.YEAR);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
// 批量发送周报通知
boolean success = weComMessageService.sendBatchWeeklyReportNotice(subscribers, startDate, endDate, year, week);
if (success) {
log.info("批量推送展业周报成功,订阅用户数: {}", subscribers.size());
} else {
log.error("批量推送展业周报失败");
}
} catch (Exception e) {
log.error("推送展业周报时发生异常", e);
}
log.info("推送展业周报完成");
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.mapper;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface DjReportSubscribeConfigMapper {
/**
* 根据用户ID查询订阅配置
* @param userId 用户ID
* @return 订阅配置
*/
DjReportSubscribeConfig selectByUserId(@Param("userId") Integer userId);
/**
* 插入订阅配置
* @param record 订阅配置
* @return 影响行数
*/
int insert(DjReportSubscribeConfig record);
/**
* 更新订阅配置
* @param record 订阅配置
* @return 影响行数
*/
int updateByPrimaryKeySelective(DjReportSubscribeConfig record);
/**
* 查询所有已订阅周报的用户
* @return 已订阅周报的用户列表
*/
java.util.List<DjReportSubscribeConfig> selectWeeklySubscribers();
/**
* 查询所有已订阅月报的用户
* @return 已订阅月报的用户列表
*/
java.util.List<DjReportSubscribeConfig> selectMonthlySubscribers();
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.remote;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import cn.com.glsx.dj.peanalysis.service.ReportSubscribeService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class ReportSubscribeRemoteImpl implements ReportSubscribeRemote {
@Autowired
private ReportSubscribeService reportSubscribeService;
@Override
public RpcResponse<Boolean> updateWeeklyReportSubscribe(Integer userId, String userName, String userPhone, Integer subscribeStatus) {
try {
boolean result = reportSubscribeService.updateWeeklyReportSubscribe(userId, userName, userPhone, subscribeStatus);
return RpcResponse.success(result);
} catch (Exception e) {
log.error("更新展业周报订阅状态失败: userId={}, subscribeStatus={}", userId, subscribeStatus, e);
return RpcResponse.error(e);
}
}
@Override
public RpcResponse<Boolean> updateMonthlyReportSubscribe(Integer userId, String userName, String userPhone, Integer subscribeStatus) {
try {
boolean result = reportSubscribeService.updateMonthlyReportSubscribe(userId, userName, userPhone, subscribeStatus);
return RpcResponse.success(result);
} catch (Exception e) {
log.error("更新展业月报订阅状态失败: userId={}, subscribeStatus={}", userId, subscribeStatus, e);
return RpcResponse.error(e);
}
}
@Override
public RpcResponse<DjReportSubscribeConfig> getSubscribeConfig(Integer userId) {
try {
DjReportSubscribeConfig config = reportSubscribeService.getSubscribeConfig(userId);
return RpcResponse.success(config);
} catch (Exception e) {
log.error("查询用户订阅配置失败: userId={}", userId, e);
return RpcResponse.error(e);
}
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.peanalysis.mapper.DjReportSubscribeConfigMapper;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ReportSubscribeService {
@Autowired
private DjReportSubscribeConfigMapper djReportSubscribeConfigMapper;
/**
* 更新用户的展业周报订阅状态
* @param userId 用户ID
* @param userName 用户名称
* @param userPhone 用户手机号
* @param subscribeStatus 订阅状态:0:未订阅,1:已订阅
* @return 更新结果
*/
public boolean updateWeeklyReportSubscribe(Integer userId, String userName, String userPhone, Integer subscribeStatus) {
try {
// 查找用户的订阅配置
DjReportSubscribeConfig config = djReportSubscribeConfigMapper.selectByUserId(userId);
if (config == null) {
// 如果不存在,创建新的配置
config = new DjReportSubscribeConfig();
config.setUserId(userId);
config.setUserName(userName);
config.setUserPhone(userPhone);
config.setWeeklyReportSubscribe(subscribeStatus);
config.setMonthlyReportSubscribe(0);
djReportSubscribeConfigMapper.insert(config);
} else {
// 如果存在,更新配置
config.setUserName(userName);
config.setUserPhone(userPhone);
config.setWeeklyReportSubscribe(subscribeStatus);
djReportSubscribeConfigMapper.updateByPrimaryKeySelective(config);
}
return true;
} catch (Exception e) {
log.error("更新展业周报订阅状态失败: userId={}, subscribeStatus={}", userId, subscribeStatus, e);
throw new RpcServiceException("更新展业周报订阅状态失败");
}
}
/**
* 更新用户的展业月报订阅状态
* @param userId 用户ID
* @param userName 用户名称
* @param userPhone 用户手机号
* @param subscribeStatus 订阅状态:0:未订阅,1:已订阅
* @return 更新结果
*/
public boolean updateMonthlyReportSubscribe(Integer userId, String userName, String userPhone, Integer subscribeStatus) {
try {
// 查找用户的订阅配置
DjReportSubscribeConfig config = djReportSubscribeConfigMapper.selectByUserId(userId);
if (config == null) {
// 如果不存在,创建新的配置
config = new DjReportSubscribeConfig();
config.setUserId(userId);
config.setUserName(userName);
config.setUserPhone(userPhone);
config.setWeeklyReportSubscribe(0);
config.setMonthlyReportSubscribe(subscribeStatus);
djReportSubscribeConfigMapper.insert(config);
} else {
// 如果存在,更新配置
config.setUserName(userName);
config.setUserPhone(userPhone);
config.setMonthlyReportSubscribe(subscribeStatus);
djReportSubscribeConfigMapper.updateByPrimaryKeySelective(config);
}
return true;
} catch (Exception e) {
log.error("更新展业月报订阅状态失败: userId={}, subscribeStatus={}", userId, subscribeStatus, e);
throw new RpcServiceException("更新展业月报订阅状态失败");
}
}
/**
* 查询用户的订阅配置
* @param userId 用户ID
* @return 订阅配置
*/
public DjReportSubscribeConfig getSubscribeConfig(Integer userId) {
try {
return djReportSubscribeConfigMapper.selectByUserId(userId);
} catch (Exception e) {
log.error("查询用户订阅配置失败: userId={}", userId, e);
throw new RpcServiceException("查询用户订阅配置失败");
}
}
/**
* 获取所有已订阅周报的用户
* @return 已订阅周报的用户列表
*/
public java.util.List<DjReportSubscribeConfig> getWeeklySubscribers() {
try {
return djReportSubscribeConfigMapper.selectWeeklySubscribers();
} catch (Exception e) {
log.error("获取已订阅周报的用户失败", e);
throw new RpcServiceException("获取已订阅周报的用户失败");
}
}
/**
* 获取所有已订阅月报的用户
* @return 已订阅月报的用户列表
*/
public java.util.List<DjReportSubscribeConfig> getMonthlySubscribers() {
try {
return djReportSubscribeConfigMapper.selectMonthlySubscribers();
} catch (Exception e) {
log.error("获取已订阅月报的用户失败", e);
throw new RpcServiceException("获取已订阅月报的用户失败");
}
}
}
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.peanalysis.mapper.DjReportSubscribeConfigMapper" >
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_phone" property="userPhone" jdbcType="VARCHAR" />
<result column="weekly_report_subscribe" property="weeklyReportSubscribe" jdbcType="INTEGER" />
<result column="monthly_report_subscribe" property="monthlyReportSubscribe" jdbcType="INTEGER" />
<result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
<result column="updated_time" property="updatedTime" jdbcType="TIMESTAMP" />
</resultMap>
<select id="selectByUserId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, user_id, user_name, user_phone, weekly_report_subscribe, monthly_report_subscribe, created_time, updated_time
from dj_report_subscribe_config
where user_id = #{userId, jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig" >
insert into dj_report_subscribe_config (user_id, user_name, user_phone, weekly_report_subscribe, monthly_report_subscribe, created_time, updated_time)
values (#{userId, jdbcType=INTEGER}, #{userName, jdbcType=VARCHAR}, #{userPhone, jdbcType=VARCHAR}, #{weeklyReportSubscribe, jdbcType=INTEGER}, #{monthlyReportSubscribe, jdbcType=INTEGER}, now(), now())
</insert>
<update id="updateByPrimaryKeySelective" parameterType="cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig" >
update dj_report_subscribe_config
<set >
<if test="userName != null" >
user_name = #{userName, jdbcType=VARCHAR},
</if>
<if test="userPhone != null" >
user_phone = #{userPhone, jdbcType=VARCHAR},
</if>
<if test="weeklyReportSubscribe != null" >
weekly_report_subscribe = #{weeklyReportSubscribe, jdbcType=INTEGER},
</if>
<if test="monthlyReportSubscribe != null" >
monthly_report_subscribe = #{monthlyReportSubscribe, jdbcType=INTEGER},
</if>
updated_time = now()
</set>
where id = #{id, jdbcType=INTEGER}
</update>
<select id="selectWeeklySubscribers" resultMap="BaseResultMap" >
select id, user_id, user_name, user_phone, weekly_report_subscribe, monthly_report_subscribe, created_time, updated_time
from dj_report_subscribe_config
where weekly_report_subscribe = 1
</select>
<select id="selectMonthlySubscribers" resultMap="BaseResultMap" >
select id, user_id, user_name, user_phone, weekly_report_subscribe, monthly_report_subscribe, created_time, updated_time
from dj_report_subscribe_config
where monthly_report_subscribe = 1
</select>
</mapper>
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.job;
import cn.com.glsx.dj.peanalysis.remote.BaseRemoteTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class MonthlyReportPushJobTest extends BaseRemoteTest {
@Autowired
private MonthlyReportPushJob monthlyReportPushJob;
@Test
public void testExecute() {
monthlyReportPushJob.execute(null);
System.out.println("MonthlyReportPushJob执行完成");
}
}
package cn.com.glsx.dj.peanalysis.job;
import cn.com.glsx.dj.peanalysis.remote.BaseRemoteTest;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class WeeklyReportPushJobTest extends BaseRemoteTest {
@Autowired
private WeeklyReportPushJob weeklyReportPushJob;
@Test
public void testExecute() {
System.out.println("测试WeeklyReportPushJob执行");
weeklyReportPushJob.execute(null);
System.out.println("WeeklyReportPushJob执行完成");
}
}
package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import cn.com.glsx.dj.peanalysis.remote.BaseRemoteTest;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
public class ReportSubscribeServiceTest extends BaseRemoteTest {
@Autowired
private ReportSubscribeService reportSubscribeService;
@Test
public void testUpdateWeeklyReportSubscribe() {
boolean result = reportSubscribeService.updateWeeklyReportSubscribe(1001, "张三", "13476670746", 1);
System.out.println("更新展业周报订阅状态(订阅): " + result);
result = reportSubscribeService.updateWeeklyReportSubscribe(1001, "张三", "13476670746", 0);
System.out.println("更新展业周报订阅状态(取消订阅): " + result);
}
@Test
public void testUpdateMonthlyReportSubscribe() {
boolean result = reportSubscribeService.updateMonthlyReportSubscribe(1002, "李四", "13800138002", 1);
System.out.println("更新展业月报订阅状态(订阅): " + result);
result = reportSubscribeService.updateMonthlyReportSubscribe(1002, "李四", "13800138002", 0);
System.out.println("更新展业月报订阅状态(取消订阅): " + result);
}
@Test
public void testGetSubscribeConfig() {
DjReportSubscribeConfig config = reportSubscribeService.getSubscribeConfig(1001);
System.out.println("查询用户订阅配置: " + JSON.toJSONString(config));
}
@Test
public void testGetWeeklySubscribers() {
List<DjReportSubscribeConfig> subscribers = reportSubscribeService.getWeeklySubscribers();
System.out.println("查询已订阅周报的用户列表: " + JSON.toJSONString(subscribers));
}
@Test
public void testGetMonthlySubscribers() {
List<DjReportSubscribeConfig> subscribers = reportSubscribeService.getMonthlySubscribers();
System.out.println("查询已订阅月报的用户列表: " + JSON.toJSONString(subscribers));
}
@Test
public void testBatchSubscribe() {
reportSubscribeService.updateWeeklyReportSubscribe(1003, "王五", "13800138003", 1);
reportSubscribeService.updateWeeklyReportSubscribe(1004, "赵六", "13800138004", 1);
reportSubscribeService.updateWeeklyReportSubscribe(1005, "孙七", "13800138005", 1);
List<DjReportSubscribeConfig> subscribers = reportSubscribeService.getWeeklySubscribers();
System.out.println("批量订阅后查询已订阅周报的用户列表: " + JSON.toJSONString(subscribers));
}
@Test
public void testUpdateExistingUser() {
reportSubscribeService.updateWeeklyReportSubscribe(1001, "张三", "13800138001", 1);
reportSubscribeService.updateMonthlyReportSubscribe(1001, "张三", "13800138001", 1);
DjReportSubscribeConfig config = reportSubscribeService.getSubscribeConfig(1001);
System.out.println("更新已有用户订阅配置后查询: " + JSON.toJSONString(config));
}
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig;
import cn.com.glsx.dj.peanalysis.remote.BaseRemoteTest;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
public class WeComMessageServiceTest extends BaseRemoteTest {
@Autowired
private WeComMessageService weComMessageService;
@Test
public void testSendBatchWeeklyReportNotice() {
System.out.println("测试批量发送展业周报通知");
// 创建测试数据
List<DjReportSubscribeConfig> subscribers = Lists.newArrayList();
DjReportSubscribeConfig config1 = new DjReportSubscribeConfig();
config1.setUserId(1001);
config1.setUserName("张三");
config1.setUserPhone("13800138001");
config1.setWeeklyReportSubscribe(1);
subscribers.add(config1);
DjReportSubscribeConfig config2 = new DjReportSubscribeConfig();
config2.setUserId(1002);
config2.setUserName("李四");
config2.setUserPhone("13476670746");
config2.setWeeklyReportSubscribe(1);
subscribers.add(config2);
// 测试批量发送
Date startDate = new Date();
Date endDate = new Date();
boolean result = weComMessageService.sendBatchWeeklyReportNotice(subscribers, startDate, endDate, 2026, 13);
System.out.println("批量发送展业周报通知结果: " + result);
}
@Test
public void testSendBatchMonthlyReportNotice() {
System.out.println("测试批量发送展业月报通知");
// 创建测试数据
List<DjReportSubscribeConfig> subscribers = Lists.newArrayList();
DjReportSubscribeConfig config1 = new DjReportSubscribeConfig();
config1.setUserId(1001);
config1.setUserName("张三");
config1.setUserPhone("13800138001");
config1.setMonthlyReportSubscribe(1);
subscribers.add(config1);
DjReportSubscribeConfig config2 = new DjReportSubscribeConfig();
config2.setUserId(1002);
config2.setUserName("李四");
config2.setUserPhone("13476670746");
config2.setMonthlyReportSubscribe(1);
subscribers.add(config2);
// 测试批量发送
Date startDate = new Date();
Date endDate = new Date();
boolean result = weComMessageService.sendBatchMonthlyReportNotice(subscribers, startDate, endDate, 2026, 3);
System.out.println("批量发送展业月报通知结果: " + result);
}
}
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。代码整体结构清晰,业务逻辑易于理解,但在处理大数据量、并发场景以及工程配置上存在几个关键隐患,需要进行优化。

    一、关键问题与优化建议(Top 3)

    1. 批量推送缺乏分片处理(企微API限制与OOM风险)

    • 问题描述:在 WeComMessageService 和定时任务中,系统一次性查出所有订阅用户(getWeeklySubscribers),将所有用户放入一个 List 发起 RPC 请求,最后将所有企微 UserId 用 | 拼接后调用企微发消息接口。
    • 影响分析:
      1. 企微API限制:企业微信发送消息接口对 touser 参数有严格限制(通常最多支持 1000 个用户),如果订阅用户超过限制,接口将直接报错。
      2. 内存与RPC性能:随着用户量增长,一次性全表查询会导致 JVM 内存溢出(OOM);同时,将成千上万个对象通过一次 RPC 传输极易导致请求超时。
    • 优化建议:引入分批处理机制(Chunking)。在数据库层面分页查询,在发送消息时按批次(如每次 500 条)调用企微接口。 ```java // 建议的分批处理示例 (使用 Guava 的 Lists.partition) List allSubscribers = reportSubscribeService.getWeeklySubscribers(); // 建议底层改为游标或分页查询 List> partitions = Lists.partition(allSubscribers, 500);

    for (List batch : partitions) { // 1. 分批获取企微ID Map phoneToUserIdMap = getBatchStaffWWUserId(batch); // 2. 拼接当前批次的 toUsers String toUsers = batch.stream() .map(sub -> phoneToUserIdMap.get(sub.getUserPhone())) .filter(Objects::nonNull) .collect(Collectors.joining("|")); // 3. 分批发送 sendBatchWechatMessage(toUsers, title, subTitle, dateRange, url); }

    
    **2. Maven 依赖重复引入(版本冲突风险)**
    - **问题描述**:在 `pom.xml` 中,`rpc-scrm-entmicro-api` 被引入了两次,且版本号不同(`3.25.9-SNAPSHOT` 和 `5.3.0-SNAPSHOT`)。
    - **影响分析**:Maven 解析依赖时会根据声明顺序或路径最短原则只保留一个版本。这会导致不可预期的行为,极易在运行时抛出 `NoSuchMethodError` 或 `ClassNotFoundException`。
    - **优化建议**:请清理 `pom.xml`,确认业务实际依赖的 API 版本,删除多余的依赖声明。
    
    **3. 订阅状态更新存在并发安全问题(Check-then-Act)**
    - **问题描述**:`ReportSubscribeService` 中的 `updateWeeklyReportSubscribe` 方法使用了“先查询后插入”的逻辑(查 `selectByUserId`,若为 null 则 `insert`,否则 `update`)。
    - **影响分析**:在并发场景下(例如用户在前端快速连点两次),两个线程可能同时判断 `config == null` 为 true,从而执行两次 `insert`。如果没有数据库唯一索引,将产生脏数据;如果有唯一索引,将抛出 `DuplicateKeyException` 导致请求失败。
    - **优化建议**:建议在数据库层面为 `user_id` 添加唯一索引(Unique Key),并使用 MySQL 的 `INSERT ... ON DUPLICATE KEY UPDATE` 语法实现原子更新。
      ```xml
      <!-- DjReportSubscribeConfigMapper.xml 示例 -->
      <insert id="upsert" parameterType="cn.com.glsx.dj.peanalysis.model.DjReportSubscribeConfig">
          insert into dj_report_subscribe_config 
          (user_id, user_name, user_phone, weekly_report_subscribe, monthly_report_subscribe, created_time, updated_time)
          values 
          (#{userId}, #{userName}, #{userPhone}, #{weeklyReportSubscribe}, #{monthlyReportSubscribe}, now(), now())
          on duplicate key update
          user_name = values(user_name),
          user_phone = values(user_phone),
          weekly_report_subscribe = values(weekly_report_subscribe),
          updated_time = now()
      </insert>
    

    二、评分明细

    • 功能实现的正确性与健壮性(28/40分):基础业务逻辑正确。扣分主要在于未考虑第三方接口(企业微信)的单次推送人数上限,以及并发情况下的插入/更新冲突。
    • 安全性与潜在风险(28/30分):无明显 SQL 注入或越权漏洞。但在 WeComMessageService 中,当环境变量取不到 url 时硬编码了 "https://www.baidu.com",这在生产环境中是不严谨的,建议改为抛出配置异常或使用系统默认页。
    • 最佳实践与可维护性(13/20分):代码结构与命名规范较好。扣分项:1) 存在严重的 Maven 依赖重复声明;2) 单元测试中通篇使用 System.out.println 而未使用 Assert 进行断言验证,失去了自动化测试的意义。
    • 性能与资源利用(2/5分):全表捞取数据放入内存、全量发起 RPC 请求,在数据量上升时存在明显的性能瓶颈和内存溢出风险。
    • 提交信息质量(4/5分):Commit 信息清晰说明了新增了周报月报推送任务,具备可追溯性。

    总分:75分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!