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 88416d4..eda7cc4 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 @@ -9,6 +9,7 @@ import com.luoo.user.service.DrawLotteryService; import com.luoo.user.service.LotteryService; import com.luoo.user.service.TaskPointService; import com.luoo.user.service.UserPointLogService; +import com.luoo.user.vo.point.UserPointLogVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -161,38 +162,38 @@ public class PointController { return Result.success(taskPointService.getTaskPointListForApp(token, type)); } - @ApiOperation(value = "2.1. 用户根据新手任务获取积分", notes = "仅限app用户调用") - @PostMapping("/log/earn/task/new") - @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 addLogNew( - @RequestHeader(value = "Authorization", required = true) String authorization, - @RequestBody String taskPointId) { - userPointLogService.addByTaskNew(taskPointId, authorization); - return Result.success(); - } - - @ApiOperation(value = "2.2. 用户根据日常任务获取积分", notes = "仅限app用户调用") - @PostMapping("/log/earn/task/daily") - @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 addLogDaily( - @RequestHeader(value = "Authorization", required = true) String authorization, - @RequestBody String taskPointId) { - userPointLogService.addByTaskDaily(taskPointId, authorization); - return Result.success(); - } +// @ApiOperation(value = "2.1. 用户根据新手任务获取积分", notes = "仅限app用户调用") +// @PostMapping("/log/earn/task/new") +// @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 addLogNew( +// @RequestHeader(value = "Authorization", required = true) String authorization, +// @RequestBody String taskPointId) { +// userPointLogService.addByTaskNew(taskPointId, authorization); +// return Result.success(); +// } +// +// @ApiOperation(value = "2.2. 用户根据日常任务获取积分", notes = "仅限app用户调用") +// @PostMapping("/log/earn/task/daily") +// @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 addLogDaily( +// @RequestHeader(value = "Authorization", required = true) String authorization, +// @RequestBody String taskPointId) { +// userPointLogService.addByTaskDaily(taskPointId, authorization); +// return Result.success(); +// } @ApiOperation(value = "2.3.用户积分日志列表(APP&PC)", notes = "用户积分日志列表") @PostMapping("/log/list/{page}/{size}") @GlobalInterceptor(checkAppUserLogin = true) - public Result> getUserPointLogList( + public Result> getUserPointLogList( @ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, @ApiParam(value = "查询对象") @RequestBody UserPointLogSearchDto userPointLogSearchDto, @ApiParam(value = "页码", required = true) @PathVariable Integer page, diff --git a/luoo_user/src/main/java/com/luoo/user/dto/point/UserPointLogSearchDto.java b/luoo_user/src/main/java/com/luoo/user/dto/point/UserPointLogSearchDto.java index 5d7e5ad..a2ceaf7 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/point/UserPointLogSearchDto.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/point/UserPointLogSearchDto.java @@ -7,6 +7,8 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import javax.persistence.Transient; + /** * DTO for {@link com.luoo.user.pojo.UserPointLog} */ @@ -17,8 +19,12 @@ import lombok.NoArgsConstructor; public class UserPointLogSearchDto implements Serializable { @ApiModelProperty(value = "类型 1-收入 2-支出 null-全部") - public Integer type; + private Integer type; + + private String userId; - public String userId; + @Transient + @ApiModelProperty(value = "所在月份", example = "202408") + private String createMonth; } \ No newline at end of file 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 62b398a..93e916d 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 @@ -80,4 +80,8 @@ public class UserPointLog implements Serializable { @ApiModelProperty(value = "更新人") private String updateUser; + @Transient + @ApiModelProperty(value = "所在月份") + private String createMonth; + } \ No newline at end of file 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 index 6c33706..783f6ab 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java @@ -5,20 +5,24 @@ import com.luoo.user.dao.TaskPointDao; import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dao.UserPointLogDao; import com.luoo.user.dto.point.UserPointLogSearchDto; -import com.luoo.user.pojo.TaskPoint; -import com.luoo.user.pojo.UserInfo; -import com.luoo.user.pojo.UserPointLog; +import com.luoo.user.pojo.*; +import com.luoo.user.vo.point.UserPointLogVO; +import com.querydsl.core.BooleanBuilder; +import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.Expressions; +import com.querydsl.core.types.dsl.StringExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; import constants.TaskPointIdConstants; import dto.UserLoginDto; import enums.PointEnums; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Objects; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; +import javax.persistence.EntityManager; +import javax.persistence.criteria.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; @@ -26,6 +30,7 @@ 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.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.jpa.domain.Specification; @@ -56,15 +61,19 @@ public class UserPointLogService { private final UserInfoDao userInfoDao; + private final JPAQueryFactory jpaQueryFactory; + + public UserPointLogService(UserPointLogDao userPointLogDao, IdWorker idWorker, JwtUtil jwtUtil, - TaskPointDao taskPointDao, RabbitTemplate rabbitTemplate, UserInfoDao userInfoDao) { + TaskPointDao taskPointDao, RabbitTemplate rabbitTemplate, UserInfoDao userInfoDao, JPAQueryFactory jpaQueryFactory) { this.userPointLogDao = userPointLogDao; this.idWorker = idWorker; this.jwtUtil = jwtUtil; this.taskPointDao = taskPointDao; this.rabbitTemplate = rabbitTemplate; this.userInfoDao = userInfoDao; + this.jpaQueryFactory = jpaQueryFactory; } private TaskPointService taskPointService; @@ -242,39 +251,74 @@ public class UserPointLogService { * @param size 每页数量 * @return 用户积分列表 */ - public PageResult getUserPointLogList(String token, + public PageResult getUserPointLogList(String token, UserPointLogSearchDto userPointLogSearchDto, Integer page, Integer size) { -// - UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); - Sort sort = new Sort(Direction.DESC, "createTime"); - PageRequest pageRequest = PageRequest.of(page - 1, size, sort); + BooleanBuilder booleanBuilder = new BooleanBuilder(); + QUserPointLog qUserPointLog = QUserPointLog.userPointLog; + checkCondition(booleanBuilder, qUserPointLog, userPointLogSearchDto); + StringExpression formattedCreateTime = Expressions.stringTemplate( + "DATE_FORMAT({0}, {1})", + qUserPointLog.createTime, + Expressions.constant("%Y%m") + ); + + // 创建分页对象 + Pageable pageable = PageRequest.of(page - 1, size); + + List userPointLogPage = jpaQueryFactory.select( + Projections.constructor(UserPointLogVO.class, + qUserPointLog.id, + qUserPointLog.score, + qUserPointLog.type, + qUserPointLog.userId, + qUserPointLog.taskPointId, + qUserPointLog.description, + qUserPointLog.createTime, + formattedCreateTime.as("createMonth") + )) + .from(qUserPointLog) + .where(booleanBuilder) + .orderBy(qUserPointLog.createTime.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + long totalElements = jpaQueryFactory.select(qUserPointLog.id.count()).from(qUserPointLog) + .where(booleanBuilder) + .fetchCount(); + + return new PageResult<>(totalElements, userPointLogPage); + } - userPointLogSearchDto.setUserId(userLoginDto.getUserId()); - Specification specification = buildSearchSpecification(userPointLogSearchDto); - Page userPointLogPage = userPointLogDao.findAll(specification, pageRequest); + public void checkCondition(BooleanBuilder booleanBuilder, QUserPointLog qUserPointLog, UserPointLogSearchDto userPointLogSearchDto){ + if(!StringUtils.isEmpty(userPointLogSearchDto.getUserId())) { + booleanBuilder.and(qUserPointLog.createUser.eq(userPointLogSearchDto.getUserId())); + } + if(userPointLogSearchDto.getType() != null) { + booleanBuilder.and(qUserPointLog.type.eq(userPointLogSearchDto.getType())); + } + if(!StringUtils.isEmpty(userPointLogSearchDto.getCreateMonth())) { + // 判断userPointLogSearchDto.getCreateMonth() 是否是当月时间 + LocalDate now = LocalDate.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM"); + String currentMonth = now.format(formatter); - long totalElements = userPointLogPage.getTotalElements(); - return new PageResult<>(totalElements, userPointLogPage.getContent()); - } + // 判断是否是当前月份 + boolean isCurrentMonth = userPointLogSearchDto.getCreateMonth().equals(currentMonth); - private Specification buildSearchSpecification(UserPointLogSearchDto param) { - return (Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) -> { - List predicateList = new ArrayList(); + if(!isCurrentMonth) { + // 不是当前月份,仅展示传入月份的记录 - if (param.getType() != null) { - predicateList.add( - criteriaBuilder.and(criteriaBuilder.equal(root.get("type"), param.getType()))); - } + StringExpression formattedCreateTime = Expressions.stringTemplate( + "DATE_FORMAT({0}, {1})", + qUserPointLog.createTime, + Expressions.constant("%Y%m") + ); - if (StringUtils.isNotBlank(param.getUserId())) { - predicateList.add( - criteriaBuilder.and(criteriaBuilder.equal(root.get("userId"), param.getUserId()))); + booleanBuilder.and(formattedCreateTime.eq(userPointLogSearchDto.getCreateMonth())); } - - return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])); - }; + } } /** diff --git a/luoo_user/src/main/java/com/luoo/user/vo/point/UserPointLogVO.java b/luoo_user/src/main/java/com/luoo/user/vo/point/UserPointLogVO.java new file mode 100644 index 0000000..a43b8c1 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/vo/point/UserPointLogVO.java @@ -0,0 +1,37 @@ +package com.luoo.user.vo.point; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.Value; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Transient; +import javax.validation.constraints.Size; +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * DTO for {@link com.luoo.user.pojo.UserPointLog} + */ +@Value +@AllArgsConstructor +public class UserPointLogVO implements Serializable { + + String id; + Integer score; + Integer type; + String userId; + String taskPointId; + String description; + + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + LocalDateTime createTime; + + @Transient + String createMonth; +} \ No newline at end of file