mp:完成公众号统计的用户累计数据、消息概况数据

This commit is contained in:
YunaiV
2023-01-07 23:49:50 +08:00
parent 09e200c364
commit d0934510af
11 changed files with 313 additions and 126 deletions

View File

@ -16,6 +16,8 @@ public interface ErrorCodeConstants {
// ========== 公众号账号 1006001000============
ErrorCode STATISTICS_GET_USER_SUMMARY_FAIL = new ErrorCode(1006001000, "获取用户增减数据失败,原因:{}");
ErrorCode STATISTICS_GET_USER_CUMULATE_FAIL = new ErrorCode(1006001001, "获得用户累计数据失败,原因:{}");
ErrorCode STATISTICS_GET_UPSTREAM_MESSAGE_FAIL = new ErrorCode(1006001002, "获得消息发送概况数据失败,原因:{}");
// TODO 要处理下
ErrorCode COMMON_NOT_EXISTS = new ErrorCode(1006001002, "用户不存在");

View File

@ -1,11 +1,16 @@
package cn.iocoder.yudao.module.mp.controller.admin.statistics;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsGetReqVO;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsUpstreamMessageRespVO;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsUserCumulateRespVO;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsUserSummaryRespVO;
import cn.iocoder.yudao.module.mp.convert.statistics.MpStatisticsConvert;
import cn.iocoder.yudao.module.mp.service.statistics.MpStatisticsService;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsGetReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeMsgResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
@ -30,9 +35,28 @@ public class MpStatisticsController {
@GetMapping("/user-summary")
@ApiOperation("获得用户增减数据")
@PreAuthorize("@ss.hasPermission('mp:statistics:query')")
public CommonResult<List<WxDataCubeUserSummary>> getAccount(MpStatisticsGetReqVO getReqVO) {
List<WxDataCubeUserSummary> list = mpStatisticsService.getUserSummary(getReqVO.getId(), getReqVO.getDate());
public CommonResult<List<MpStatisticsUserSummaryRespVO>> getUserSummary(MpStatisticsGetReqVO getReqVO) {
List<WxDataCubeUserSummary> list = mpStatisticsService.getUserSummary(
getReqVO.getAccountId(), getReqVO.getDate());
return success(MpStatisticsConvert.INSTANCE.convertList01(list));
}
@GetMapping("/user-cumulate")
@ApiOperation("获得用户累计数据")
@PreAuthorize("@ss.hasPermission('mp:statistics:query')")
public CommonResult<List<MpStatisticsUserCumulateRespVO>> getUserCumulate(MpStatisticsGetReqVO getReqVO) {
List<WxDataCubeUserCumulate> list = mpStatisticsService.getUserCumulate(
getReqVO.getAccountId(), getReqVO.getDate());
return success(MpStatisticsConvert.INSTANCE.convertList02(list));
}
@GetMapping("/upstream-message")
@ApiOperation("获取消息发送概况数据")
@PreAuthorize("@ss.hasPermission('mp:statistics:query')")
public CommonResult<List<MpStatisticsUpstreamMessageRespVO>> getUpstreamMessage(MpStatisticsGetReqVO getReqVO) {
List<WxDataCubeMsgResult> list = mpStatisticsService.getUpstreamMessage(
getReqVO.getAccountId(), getReqVO.getDate());
return success(MpStatisticsConvert.INSTANCE.convertList03(list));
}
}

View File

@ -16,7 +16,7 @@ public class MpStatisticsGetReqVO {
@ApiModelProperty(value = "公众号账号的编号", required = true, example = "1024")
@NotNull(message = "公众号账号的编号不能为空")
private Long id;
private Long accountId;
@ApiModelProperty(value = "查询时间范围")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)

View File

@ -0,0 +1,22 @@
package cn.iocoder.yudao.module.mp.controller.admin.statistics.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@ApiModel("管理后台 - 某一天的用户增减数据 Response VO")
@Data
public class MpStatisticsUpstreamMessageRespVO {
@ApiModelProperty(value = "日期", required = true)
private Date refDate;
@ApiModelProperty(value = "上行发送了(向公众号发送了)消息的用户数", required = true, example = "10")
private Integer messageUser;
@ApiModelProperty(value = "上行发送了消息的消息总数", required = true, example = "20")
private Integer messageCount;
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.mp.controller.admin.statistics.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@ApiModel("管理后台 - 某一天的消息发送概况数据 Response VO")
@Data
public class MpStatisticsUserCumulateRespVO {
@ApiModelProperty(value = "日期", required = true)
private Date refDate;
@ApiModelProperty(value = "累计用户量", required = true, example = "10")
private Integer cumulateUser;
}

View File

@ -1,7 +1,14 @@
package cn.iocoder.yudao.module.mp.convert.statistics;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsUpstreamMessageRespVO;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsUserCumulateRespVO;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsUserSummaryRespVO;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeMsgResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@ -11,6 +18,17 @@ public interface MpStatisticsConvert {
MpStatisticsConvert INSTANCE = Mappers.getMapper(MpStatisticsConvert.class);
List<WxDataCubeUserSummary> convertList01(List<WxDataCubeUserSummary> list);
List<MpStatisticsUserSummaryRespVO> convertList01(List<WxDataCubeUserSummary> list);
List<MpStatisticsUserCumulateRespVO> convertList02(List<WxDataCubeUserCumulate> list);
List<MpStatisticsUpstreamMessageRespVO> convertList03(List<WxDataCubeMsgResult> list);
@Mappings({
@Mapping(source = "refDate", target = "refDate", dateFormat = "yyyy-MM-dd"),
@Mapping(source = "msgUser", target = "messageUser"),
@Mapping(source = "msgCount", target = "messageCount"),
})
MpStatisticsUpstreamMessageRespVO convert(WxDataCubeMsgResult bean);
}

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.module.mp.service.statistics;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeMsgResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import java.time.LocalDateTime;
@ -15,10 +17,28 @@ public interface MpStatisticsService {
/**
* 获取用户增减数据
*
* @param id 公众号账号编号
* @param accountId 公众号账号编号
* @param date 时间区间
* @return 用户增减数据
*/
List<WxDataCubeUserSummary> getUserSummary(Long id, LocalDateTime[] date);
List<WxDataCubeUserSummary> getUserSummary(Long accountId, LocalDateTime[] date);
/**
* 获取用户累计数据
*
* @param accountId 公众号账号编号
* @param date 时间区间
* @return 用户累计数据
*/
List<WxDataCubeUserCumulate> getUserCumulate(Long accountId, LocalDateTime[] date);
/**
* 获取消息发送概况数据
*
* @param accountId 公众号账号编号
* @param date 时间区间
* @return 消息发送概况数据
*/
List<WxDataCubeMsgResult> getUpstreamMessage(Long accountId, LocalDateTime[] date);
}

View File

@ -4,6 +4,8 @@ import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeMsgResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@ -13,7 +15,7 @@ import java.time.LocalDateTime;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.STATISTICS_GET_USER_SUMMARY_FAIL;
import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.*;
/**
* 公众号统计 Service 实现类
@ -28,8 +30,8 @@ public class MpStatisticsServiceImpl implements MpStatisticsService {
private MpServiceFactory mpServiceFactory;
@Override
public List<WxDataCubeUserSummary> getUserSummary(Long id, LocalDateTime[] date) {
WxMpService mpService = mpServiceFactory.getRequiredMpService(id);
public List<WxDataCubeUserSummary> getUserSummary(Long accountId, LocalDateTime[] date) {
WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId);
try {
return mpService.getDataCubeService().getUserSummary(
DateUtil.date(date[0]), DateUtil.date(date[1]));
@ -38,5 +40,26 @@ public class MpStatisticsServiceImpl implements MpStatisticsService {
}
}
@Override
public List<WxDataCubeUserCumulate> getUserCumulate(Long accountId, LocalDateTime[] date) {
WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId);
try {
return mpService.getDataCubeService().getUserCumulate(
DateUtil.date(date[0]), DateUtil.date(date[1]));
} catch (WxErrorException e) {
throw exception(STATISTICS_GET_USER_CUMULATE_FAIL, e.getError().getErrorMsg());
}
}
@Override
public List<WxDataCubeMsgResult> getUpstreamMessage(Long accountId, LocalDateTime[] date) {
WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId);
try {
return mpService.getDataCubeService().getUpstreamMsg(
DateUtil.date(date[0]), DateUtil.date(date[1]));
} catch (WxErrorException e) {
throw exception(STATISTICS_GET_UPSTREAM_MESSAGE_FAIL, e.getError().getErrorMsg());
}
}
}