release: 用户根据任务获取积分,查看积分列表

release-2024-08-08
huangyw 4 months ago
parent 1122944839
commit b37d12cefd

@ -8,6 +8,12 @@ public enum PointEnums {
VALID(1, "启用"), VALID(1, "启用"),
INVALID(2, "禁用"), 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 Integer code;
private final String description; private final String description;

@ -4,7 +4,9 @@ import annotation.GlobalInterceptor;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import com.luoo.user.pojo.TaskPoint; import com.luoo.user.pojo.TaskPoint;
import com.luoo.user.pojo.UserPointLog;
import com.luoo.user.service.TaskPointService; import com.luoo.user.service.TaskPointService;
import com.luoo.user.service.UserPointLogService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
@ -13,6 +15,7 @@ import io.swagger.annotations.ApiParam;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; 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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -31,6 +34,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@Slf4j @Slf4j
@CrossOrigin
@RequestMapping("/point") @RequestMapping("/point")
@Api(tags = "积分模块") @Api(tags = "积分模块")
public class PointController { public class PointController {
@ -38,13 +42,16 @@ public class PointController {
@Autowired @Autowired
private TaskPointService taskPointService; private TaskPointService taskPointService;
@Autowired
private UserPointLogService userPointLogService;
@ApiOperation(value = "1.1.新增任务积分配置", notes = "仅限admin权限用户调用") @ApiOperation(value = "1.1.新增任务积分配置", notes = "仅限admin权限用户调用")
@PostMapping("/task/add") @PostMapping("/task/add")
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "taskPoint", value = "任务积分配置", required = true, dataType = "TaskPoint", paramType = "body"), @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<Void> add( public Result<Void> add(
@RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "Authorization", required = true) String authorization,
@RequestBody TaskPoint taskPoint) { @RequestBody TaskPoint taskPoint) {
@ -57,7 +64,7 @@ public class PointController {
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "taskPoint", value = "任务积分配置", required = true, dataType = "TaskPoint", paramType = "body"), @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<Void> update( public Result<Void> update(
@RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "Authorization", required = true) String authorization,
@ -71,7 +78,7 @@ public class PointController {
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "taskPoints", value = "任务积分配置", required = true, dataType = "List<TaskPoint>", paramType = "body"), @ApiImplicitParam(name = "taskPoints", value = "任务积分配置", required = true, dataType = "List<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<Void> batchUpdate( public Result<Void> batchUpdate(
@RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "Authorization", required = true) String authorization,
@ -85,7 +92,7 @@ public class PointController {
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "ids", value = "任务积分配置id", required = true, dataType = "List<String>", paramType = "body"), @ApiImplicitParam(name = "ids", value = "任务积分配置id", required = true, dataType = "List<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") @ApiImplicitParam(name = "valid", value = "是否生效 1-生效 2-不生效", required = true, dataType = "Integer", paramType = "query")
} }
) )
@ -102,7 +109,7 @@ public class PointController {
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "任务积分配置id", required = true, dataType = "String", paramType = "body"), @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") @ApiImplicitParam(name = "valid", value = "是否生效 1-生效 2-不生效", required = true, dataType = "Integer", paramType = "query")
}) })
public Result<Void> disable( public Result<Void> disable(
@ -115,12 +122,40 @@ public class PointController {
@ApiOperation(value = "1.6. 任务积分列表", notes = "任务积分列表") @ApiOperation(value = "1.6. 任务积分列表", notes = "任务积分列表")
@GetMapping("/task/list/{page}/{size}") @GetMapping("/task/list/{page}/{size}")
public Result<PageResult<TaskPoint>> getUnApproveList(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, public Result<PageResult<TaskPoint>> getUnApproveList(
@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token,
@ApiParam(value = "页码", required = true) @PathVariable Integer page, @ApiParam(value = "页码", required = true) @PathVariable Integer page,
@ApiParam(value = "每页条数", required = true) @PathVariable Integer size) { @ApiParam(value = "每页条数", required = true) @PathVariable Integer size) {
return Result.success(taskPointService.getTaskPointList(page, 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<Void> 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<PageResult<UserPointLog>> 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));
}
} }

@ -1,15 +1,15 @@
package com.luoo.user.dao; package com.luoo.user.dao;
import com.luoo.user.pojo.TaskPoint; import com.luoo.user.pojo.TaskPoint;
import java.awt.print.Pageable;
import org.springframework.data.domain.Page; 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.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
public interface TaskPointDao extends JpaRepository<TaskPoint, String>, public interface TaskPointDao extends JpaRepository<TaskPoint, String>, JpaSpecificationExecutor<TaskPoint> {
JpaSpecificationExecutor<TaskPoint> {
@Query(value = " select * from tb_task_point order by id desc", countProjection = "id", nativeQuery = true) @Query(value = " select * from tb_task_point order by id desc", countProjection = "id", nativeQuery = true)
public Page<TaskPoint> findAll(Pageable pageable); public Page<TaskPoint> selectAll(Pageable pageable);
} }

@ -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<UserPointLog, String>, JpaSpecificationExecutor<UserPointLog> {
public List<UserPointLog> 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<UserPointLog> getUserPointLogList(String userId, Pageable pageable);
}

@ -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);
}
}

