Merge remote-tracking branch 'yudao/feature/mall_product' into feature/mall_product

# Conflicts:
#	yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/combination/CombinationActivityApiImpl.java
This commit is contained in:
puhui999 2023-09-19 18:58:53 +08:00
commit 04a391cd4b
38 changed files with 1159 additions and 56 deletions

View File

@ -4,18 +4,18 @@
DROP TABLE IF EXISTS `pay_wallet`;
CREATE TABLE `pay_wallet`
(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`user_id` bigint NOT NULL COMMENT '用户编号',
`user_type` tinyint NOT NULL DEFAULT 0 COMMENT '用户类型',
`balance` int NOT NULL DEFAULT 0 COMMENT '余额单位分',
`total_expense` int NOT NULL DEFAULT 0 COMMENT '累计支出单位分',
`total_recharge` int NOT NULL DEFAULT 0 COMMENT '累计充值单位分',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`user_id` bigint NOT NULL COMMENT '用户编号',
`user_type` tinyint NOT NULL DEFAULT 0 COMMENT '用户类型',
`balance` int NOT NULL DEFAULT 0 COMMENT '余额单位分',
`total_expense` int NOT NULL DEFAULT 0 COMMENT '累计支出单位分',
`total_recharge` int NOT NULL DEFAULT 0 COMMENT '累计充值单位分',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='会员钱包表';
@ -41,3 +41,33 @@ CREATE TABLE `pay_wallet_transaction`
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='会员钱包流水表';
-- ----------------------------
-- 会员钱包充值
-- ----------------------------
DROP TABLE IF EXISTS `pay_wallet_recharge`;
CREATE TABLE `pay_wallet_recharge`
(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`wallet_id` bigint NOT NULL COMMENT '会员钱包 id',
`price` int NOT NULL COMMENT '用户实际到账余额例如充 100 20则该值是 120',
`pay_price` int NOT NULL COMMENT '实际支付金额',
`wallet_bonus` int NOT NULL COMMENT '钱包赠送金额',
`pay_status` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否已支付[0:未支付 1:已经支付过]',
`pay_order_id` bigint NULL COMMENT '支付订单编号',
`pay_channel_code` varchar(16) NULL COMMENT '支付成功的支付渠道',
`pay_time` datetime NULL COMMENT '订单支付时间',
`pay_refund_id` bigint NULL COMMENT '支付退款单编号',
`refund_price` int NOT NULL DEFAULT 0 COMMENT '退款金额包含赠送金额',
`refund_pay_price` int NOT NULL DEFAULT 0 COMMENT '退款支付金额',
`refund_wallet_bonus` int NOT NULL DEFAULT 0 COMMENT '退款钱包赠送金额',
`refund_time` datetime NULL COMMENT '退款时间',
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='会员钱包充值';

View File

@ -21,6 +21,7 @@ import cn.iocoder.yudao.module.promotion.dal.mysql.combination.CombinationActivi
import cn.iocoder.yudao.module.promotion.dal.mysql.combination.CombinationProductMapper;
import cn.iocoder.yudao.module.promotion.enums.combination.CombinationRecordStatusEnum;
import cn.iocoder.yudao.module.trade.api.order.TradeOrderApi;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@ -50,7 +51,9 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic
private CombinationActivityMapper combinationActivityMapper;
@Resource
private CombinationProductMapper combinationProductMapper;
@Resource
@Lazy // TODO @puhui999我感觉 validateCombination 可以挪到 CombinationRecordServiceImpl 因为它更偏向能不能创建拼团记录
private CombinationRecordService combinationRecordService;
@Resource

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.trade.enums.order;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* 订单操作类型的枚举
*
* @author 陈賝
* @since 2023/7/6 15:31
*/
@RequiredArgsConstructor
@Getter
public enum TradeOrderOperateTypeEnum {
MEMBER_CREATE(1, "用户下单"),
TEST(2, "用户({nickname})做了({thing})"),
;
/**
* 类型
*/
private final Integer type;
/**
* 类型
*/
private final String content;
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.trade.controller.app.order;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
@ -11,8 +12,11 @@ import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderOperateTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties;
import cn.iocoder.yudao.module.trade.framework.order.core.annotations.TradeOrderLog;
import cn.iocoder.yudao.module.trade.framework.order.core.utils.TradeOrderLogUtils;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
@ -61,7 +65,10 @@ public class AppTradeOrderController {
@PostMapping("/create")
@Operation(summary = "创建订单")
@PreAuthenticated
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.TEST)
public CommonResult<AppTradeOrderCreateRespVO> createOrder(@RequestBody AppTradeOrderCreateReqVO createReqVO) {
TradeOrderLogUtils.setOrderInfo(10L, 1, 2,
MapUtil.<String, Object>builder().put("nickname", "小明").put("thing", "种土豆").build());
TradeOrderDO order = tradeOrderUpdateService.createOrder(getLoginUserId(), getClientIP(), createReqVO);
return success(new AppTradeOrderCreateRespVO().setId(order.getId()).setPayOrderId(order.getPayOrderId()));
}

View File

@ -0,0 +1,81 @@
package cn.iocoder.yudao.module.trade.dal.dataobject.order;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderOperateTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
/**
* 订单日志 DO
*
* @author 陈賝
*/
@TableName("trade_order_log")
@KeySequence("trade_order_log_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TradeOrderLogDO extends BaseDO {
/**
* 用户类型 - 系统
*
* 例如说Job 自动过期订单时通过系统自动操作
*/
public static final Integer USER_TYPE_SYSTEM = 0;
/**
* 用户编号 - 系统
*/
public static final Long USER_ID_SYSTEM = 0L;
/**
* 编号
*/
@TableId
private Long id;
/**
* 用户编号
*
* 关联 AdminUserDO id 字段或者 MemberUserDO id 字段
*/
private Long userId;
/**
* 用户类型
*
* 枚举 {@link UserTypeEnum}
*/
private Integer userType;
/**
* 订单号
*
* 关联 {@link TradeOrderDO#getId()}
*/
private Long orderId;
/**
* 操作前状态
*/
private Integer beforeStatus;
/**
* 操作后状态
*/
private Integer afterStatus;
/**
* 操作类型
*
* {@link TradeOrderOperateTypeEnum}
*/
private Integer operateType;
/**
* 订单日志信息
*/
private String content;
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.trade.framework.order.core.annotations;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderOperateTypeEnum;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
/**
* 交易订单的操作日志 AOP 注解
*
* @author 陈賝
* @since 2023/7/6 15:37
*/
@Target({METHOD, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TradeOrderLog {
/**
* 操作类型
*/
TradeOrderOperateTypeEnum operateType();
}

View File

@ -0,0 +1,112 @@
package cn.iocoder.yudao.module.trade.framework.order.core.aop;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderLogDO;
import cn.iocoder.yudao.module.trade.framework.order.core.annotations.TradeOrderLog;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderLogService;
import cn.iocoder.yudao.module.trade.service.order.bo.logger.TradeOrderLogCreateReqBO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static java.util.Collections.emptyMap;
/**
* 交易订单的操作日志的记录 AOP 切面
*
* @author 陈賝
* @since 2023/6/13 13:54
*/
@Component
@Aspect
@Slf4j
public class TradeOrderLogAspect {
/**
* 订单编号
*/
private static final ThreadLocal<Long> ORDER_ID = new ThreadLocal<>();
/**
* 操作前的状态
*/
private static final ThreadLocal<Integer> BEFORE_STATUS = new ThreadLocal<>();
/**
* 操作后的状态
*/
private static final ThreadLocal<Integer> AFTER_STATUS = new ThreadLocal<>();
/**
* 拓展参数 Map用于格式化操作内容
*/
private static final ThreadLocal<Map<String, Object>> EXTS = new ThreadLocal<>();
public TradeOrderLogAspect() {
System.out.println();
}
@Resource
private TradeOrderLogService orderLogService;
@AfterReturning("@annotation(orderLog)")
public void doAfterReturning(JoinPoint joinPoint, TradeOrderLog orderLog) {
try {
// 1.1 操作用户
Integer userType = getUserType();
Long userId = getUserId();
// 1.2 订单信息
Long orderId = ORDER_ID.get();
Integer beforeStatus = BEFORE_STATUS.get();
Integer afterStatus = AFTER_STATUS.get();
Map<String, Object> exts = ObjectUtil.defaultIfNull(EXTS.get(), emptyMap());
String content = StrUtil.format(orderLog.operateType().getContent(), exts);
// 2.1 记录日志
TradeOrderLogCreateReqBO createBO = new TradeOrderLogCreateReqBO()
.setUserId(userId).setUserType(userType)
.setOrderId(orderId).setBeforeStatus(beforeStatus).setAfterStatus(afterStatus)
.setOperateType(orderLog.operateType().getType()).setContent(content);
orderLogService.createOrderLog(createBO);
} catch (Exception ex) {
// todo 芋艿清理上下文
log.error("[doAfterReturning][orderLog({}) 订单日志错误]", toJsonString(orderLog), ex);
}
}
/**
* 获得用户类型
*
* 如果没有则约定为 {@link TradeOrderLogDO#getUserType()} 系统
*
* @return 用户类型
*/
private static Integer getUserType() {
return ObjectUtil.defaultIfNull(WebFrameworkUtils.getLoginUserType(), TradeOrderLogDO.USER_TYPE_SYSTEM);
}
/**
* 获得用户编号
*
* 如果没有则约定为 {@link TradeOrderLogDO#getUserId()} 系统
*
* @return 用户类型
*/
private static Long getUserId() {
return ObjectUtil.defaultIfNull(WebFrameworkUtils.getLoginUserId(), TradeOrderLogDO.USER_ID_SYSTEM);
}
public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus, Map<String, Object> exts) {
ORDER_ID.set(id);
BEFORE_STATUS.set(beforeStatus);
AFTER_STATUS.set(afterStatus);
EXTS.set(exts);
}
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.trade.framework.order.core.utils;
import cn.iocoder.yudao.module.trade.framework.order.core.aop.TradeOrderLogAspect;
import java.util.Map;
/**
* 交易订单的操作日志 Utils
*
* @author 芋道源码
*/
public class TradeOrderLogUtils {
public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus) {
TradeOrderLogAspect.setOrderInfo(id, beforeStatus, afterStatus, null);
}
public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus,
Map<String, Object> exts) {
TradeOrderLogAspect.setOrderInfo(id, beforeStatus, afterStatus, exts);
}
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.trade.service.order;
import cn.iocoder.yudao.module.trade.service.order.bo.logger.TradeOrderLogCreateReqBO;
import org.springframework.scheduling.annotation.Async;
/**
* 交易下单日志 Service 接口
*
* @author 陈賝
* @since 2023/7/6 15:44
*/
public interface TradeOrderLogService {
/**
* 创建交易下单日志
*
* @param logDTO 日志记录
* @author 陈賝
* @since 2023/7/6 15:45
*/
@Async
void createOrderLog(TradeOrderLogCreateReqBO logDTO);
}

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.trade.service.order;
import cn.iocoder.yudao.module.trade.service.order.bo.logger.TradeOrderLogCreateReqBO;
import org.springframework.stereotype.Service;
/**
* 交易下单日志 Service 实现类
*
* @author 陈賝
* @since 2023/7/6 15:44
*/
@Service
public class TradeOrderLogServiceImpl implements TradeOrderLogService {
@Override
public void createOrderLog(TradeOrderLogCreateReqBO createReqBO) {
// TODO 芋艿存储还没搞
System.out.println();
}
}

View File

@ -0,0 +1,51 @@
package cn.iocoder.yudao.module.trade.service.order.bo.logger;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 订单日志的创建 Request BO
*
* @author 陈賝
* @since 2023/7/6 15:27
*/
@Data
public class TradeOrderLogCreateReqBO {
/**
* 用户编号
*/
@NotNull(message = "用户编号不能为空")
private Long userId;
/**
* 用户类型
*/
@NotNull(message = "用户类型不能为空")
private Integer userType;
/**
* 订单编号
*/
@NotNull(message = "订单编号")
private Long orderId;
/**
* 操作前状态
*/
private Integer beforeStatus;
/**
* 操作后状态
*/
@NotNull(message = "操作后的状态不能为空")
private Integer afterStatus;
/**
* 操作类型
*/
private Integer operateType;
/**
* 操作明细
*/
private String content;
}

View File

@ -39,7 +39,7 @@ public interface ErrorCodeConstants {
ErrorCode SIGN_IN_CONFIG_EXISTS = new ErrorCode(1004009001, "签到天数规则已存在");
//========== 签到配置 1004010000 ==========
ErrorCode SIGN_IN_RECORD_TODAY_EXISTS = new ErrorCode(1004010000,"今日已签到,请勿重复签到");
//========== 用户等级 1004011000 ==========
ErrorCode LEVEL_NOT_EXISTS = new ErrorCode(1004011000, "用户等级不存在");

View File

@ -0,0 +1,45 @@
package cn.iocoder.yudao.module.member.controller.app.signin;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.RestController;
import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
// TODO @xiaqingsign-in
@Tag(name = "签到APP - 签到")
@RestController
@RequestMapping("/member/signin")
public class AppMemberSignInController {
@Resource
private MemberSignInRecordService signInRecordService;
// TODO @xiaqing泛型
// TODO @xiaqing合并到 AppMemberSignInRecordController getSignInRecordSummary 里哈
@Operation(summary = "个人签到信息")
@GetMapping("/get-summary")
public CommonResult getUserSummary(){
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

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.member.controller.app.signin.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "用户签到积分 Response VO")
@Data
public class AppMemberSignInRecordRespVO {
@Schema(description = "第几天签到", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer day;
@Schema(description = "签到的分数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer point;
@Schema(description = "签到时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
}

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.member.controller.app.signin.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "用户签到统计信息 Response VO")
@Data
public class AppMemberSignInSummaryRespVO {
@Schema(description = "持续签到天数", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
private Integer continuousDay;
@Schema(description = "总签到天数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer totalDay;
@Schema(description = "当天是否签到", requiredMode = Schema.RequiredMode.REQUIRED,example = "true")
private Boolean todaySignIn ;
}

View File

@ -36,4 +36,6 @@ public interface MemberSignInRecordConvert {
PageResult<AppMemberSignInRecordRespVO> convertPage02(PageResult<MemberSignInRecordDO> pageResult);
AppMemberSignInRecordRespVO coverRecordToAppRecordVo(MemberSignInRecordDO memberSignInRecordDO);
}

View File

@ -18,7 +18,7 @@ public interface MemberSignInConfigMapper extends BaseMapperX<MemberSignInConfig
return selectOne(MemberSignInConfigDO::getDay, day);
}
default List<MemberSignInConfigDO> selectListByStatus(Integer status) {
default List <MemberSignInConfigDO> selectListByStatus(Integer status) {
return selectList(MemberSignInConfigDO::getStatus, status);
}
}

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSi
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Set;
/**
@ -33,4 +34,12 @@ public interface MemberSignInRecordMapper extends BaseMapperX<MemberSignInRecord
.orderByDesc(MemberSignInRecordDO::getId));
}
//获取用户的签到记录列表信息,根据签到时间倒序
default List<MemberSignInRecordDO> selectListByUserId(Long userId){
return selectList(new LambdaQueryWrapperX <MemberSignInRecordDO>()
.eq(MemberSignInRecordDO::getUserId, userId)
.orderByDesc(MemberSignInRecordDO::getCreateTime));
}
}

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.member.service.signin;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSignInRecordPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.signin.vo.AppMemberSignInSummaryRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO;
/**
@ -29,4 +30,21 @@ public interface MemberSignInRecordService {
*/
PageResult<MemberSignInRecordDO> getSignRecordPage(Long userId, PageParam pageParam);
/**
* 创建签到记录
*
* @param userId 用户编号
* @return 签到记录
*/
MemberSignInRecordDO createSignRecord(Long userId);
/**
* 根据用户编号获得个人签到统计信息
*
* @param userId 用户编号
* @return 个人签到统计信息
*/
AppMemberSignInSummaryRespVO getSignInRecordSummary(Long userId);
}

View File

@ -1,21 +1,30 @@
package cn.iocoder.yudao.module.member.service.signin;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSignInRecordPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.signin.vo.AppMemberSignInSummaryRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInConfigDO;
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.MemberSignInRecordMapper;
import cn.iocoder.yudao.module.member.enums.ErrorCodeConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
/**
@ -28,17 +37,66 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
public class MemberSignInRecordServiceImpl implements MemberSignInRecordService {
@Resource
private MemberSignInRecordMapper memberSignInRecordMapper;
private MemberSignInRecordMapper signInRecordMapper;
@Resource
private MemberSignInConfigMapper signInConfigMapper;
@Resource
private MemberUserApi memberUserApi;
@Override
public PageResult<MemberSignInRecordDO> getSignInRecordPage(MemberSignInRecordPageReqVO pageReqVO) {
// 根据用户昵称查询出用户 ids
Set<Long> userIds = null;
public AppMemberSignInSummaryRespVO getSignInRecordSummary(Long userId) {
AppMemberSignInSummaryRespVO vo = new AppMemberSignInSummaryRespVO();
vo.setTotalDay(0);
vo.setContinuousDay(0);
vo.setTodaySignIn(false);
//获取用户签到的记录按照天数倒序获取
List <MemberSignInRecordDO> signInRecordDOList = signInRecordMapper.selectListByUserId(userId);
// TODO @xiaqingif 空的时候直接 return这样括号少逻辑更简洁
if(!CollectionUtils.isEmpty(signInRecordDOList)){
//设置总签到天数
vo.setTotalDay(signInRecordDOList.size()); // TODO @xiaqing是不是不用读取 signInRecordDOList 所有的而是 count下然后另外再读取一条最后一条
//判断当天是否有签到复用校验方法
// TODO @xiaqing不要用异常实现逻辑还是判断哈
try {
validSignDay(signInRecordDOList.get(0));
vo.setTodaySignIn(false);
}catch (Exception e){
vo.setTodaySignIn(true);
}
//如果当天签到了则说明连续签到天数有意义否则直接用默认值0
if(vo.getTodaySignIn()){
//下方计算连续签到从2天开始此处直接设置一天连续签到
vo.setContinuousDay(1);
//判断连续签到天数
// TODO @xiaqing这里逻辑想想怎么在简化下可读性可以在提升下哈
for (int i = 1; i < signInRecordDOList.size(); i++) {
//前一天减1等于当前天数则说明连续继续循环
LocalDate cur = signInRecordDOList.get(i).getCreateTime().toLocalDate();
LocalDate pre = signInRecordDOList.get(i-1).getCreateTime().toLocalDate();
if(1==daysBetween(cur,pre)){
vo.setContinuousDay(i+1);
}else{
break;
}
}
}
}
return vo;
}
private long daysBetween(LocalDate date1,LocalDate date2){
return ChronoUnit.DAYS.between(date1, date2);
}
@Override
public PageResult <MemberSignInRecordDO> getSignInRecordPage(MemberSignInRecordPageReqVO pageReqVO) {
// 根据用户昵称查询出用户ids
Set <Long> userIds = null;
if (StringUtils.isNotBlank(pageReqVO.getNickname())) {
List<MemberUserRespDTO> users = memberUserApi.getUserListByNickname(pageReqVO.getNickname());
List <MemberUserRespDTO> users = memberUserApi.getUserListByNickname(pageReqVO.getNickname());
// 如果查询用户结果为空直接返回无需继续查询
if (CollectionUtils.isEmpty(users)) {
return PageResult.empty();
@ -46,12 +104,72 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
userIds = convertSet(users, MemberUserRespDTO::getId);
}
// 分页查询
return memberSignInRecordMapper.selectPage(pageReqVO, userIds);
return signInRecordMapper.selectPage(pageReqVO, userIds);
}
@Override
public PageResult<MemberSignInRecordDO> getSignRecordPage(Long userId, PageParam pageParam) {
return memberSignInRecordMapper.selectPage(userId, pageParam);
return signInRecordMapper.selectPage(userId, pageParam);
}
@Override
public MemberSignInRecordDO createSignRecord(Long userId) {
// 获取当前用户签到的最大天数
// TODO @xiaqingdb 操作dou封装到 mapper
// TODO @xiaqingmaxSignDay是不是变量叫 lastRecord 会更容易理解哈
MemberSignInRecordDO maxSignDay = signInRecordMapper.selectOne(new LambdaQueryWrapperX <MemberSignInRecordDO>()
.eq(MemberSignInRecordDO::getUserId, userId)
.orderByDesc(MemberSignInRecordDO::getDay)
.last("limit 1"));
// 判断是否重复签到
validSignDay(maxSignDay);
// TODO @xiaqing可以使用 // 进行注释
/**1.查询出当前签到的天数**/
MemberSignInRecordDO sign = new MemberSignInRecordDO().setUserId(userId); // TODO @xiaqing应该使用 record 变量会更合适
sign.setDay(1); // 设置签到初始化天数
sign.setPoint(0); // 设置签到分数默认为 0
// 如果不为空则修改当前签到对应的天数
// TODO @xiaqing应该是要判断连续哈就是昨天
if (maxSignDay != null) {
sign.setDay(maxSignDay.getDay() + 1);
}
/**2.获取签到对应的分数**/
// 获取所有的签到规则按照天数排序只获取启用的 TODO @xiaqing不要使用 signInConfigMapper 直接查询而是要通过 SigninConfigService
List <MemberSignInConfigDO> configDOList = signInConfigMapper.selectList(new LambdaQueryWrapperX <MemberSignInConfigDO>()
.eq(MemberSignInConfigDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
.orderByAsc(MemberSignInConfigDO::getDay));
// 如果签到的天数大于最大启用的规则天数直接给最大签到的分数
// TODO @xiaqing超过最大配置的天数应该直接重置到第一天哈
MemberSignInConfigDO lastConfig = configDOList.get(configDOList.size() - 1);
if (sign.getDay() > lastConfig.getDay()) {
sign.setPoint(lastConfig.getPoint());
} else {
configDOList.forEach(el -> {
// 循环匹配对应天数设置对应分数
// TODO @xiaqing使用 equals另外这种不应该去遍历比较从可读性来说应该 CollUtil.findOne()
if (el.getDay() == sign.getDay()) {
sign.setPoint(el.getPoint());
}
});
}
// 3. 插入当前签到获取的分数
signInRecordMapper.insert(sign);
return sign;
}
// TODO @xiaqing校验使用 validate 动词哈可以改成 validateSigned
private void validSignDay(MemberSignInRecordDO signInRecordDO) {
// TODO @xiaqing代码格式if () {} 要有括号哈
if (signInRecordDO == null)
return;
// TODO @xiaqing可以直接使用 DateUtils.isToday()
LocalDate today = LocalDate.now();
if (today.equals(signInRecordDO.getCreateTime().toLocalDate())) {
throw exception(ErrorCodeConstants.SIGN_IN_RECORD_TODAY_EXISTS);
}
}
}

View File

@ -47,6 +47,14 @@ public interface ErrorCodeConstants {
ErrorCode WALLET_REFUND_AMOUNT_ERROR = new ErrorCode(1007007003, "钱包退款金额不对");
ErrorCode WALLET_REFUND_EXIST = new ErrorCode(1007007004, "已经存在钱包退款");
// TODO @jason把钱包充值单独搞个错误码段哈
ErrorCode WALLET_RECHARGE_NOT_FOUND = new ErrorCode(1007007005, "钱包充值记录不存在");
ErrorCode WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007007006, "钱包充值更新支付状态失败,钱包充值记录不是【未支付】状态");
ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR = new ErrorCode(1007007007, "钱包充值更新支付状态失败,支付单编号不匹配");
ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS = new ErrorCode(1007007008, "钱包充值更新支付状态失败,支付单状态不是【支付成功】状态");
ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH = new ErrorCode(1007007009, "钱包充值更新支付状态失败,支付单金额不匹配");
// ========== 示例订单 1007900000 ==========
ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在");
ErrorCode DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007900001, "示例订单更新支付状态失败,订单不是【未支付】状态");

View File

@ -0,0 +1,59 @@
package cn.iocoder.yudao.module.pay.controller.app.wallet;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateRespVO;
import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletRechargeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType;
@Tag(name = "用户 APP - 钱包充值")
@RestController
@RequestMapping("/pay/wallet-recharge")
@Validated
@Slf4j
public class AppPayWalletRechargeController {
@Resource
private PayWalletRechargeService walletRechargeService;
@PostMapping("/create")
@Operation(summary = "创建钱包充值记录(发起充值)")
public CommonResult<AppPayWalletRechargeCreateRespVO> createWalletRecharge(
@Valid @RequestBody AppPayWalletRechargeCreateReqVO reqVO) {
PayWalletRechargeDO walletRecharge = walletRechargeService.createWalletRecharge(
getLoginUserId(), getLoginUserType(), reqVO);
return success(PayWalletRechargeConvert.INSTANCE.convert(walletRecharge));
}
@PostMapping("/update-paid")
@Operation(summary = "更新钱包充值为已充值") // pay-module 支付服务进行回调可见 PayNotifyJob
@PermitAll // 无需登录安全由 内部校验实现
@OperateLog(enable = false) // 禁用操作日志因为没有操作人
public CommonResult<Boolean> updateWalletRechargerPaid(@Valid @RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
walletRechargeService.updateWalletRechargerPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
notifyReqDTO.getPayOrderId());
return success(true);
}
// TODO @jason管理后台是不是可以搞个发起退款
}

View File

@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
@Schema(description = "用户 APP - 创建钱包充值 Request VO")
@Data
public class AppPayWalletRechargeCreateReqVO {
@Schema(description = "支付金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
@NotNull(message = "支付金额不能为空")
// TODO @jason999是不是 @Min
@DecimalMin(value = "0", inclusive = false, message = "支付金额必须大于零")
private Integer payPrice;
// TODO @jason这个是不是后端计算出来呀不然前端可以直接搞了
@Schema(description = "钱包赠送金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
@NotNull(message = "钱包赠送金额不能为空")
@DecimalMin(value = "0", message = "钱包赠送金额必须大于等于零")
private Integer walletBonus;
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "用户 APP - 创建钱包充值 Resp VO")
@Data
public class AppPayWalletRechargeCreateRespVO {
@Schema(description = "钱包充值编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Long id;
@Schema(description = "支付订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
private Long payOrderId;
}

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.pay.convert.wallet;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateRespVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author jason
*/
@Mapper
public interface PayWalletRechargeConvert {
PayWalletRechargeConvert INSTANCE = Mappers.getMapper(PayWalletRechargeConvert.class);
PayWalletRechargeDO convert(AppPayWalletRechargeCreateReqVO vo);
// TODO @jason好像 price 相加可以写个表达式的通过 @Mapping
default PayWalletRechargeDO convert(Long walletId, AppPayWalletRechargeCreateReqVO vo) {
PayWalletRechargeDO walletRecharge = convert(vo);
return walletRecharge.setWalletId(walletId)
.setPrice(walletRecharge.getPayPrice() + walletRecharge.getWalletBonus());
}
AppPayWalletRechargeCreateRespVO convert(PayWalletRechargeDO bean);
}

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.pay.convert.wallet;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.transaction.AppPayWalletTransactionRespVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO;
import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@ -14,6 +14,6 @@ public interface PayWalletTransactionConvert {
PageResult<AppPayWalletTransactionRespVO> convertPage(PageResult<PayWalletTransactionDO> page);
PayWalletTransactionDO convert(CreateWalletTransactionBO bean);
PayWalletTransactionDO convert(WalletTransactionCreateReqBO bean);
}

View File

@ -0,0 +1,101 @@
package cn.iocoder.yudao.module.pay.dal.dataobject.wallet;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 会员钱包充值
*/
@TableName(value ="pay_wallet_recharge")
@KeySequence("pay_wallet_recharge_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
public class PayWalletRechargeDO extends BaseDO {
/**
* 编号
*/
@TableId
private Long id;
/**
* 钱包编号
*
* 关联 {@link PayWalletDO#getId()}
*/
private Long walletId;
// TODO @jason要不改成 totalPrice
/**
* 用户实际到账余额
*
* 例如充 100 20则该值是 120
*/
private Integer price;
/**
* 实际支付金额
*/
private Integer payPrice;
// TODO @jasonbonusPrice 更统一一点
/**
* 钱包赠送金额
*/
private Integer walletBonus;
/**
* 是否已支付
*
* true - 已支付
* false - 未支付
*/
private Boolean payStatus;
/**
* 支付订单编号
*
* 关联 {@link PayOrderDO#getId()}
*/
private Long payOrderId;
/**
* 支付成功的支付渠道
*
* 冗余 {@link PayOrderDO#getChannelCode()}
*/
private String payChannelCode;
/**
* 订单支付时间
*/
private LocalDateTime payTime;
/**
* 支付退款单编号
*
* 关联 {@link PayRefundDO#getId()}
*/
private Long payRefundId;
// TODO @jason要不改成 refundTotalPrice
/**
* 退款金额包含赠送金额
*/
private Integer refundPrice;
/**
* 退款支付金额
*/
private Integer refundPayPrice;
// TODO @jason要不改成 refundBonusPrice
/**
* 退款钱包赠送金额
*/
private Integer refundWalletBonus;
/**
* 退款时间
*/
private LocalDateTime refundTime;
}

View File

@ -14,6 +14,8 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
PayWalletDO::getUserType, userType);
}
// TODO @jason下面几个更新方法 id 放前面哈一般来说重要参数放前面
/**
* 当消费退款时候 更新钱包
*
@ -42,6 +44,20 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
.ge(PayWalletDO::getBalance, price); // cas 逻辑
return update(null, lambdaUpdateWrapper);
}
/**
* 当充值的时候更新钱包
* @param price 钱包金额
* @param id 钱包 id
*/
default int updateWhenRecharge(Integer price, Long id){
LambdaUpdateWrapper<PayWalletDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<PayWalletDO>()
.setSql(" balance = balance + " + price
+ ", total_recharge = total_recharge + " + price)
.eq(PayWalletDO::getId, id);
return update(null, lambdaUpdateWrapper);
}
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.yudao.module.pay.dal.mysql.wallet;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PayWalletRechargeMapper extends BaseMapperX<PayWalletRechargeDO> {
default int updateByIdAndPaid(Long id, boolean wherePayStatus, PayWalletRechargeDO updateObj){
return update(updateObj, new LambdaQueryWrapperX<PayWalletRechargeDO>()
.eq(PayWalletRechargeDO::getId, id).eq(PayWalletRechargeDO::getPayStatus, wherePayStatus));
}
}

View File

@ -0,0 +1,32 @@
package cn.iocoder.yudao.module.pay.service.wallet;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
/**
* 钱包充值 Service 接口
*
* @author jason
*/
public interface PayWalletRechargeService {
/**
* 创建钱包充值记录发起充值
*
* @param userId 用户 id
* @param userType 用户类型
* @param createReqVO 钱包充值请求 VO
* @return 钱包充值记录
*/
PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType,
AppPayWalletRechargeCreateReqVO createReqVO);
/**
* 更新钱包充值成功
*
* @param id 钱包充值记录 id
* @param payOrderId 支付订单 id
*/
void updateWalletRechargerPaid(Long id, Long payOrderId);
}

View File

@ -0,0 +1,141 @@
package cn.iocoder.yudao.module.pay.service.wallet;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.recharge.AppPayWalletRechargeCreateReqVO;
import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletRechargeMapper;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import static cn.hutool.core.util.ObjectUtil.notEqual;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.addTime;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
/**
* 钱包充值 Service 实现类
*
* @author jason
*/
@Service
@Slf4j
public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
/**
* TODO 放到 配置文件中
*/
private static final Long WALLET_PAY_APP_ID = 8L;
private static final String WALLET_RECHARGE_ORDER_SUBJECT = "钱包余额充值";
@Resource
private PayWalletRechargeMapper walletRechargeMapper;
@Resource
private PayWalletService payWalletService;
@Resource
private PayOrderService payOrderService;
@Override
@Transactional(rollbackFor = Exception.class)
public PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType,
AppPayWalletRechargeCreateReqVO createReqVO) {
// 1. 新增钱包充值记录
PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
PayWalletRechargeDO walletRecharge = PayWalletRechargeConvert.INSTANCE.convert(wallet.getId(), createReqVO);
walletRechargeMapper.insert(walletRecharge);
// 2.1 创建支付单
Long payOrderId = payOrderService.createOrder(new PayOrderCreateReqDTO()
.setAppId(WALLET_PAY_APP_ID).setUserIp(getClientIP())
.setMerchantOrderId(walletRecharge.getId().toString()) // 业务的订单编号
.setSubject(WALLET_RECHARGE_ORDER_SUBJECT).setBody("").setPrice(walletRecharge.getPayPrice())
.setExpireTime(addTime(Duration.ofHours(2L))));
// 2.2 更新钱包充值记录中支付订单
walletRechargeMapper.updateById(new PayWalletRechargeDO().setPayOrderId(payOrderId)
.setId(walletRecharge.getId()));
// TODO @jason是不是你直接返回就好啦然后 payOrderId 设置下
return walletRechargeMapper.selectById(walletRecharge.getId());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateWalletRechargerPaid(Long id, Long payOrderId) {
// 1.1 获取钱包充值记录
PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(id);
if (walletRecharge == null) {
log.error("[updateWalletRechargerPaid][钱包充值记录不存在,钱包充值记录 id({})]", id);
throw exception(WALLET_RECHARGE_NOT_FOUND);
}
// 1.2 校验钱包充值是否可以支付
PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId);
// 2. 更新钱包充值的支付状态
int updateCount = walletRechargeMapper.updateByIdAndPaid(id,false,
new PayWalletRechargeDO().setId(id).setPayStatus(true).setPayTime(LocalDateTime.now())
.setPayChannelCode(payOrderDO.getChannelCode()));
if (updateCount == 0) {
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID);
}
// 3. 更新钱包余额
// TODO @jason这样的话未来提现会不会把充值的也提现走哈类似先充 100 110然后提现 110
payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(id),
PayWalletBizTypeEnum.RECHARGE, walletRecharge.getPrice());
}
private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) {
// 1.1 校验充值记录的支付状态
if (walletRecharge.getPayStatus()) {
log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]",
walletRecharge.getId(), toJsonString(walletRecharge));
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID);
}
// 1.2 校验支付订单匹配
if (notEqual(walletRecharge.getPayOrderId(), payOrderId)) { // 支付单号
log.error("[validateWalletRechargerCanPaid][钱包({}) 支付单不匹配({}),请进行处理! 钱包数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
// 2.1 校验支付单是否存在
PayOrderDO payOrder = payOrderService.getOrder(payOrderId);
if (payOrder == null) {
log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 不存在,请进行处理!]",
walletRecharge.getId(), payOrderId);
throw exception(PAY_ORDER_NOT_FOUND);
}
// 2.2 校验支付单已支付
if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) {
log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS);
}
// 2.3 校验支付金额一致
if (notEqual(payOrder.getPrice(), walletRecharge.getPayPrice())) {
log.error("[validateDemoOrderCanPaid][钱包({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{}payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge), toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH);
}
// 2.4 校验支付订单的商户订单匹配
if (notEqual(payOrder.getMerchantOrderId(), walletRecharge.getId().toString())) {
log.error("[validateDemoOrderCanPaid][钱包({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
return payOrder;
}
}

View File

@ -21,6 +21,13 @@ public interface PayWalletService {
*/
PayWalletDO getOrCreateWallet(Long userId, Integer userType);
/**
* 获取钱包信息
*
* @param walletId 钱包 id
*/
PayWalletDO getWallet(Long walletId);
/**
* 钱包订单支付
*
@ -56,14 +63,13 @@ public interface PayWalletService {
/**
* 增加钱包余额
*
* @param userId 用户 id
* @param userType 用户类型
* @param walletId 钱包 id
* @param bizId 业务关联 id
* @param bizType 业务关联分类
* @param price 增加金额
* @return 钱包流水
*/
PayWalletTransactionDO addWalletBalance(Long userId, Integer userType,
Long bizId, PayWalletBizTypeEnum bizType, Integer price);
PayWalletTransactionDO addWalletBalance(Long walletId, String bizId,
PayWalletBizTypeEnum bizType, Integer price);
}

View File

@ -9,7 +9,7 @@ import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO;
import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@ -55,6 +55,11 @@ public class PayWalletServiceImpl implements PayWalletService {
return wallet;
}
@Override
public PayWalletDO getWallet(Long walletId) {
return walletMapper.selectById(walletId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price) {
@ -79,8 +84,9 @@ public class PayWalletServiceImpl implements PayWalletService {
Long walletId = validateWalletCanRefund(payRefund.getId(), payRefund.getChannelOrderNo(), refundPrice);
PayWalletDO wallet = walletMapper.selectById(walletId);
Assert.notNull(wallet, "钱包 {} 不存在", walletId);
// 2. 增加余额
return addWalletBalance(wallet.getUserId(), wallet.getUserType(), payRefund.getId(), PAYMENT_REFUND, refundPrice);
return addWalletBalance(walletId, String.valueOf(payRefund.getId()), PAYMENT_REFUND, refundPrice);
}
/**
@ -132,34 +138,39 @@ public class PayWalletServiceImpl implements PayWalletService {
}
// 2.2 生成钱包流水
Integer afterBalance = payWallet.getBalance() - price;
CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId())
WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId())
.setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId))
.setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(bo);
}
@Override
public PayWalletTransactionDO addWalletBalance(Long userId, Integer userType,
Long bizId, PayWalletBizTypeEnum bizType, Integer price) {
// 1. 获取钱包
PayWalletDO payWallet = getOrCreateWallet(userId, userType);
public PayWalletTransactionDO addWalletBalance(Long walletId, String bizId,
PayWalletBizTypeEnum bizType, Integer price) {
// 1.1 获取钱包
PayWalletDO payWallet = getWallet(walletId);
if (payWallet == null) {
log.error("[addWalletBalance],用户钱包({})不存在.", walletId);
throw exception(WALLET_NOT_FOUND);
}
// 1.2 更新钱包金额
switch (bizType) {
case PAYMENT_REFUND: {
// 更新退款
case PAYMENT_REFUND: { // 退款更新
walletMapper.updateWhenConsumptionRefund(price, payWallet.getId());
break;
}
case RECHARGE: {
//TODO
case RECHARGE: { // 充值更新
walletMapper.updateWhenRecharge(price, payWallet.getId());
break;
}
// TODO 其它类型这里可以先跑异常避免有业务搞错
}
// 2. 生成钱包流水
CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId())
.setPrice(price).setBalance(payWallet.getBalance()+price).setBizId(String.valueOf(bizId))
.setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(bo);
WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO()
.setWalletId(payWallet.getId()).setPrice(price).setBalance(payWallet.getBalance() + price)
.setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(transactionCreateReqBO);
}
}

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.transaction.AppPayWalletTransactionPageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO;
import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import javax.validation.Valid;
@ -31,7 +31,7 @@ public interface PayWalletTransactionService {
* @param bo 创建钱包流水 bo
* @return 新建的钱包 do
*/
PayWalletTransactionDO createWalletTransaction(@Valid CreateWalletTransactionBO bo);
PayWalletTransactionDO createWalletTransaction(@Valid WalletTransactionCreateReqBO bo);
/**
* 根据 no获取钱包余流水
@ -48,5 +48,5 @@ public interface PayWalletTransactionService {
* @return 钱包流水
*/
PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type);
}

View File

@ -8,7 +8,7 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletTransactionMapper;
import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO;
import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -43,7 +43,7 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
}
@Override
public PayWalletTransactionDO createWalletTransaction(CreateWalletTransactionBO bo) {
public PayWalletTransactionDO createWalletTransaction(WalletTransactionCreateReqBO bo) {
PayWalletTransactionDO transaction = PayWalletTransactionConvert.INSTANCE.convert(bo)
.setNo(noRedisDAO.generate(WALLET_NO_PREFIX));
payWalletTransactionMapper.insert(transaction);

View File

@ -9,7 +9,7 @@ import lombok.Data;
* @author jason
*/
@Data
public class CreateWalletTransactionBO {
public class WalletTransactionCreateReqBO {
// TODO @jasonbo 的话最好加个参数校验哈

View File

@ -44,31 +44,31 @@ spring:
primary: master
datasource:
master:
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
name: mall
url: jdbc:mysql://10.211.55.5:3308/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
# url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.master.name} # PostgreSQL 连接的示例
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例
username: root
password: 123456
password: 1qaz!QAZ
# username: sa
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
slave: # 模拟从库,可根据自己需要修改
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
name: mall
url: jdbc:mysql://10.211.55.5:3308/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true # MySQL Connector/J 8.X 连接的示例
# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT # MySQL Connector/J 5.X 连接的示例
# url: jdbc:postgresql://127.0.0.1:5432/${spring.datasource.dynamic.datasource.slave.name} # PostgreSQL 连接的示例
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.slave.name} # SQLServer 连接的示例
username: root
password: 123456
password: 1qaz!QAZ
# username: sa
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
redis:
host: 127.0.0.1 # 地址
host: 10.211.55.5 # 地址
port: 6379 # 端口
database: 0 # 数据库索引
# password: dev # 密码,建议生产环境开启