feat(notify): 新增用户通知授权管理功能

- 添加用户通知授权记录的完整CRUD操作
- 实现授权次数的记录与消费逻辑
- 提供授权状态检查与剩余次数查询接口
- 支持按用户、模板或景区维度查询授权记录
- 新增授权统计信息接口,包括总授权数、消费数等
- 完成移动端相关请求/响应DTO定义
- 集成MyBatis Mapper实现数据持久化操作
- 添加服务层事务控制确保操作一致性
This commit is contained in:
2025-10-14 21:54:45 +08:00
parent 19ca91778f
commit 44b20890d5
13 changed files with 1001 additions and 0 deletions

View File

@@ -0,0 +1,237 @@
package com.ycwl.basic.controller.mobile.notify;
import com.ycwl.basic.config.JwtInfo;
import com.ycwl.basic.model.mobile.notify.req.NotificationAuthCheckReq;
import com.ycwl.basic.model.mobile.notify.req.NotificationAuthConsumeReq;
import com.ycwl.basic.model.mobile.notify.req.NotificationAuthRecordReq;
import com.ycwl.basic.model.mobile.notify.req.NotificationAuthRecordsReq;
import com.ycwl.basic.model.mobile.notify.resp.NotificationAuthCheckResp;
import com.ycwl.basic.model.mobile.notify.resp.NotificationAuthConsumeResp;
import com.ycwl.basic.model.mobile.notify.resp.NotificationAuthRecordResp;
import com.ycwl.basic.model.pc.notify.entity.UserNotificationAuthorizationEntity;
import com.ycwl.basic.service.UserNotificationAuthorizationService;
import com.ycwl.basic.utils.ResponseData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 用户通知授权记录Controller (移动端API)
*
* @Author: System
* @Date: 2024/12/28
*/
@RestController
@RequestMapping("/api/mobile/notify/auth")
@Slf4j
public class UserNotificationAuthController {
@Autowired
private UserNotificationAuthorizationService userNotificationAuthorizationService;
/**
* 检查用户通知授权状态
*/
@PostMapping("/check")
public ResponseData<NotificationAuthCheckResp> checkAuthorization(
@JwtInfo Long memberId,
@Valid @RequestBody NotificationAuthCheckReq req) {
log.info("检查用户通知授权状态: memberId={}, templateId={}, scenicId={}",
memberId, req.getTemplateId(), req.getScenicId());
try {
UserNotificationAuthorizationEntity record = userNotificationAuthorizationService
.checkAuthorization(memberId, req.getTemplateId(), req.getScenicId());
NotificationAuthCheckResp resp = new NotificationAuthCheckResp();
if (record != null) {
resp.setHasAuthorization(true);
resp.setRemainingCount(record.getRemainingCount());
resp.setAuthorizationCount(record.getAuthorizationCount());
resp.setConsumedCount(record.getConsumedCount());
resp.setLastAuthorizedTime(record.getLastAuthorizedTime());
resp.setLastConsumedTime(record.getLastConsumedTime());
resp.setTemplateId(record.getTemplateId());
resp.setScenicId(record.getScenicId());
} else {
resp.setHasAuthorization(false);
resp.setRemainingCount(0);
resp.setAuthorizationCount(0);
resp.setConsumedCount(0);
resp.setTemplateId(req.getTemplateId());
resp.setScenicId(req.getScenicId());
}
return ResponseData.success(resp);
} catch (Exception e) {
log.error("检查用户通知授权状态失败", e);
return ResponseData.fail("检查授权状态失败: " + e.getMessage());
}
}
/**
* 记录用户通知授权
*/
@PostMapping("/record")
public ResponseData<NotificationAuthRecordResp> recordAuthorization(
@JwtInfo Long memberId,
@Valid @RequestBody NotificationAuthRecordReq req) {
log.info("记录用户通知授权: memberId={}, templateId={}, scenicId={}",
memberId, req.getTemplateId(), req.getScenicId());
try {
UserNotificationAuthorizationEntity record = userNotificationAuthorizationService
.recordAuthorization(memberId, req.getTemplateId(), req.getScenicId());
NotificationAuthRecordResp resp = new NotificationAuthRecordResp();
BeanUtils.copyProperties(record, resp);
return ResponseData.success(resp);
} catch (Exception e) {
log.error("记录用户通知授权失败", e);
return ResponseData.fail("记录授权失败: " + e.getMessage());
}
}
/**
* 消费通知授权
*/
@PostMapping("/consume")
public ResponseData<NotificationAuthConsumeResp> consumeAuthorization(
@JwtInfo Long memberId,
@Valid @RequestBody NotificationAuthConsumeReq req) {
log.info("消费通知授权: memberId={}, templateId={}, scenicId={}",
memberId, req.getTemplateId(), req.getScenicId());
try {
boolean consumeSuccess = userNotificationAuthorizationService
.consumeAuthorization(memberId, req.getTemplateId(), req.getScenicId());
NotificationAuthConsumeResp resp = new NotificationAuthConsumeResp();
resp.setConsumeSuccess(consumeSuccess);
if (consumeSuccess) {
Integer remainingCount = userNotificationAuthorizationService
.getRemainingCount(memberId, req.getTemplateId(), req.getScenicId());
resp.setRemainingCount(remainingCount);
} else {
resp.setFailReason("授权次数不足或授权记录不存在");
resp.setRemainingCount(0);
}
return ResponseData.success(resp);
} catch (Exception e) {
log.error("消费通知授权失败", e);
return ResponseData.fail("消费授权失败: " + e.getMessage());
}
}
/**
* 查询用户通知授权记录
*/
@GetMapping("/records")
public ResponseData<List<NotificationAuthRecordResp>> getUserAuthorizationRecords(
@JwtInfo Long memberId,
@Valid NotificationAuthRecordsReq req) {
log.info("查询用户通知授权记录: memberId={}, templateId={}", memberId, req.getTemplateId());
try {
List<UserNotificationAuthorizationEntity> records;
if (req.getTemplateId() != null && !req.getTemplateId().trim().isEmpty()) {
records = userNotificationAuthorizationService
.getUserAuthorizationsByTemplate(memberId, req.getTemplateId());
} else {
records = userNotificationAuthorizationService.getUserAuthorizations(memberId);
}
List<NotificationAuthRecordResp> respList = new ArrayList<>();
if (!CollectionUtils.isEmpty(records)) {
for (UserNotificationAuthorizationEntity record : records) {
NotificationAuthRecordResp resp = new NotificationAuthRecordResp();
BeanUtils.copyProperties(record, resp);
// TODO: 可以在这里添加景区名称查询
respList.add(resp);
}
}
return ResponseData.success(respList);
} catch (Exception e) {
log.error("查询用户通知授权记录失败", e);
return ResponseData.fail("查询授权记录失败: " + e.getMessage());
}
}
/**
* 获取用户授权统计信息
*/
@GetMapping("/stats")
public ResponseData<AuthorizationStats> getAuthorizationStats(@JwtInfo Long memberId) {
log.info("获取用户授权统计信息: memberId={}", memberId);
try {
List<UserNotificationAuthorizationEntity> records = userNotificationAuthorizationService
.getUserAuthorizations(memberId);
int totalTemplates = records.size();
int totalAuthorizations = records.stream()
.mapToInt(UserNotificationAuthorizationEntity::getAuthorizationCount)
.sum();
int totalConsumed = records.stream()
.mapToInt(UserNotificationAuthorizationEntity::getConsumedCount)
.sum();
int totalRemaining = totalAuthorizations - totalConsumed;
// 创建统计信息对象
AuthorizationStats stats = new AuthorizationStats();
stats.totalTemplates = totalTemplates;
stats.totalAuthorizations = totalAuthorizations;
stats.totalConsumed = totalConsumed;
stats.totalRemaining = totalRemaining;
stats.queryTime = new Date();
return ResponseData.success(stats);
} catch (Exception e) {
log.error("获取用户授权统计信息失败", e);
return ResponseData.fail("获取统计信息失败: " + e.getMessage());
}
}
/**
* 授权统计信息DTO
*/
@Data
public static class AuthorizationStats {
/**
* 总模板数
*/
private int totalTemplates;
/**
* 总授权次数
*/
private int totalAuthorizations;
/**
* 总消费次数
*/
private int totalConsumed;
/**
* 剩余授权次数
*/
private int totalRemaining;
/**
* 查询时间
*/
private Date queryTime;
}
}