@ -12,11 +12,9 @@ import javax.persistence.Table;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.proxy.HibernateProxy; 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.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
@Getter @Data
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@EqualsAndHashCode
@Builder @Builder
@Entity @Entity
@DynamicInsert @DynamicInsert
@ -83,30 +79,4 @@ public class TaskPoint {
@ApiModelProperty("是否生效1-生效 2-不生效") @ApiModelProperty("是否生效1-生效 2-不生效")
private Integer valid; 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();
}
} }

@ -202,5 +202,10 @@ public class UserInfo implements Serializable {
@DateTimeFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate vipExpireTime; private LocalDate vipExpireTime;
/**
*
*/
private Integer point;
} }

@ -45,6 +45,11 @@ public class UserPointLog implements Serializable {
@ApiModelProperty(value = "用户id") @ApiModelProperty(value = "用户id")
private String userId; private String userId;
@Size(max = 20)
@Column(name = "task_point_id", length = 20)
@ApiModelProperty(value = "任务积分id")
private String taskPointId;
@Size(max = 255) @Size(max = 255)
@Column(name = "description") @Column(name = "description")
@ApiModelProperty(value = "事件描述") @ApiModelProperty(value = "事件描述")

@ -117,7 +117,7 @@ public class TaskPointService {
*/ */
public PageResult<TaskPoint> getTaskPointList(Integer page, Integer size) { public PageResult<TaskPoint> getTaskPointList(Integer page, Integer size) {
Pageable pageable = PageRequest.of(page - 1, size); Pageable pageable = PageRequest.of(page - 1, size);
Page<TaskPoint> taskPointPage = taskPointDao.findAll(pageable); Page<TaskPoint> taskPointPage = taskPointDao.selectAll(pageable);
long totalElements = taskPointPage.getTotalElements(); long totalElements = taskPointPage.getTotalElements();
return new PageResult<>(totalElements, taskPointPage.getContent()); return new PageResult<>(totalElements, taskPointPage.getContent());
} }

@ -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;
}
/**
*
* <p>
* ID tokenRabbitMQ
*
*
* @param taskPointId ID
* @param token tokenID
*/
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);
}
/**
*
* <p>
* @Transactional
*
* @param userPointLog IDID
*/
@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<UserPointLog> getUserPointLogList(String token, Integer page, Integer size) {
Pageable pageable = PageRequest.of(page - 1, size);
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
Page<UserPointLog> taskPointPage = userPointLogDao.getUserPointLogList(userLoginDto.getUserId(),
pageable);
long totalElements = taskPointPage.getTotalElements();
return new PageResult<>(totalElements, taskPointPage.getContent());
}
}

@ -12,7 +12,6 @@ create table tb_membership_code
update_user varchar(20) null comment '修改人' update_user varchar(20) null comment '修改人'
) comment '会员码'; ) comment '会员码';
create table tb_user_point_log create table tb_user_point_log
( (
id varchar(20) not null comment 'id' id varchar(20) not null comment 'id'
@ -24,8 +23,12 @@ create table tb_user_point_log
create_time datetime null comment '创建时间', create_time datetime null comment '创建时间',
update_time datetime null comment '修改时间', update_time datetime null comment '修改时间',
create_user varchar(20) null comment '创建人', create_user varchar(20) null comment '创建人',
update_user varchar(20) null comment '修改人' update_user varchar(20) null comment '修改人',
) comment '积分记录表'; task_point_id varchar(20) not null comment 'tb_task_point表id'
)
comment '积分记录表';
alter table tb_user_info alter table tb_user_info
add vip_status tinyint default 0 not null comment '会员状态 0-未开通 1-生效中 2-已过期'; add vip_status tinyint default 0 not null comment '会员状态 0-未开通 1-生效中 2-已过期';

@ -0,0 +1,3 @@
alter table tb_user_info
add point int default 0 not null comment '积分';
Loading…
Cancel
Save