签到:签到后增加用户积分、经验

This commit is contained in:
owen 2023-10-01 09:53:54 +08:00
parent a04a3a27a9
commit 4f03635fcd
14 changed files with 112 additions and 67 deletions

View File

@ -788,7 +788,7 @@ CREATE TABLE `member_level_record` (
`user_id` bigint NOT NULL DEFAULT 0 COMMENT '用户编号', `user_id` bigint NOT NULL DEFAULT 0 COMMENT '用户编号',
`level_id` bigint NOT NULL DEFAULT 0 COMMENT '等级编号', `level_id` bigint NOT NULL DEFAULT 0 COMMENT '等级编号',
`level` int NOT NULL DEFAULT 0 COMMENT '会员等级', `level` int NOT NULL DEFAULT 0 COMMENT '会员等级',
`discount_percent` tinyint NOT NULL DEFAULT 100 COMMENT '享受折扣', `discount_percent` int NOT NULL DEFAULT 100 COMMENT '享受折扣',
`experience` int NOT NULL DEFAULT 0 COMMENT '升级经验', `experience` int NOT NULL DEFAULT 0 COMMENT '升级经验',
`user_experience` int NOT NULL DEFAULT 0 COMMENT '会员此时的经验', `user_experience` int NOT NULL DEFAULT 0 COMMENT '会员此时的经验',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '备注', `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '备注',
@ -888,7 +888,8 @@ DROP TABLE IF EXISTS `member_sign_in_config`;
CREATE TABLE `member_sign_in_config` ( CREATE TABLE `member_sign_in_config` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '编号', `id` int NOT NULL AUTO_INCREMENT COMMENT '编号',
`day` int NOT NULL COMMENT '第几天', `day` int NOT NULL COMMENT '第几天',
`point` int NOT NULL COMMENT '奖励积分', `point` int NOT NULL DEFAULT 0 COMMENT '奖励积分',
`experience` int NOT NULL DEFAULT 0 COMMENT '奖励经验',
`status` tinyint NOT NULL COMMENT '状态', `status` tinyint NOT NULL COMMENT '状态',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者', `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
@ -903,18 +904,18 @@ CREATE TABLE `member_sign_in_config` (
-- Records of member_sign_in_config -- Records of member_sign_in_config
-- ---------------------------- -- ----------------------------
BEGIN; BEGIN;
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (1, 1, 10, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (1, 1, 10, 10, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (2, 2, 20, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (2, 2, 20, 20, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (3, 7, 1001, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (3, 7, 1001, 1001, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (4, 6, 12121, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (4, 6, 12121, 12121, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (5, 2, 12, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (5, 3, 12, 12, 0, '', '2023-08-20 01:38:56', '', '2023-08-20 01:38:56', b'0', 0);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (6, 1, 10, 0, '1', '2023-08-20 19:20:42', '1', '2023-08-20 19:20:56', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (6, 1, 10, 10, 0, '1', '2023-08-20 19:20:42', '1', '2023-08-20 19:20:56', b'0', 1);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (7, 7, 22, 0, '1', '2023-08-20 19:20:48', '1', '2023-08-20 19:20:48', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (7, 7, 22, 22, 0, '1', '2023-08-20 19:20:48', '1', '2023-08-20 19:20:48', b'0', 1);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (8, 2, 3, 0, '1', '2023-08-21 20:22:44', '1', '2023-08-21 20:22:44', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (8, 2, 3, 3, 0, '1', '2023-08-21 20:22:44', '1', '2023-08-21 20:22:44', b'0', 1);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (9, 3, 4, 0, '1', '2023-08-21 20:22:48', '1', '2023-08-21 20:22:48', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (9, 3, 4, 4, 0, '1', '2023-08-21 20:22:48', '1', '2023-08-21 20:22:48', b'0', 1);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (10, 4, 5, 0, '1', '2023-08-21 20:22:51', '1', '2023-08-21 20:22:51', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (10, 4, 5, 5, 0, '1', '2023-08-21 20:22:51', '1', '2023-08-21 20:22:51', b'0', 1);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (11, 5, 6, 0, '1', '2023-08-21 20:22:56', '1', '2023-08-21 20:22:56', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (11, 5, 6, 6, 0, '1', '2023-08-21 20:22:56', '1', '2023-08-21 20:22:56', b'0', 1);
INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (12, 6, 7, 0, '1', '2023-08-21 20:22:59', '1', '2023-08-21 20:22:59', b'0', 1); INSERT INTO `member_sign_in_config` (`id`, `day`, `point`, `experience`, `status`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `tenant_id`) VALUES (12, 6, 7, 7, 0, '1', '2023-08-21 20:22:59', '1', '2023-08-21 20:22:59', b'0', 1);
COMMIT; COMMIT;
-- ---------------------------- -- ----------------------------
@ -925,7 +926,8 @@ CREATE TABLE `member_sign_in_record` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '签到自增id', `id` bigint NOT NULL AUTO_INCREMENT COMMENT '签到自增id',
`user_id` int NULL DEFAULT NULL COMMENT '签到用户', `user_id` int NULL DEFAULT NULL COMMENT '签到用户',
`day` int NULL DEFAULT NULL COMMENT '第几天签到', `day` int NULL DEFAULT NULL COMMENT '第几天签到',
`point` int NULL DEFAULT NULL COMMENT '签到的分数', `point` int NOT NULL DEFAULT 0 COMMENT '签到的积分',
`experience` int NOT NULL DEFAULT 0 COMMENT '签到的经验',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者', `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者', `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
@ -935,6 +937,8 @@ CREATE TABLE `member_sign_in_record` (
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '签到记录'; ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '签到记录';
CREATE INDEX user_id_index ON member_sign_in_record (user_id);
-- ---------------------------- -- ----------------------------
-- Records of member_sign_in_record -- Records of member_sign_in_record
-- ---------------------------- -- ----------------------------

View File

@ -38,6 +38,7 @@ public interface ErrorCodeConstants {
//========== 签到配置 1-004-009-000 ========== //========== 签到配置 1-004-009-000 ==========
ErrorCode SIGN_IN_CONFIG_NOT_EXISTS = new ErrorCode(1_004_009_000, "签到天数规则不存在"); ErrorCode SIGN_IN_CONFIG_NOT_EXISTS = new ErrorCode(1_004_009_000, "签到天数规则不存在");
ErrorCode SIGN_IN_CONFIG_EXISTS = new ErrorCode(1_004_009_001, "签到天数规则已存在"); ErrorCode SIGN_IN_CONFIG_EXISTS = new ErrorCode(1_004_009_001, "签到天数规则已存在");
ErrorCode SIGN_IN_CONFIG_AWARD_EMPTY = new ErrorCode(1_004_009_002, "签到奖励积分和经验不能同时为空");
//========== 签到配置 1-004-010-000 ========== //========== 签到配置 1-004-010-000 ==========
ErrorCode SIGN_IN_RECORD_TODAY_EXISTS = new ErrorCode(1_004_010_000,"今日已签到,请勿重复签到"); ErrorCode SIGN_IN_RECORD_TODAY_EXISTS = new ErrorCode(1_004_010_000,"今日已签到,请勿重复签到");

View File

@ -6,6 +6,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
/** /**
* 签到规则 Base VO提供给添加修改详细的子 VO 使用 * 签到规则 Base VO提供给添加修改详细的子 VO 使用
@ -20,8 +21,14 @@ public class MemberSignInConfigBaseVO {
@Schema(description = "奖励积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") @Schema(description = "奖励积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
@NotNull(message = "奖励积分不能为空") @NotNull(message = "奖励积分不能为空")
@PositiveOrZero(message = "奖励积分不能小于 0")
private Integer point; private Integer point;
@Schema(description = "奖励经验", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
@NotNull(message = "奖励经验不能为空")
@PositiveOrZero(message = "奖励经验不能小于 0")
private Integer experience;
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "状态不能为空") @NotNull(message = "状态不能为空")
@InEnum(CommonStatusEnum.class) @InEnum(CommonStatusEnum.class)

View File

@ -21,7 +21,7 @@ public class MemberSignInRecordRespVO {
@Schema(description = "第几天签到", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "第几天签到", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer day; private Integer day;
@Schema(description = "签到的", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") @Schema(description = "签到的", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer point; private Integer point;
@Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED)

View File

@ -1,13 +1,10 @@
package cn.iocoder.yudao.module.member.controller.app.signin; package cn.iocoder.yudao.module.member.controller.app.signin;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.member.convert.signin.MemberSignInRecordConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO;
import cn.iocoder.yudao.module.member.service.signin.MemberSignInRecordService; import cn.iocoder.yudao.module.member.service.signin.MemberSignInRecordService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -29,17 +26,8 @@ public class AppMemberSignInController {
// TODO @xiaqing合并到 AppMemberSignInRecordController getSignInRecordSummary 里哈 // TODO @xiaqing合并到 AppMemberSignInRecordController getSignInRecordSummary 里哈
@Operation(summary = "个人签到信息") @Operation(summary = "个人签到信息")
@GetMapping("/get-summary") @GetMapping("/get-summary")
public CommonResult getUserSummary(){ public CommonResult getUserSummary() {
return success(signInRecordService.getSignInRecordSummary(getLoginUserId())); return success(signInRecordService.getSignInRecordSummary(getLoginUserId()));
} }
// TODO @xiaqing泛型
// TODO @xiaqing合并到 AppMemberSignInRecordController createSignInRecord 里哈
@Operation(summary = "会员签到")
@PostMapping("/create")
public CommonResult create(){
MemberSignInRecordDO recordDO = signInRecordService.createSignRecord(getLoginUserId());
return success(MemberSignInRecordConvert.INSTANCE.coverRecordToAppRecordVo(recordDO));
}
} }

View File

@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -50,16 +49,12 @@ public class AppMemberSignInRecordController {
return success(respVO); return success(respVO);
} }
// TODO 芋艿临时 mock => UserSignController.info
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "签到") @Operation(summary = "签到")
@PreAuthenticated @PreAuthenticated
public CommonResult<AppMemberSignInRecordRespVO> createSignInRecord() { public CommonResult<AppMemberSignInRecordRespVO> createSignInRecord() {
AppMemberSignInRecordRespVO respVO = new AppMemberSignInRecordRespVO() MemberSignInRecordDO recordDO = signInRecordService.createSignRecord(getLoginUserId());
.setPoint(10) return success(MemberSignInRecordConvert.INSTANCE.coverRecordToAppRecordVo(recordDO));
.setDay(10)
.setCreateTime(LocalDateTime.now());
return success(respVO);
} }
@GetMapping("/page") @GetMapping("/page")

View File

@ -12,9 +12,12 @@ public class AppMemberSignInRecordRespVO {
@Schema(description = "第几天签到", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "第几天签到", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer day; private Integer day;
@Schema(description = "签到的", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") @Schema(description = "签到的", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer point; private Integer point;
@Schema(description = "签到的经验", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer experience;
@Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime; private LocalDateTime createTime;

View File

@ -15,6 +15,9 @@ public class AppMemberSignInRecordRespVO {
@Schema(description = "签到的分数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") @Schema(description = "签到的分数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer point; private Integer point;
@Schema(description = "签到的经验", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer experience;
@Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime; private LocalDateTime createTime;

View File

@ -35,6 +35,10 @@ public class MemberSignInConfigDO extends BaseDO {
* 奖励积分 * 奖励积分
*/ */
private Integer point; private Integer point;
/**
* 奖励经验
*/
private Integer experience;
/** /**
* 状态 * 状态

View File

@ -35,10 +35,12 @@ public class MemberSignInRecordDO extends BaseDO {
*/ */
private Integer day; private Integer day;
/** /**
* 签到的 * 签到的
*/ */
private Integer point; private Integer point;
/**
// TODO 疯狂签到的经验 * 签到的经验
*/
private Integer experience;
} }

View File

@ -239,7 +239,8 @@ public class MemberLevelServiceImpl implements MemberLevelService {
// 1. 创建经验记录 // 1. 创建经验记录
MemberUserDO user = memberUserService.getUser(userId); MemberUserDO user = memberUserService.getUser(userId);
int userExperience = NumberUtil.max(user.getExperience() + experience, 0); // 防止扣出负数 Integer userExperience = ObjUtil.defaultIfNull(user.getExperience(), 0);
userExperience = NumberUtil.max(userExperience + experience, 0); // 防止扣出负数
MemberLevelRecordDO levelRecord = new MemberLevelRecordDO() MemberLevelRecordDO levelRecord = new MemberLevelRecordDO()
.setUserId(user.getId()) .setUserId(user.getId())
.setExperience(experience) .setExperience(experience)

View File

@ -66,6 +66,9 @@ public class MemberPointRecordServiceImpl implements MemberPointRecordService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId) { public void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId) {
if (point == 0) {
return;
}
// 1. 校验用户积分余额 // 1. 校验用户积分余额
MemberUserDO user = memberUserService.getUser(userId); MemberUserDO user = memberUserService.getUser(userId);
Integer userPoint = ObjectUtil.defaultIfNull(user.getPoint(), 0); Integer userPoint = ObjectUtil.defaultIfNull(user.getPoint(), 0);

View File

@ -11,10 +11,10 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SIGN_IN_CONFIG_EXISTS; import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SIGN_IN_CONFIG_NOT_EXISTS;
/** /**
* 签到规则 Service 实现类 * 签到规则 Service 实现类
@ -30,6 +30,8 @@ public class MemberSignInConfigServiceImpl implements MemberSignInConfigService
@Override @Override
public Long createSignInConfig(MemberSignInConfigCreateReqVO createReqVO) { public Long createSignInConfig(MemberSignInConfigCreateReqVO createReqVO) {
// 校验奖励积分奖励经验
validatePointAndExperience(createReqVO.getPoint(), createReqVO.getExperience());
// 判断是否重复插入签到天数 // 判断是否重复插入签到天数
validateSignInConfigDayDuplicate(createReqVO.getDay(), null); validateSignInConfigDayDuplicate(createReqVO.getDay(), null);
@ -42,6 +44,8 @@ public class MemberSignInConfigServiceImpl implements MemberSignInConfigService
@Override @Override
public void updateSignInConfig(MemberSignInConfigUpdateReqVO updateReqVO) { public void updateSignInConfig(MemberSignInConfigUpdateReqVO updateReqVO) {
// 校验奖励积分奖励经验
validatePointAndExperience(updateReqVO.getPoint(), updateReqVO.getExperience());
// 校验存在 // 校验存在
validateSignInConfigExists(updateReqVO.getId()); validateSignInConfigExists(updateReqVO.getId());
// 判断是否重复插入签到天数 // 判断是否重复插入签到天数
@ -70,7 +74,7 @@ public class MemberSignInConfigServiceImpl implements MemberSignInConfigService
* 校验 day 是否重复 * 校验 day 是否重复
* *
* @param day * @param day
* @param id 编号只有更新的时候会传递 * @param id 编号只有更新的时候会传递
*/ */
private void validateSignInConfigDayDuplicate(Integer day, Long id) { private void validateSignInConfigDayDuplicate(Integer day, Long id) {
MemberSignInConfigDO config = memberSignInConfigMapper.selectByDay(day); MemberSignInConfigDO config = memberSignInConfigMapper.selectByDay(day);
@ -84,13 +88,20 @@ public class MemberSignInConfigServiceImpl implements MemberSignInConfigService
} }
} }
private void validatePointAndExperience(Integer point, Integer experience) {
// 奖励积分经验 至少要配置一个否则没有意义
if (Objects.equals(point, 0) && Objects.equals(experience, 0)) {
throw exception(SIGN_IN_CONFIG_AWARD_EMPTY);
}
}
@Override @Override
public MemberSignInConfigDO getSignInConfig(Long id) { public MemberSignInConfigDO getSignInConfig(Long id) {
return memberSignInConfigMapper.selectById(id); return memberSignInConfigMapper.selectById(id);
} }
@Override @Override
public List <MemberSignInConfigDO> getSignInConfigList() { public List<MemberSignInConfigDO> getSignInConfigList() {
List<MemberSignInConfigDO> list = memberSignInConfigMapper.selectList(); List<MemberSignInConfigDO> list = memberSignInConfigMapper.selectList();
list.sort(Comparator.comparing(MemberSignInConfigDO::getDay)); list.sort(Comparator.comparing(MemberSignInConfigDO::getDay));
return list; return list;

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.member.service.signin;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi; import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
@ -13,8 +14,13 @@ import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO
import cn.iocoder.yudao.module.member.dal.mysql.signin.MemberSignInConfigMapper; import cn.iocoder.yudao.module.member.dal.mysql.signin.MemberSignInConfigMapper;
import cn.iocoder.yudao.module.member.dal.mysql.signin.MemberSignInRecordMapper; import cn.iocoder.yudao.module.member.dal.mysql.signin.MemberSignInRecordMapper;
import cn.iocoder.yudao.module.member.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.member.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -40,20 +46,24 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
private MemberSignInRecordMapper signInRecordMapper; private MemberSignInRecordMapper signInRecordMapper;
@Resource @Resource
private MemberSignInConfigMapper signInConfigMapper; private MemberSignInConfigMapper signInConfigMapper;
@Resource
private MemberPointRecordService pointRecordService;
@Resource
private MemberLevelService memberLevelService;
@Resource @Resource
private MemberUserApi memberUserApi; private MemberUserApi memberUserApi;
@Override @Override
public AppMemberSignInSummaryRespVO getSignInRecordSummary(Long userId) { public AppMemberSignInSummaryRespVO getSignInRecordSummary(Long userId) {
AppMemberSignInSummaryRespVO vo = new AppMemberSignInSummaryRespVO(); AppMemberSignInSummaryRespVO vo = new AppMemberSignInSummaryRespVO();
vo.setTotalDay(0); vo.setTotalDay(0);
vo.setContinuousDay(0); vo.setContinuousDay(0);
vo.setTodaySignIn(false); vo.setTodaySignIn(false);
//获取用户签到的记录按照天数倒序获取 //获取用户签到的记录按照天数倒序获取
List <MemberSignInRecordDO> signInRecordDOList = signInRecordMapper.selectListByUserId(userId); List<MemberSignInRecordDO> signInRecordDOList = signInRecordMapper.selectListByUserId(userId);
// TODO @xiaqingif 空的时候直接 return这样括号少逻辑更简洁 // TODO @xiaqingif 空的时候直接 return这样括号少逻辑更简洁
if(!CollectionUtils.isEmpty(signInRecordDOList)){ if (!CollectionUtils.isEmpty(signInRecordDOList)) {
//设置总签到天数 //设置总签到天数
vo.setTotalDay(signInRecordDOList.size()); // TODO @xiaqing是不是不用读取 signInRecordDOList 所有的而是 count下然后另外再读取一条最后一条 vo.setTotalDay(signInRecordDOList.size()); // TODO @xiaqing是不是不用读取 signInRecordDOList 所有的而是 count下然后另外再读取一条最后一条
//判断当天是否有签到复用校验方法 //判断当天是否有签到复用校验方法
@ -61,11 +71,11 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
try { try {
validSignDay(signInRecordDOList.get(0)); validSignDay(signInRecordDOList.get(0));
vo.setTodaySignIn(false); vo.setTodaySignIn(false);
}catch (Exception e){ } catch (Exception e) {
vo.setTodaySignIn(true); vo.setTodaySignIn(true);
} }
//如果当天签到了则说明连续签到天数有意义否则直接用默认值0 //如果当天签到了则说明连续签到天数有意义否则直接用默认值0
if(vo.getTodaySignIn()){ if (vo.getTodaySignIn()) {
//下方计算连续签到从2天开始此处直接设置一天连续签到 //下方计算连续签到从2天开始此处直接设置一天连续签到
vo.setContinuousDay(1); vo.setContinuousDay(1);
//判断连续签到天数 //判断连续签到天数
@ -73,10 +83,10 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
for (int i = 1; i < signInRecordDOList.size(); i++) { for (int i = 1; i < signInRecordDOList.size(); i++) {
//前一天减1等于当前天数则说明连续继续循环 //前一天减1等于当前天数则说明连续继续循环
LocalDate cur = signInRecordDOList.get(i).getCreateTime().toLocalDate(); LocalDate cur = signInRecordDOList.get(i).getCreateTime().toLocalDate();
LocalDate pre = signInRecordDOList.get(i-1).getCreateTime().toLocalDate(); LocalDate pre = signInRecordDOList.get(i - 1).getCreateTime().toLocalDate();
if(1==daysBetween(cur,pre)){ if (1 == daysBetween(cur, pre)) {
vo.setContinuousDay(i+1); vo.setContinuousDay(i + 1);
}else{ } else {
break; break;
} }
} }
@ -87,16 +97,16 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
return vo; return vo;
} }
private long daysBetween(LocalDate date1,LocalDate date2){ private long daysBetween(LocalDate date1, LocalDate date2) {
return ChronoUnit.DAYS.between(date1, date2); return ChronoUnit.DAYS.between(date1, date2);
} }
@Override @Override
public PageResult <MemberSignInRecordDO> getSignInRecordPage(MemberSignInRecordPageReqVO pageReqVO) { public PageResult<MemberSignInRecordDO> getSignInRecordPage(MemberSignInRecordPageReqVO pageReqVO) {
// 根据用户昵称查询出用户ids // 根据用户昵称查询出用户ids
Set <Long> userIds = null; Set<Long> userIds = null;
if (StringUtils.isNotBlank(pageReqVO.getNickname())) { if (StringUtils.isNotBlank(pageReqVO.getNickname())) {
List <MemberUserRespDTO> users = memberUserApi.getUserListByNickname(pageReqVO.getNickname()); List<MemberUserRespDTO> users = memberUserApi.getUserListByNickname(pageReqVO.getNickname());
// 如果查询用户结果为空直接返回无需继续查询 // 如果查询用户结果为空直接返回无需继续查询
if (CollectionUtils.isEmpty(users)) { if (CollectionUtils.isEmpty(users)) {
return PageResult.empty(); return PageResult.empty();
@ -113,50 +123,63 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public MemberSignInRecordDO createSignRecord(Long userId) { public MemberSignInRecordDO createSignRecord(Long userId) {
// 获取当前用户签到的最大天数 // 获取当前用户签到的最大天数
// TODO @xiaqingdb 操作dou封装到 mapper // TODO @xiaqingdb 操作dou封装到 mapper
// TODO @xiaqingmaxSignDay是不是变量叫 lastRecord 会更容易理解哈 // TODO @xiaqingmaxSignDay是不是变量叫 lastRecord 会更容易理解哈
MemberSignInRecordDO maxSignDay = signInRecordMapper.selectOne(new LambdaQueryWrapperX <MemberSignInRecordDO>() MemberSignInRecordDO maxSignDay = signInRecordMapper.selectOne(new LambdaQueryWrapperX<MemberSignInRecordDO>()
.eq(MemberSignInRecordDO::getUserId, userId) .eq(MemberSignInRecordDO::getUserId, userId)
.orderByDesc(MemberSignInRecordDO::getDay) .orderByDesc(MemberSignInRecordDO::getDay)
.last("limit 1")); .last("limit 1"));
// 判断是否重复签到 // 判断是否重复签到
validSignDay(maxSignDay); validSignDay(maxSignDay);
// TODO @xiaqing可以使用 // 进行注释 // 1. 查询出当前签到的天数
/**1.查询出当前签到的天数**/
MemberSignInRecordDO sign = new MemberSignInRecordDO().setUserId(userId); // TODO @xiaqing应该使用 record 变量会更合适 MemberSignInRecordDO sign = new MemberSignInRecordDO().setUserId(userId); // TODO @xiaqing应该使用 record 变量会更合适
sign.setDay(1); // 设置签到初始化天数 sign.setDay(1); // 设置签到初始化天数
sign.setPoint(0); // 设置签到分数默认为 0 sign.setPoint(0); // 设置签到积分默认为 0
sign.setExperience(0); // 设置签到经验默认为 0
// 如果不为空则修改当前签到对应的天数 // 如果不为空则修改当前签到对应的天数
// TODO @xiaqing应该是要判断连续哈就是昨天 // TODO @xiaqing应该是要判断连续哈就是昨天
if (maxSignDay != null) { if (maxSignDay != null) {
sign.setDay(maxSignDay.getDay() + 1); sign.setDay(maxSignDay.getDay() + 1);
} }
/**2.获取签到对应的分数**/ // 2. 获取签到对应的积分数
// 获取所有的签到规则按照天数排序只获取启用的 TODO @xiaqing不要使用 signInConfigMapper 直接查询而是要通过 SigninConfigService // 获取所有的签到规则按照天数排序只获取启用的 TODO @xiaqing不要使用 signInConfigMapper 直接查询而是要通过 SigninConfigService
List <MemberSignInConfigDO> configDOList = signInConfigMapper.selectList(new LambdaQueryWrapperX <MemberSignInConfigDO>() List<MemberSignInConfigDO> configDOList = signInConfigMapper.selectList(new LambdaQueryWrapperX<MemberSignInConfigDO>()
.eq(MemberSignInConfigDO::getStatus, CommonStatusEnum.ENABLE.getStatus()) .eq(MemberSignInConfigDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
.orderByAsc(MemberSignInConfigDO::getDay)); .orderByAsc(MemberSignInConfigDO::getDay));
// 如果签到的天数大于最大启用的规则天数直接给最大签到的分数 // 如果签到的天数大于最大启用的规则天数直接给最大签到的分数
// TODO @xiaqing超过最大配置的天数应该直接重置到第一天哈 // TODO @xiaqing超过最大配置的天数应该直接重置到第一天哈
MemberSignInConfigDO lastConfig = configDOList.get(configDOList.size() - 1); MemberSignInConfigDO lastConfig = configDOList.get(configDOList.size() - 1);
if (sign.getDay() > lastConfig.getDay()) { if (sign.getDay() > lastConfig.getDay()) {
sign.setPoint(lastConfig.getPoint()); sign.setPoint(lastConfig.getPoint());
sign.setExperience(lastConfig.getExperience());
} else { } else {
configDOList.forEach(el -> { configDOList.forEach(el -> {
// 循环匹配对应天数设置对应分数 // 循环匹配对应天数设置对应分数
// TODO @xiaqing使用 equals另外这种不应该去遍历比较从可读性来说应该 CollUtil.findOne() // TODO @xiaqing使用 equals另外这种不应该去遍历比较从可读性来说应该 CollUtil.findOne()
if (el.getDay() == sign.getDay()) { if (el.getDay() == sign.getDay()) {
sign.setPoint(el.getPoint()); sign.setPoint(el.getPoint());
sign.setExperience(el.getExperience());
} }
}); });
} }
// 3. 插入当前签到获取的分数 // 3. 插入签到记录
signInRecordMapper.insert(sign); signInRecordMapper.insert(sign);
// 4. 增加积分
if (!ObjectUtils.equalsAny(sign.getPoint(), null, 0)) {
pointRecordService.createPointRecord(userId, sign.getPoint(), MemberPointBizTypeEnum.SIGN, String.valueOf(sign.getId()));
}
// 5. 增加经验
if (!ObjectUtils.equalsAny(sign.getPoint(), null, 0)) {
memberLevelService.addExperience(userId, sign.getExperience(), MemberExperienceBizTypeEnum.SIGN_IN, String.valueOf(sign.getId()));
}
return sign; return sign;
} }