You've already forked FrameTour-BE
feat(notify): 新增用户通知授权管理功能
- 添加用户通知授权记录的完整CRUD操作 - 实现授权次数的记录与消费逻辑 - 提供授权状态检查与剩余次数查询接口 - 支持按用户、模板或景区维度查询授权记录 - 新增授权统计信息接口,包括总授权数、消费数等 - 完成移动端相关请求/响应DTO定义 - 集成MyBatis Mapper实现数据持久化操作 - 添加服务层事务控制确保操作一致性
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package com.ycwl.basic.service;
|
||||
|
||||
import com.ycwl.basic.model.pc.notify.entity.UserNotificationAuthorizationEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户通知授权记录Service接口
|
||||
*
|
||||
* @Author: System
|
||||
* @Date: 2024/12/28
|
||||
*/
|
||||
public interface UserNotificationAuthorizationService {
|
||||
|
||||
/**
|
||||
* 检查用户是否对指定模板有授权
|
||||
*
|
||||
* @param memberId 用户ID
|
||||
* @param templateId 模板ID
|
||||
* @param scenicId 景区ID
|
||||
* @return 授权记录,如果没有授权返回null
|
||||
*/
|
||||
UserNotificationAuthorizationEntity checkAuthorization(Long memberId, String templateId, Long scenicId);
|
||||
|
||||
/**
|
||||
* 检查用户是否还有剩余授权次数
|
||||
*
|
||||
* @param memberId 用户ID
|
||||
* @param templateId 模板ID
|
||||
* @param scenicId 景区ID
|
||||
* @return 剩余授权次数,0表示没有剩余
|
||||
*/
|
||||
Integer getRemainingCount(Long memberId, String templateId, Long scenicId);
|
||||
|
||||
/**
|
||||
* 记录用户授权
|
||||
*
|
||||
* @param memberId 用户ID
|
||||
* @param templateId 模板ID
|
||||
* @param scenicId 景区ID
|
||||
* @return 授权记录
|
||||
*/
|
||||
UserNotificationAuthorizationEntity recordAuthorization(Long memberId, String templateId, Long scenicId);
|
||||
|
||||
/**
|
||||
* 消费一次授权
|
||||
*
|
||||
* @param memberId 用户ID
|
||||
* @param templateId 模板ID
|
||||
* @param scenicId 景区ID
|
||||
* @return 是否消费成功
|
||||
*/
|
||||
boolean consumeAuthorization(Long memberId, String templateId, Long scenicId);
|
||||
|
||||
/**
|
||||
* 查询用户的授权记录列表
|
||||
*
|
||||
* @param memberId 用户ID
|
||||
* @return 授权记录列表
|
||||
*/
|
||||
List<UserNotificationAuthorizationEntity> getUserAuthorizations(Long memberId);
|
||||
|
||||
/**
|
||||
* 查询用户对指定模板的授权记录列表
|
||||
*
|
||||
* @param memberId 用户ID
|
||||
* @param templateId 模板ID
|
||||
* @return 授权记录列表
|
||||
*/
|
||||
List<UserNotificationAuthorizationEntity> getUserAuthorizationsByTemplate(Long memberId, String templateId);
|
||||
|
||||
/**
|
||||
* 查询景区的授权记录列表
|
||||
*
|
||||
* @param scenicId 景区ID
|
||||
* @return 授权记录列表
|
||||
*/
|
||||
List<UserNotificationAuthorizationEntity> getScenicAuthorizations(Long scenicId);
|
||||
|
||||
/**
|
||||
* 根据ID查询授权记录
|
||||
*
|
||||
* @param id 记录ID
|
||||
* @return 授权记录
|
||||
*/
|
||||
UserNotificationAuthorizationEntity getById(Long id);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.ycwl.basic.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ycwl.basic.mapper.UserNotificationAuthorizationMapper;
|
||||
import com.ycwl.basic.model.pc.notify.entity.UserNotificationAuthorizationEntity;
|
||||
import com.ycwl.basic.service.UserNotificationAuthorizationService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户通知授权记录Service实现类
|
||||
*
|
||||
* @Author: System
|
||||
* @Date: 2024/12/28
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UserNotificationAuthorizationServiceImpl implements UserNotificationAuthorizationService {
|
||||
|
||||
@Autowired
|
||||
private UserNotificationAuthorizationMapper userNotificationAuthorizationMapper;
|
||||
|
||||
@Override
|
||||
public UserNotificationAuthorizationEntity checkAuthorization(Long memberId, String templateId, Long scenicId) {
|
||||
log.debug("检查用户授权: memberId={}, templateId={}, scenicId={}", memberId, templateId, scenicId);
|
||||
return userNotificationAuthorizationMapper.selectByMemberAndTemplateAndScenic(memberId, templateId, scenicId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getRemainingCount(Long memberId, String templateId, Long scenicId) {
|
||||
log.debug("获取剩余授权次数: memberId={}, templateId={}, scenicId={}", memberId, templateId, scenicId);
|
||||
Integer remainingCount = userNotificationAuthorizationMapper.selectRemainingCount(memberId, templateId, scenicId);
|
||||
return remainingCount != null ? remainingCount : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public UserNotificationAuthorizationEntity recordAuthorization(Long memberId, String templateId, Long scenicId) {
|
||||
log.info("记录用户授权: memberId={}, templateId={}, scenicId={}", memberId, templateId, scenicId);
|
||||
|
||||
// 先查询是否已存在记录
|
||||
UserNotificationAuthorizationEntity existingRecord = userNotificationAuthorizationMapper
|
||||
.selectByMemberAndTemplateAndScenic(memberId, templateId, scenicId);
|
||||
|
||||
if (existingRecord != null) {
|
||||
// 增加授权次数
|
||||
int updated = userNotificationAuthorizationMapper.increaseAuthorizationCount(existingRecord.getId(), 1);
|
||||
if (updated > 0) {
|
||||
log.info("成功增加授权次数: recordId={}, newCount={}", existingRecord.getId(),
|
||||
existingRecord.getAuthorizationCount() + 1);
|
||||
// 重新查询最新数据
|
||||
return userNotificationAuthorizationMapper.selectById(existingRecord.getId());
|
||||
} else {
|
||||
log.error("增加授权次数失败: recordId={}", existingRecord.getId());
|
||||
throw new RuntimeException("增加授权次数失败");
|
||||
}
|
||||
} else {
|
||||
// 创建新记录
|
||||
UserNotificationAuthorizationEntity newRecord = new UserNotificationAuthorizationEntity();
|
||||
newRecord.setMemberId(memberId);
|
||||
newRecord.setTemplateId(templateId);
|
||||
newRecord.setScenicId(scenicId);
|
||||
newRecord.setAuthorizationCount(1);
|
||||
newRecord.setConsumedCount(0);
|
||||
newRecord.setLastAuthorizedTime(new Date());
|
||||
newRecord.setStatus(1);
|
||||
newRecord.setCreateTime(new Date());
|
||||
newRecord.setUpdateTime(new Date());
|
||||
|
||||
int inserted = userNotificationAuthorizationMapper.insert(newRecord);
|
||||
if (inserted > 0) {
|
||||
log.info("成功创建授权记录: recordId={}", newRecord.getId());
|
||||
return newRecord;
|
||||
} else {
|
||||
log.error("创建授权记录失败: memberId={}, templateId={}, scenicId={}", memberId, templateId, scenicId);
|
||||
throw new RuntimeException("创建授权记录失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean consumeAuthorization(Long memberId, String templateId, Long scenicId) {
|
||||
log.info("消费授权: memberId={}, templateId={}, scenicId={}", memberId, templateId, scenicId);
|
||||
|
||||
UserNotificationAuthorizationEntity record = userNotificationAuthorizationMapper
|
||||
.selectByMemberAndTemplateAndScenic(memberId, templateId, scenicId);
|
||||
|
||||
if (record == null) {
|
||||
log.warn("未找到授权记录: memberId={}, templateId={}, scenicId={}", memberId, templateId, scenicId);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (record.getRemainingCount() <= 0) {
|
||||
log.warn("剩余授权次数不足: recordId={}, remainingCount={}", record.getId(), record.getRemainingCount());
|
||||
return false;
|
||||
}
|
||||
|
||||
int updated = userNotificationAuthorizationMapper.increaseConsumedCount(record.getId(), 1);
|
||||
if (updated > 0) {
|
||||
log.info("成功消费授权: recordId={}, newConsumedCount={}", record.getId(),
|
||||
record.getConsumedCount() + 1);
|
||||
return true;
|
||||
} else {
|
||||
log.error("消费授权失败: recordId={}", record.getId());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserNotificationAuthorizationEntity> getUserAuthorizations(Long memberId) {
|
||||
log.debug("查询用户授权记录: memberId={}", memberId);
|
||||
return userNotificationAuthorizationMapper.selectByMemberId(memberId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserNotificationAuthorizationEntity> getUserAuthorizationsByTemplate(Long memberId, String templateId) {
|
||||
log.debug("查询用户模板授权记录: memberId={}, templateId={}", memberId, templateId);
|
||||
return userNotificationAuthorizationMapper.selectByMemberIdAndTemplateId(memberId, templateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserNotificationAuthorizationEntity> getScenicAuthorizations(Long scenicId) {
|
||||
log.debug("查询景区授权记录: scenicId={}", scenicId);
|
||||
return userNotificationAuthorizationMapper.selectByScenicId(scenicId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserNotificationAuthorizationEntity getById(Long id) {
|
||||
log.debug("根据ID查询授权记录: id={}", id);
|
||||
return userNotificationAuthorizationMapper.selectById(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user