From b37d12cefda9fe00725fc6b0a12dd20cf8208d78 Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Wed, 24 Jul 2024 15:11:19 +0800 Subject: [PATCH] =?UTF-8?q?release:=20=E7=94=A8=E6=88=B7=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=8E=B7=E5=8F=96=E7=A7=AF=E5=88=86=EF=BC=8C?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E7=A7=AF=E5=88=86=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/enums/PointEnums.java | 6 + .../luoo/user/controller/PointController.java | 47 +++++- .../java/com/luoo/user/dao/TaskPointDao.java | 8 +- .../com/luoo/user/dao/UserPointLogDao.java | 18 +++ .../luoo/user/listener/PointLogListener.java | 33 ++++ .../java/com/luoo/user/pojo/TaskPoint.java | 38 +---- .../java/com/luoo/user/pojo/UserInfo.java | 5 + .../java/com/luoo/user/pojo/UserPointLog.java | 5 + .../luoo/user/service/TaskPointService.java | 2 +- .../user/service/UserPointLogService.java | 144 ++++++++++++++++++ luoo_user/src/main/resources/sql/20240719.sql | 25 +-- luoo_user/src/main/resources/sql/20240724.sql | 3 + 12 files changed, 278 insertions(+), 56 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java create mode 100644 luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java create mode 100644 luoo_user/src/main/resources/sql/20240724.sql diff --git a/luoo_common/src/main/java/enums/PointEnums.java b/luoo_common/src/main/java/enums/PointEnums.java index d4064c2..f0722ae 100644 --- a/luoo_common/src/main/java/enums/PointEnums.java +++ b/luoo_common/src/main/java/enums/PointEnums.java @@ -8,6 +8,12 @@ public enum PointEnums { VALID(1, "启用"), INVALID(2, "禁用"), + TASK_TYPE_NEW(1, "新手任务"), + TASK_TYPE_DAILY(2, "日常任务"), + + TASK_POINT_TYPE_ADD(1, "增加"), + TASK_POINT_TYPE_REDUCE(2, "减少"), + ; private final Integer code; private final String description; diff --git a/luoo_user/src/main/java/com/luoo/user/controller/PointController.java b/luoo_user/src/main/java/com/luoo/user/controller/PointController.java index 3adbb80..620adb4 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/PointController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/PointController.java @@ -4,7 +4,9 @@ import annotation.GlobalInterceptor; import api.PageResult; import api.Result; import com.luoo.user.pojo.TaskPoint; +import com.luoo.user.pojo.UserPointLog; import com.luoo.user.service.TaskPointService; +import com.luoo.user.service.UserPointLogService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -13,6 +15,7 @@ import io.swagger.annotations.ApiParam; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -31,6 +34,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j +@CrossOrigin @RequestMapping("/point") @Api(tags = "积分模块") public class PointController { @@ -38,13 +42,16 @@ public class PointController { @Autowired private TaskPointService taskPointService; + @Autowired + private UserPointLogService userPointLogService; + @ApiOperation(value = "1.1.新增任务积分配置", notes = "仅限admin权限用户调用") @PostMapping("/task/add") @GlobalInterceptor(checkAdminLogin = true) @ApiImplicitParams({ @ApiImplicitParam(name = "taskPoint", value = "任务积分配置", required = true, dataType = "TaskPoint", paramType = "body"), - @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")}) + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")}) public Result add( @RequestHeader(value = "Authorization", required = true) String authorization, @RequestBody TaskPoint taskPoint) { @@ -57,7 +64,7 @@ public class PointController { @GlobalInterceptor(checkAdminLogin = true) @ApiImplicitParams({ @ApiImplicitParam(name = "taskPoint", value = "任务积分配置", required = true, dataType = "TaskPoint", paramType = "body"), - @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")} + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")} ) public Result update( @RequestHeader(value = "Authorization", required = true) String authorization, @@ -71,7 +78,7 @@ public class PointController { @GlobalInterceptor(checkAdminLogin = true) @ApiImplicitParams({ @ApiImplicitParam(name = "taskPoints", value = "任务积分配置", required = true, dataType = "List", paramType = "body"), - @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")} + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header")} ) public Result batchUpdate( @RequestHeader(value = "Authorization", required = true) String authorization, @@ -85,7 +92,7 @@ public class PointController { @GlobalInterceptor(checkAdminLogin = true) @ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "任务积分配置id", required = true, dataType = "List", paramType = "body"), - @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header"), + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header"), @ApiImplicitParam(name = "valid", value = "是否生效 1-生效 2-不生效", required = true, dataType = "Integer", paramType = "query") } ) @@ -102,7 +109,7 @@ public class PointController { @GlobalInterceptor(checkAdminLogin = true) @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "任务积分配置id", required = true, dataType = "String", paramType = "body"), - @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header"), + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header"), @ApiImplicitParam(name = "valid", value = "是否生效 1-生效 2-不生效", required = true, dataType = "Integer", paramType = "query") }) public Result disable( @@ -115,12 +122,40 @@ public class PointController { @ApiOperation(value = "1.6. 任务积分列表", notes = "任务积分列表") @GetMapping("/task/list/{page}/{size}") - public Result> getUnApproveList(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, + public Result> getUnApproveList( + @ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, @ApiParam(value = "页码", required = true) @PathVariable Integer page, @ApiParam(value = "每页条数", required = true) @PathVariable Integer size) { return Result.success(taskPointService.getTaskPointList(page, size)); } + @ApiOperation(value = "2.1. 用户根据任务获取积分", notes = "仅限app用户调用") + @PostMapping("/log/earn/task") + @GlobalInterceptor(checkAppUserLogin = true) + @ApiImplicitParams({ + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header"), + @ApiImplicitParam(name = "taskPointId", value = "任务积分配置id", required = true, dataType = "String", paramType = "body") + }) + public Result addLog( + @RequestHeader(value = "Authorization", required = true) String authorization, + @RequestBody String taskPointId) { + userPointLogService.addByTask(taskPointId, authorization); + return Result.success(); + } + + @ApiOperation(value = "2.2.用户积分日志列表", notes = "用户积分日志列表") + @GetMapping("/log/list/{page}/{size}") + @ApiImplicitParams({ + @ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "String", paramType = "header"), + @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer", paramType = "path"), + @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataType = "Integer", paramType = "path") + }) + public Result> getUserPointLogList( + @ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, + @ApiParam(value = "页码", required = true) @PathVariable Integer page, + @ApiParam(value = "每页条数", required = true) @PathVariable Integer size) { + return Result.success(userPointLogService.getUserPointLogList(token, page, size)); + } } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/TaskPointDao.java b/luoo_user/src/main/java/com/luoo/user/dao/TaskPointDao.java index 3a8a50c..57a0de0 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/TaskPointDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/TaskPointDao.java @@ -1,15 +1,15 @@ package com.luoo.user.dao; import com.luoo.user.pojo.TaskPoint; -import java.awt.print.Pageable; import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; -public interface TaskPointDao extends JpaRepository, - JpaSpecificationExecutor { +public interface TaskPointDao extends JpaRepository, JpaSpecificationExecutor { @Query(value = " select * from tb_task_point order by id desc", countProjection = "id", nativeQuery = true) - public Page findAll(Pageable pageable); + public Page selectAll(Pageable pageable); + } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java new file mode 100644 index 0000000..1aa176b --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java @@ -0,0 +1,18 @@ +package com.luoo.user.dao; + +import com.luoo.user.pojo.MembershipCode; +import com.luoo.user.pojo.UserPointLog; +import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; + +public interface UserPointLogDao extends JpaRepository, JpaSpecificationExecutor { + + public List findUserPointLogByUserIdAndTaskPointId(String userId, String taskPointId); + + @Query(value = " select * from tb_user_point_log where user_id = ?1 order by create_time desc", countProjection = "id", nativeQuery = true) + public Page getUserPointLogList(String userId, Pageable pageable); +} diff --git a/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java b/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java new file mode 100644 index 0000000..856c1a1 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java @@ -0,0 +1,33 @@ +package com.luoo.user.listener; + +import com.luoo.user.pojo.UserPointLog; +import com.luoo.user.service.UserPointLogService; +import dto.MusicPointDto; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @program: luoo_parent + * @description: 积分日志队列 + * @author: yawei.huang + * @create: 2024-07-24 13:05 + **/ +@Component +@RabbitListener(queues = "pointLog") +@Slf4j +public class PointLogListener { + + @Autowired + private UserPointLogService userPointLogService; + + @RabbitHandler + public void executePointLog(UserPointLog userPointLog) { + log.info("userPointLog:{}", userPointLog); + userPointLogService.add(userPointLog); + } + +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/TaskPoint.java b/luoo_user/src/main/java/com/luoo/user/pojo/TaskPoint.java index c9f499b..1d4c6ff 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/TaskPoint.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/TaskPoint.java @@ -12,11 +12,9 @@ import javax.persistence.Table; import javax.validation.constraints.Size; import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.Getter; +import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -import lombok.RequiredArgsConstructor; -import lombok.Setter; -import lombok.ToString; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.proxy.HibernateProxy; @@ -25,12 +23,10 @@ import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import org.springframework.format.annotation.DateTimeFormat; -@Getter -@Setter -@ToString -@RequiredArgsConstructor +@Data @NoArgsConstructor @AllArgsConstructor +@EqualsAndHashCode @Builder @Entity @DynamicInsert @@ -83,30 +79,4 @@ public class TaskPoint { @ApiModelProperty("是否生效,1-生效 2-不生效") private Integer valid; - @Override - public final boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null) { - return false; - } - Class oEffectiveClass = o instanceof HibernateProxy - ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() - : o.getClass(); - Class thisEffectiveClass = this instanceof HibernateProxy - ? ((HibernateProxy) this).getHibernateLazyInitializer() - .getPersistentClass() : this.getClass(); - if (thisEffectiveClass != oEffectiveClass) { - return false; - } - TaskPoint point = (TaskPoint) o; - return getId() != null && Objects.equals(getId(), point.getId()); - } - - @Override - public final int hashCode() { - return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer() - .getPersistentClass().hashCode() : getClass().hashCode(); - } } \ No newline at end of file diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java index bc284e0..cf116bc 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java @@ -202,5 +202,10 @@ public class UserInfo implements Serializable { @DateTimeFormat(pattern = "yyyy-MM-dd") private LocalDate vipExpireTime; + /** + * 积分 + */ + private Integer point; + } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserPointLog.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserPointLog.java index 99109b2..3c6fecb 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserPointLog.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserPointLog.java @@ -45,6 +45,11 @@ public class UserPointLog implements Serializable { @ApiModelProperty(value = "用户id") private String userId; + @Size(max = 20) + @Column(name = "task_point_id", length = 20) + @ApiModelProperty(value = "任务积分id") + private String taskPointId; + @Size(max = 255) @Column(name = "description") @ApiModelProperty(value = "事件描述") diff --git a/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java b/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java index ceb2adc..21af1ab 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java @@ -117,7 +117,7 @@ public class TaskPointService { */ public PageResult getTaskPointList(Integer page, Integer size) { Pageable pageable = PageRequest.of(page - 1, size); - Page taskPointPage = taskPointDao.findAll(pageable); + Page taskPointPage = taskPointDao.selectAll(pageable); long totalElements = taskPointPage.getTotalElements(); return new PageResult<>(totalElements, taskPointPage.getContent()); } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java new file mode 100644 index 0000000..1436e2d --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java @@ -0,0 +1,144 @@ +package com.luoo.user.service; + +import api.PageResult; +import com.luoo.user.dao.TaskPointDao; +import com.luoo.user.dao.UserPointLogDao; +import com.luoo.user.pojo.TaskPoint; +import com.luoo.user.pojo.UserInfo; +import com.luoo.user.pojo.UserPointLog; +import dto.UserLoginDto; +import enums.PointEnums; +import java.util.Objects; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import util.IdWorker; +import util.JwtUtil; + +/** + * @program: luoo_parent + * @description: 积分明细 + * @author: yawei.huang + * @create: 2024-07-24 08:41 + **/ +@Service +@Slf4j +public class UserPointLogService { + + private final UserPointLogDao userPointLogDao; + + private final IdWorker idWorker; + + private final JwtUtil jwtUtil; + + private final TaskPointDao taskPointDao; + + private final RabbitTemplate rabbitTemplate; + + private final UserInfoService userInfoService; + + + public UserPointLogService(UserPointLogDao userPointLogDao, IdWorker idWorker, JwtUtil jwtUtil, + TaskPointService taskPointService, TaskPointDao taskPointDao, RabbitTemplate rabbitTemplate, + UserInfoService userInfoService) { + this.userPointLogDao = userPointLogDao; + this.idWorker = idWorker; + this.jwtUtil = jwtUtil; + this.taskPointDao = taskPointDao; + this.rabbitTemplate = rabbitTemplate; + this.userInfoService = userInfoService; + } + + /** + * 通过任务添加积分明细。 + *

+ * 此方法用于处理用户完成特定任务后增加积分的逻辑。它首先根据任务点ID获取任务点信息, 然后根据用户token获取用户信息,最后创建一个积分日志对象并将其发送到RabbitMQ, + * 以异步方式记录用户的积分变动。 + * + * @param taskPointId 任务点ID,用于查找任务点信息。 + * @param token 用户的token,用于验证用户身份并获取用户ID。 + */ + public void addByTask(String taskPointId, String token) { + + TaskPoint taskPoint = taskPointDao.findById(taskPointId).get(); + + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); + + UserPointLog userPointLog = UserPointLog.builder() + .id(String.valueOf(idWorker.nextId())) + .score(taskPoint.getScore()) + .type(PointEnums.TASK_POINT_TYPE_ADD.getCode()) + .description(taskPoint.getDescription()) + .createUser(userLoginDto.getUserId()) + .taskPointId(taskPointId) + .userId(userLoginDto.getUserId()) + .build(); + + rabbitTemplate.convertAndSend("pointLog", userPointLog); + } + + /** + * 添加用户积分记录。 + *

+ * 通过@Transactional注解确保该方法操作的数据库事务一致性,任何异常都将导致事务回滚。 + * + * @param userPointLog 用户积分记录对象,包含积分任务ID、用户ID和积分得分。 + */ + @Transactional(rollbackFor = Exception.class) + public void add(UserPointLog userPointLog) { + // 根据积分任务ID获取积分任务详情 + // 保存积分记录 + String taskPointId = userPointLog.getTaskPointId(); + TaskPoint taskPoint = taskPointDao.findById(taskPointId).get(); + + // 获取积分任务类型 + Integer type = taskPoint.getType(); + + // 对于新手任务,只允许完成一次,如果用户已经完成过,则直接返回,不重复添加积分 + if (Objects.equals(type, PointEnums.TASK_TYPE_NEW.getCode())) { + // 新手任务,只触发一次 + if (!userPointLogDao.findUserPointLogByUserIdAndTaskPointId(userPointLog.getUserId(), + userPointLog.getTaskPointId()).isEmpty()) { + return; + } + } + + // 保存用户积分记录 + userPointLogDao.save(userPointLog); + + // 更新用户积分,获取用户信息并累加积分得分 + // 对用户进行积分计算 + UserInfo userInfo = userInfoService.findById(userPointLog.getUserId()); + Integer point = userInfo.getPoint(); + point += userPointLog.getScore(); + userInfo.setPoint(point); + // 更新用户积分信息 + userInfoService.update(userInfo); + + } + + /** + * 分页查询用户积分列表 + * + * @param token 用户token + * @param page 页码 + * @param size 每页数量 + * @return 用户积分列表 + */ + public PageResult getUserPointLogList(String token, Integer page, Integer size) { + Pageable pageable = PageRequest.of(page - 1, size); + + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); + Page taskPointPage = userPointLogDao.getUserPointLogList(userLoginDto.getUserId(), + pageable); + long totalElements = taskPointPage.getTotalElements(); + + return new PageResult<>(totalElements, taskPointPage.getContent()); + } + +} diff --git a/luoo_user/src/main/resources/sql/20240719.sql b/luoo_user/src/main/resources/sql/20240719.sql index c509d84..01c8c97 100644 --- a/luoo_user/src/main/resources/sql/20240719.sql +++ b/luoo_user/src/main/resources/sql/20240719.sql @@ -12,20 +12,23 @@ create table tb_membership_code update_user varchar(20) null comment '修改人' ) comment '会员码'; - create table tb_user_point_log ( - id varchar(20) not null comment 'id' + id varchar(20) not null comment 'id' primary key, - score int null comment '分数', - type tinyint null comment '1- 增加 2-减少', - user_id varchar(20) null comment '所属用户', - description varchar(255) null comment '事件描述', - create_time datetime null comment '创建时间', - update_time datetime null comment '修改时间', - create_user varchar(20) null comment '创建人', - update_user varchar(20) null comment '修改人' -) comment '积分记录表'; + score int null comment '分数', + type tinyint null comment '1- 增加 2-减少', + user_id varchar(20) null comment '所属用户', + description varchar(255) null comment '事件描述', + create_time datetime null comment '创建时间', + update_time datetime null comment '修改时间', + create_user varchar(20) null comment '创建人', + update_user varchar(20) null comment '修改人', + task_point_id varchar(20) not null comment 'tb_task_point表id' +) + comment '积分记录表'; + + alter table tb_user_info add vip_status tinyint default 0 not null comment '会员状态 0-未开通 1-生效中 2-已过期'; diff --git a/luoo_user/src/main/resources/sql/20240724.sql b/luoo_user/src/main/resources/sql/20240724.sql new file mode 100644 index 0000000..1ad03fd --- /dev/null +++ b/luoo_user/src/main/resources/sql/20240724.sql @@ -0,0 +1,3 @@ +alter table tb_user_info + add point int default 0 not null comment '积分'; +