From 8ffbc15cec03226981f924a390992d331bd49768 Mon Sep 17 00:00:00 2001 From: owen Date: Mon, 21 Aug 2023 21:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E5=91=98=EF=BC=9A=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BC=9A=E5=91=98=E7=AD=89=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/mysql/member_level.sql | 67 +++------- .../member/api/level/MemberLevelApi.java | 21 +++ .../member/enums/ErrorCodeConstants.java | 1 + .../enums/MemberExperienceBizTypeEnum.java | 14 +- .../member/api/level/MemberLevelApiImpl.java | 25 ++++ .../admin/level/MemberLevelController.java | 5 +- .../admin/user/MemberUserController.java | 10 +- .../admin/user/vo/MemberUserBaseVO.java | 3 + .../admin/user/vo/MemberUserPageReqVO.java | 5 +- .../admin/user/vo/MemberUserRespVO.java | 3 + .../admin/user/vo/MemberUserUpdateReqVO.java | 3 + .../convert/user/MemberUserConvert.java | 12 +- .../dal/mysql/level/MemberLevelMapper.java | 4 +- .../dal/mysql/user/MemberUserMapper.java | 13 ++ .../level/MemberExperienceLogService.java | 20 +++ .../level/MemberExperienceLogServiceImpl.java | 22 ++++ .../service/level/MemberLevelLogService.java | 27 ++++ .../level/MemberLevelLogServiceImpl.java | 51 ++++++++ .../service/level/MemberLevelService.java | 33 +++++ .../service/level/MemberLevelServiceImpl.java | 123 ++++++++++++++++++ .../service/user/MemberUserServiceImpl.java | 12 +- 21 files changed, 413 insertions(+), 61 deletions(-) create mode 100644 yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApi.java create mode 100644 yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApiImpl.java diff --git a/sql/mysql/member_level.sql b/sql/mysql/member_level.sql index 1687b842f3..cdb8618e50 100644 --- a/sql/mysql/member_level.sql +++ b/sql/mysql/member_level.sql @@ -9,7 +9,7 @@ create table member_level name varchar(30) default '' not null comment '等级名称', experience int default 0 not null comment '升级经验', level int default 0 not null comment '等级', - discount int(4) default 100 not null comment '享受折扣', + discount tinyint default 100 not null comment '享受折扣', icon varchar(255) default '' not null comment '等级图标', background_url varchar(255) default '' not null comment '等级背景图', status tinyint default 0 not null comment '状态', @@ -28,9 +28,9 @@ create table member_level_log user_id bigint default 0 not null comment '用户编号', level_id bigint default 0 not null comment '等级编号', level int default 0 not null comment '会员等级', - discount int(4) default 100 not null comment '享受折扣', - experience int(4) default 0 not null comment '升级经验', - user_experience int(4) default 0 not null comment '会员此时的经验', + discount tinyint default 100 not null comment '享受折扣', + experience int default 0 not null comment '升级经验', + user_experience int default 0 not null comment '会员此时的经验', remark varchar(255) default '' not null comment '备注', description varchar(255) default '' not null comment '描述', creator varchar(64) default '' null comment '创建者', @@ -68,54 +68,27 @@ create index idx_user_biz_type on member_experience_log (user_id, biz_type) comm -- 增加字典 insert system_dict_type(name, type) values ('会员经验业务类型', 'member_experience_biz_type'); -insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '系统', '0', 0); -insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '订单', '1', 1); -insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '签到', '2', 2); +insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '管理员调整', '0', 0); +insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '邀新奖励', '1', 1); +insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '下单奖励', '2', 2); +insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '退单扣除', '3', 3); +insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '签到奖励', '4', 4); +insert system_dict_data(dict_type, label, value, sort) values ('member_experience_biz_type', '抽奖奖励', '5', 5); -- 菜单 SQL -INSERT INTO system_menu( - name, permission, type, sort, parent_id, - path, icon, component, status, component_name -) -VALUES ( - '会员等级', '', 2, 3, 2262, - 'level', '', 'member/level/index', 0, 'MemberLevel' -); +INSERT INTO system_menu(name, permission, type, sort, parent_id, path, icon, component, status, component_name) +VALUES ('会员等级', '', 2, 3, 2262, 'level', '', 'member/level/index', 0, 'MemberLevel'); -- 按钮父菜单ID -- 暂时只支持 MySQL。如果你是 Oracle、PostgreSQL、SQLServer 的话,需要手动修改 @parentId 的部分的代码 SELECT @parentId := LAST_INSERT_ID(); -- 按钮 SQL -INSERT INTO system_menu( - name, permission, type, sort, parent_id, - path, icon, component, status -) -VALUES ( - '会员等级查询', 'member:level:query', 3, 1, @parentId, - '', '', '', 0 -); -INSERT INTO system_menu( - name, permission, type, sort, parent_id, - path, icon, component, status -) -VALUES ( - '会员等级创建', 'member:level:create', 3, 2, @parentId, - '', '', '', 0 -); -INSERT INTO system_menu( - name, permission, type, sort, parent_id, - path, icon, component, status -) -VALUES ( - '会员等级更新', 'member:level:update', 3, 3, @parentId, - '', '', '', 0 -); -INSERT INTO system_menu( - name, permission, type, sort, parent_id, - path, icon, component, status -) -VALUES ( - '会员等级删除', 'member:level:delete', 3, 4, @parentId, - '', '', '', 0 -); +INSERT INTO system_menu(name, permission, type, sort, parent_id, path, icon, component, status) +VALUES ('会员等级查询', 'member:level:query', 3, 1, @parentId, '', '', '', 0); +INSERT INTO system_menu(name, permission, type, sort, parent_id, path, icon, component, status) +VALUES ('会员等级创建', 'member:level:create', 3, 2, @parentId, '', '', '', 0); +INSERT INTO system_menu(name, permission, type, sort, parent_id, path, icon, component, status) +VALUES ('会员等级更新', 'member:level:update', 3, 3, @parentId, '', '', '', 0); +INSERT INTO system_menu(name, permission, type, sort, parent_id, path, icon, component, status) +VALUES ('会员等级删除', 'member:level:delete', 3, 4, @parentId, '', '', '', 0); diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApi.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApi.java new file mode 100644 index 0000000000..28ebef94e3 --- /dev/null +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApi.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.member.api.level; + +import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; + +/** + * 会员等级 API接口 + * + * @author owen + */ +public interface MemberLevelApi { + + /** + * 增加会员经验 + * + * @param userId 会员ID + * @param experience 经验 + * @param bizType 业务类型 + * @param bizId 业务编号 + */ + void plusExperience(Long userId, Integer experience, MemberExperienceBizTypeEnum bizType, String bizId); +} diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java index c2b20cea75..bb8a178048 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java @@ -51,4 +51,5 @@ public interface ErrorCodeConstants { ErrorCode LEVEL_LOG_NOT_EXISTS = new ErrorCode(1004007100, "会员等级记录不存在"); ErrorCode EXPERIENCE_LOG_NOT_EXISTS = new ErrorCode(1004007200, "会员经验记录不存在"); + ErrorCode LEVEL_REASON_NOT_EXISTS = new ErrorCode(1004007300, "会员等级调整原因不能为空"); } diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java index e4e0ffdc8b..93cb886fb9 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java @@ -12,13 +12,17 @@ import lombok.Getter; @AllArgsConstructor public enum MemberExperienceBizTypeEnum { /** - * + * 管理员调整、邀请新用户、下单、退单、签到、抽奖 */ - SYSTEM(0, "系统"), - ORDER(1, "订单"), - SIGN_IN(2, "签到"), + ADMIN(0, "管理员调整","管理员调整获得{}经验"), + INVITE_REGISTER(1, "邀新奖励","邀请好友获得{}经验"), + ORDER(2, "下单奖励", "下单获得{}经验"), + REFUND(3, "退单扣除","退单获得{}经验"), + SIGN_IN(4, "签到奖励","签到获得{}经验"), + LOTTERY(5, "抽奖奖励","抽奖获得{}经验"), ; private final int value; - private final String name; + private final String title; + private final String desc; } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApiImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApiImpl.java new file mode 100644 index 0000000000..615539cef3 --- /dev/null +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/level/MemberLevelApiImpl.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.module.member.api.level; + +import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; +import cn.iocoder.yudao.module.member.service.level.MemberLevelService; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +import javax.annotation.Resource; + +/** + * 会员等级 API 实现类 + * + * @author owen + */ +@Service +@Validated +public class MemberLevelApiImpl implements MemberLevelApi { + + @Resource + private MemberLevelService memberLevelService; + + public void plusExperience(Long userId, Integer experience, MemberExperienceBizTypeEnum bizType, String bizId) { + memberLevelService.plusExperience(userId, experience, bizType, bizId); + } +} diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/level/MemberLevelController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/level/MemberLevelController.java index 283680dabc..753e384214 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/level/MemberLevelController.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/level/MemberLevelController.java @@ -1,6 +1,5 @@ package cn.iocoder.yudao.module.member.controller.admin.level; -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.*; @@ -77,9 +76,9 @@ public class MemberLevelController { @GetMapping("/list-all-simple") @Operation(summary = "获取会员等级精简信息列表", description = "只包含被开启的会员等级,主要用于前端的下拉选项") - public CommonResult> getSimpleUserList() { + public CommonResult> getSimpleLevelList() { // 获用户列表,只要开启状态的 - List list = levelService.getLevelListByStatus(CommonStatusEnum.ENABLE.getStatus()); + List list = levelService.getEnableLevelList(); // 排序后,返回给前端 return success(MemberLevelConvert.INSTANCE.convertSimpleList(list)); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/MemberUserController.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/MemberUserController.java index 56b563dd2f..04e991700c 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/MemberUserController.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/MemberUserController.java @@ -7,8 +7,10 @@ import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReq import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserRespVO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO; import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert; +import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO; import cn.iocoder.yudao.module.member.dal.dataobject.tag.MemberTagDO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; +import cn.iocoder.yudao.module.member.service.level.MemberLevelService; import cn.iocoder.yudao.module.member.service.tag.MemberTagService; import cn.iocoder.yudao.module.member.service.user.MemberUserService; import io.swagger.v3.oas.annotations.Operation; @@ -38,6 +40,8 @@ public class MemberUserController { private MemberUserService memberUserService; @Resource private MemberTagService memberTagService; + @Resource + private MemberLevelService memberLevelService; @PutMapping("/update") @Operation(summary = "更新会员用户") @@ -72,7 +76,11 @@ public class MemberUserController { .flatMap(Collection::stream) .collect(Collectors.toSet()); List tags = memberTagService.getTagList(tagIds); - return success(MemberUserConvert.INSTANCE.convertPage(pageResult, tags)); + + // 处理会员级别返显 + List levels = memberLevelService.getEnableLevelList(); + + return success(MemberUserConvert.INSTANCE.convertPage(pageResult, tags, levels)); } } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserBaseVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserBaseVO.java index f411c5a160..6e67e40b33 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserBaseVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserBaseVO.java @@ -53,4 +53,7 @@ public class MemberUserBaseVO { @Schema(description = "会员标签", example = "[1, 2]") private List tagIds; + @Schema(description = "会员等级编号", example = "1") + private Long levelId; + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserPageReqVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserPageReqVO.java index c4c9ae3dcc..4dc823afa2 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserPageReqVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserPageReqVO.java @@ -32,7 +32,10 @@ public class MemberUserPageReqVO extends PageParam { @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) private LocalDateTime[] createTime; - @Schema(description = "会员标签", example = "[1, 2]") + @Schema(description = "会员标签编号列表", example = "[1, 2]") private List tagIds; + @Schema(description = "会员等级标号", example = "1") + private Long levelId; + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserRespVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserRespVO.java index 7b47cd5fb6..17d8a1c293 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserRespVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserRespVO.java @@ -35,4 +35,7 @@ public class MemberUserRespVO extends MemberUserBaseVO { @Schema(description = "会员标签", example = "[红色, 快乐]") private List tagNames; + @Schema(description = "会员等级", example = "黄金会员") + private String levelName; + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserUpdateReqVO.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserUpdateReqVO.java index c6a92758dc..f4920aa1b0 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserUpdateReqVO.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/admin/user/vo/MemberUserUpdateReqVO.java @@ -17,4 +17,7 @@ public class MemberUserUpdateReqVO extends MemberUserBaseVO { @NotNull(message = "编号不能为空") private Long id; + @Schema(description = "会员级别修改原因", requiredMode = Schema.RequiredMode.REQUIRED, example = "推广需要") + private String levelReason; + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/user/MemberUserConvert.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/user/MemberUserConvert.java index 63e28b2f1e..1acc1ecce5 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/user/MemberUserConvert.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/user/MemberUserConvert.java @@ -1,10 +1,13 @@ package cn.iocoder.yudao.module.member.convert.user; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserRespVO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserInfoRespVO; +import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO; import cn.iocoder.yudao.module.member.dal.dataobject.tag.MemberTagDO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import org.mapstruct.Mapper; @@ -34,11 +37,18 @@ public interface MemberUserConvert { MemberUserRespVO convert03(MemberUserDO bean); default PageResult convertPage(PageResult pageResult, - List tags) { + List tags, + List levels) { PageResult result = convertPage(pageResult); + + // 处理关联数据 Map tagMap = convertMap(tags, MemberTagDO::getId, MemberTagDO::getName); + Map levelMap = convertMap(levels, MemberLevelDO::getId, MemberLevelDO::getName); + + // 填充关联数据 for (MemberUserRespVO vo : result.getList()) { vo.setTagNames(convertList(vo.getTagIds(), tagMap::get)); + vo.setLevelName(MapUtil.getStr(levelMap, vo.getLevelId(), StrUtil.EMPTY)); } return result; } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/level/MemberLevelMapper.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/level/MemberLevelMapper.java index 14d5fd3625..239e096d3e 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/level/MemberLevelMapper.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/level/MemberLevelMapper.java @@ -26,6 +26,8 @@ public interface MemberLevelMapper extends BaseMapperX { default List selectListByStatus(Integer status) { - return selectList(MemberLevelDO::getStatus, status); + return selectList(new LambdaQueryWrapperX() + .eq(MemberLevelDO::getStatus, status) + .orderByAsc(MemberLevelDO::getLevel)); } } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/user/MemberUserMapper.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/user/MemberUserMapper.java index 428458f3f4..6234030f51 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/user/MemberUserMapper.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/user/MemberUserMapper.java @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import org.apache.ibatis.annotations.Mapper; import java.util.List; @@ -47,4 +48,16 @@ public interface MemberUserMapper extends BaseMapperX { .orderByDesc(MemberUserDO::getId)); } + /** + * 取消会员的等级 + * + * @param userId 会员编号 + * @return 受影响的行数 + */ + default int cancelUserLevel(Long userId) { + return update(null, new LambdaUpdateWrapper() + .eq(MemberUserDO::getId, userId) + .set(MemberUserDO::getExperience, 0) + .set(MemberUserDO::getLevelId, null)); + } } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogService.java index 1c9315e528..30d922c467 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogService.java @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.experience.MemberExperienceLogExportReqVO; import cn.iocoder.yudao.module.member.controller.admin.level.vo.experience.MemberExperienceLogPageReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberExperienceLogDO; +import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; import java.util.Collection; import java.util.List; @@ -54,4 +55,23 @@ public interface MemberExperienceLogService { */ List getExperienceLogList(MemberExperienceLogExportReqVO exportReqVO); + /** + * 创建 手动调整 经验变动记录 + * + * @param userId 会员编号 + * @param experience 变动经验值 + * @param totalExperience 会员当前的经验 + */ + void createAdjustLog(Long userId, int experience, int totalExperience); + + /** + * 根据业务类型, 创建 经验变动记录 + * + * @param userId 会员编号 + * @param experience 变动经验值 + * @param totalExperience 会员当前的经验 + * @param bizType 业务类型 + * @param bizId 业务ID + */ + void createBizLog(Long userId, int experience, int totalExperience, MemberExperienceBizTypeEnum bizType, String bizId); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogServiceImpl.java index d3bcf4f7e8..d355cf2391 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberExperienceLogServiceImpl.java @@ -1,10 +1,12 @@ package cn.iocoder.yudao.module.member.service.level; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.experience.MemberExperienceLogExportReqVO; import cn.iocoder.yudao.module.member.controller.admin.level.vo.experience.MemberExperienceLogPageReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberExperienceLogDO; import cn.iocoder.yudao.module.member.dal.mysql.level.MemberExperienceLogMapper; +import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -61,4 +63,24 @@ public class MemberExperienceLogServiceImpl implements MemberExperienceLogServic return experienceLogMapper.selectList(exportReqVO); } + @Override + public void createAdjustLog(Long userId, int experience, int totalExperience) { + // 管理员调整时, 没有业务编号, 记录对应的枚举值 + String bizId = MemberExperienceBizTypeEnum.ADMIN.getValue() + ""; + this.createBizLog(userId, experience, totalExperience, MemberExperienceBizTypeEnum.ADMIN, bizId); + } + + @Override + public void createBizLog(Long userId, int experience, int totalExperience, MemberExperienceBizTypeEnum bizType, String bizId) { + MemberExperienceLogDO experienceLogDO = new MemberExperienceLogDO(); + experienceLogDO.setUserId(userId); + experienceLogDO.setExperience(experience); + experienceLogDO.setTotalExperience(totalExperience); + experienceLogDO.setBizId(bizId); + experienceLogDO.setBizType(bizType.getValue()); + experienceLogDO.setTitle(bizType.getTitle()); + experienceLogDO.setDescription(StrUtil.format(bizType.getDesc(), experience)); + experienceLogMapper.insert(experienceLogDO); + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogService.java index 219e9ca0d8..6d2429233a 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogService.java @@ -3,7 +3,9 @@ package cn.iocoder.yudao.module.member.service.level; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.log.MemberLevelLogExportReqVO; import cn.iocoder.yudao.module.member.controller.admin.level.vo.log.MemberLevelLogPageReqVO; +import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO; import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelLogDO; +import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import java.util.Collection; import java.util.List; @@ -54,4 +56,29 @@ public interface MemberLevelLogService { */ List getLevelLogList(MemberLevelLogExportReqVO exportReqVO); + /** + * 创建记录: 取消等级 + * + * @param userId 会员编号 + * @param reason 调整原因 + */ + void createCancelLog(Long userId, String reason); + + /** + * 创建记录: 手动调整 + * + * @param user 会员 + * @param level 等级 + * @param experience 变动经验值 + * @param reason 调整原因 + */ + void createAdjustLog(MemberUserDO user, MemberLevelDO level, int experience, String reason); + + /** + * 创建记录: 自动升级 + * + * @param user 会员 + * @param level 等级 + */ + void createAutoUpgradeLog(MemberUserDO user, MemberLevelDO level); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogServiceImpl.java index 494627e190..c0a6b22320 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelLogServiceImpl.java @@ -3,7 +3,9 @@ package cn.iocoder.yudao.module.member.service.level; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.log.MemberLevelLogExportReqVO; import cn.iocoder.yudao.module.member.controller.admin.level.vo.log.MemberLevelLogPageReqVO; +import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO; import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelLogDO; +import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.level.MemberLevelLogMapper; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -61,4 +63,53 @@ public class MemberLevelLogServiceImpl implements MemberLevelLogService { return levelLogMapper.selectList(exportReqVO); } + @Override + public void createCancelLog(Long userId, String reason) { + MemberLevelLogDO levelLogDO = new MemberLevelLogDO(); + levelLogDO.setUserId(userId); + levelLogDO.setRemark(reason); + levelLogDO.setDescription("管理员取消"); + levelLogMapper.insert(levelLogDO); + + // 给会员发送等级变动消息 + notifyMember(userId, levelLogDO); + } + + @Override + public void createAdjustLog(MemberUserDO user, MemberLevelDO level, int experience, String reason) { + MemberLevelLogDO levelLogDO = new MemberLevelLogDO(); + levelLogDO.setUserId(user.getId()); + levelLogDO.setLevelId(level.getId()); + levelLogDO.setLevel(level.getLevel()); + levelLogDO.setDiscount(level.getDiscount()); + levelLogDO.setUserExperience(level.getExperience()); + levelLogDO.setExperience(experience); + levelLogDO.setRemark(reason); + levelLogDO.setDescription("管理员调整为:" + level.getName()); + levelLogMapper.insert(levelLogDO); + + // 给会员发送等级变动消息 + notifyMember(user.getId(), levelLogDO); + } + + @Override + public void createAutoUpgradeLog(MemberUserDO user, MemberLevelDO level) { + MemberLevelLogDO levelLogDO = new MemberLevelLogDO(); + levelLogDO.setUserId(user.getId()); + levelLogDO.setLevelId(level.getId()); + levelLogDO.setLevel(level.getLevel()); + levelLogDO.setDiscount(level.getDiscount()); + levelLogDO.setExperience(level.getExperience()); + levelLogDO.setUserExperience(user.getExperience()); + levelLogDO.setDescription("成为:" + level.getName()); + levelLogMapper.insert(levelLogDO); + + // 给会员发送等级变动消息 + notifyMember(user.getId(), levelLogDO); + } + + private void notifyMember(Long userId, MemberLevelLogDO level) { + //todo: 给会员发消息 + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelService.java index 9fd401745b..c8fb5107d2 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelService.java @@ -1,11 +1,15 @@ package cn.iocoder.yudao.module.member.service.level; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.MemberLevelCreateReqVO; import cn.iocoder.yudao.module.member.controller.admin.level.vo.MemberLevelPageReqVO; import cn.iocoder.yudao.module.member.controller.admin.level.vo.MemberLevelUpdateReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO; +import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; +import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; +import javax.annotation.Nullable; import javax.validation.Valid; import java.util.Collection; import java.util.List; @@ -71,4 +75,33 @@ public interface MemberLevelService { * @return 会员等级列表 */ List getLevelListByStatus(Integer status); + + + /** + * 获得开启状态的会员等级列表 + * + * @return 会员等级列表 + */ + default List getEnableLevelList() { + return getLevelListByStatus(CommonStatusEnum.ENABLE.getStatus()); + } + + /** + * 修改会员的等级 + * + * @param user 会员 + * @param levelId 要修改的等级编号,编号为空时,代表取消会员的等级 + * @param levelReason 修改原因 + */ + void updateUserLevel(MemberUserDO user, @Nullable Long levelId, String levelReason); + + /** + * 增加会员经验 + * + * @param userId 会员ID + * @param experience 经验 + * @param bizType 业务类型 + * @param bizId 业务编号 + */ + void plusExperience(Long userId, Integer experience, MemberExperienceBizTypeEnum bizType, String bizId); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java index 97b5369f12..b1bca4695c 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java @@ -1,6 +1,9 @@ package cn.iocoder.yudao.module.member.service.level; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.ObjUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.admin.level.vo.MemberLevelCreateReqVO; @@ -8,14 +11,19 @@ import cn.iocoder.yudao.module.member.controller.admin.level.vo.MemberLevelPageR import cn.iocoder.yudao.module.member.controller.admin.level.vo.MemberLevelUpdateReqVO; import cn.iocoder.yudao.module.member.convert.level.MemberLevelConvert; import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO; +import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.level.MemberLevelMapper; +import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; +import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; import com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Collection; +import java.util.Comparator; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -33,6 +41,12 @@ public class MemberLevelServiceImpl implements MemberLevelService { @Resource private MemberLevelMapper levelMapper; + @Resource + private MemberLevelLogService memberLevelLogService; + @Resource + private MemberExperienceLogService memberExperienceLogService; + @Resource + private MemberUserMapper memberUserMapper; @Override public Long createLevel(MemberLevelCreateReqVO createReqVO) { @@ -153,4 +167,113 @@ public class MemberLevelServiceImpl implements MemberLevelService { public List getLevelListByStatus(Integer status) { return levelMapper.selectListByStatus(status); } + + @Transactional(rollbackFor = Exception.class) + @Override + public void updateUserLevel(MemberUserDO user, Long levelId, String reason) { + // 未调整的情况1 + if (user.getLevelId() == null && levelId == null) { + return; + } + // 未调整的情况2 + if (ObjUtil.equal(user.getLevelId(), levelId)) { + return; + } + + // 需要后台用户填写为什么调整会员的等级 + if (StrUtil.isBlank(reason)) { + throw exception(LEVEL_REASON_NOT_EXISTS); + } + + int experience; + int totalExperience = 0; + // 记录等级变动 + if (levelId == null) { + experience = -user.getExperience(); + + // 取消了会员的等级 + memberLevelLogService.createCancelLog(user.getId(), reason); + memberUserMapper.cancelUserLevel(user.getId()); + } else { + MemberLevelDO level = validateLevelExists(levelId); + // 变动经验值 = 等级的升级经验 - 会员当前的经验;正数为增加经验,负数为扣减经验 + experience = level.getExperience() - user.getExperience(); + // 会员当前的经验 = 等级的升级经验 + totalExperience = level.getExperience(); + + memberLevelLogService.createAdjustLog(user, level, experience, reason); + + // 更新会员表上的等级编号、经验值 + updateUserLevelIdAndExperience(user.getId(), levelId, totalExperience); + } + + + // 记录会员经验变动 + memberExperienceLogService.createAdjustLog(user.getId(), experience, totalExperience); + } + + @Transactional(rollbackFor = Exception.class) + @Override + public void plusExperience(Long userId, Integer experience, MemberExperienceBizTypeEnum bizType, String bizId) { + if (experience == 0) { + return; + } + + MemberUserDO user = memberUserMapper.selectById(userId); + if (user.getExperience() == null) { + user.setExperience(0); + } + + // 防止扣出负数 + int userExperience = NumberUtil.max(user.getExperience() + experience, 0); + + // 创建经验记录 + memberExperienceLogService.createBizLog(userId, experience, userExperience, bizType, bizId); + + // 计算会员等级 + Long levelId = calcLevel(user, userExperience); + + // 更新会员表上的等级编号、经验值 + updateUserLevelIdAndExperience(user.getId(), levelId, userExperience); + } + + private void updateUserLevelIdAndExperience(Long userId, Long levelId, Integer experience) { + memberUserMapper.updateById(new MemberUserDO() + .setId(userId) + .setLevelId(levelId).setExperience(experience) + ); + } + + /** + * 计算会员等级 + * + * @param user 会员 + * @param userExperience 会员当前的经验值 + * @return 会员等级编号,null表示无变化 + */ + private Long calcLevel(MemberUserDO user, int userExperience) { + List list = getEnableLevelList(); + if (CollUtil.isEmpty(list)) { + log.warn("计算会员等级失败:会员等级配置不存在"); + return null; + } + + MemberLevelDO matchLevel = list.stream() + .filter(level -> userExperience >= level.getExperience()) + .max(Comparator.nullsFirst(Comparator.comparing(MemberLevelDO::getLevel))) + .orElse(null); + if (matchLevel == null) { + log.warn("计算会员等级失败:未找到会员{}经验{}对应的等级配置", user.getId(), userExperience); + return null; + } + + // 等级没有变化 + if (ObjectUtil.equal(matchLevel.getId(), user.getLevelId())) { + return null; + } + + // 保存等级变更记录 + memberLevelLogService.createAutoUpgradeLog(user, matchLevel); + return matchLevel.getId(); + } } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index b1ab98ac2e..3f299d0673 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java @@ -8,13 +8,14 @@ import cn.iocoder.yudao.module.infra.api.file.FileApi; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO; import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserUpdateReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserResetPasswordReqVO; +import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdatePasswordReqVO; import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateReqVO; -import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserUpdateMobileReqVO; import cn.iocoder.yudao.module.member.convert.auth.AuthConvert; import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper; +import cn.iocoder.yudao.module.member.service.level.MemberLevelService; import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; @@ -55,6 +56,9 @@ public class MemberUserServiceImpl implements MemberUserService { @Resource private PasswordEncoder passwordEncoder; + @Resource + private MemberLevelService memberLevelService; + @Override public MemberUserDO getUserByMobile(String mobile) { return memberUserMapper.selectByMobile(mobile); @@ -180,16 +184,20 @@ public class MemberUserServiceImpl implements MemberUserService { return passwordEncoder.encode(password); } + @Transactional(rollbackFor = Exception.class) @Override public void updateUser(MemberUserUpdateReqVO updateReqVO) { // 校验存在 - validateUserExists(updateReqVO.getId()); + MemberUserDO user = validateUserExists(updateReqVO.getId()); // 校验手机唯一 validateMobileUnique(updateReqVO.getId(), updateReqVO.getMobile()); // 更新 MemberUserDO updateObj = MemberUserConvert.INSTANCE.convert(updateReqVO); memberUserMapper.updateById(updateObj); + + // 会员级别修改 + memberLevelService.updateUserLevel(user, updateReqVO.getLevelId(), updateReqVO.getLevelReason()); } @VisibleForTesting