积分业务(1)

积分业务

1、数据表设计

用户表
CREATE TABLE `users` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '用户唯一标识',
    `username` VARCHAR(50) NOT NULL UNIQUE COMMENT '用户名',
    `email` VARCHAR(100) NOT NULL UNIQUE COMMENT '用户邮箱',
    `points` INT UNSIGNED DEFAULT 0 COMMENT '用户总积分',
    `create_time` INT UNSIGNED COMMENT '记录创建时间',
    `update_time` INT UNSIGNED COMMENT '记录更新时间',
    `is_deleted` TINYINT(1) DEFAULT 0 COMMENT '记录是否被删除,0表示未删除,1表示已删除'
) COMMENT='用户信息表';

商品表

CREATE TABLE `products` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '商品唯一标识',
    `name` VARCHAR(100) NOT NULL COMMENT '商品名称',
    `description` TEXT COMMENT '商品描述',
    `points_required` INT UNSIGNED NOT NULL COMMENT '兑换该商品所需积分',
    `stock` INT UNSIGNED NOT NULL COMMENT '商品库存数量',
    `create_time` INT UNSIGNED COMMENT '记录创建时间',
    `update_time` INT UNSIGNED COMMENT '记录更新时间',
    `is_deleted` TINYINT(1) DEFAULT 0 COMMENT '记录是否被删除,0表示未删除,1表示已删除'
) COMMENT='商品信息表';
订单表
CREATE TABLE `orders` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '订单唯一标识',
    `user_id` INT UNSIGNED NOT NULL COMMENT '下单用户ID',
    `product_id` INT UNSIGNED NOT NULL COMMENT '商品ID',
    `quantity` INT UNSIGNED NOT NULL COMMENT '购买数量',
    `total_points` INT UNSIGNED NOT NULL COMMENT '订单所需总积分',
		`status` TINYINT(4) NOT NULL DEFAULT 0 COMMENT '订单状态:0待处理,1已下单,2已取消,3已发货,4已收货,5已完成',
    `snapshot_id` INT UNSIGNED COMMENT '关联的商品快照ID',
   `is_ship` TINYINT(2) DEFAULT 0 COMMENT '默认未发货,0表示未发货,1表示已发货,2表示已收货',
    `create_time` INT UNSIGNED DEFAULT CURRENT_TIMESTAMP COMMENT '记录创建时间',
    `update_time` INT UNSIGNED COMMENT '记录更新时间',
    `is_deleted` TINYINT(1) DEFAULT 0 COMMENT '记录是否被删除,0表示未删除,1表示已删除',
    FOREIGN KEY (`snapshot_id`) REFERENCES `snapshots`(`id`) ON DELETE CASCADE
) COMMENT='订单信息表';
商品快照信息表
CREATE TABLE `snapshot` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '快照唯一标识',
    `product_id` INT UNSIGNED NOT NULL COMMENT '商品ID',
    `points_required` INT UNSIGNED NOT NULL COMMENT '快照时所需积分',
    `stock` INT UNSIGNED NOT NULL COMMENT '快照时库存数量',
    `snapshot_date` INT UNSIGNED COMMENT '快照创建时间',
    `update_time` INT UNSIGNED COMMENT '记录更新时间',
    `is_deleted` TINYINT(1) DEFAULT 0 COMMENT '记录是否被删除,0表示未删除,1表示已删除'
) COMMENT='商品快照信息表';
积分流水信息表
CREATE TABLE `point_log` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '流水记录唯一标识',
    `user_id` INT UNSIGNED NOT NULL COMMENT '用户ID',
    `points` INT UNSIGNED NOT NULL COMMENT '变动的积分数量',
    `reason` VARCHAR(100) NOT NULL COMMENT '变动原因',
    `transaction_type` ENUM('ADD', 'DEDUCT',"Transfer") NOT NULL COMMENT '变动类型',
    `create_time` INT UNSIGNED COMMENT '记录创建时间',
    `update_time` INT UNSIGNED COMMENT '记录更新时间',
    `is_deleted` TINYINT(1) DEFAULT 0 COMMENT '记录是否被删除,0
  表示未删除,1表示已删除'
) COMMENT='积分流水记录表';

业务流程

1. 用户查询积分

用户可以查询自己的当前积分。

流程:

  1. 用户请求查询积分。
  2. 系统从user表中查询用户的积分。

userMapper.getPoints(userId);  //查看用户积分

2. 用户浏览商品

用户可以浏览可兑换的商品及其所需积分。

流程:

  1. 用户请求浏览商品。
  2. 系统从products表中查询所有商品及其信息。
public getProducts(){

	return (SELECT * FROM products)
} // 查看商品数据

3. 用户下单兑换商品

用户选择商品并下单,系统检查积分和库存,然后处理订单。

流程:

  1. 验证用户是否拥有足够的积分。
  2. 检查所选商品的库存是否充足。
  3. 扣除相应的积分。
  4. 减少商品库存。
  5. 记录商品快照。。
  6. 创建订单。
  7. 记录积分变动。
  8. 更新订单状态:系统将订单状态更新为已下单(状态1)
# 检查用户积分是否足够


Point point = mappe.getById(userId);//userId查找用户积分


Product product = mapper.getProductById(productId); // 商品信息

int requiredPoints = product.getPointsRequired() * quantity; // 商品积分数量  quantity为商品购买数量

point >requiredPoints --- 购买
point < requiredPoints --- 积分不足


#检查所选商品的库存是否充足

product.getStock() < quantity  
 throw new IllegalArgumentException("Insufficient stock"); //库藏不足事务回退
 
 
#扣除相应的积分

Points points = new Points();

Product product = mapper.getProductById(productId); // 商品信息

int requiredPoints = product.getPointsRequired() * quantity; // 商品积分数量  quantity为商品购买数量

point.setPoints(requiredPoints)
xx.update(user); //更新用户积分



// 减少商品库存
Product product =new Product()
int stock = getProductStock()-quantity
product.setxx(stock)
productMapper.update(product);


 // 记录商品快照
        ProductSnapshot snapshot = new ProductSnapshot();
        snapshot.setProductId(productId); 
        snapshot.setPointsRequired(product.getPointsRequired());
        snapshot.setStock(product.getStock());
        snapshot.setnapshot_date(LocalDateTime.now()));
        productSnapshotMapper.insert(snapshot);


  // 创建订单

			int requiredPoints = product.getPointsRequired() * quantity; // 商品积分数量  quantity为商品购买数量
        Order order = new Order();
        order.setPhone(phoneNumber);
        order.setProductId(productId);
        order.setQuantity(quantity);
        order.setTotalPoints(requiredPoints);
        order.setStatus(0);//表示订单待处理
        order.setSnapshotId(null);  // 与快照关联,初始为null,等快照创建完成与创建快照id关联
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        order.setIsDeleted(0);
 			 // status默认状态为0,下单后状态改为1.
       	Mapper.insert(order);

 // 设置订单快照ID
        order.setSnapshotId(snapshot.getId());
        order.setStatus(1); // 订单完成
        orderMapper.update(order);

 # 记录积分变动
Point point = new Point();
int userPoint = user.getPoints();
points.setxx()
.
.
.
points.setxx(userPoints)

pointsMapper.addXX(points); 
 
积分流水的类型

积分流水的类型包括:

  • DEDUCT:积分减少

  • 	// 执行兑换商品操作  
            user.setPoints(user.getPoints() - points);
            userMapper.update(user);  用户积分减少
    points.serTransactionType(“DEDUCT”) 积分类型
    
    
  • Transfer :积分转移

  • 	// 商品店家订单已完成后获取积分
    				if(order.getStatus()) --->Status = 3  //判断是否完成订单,完成后店家获取积分
            user.setPoints(user.getPoints() + points);
            userMapper.update(user);  店家积分增加
    points.serTransactionType(“Transfer”) 积分类型
    
  • ADD:积分充值

    			// 执行充值操作
            user.setPoints(user.getPoints() + points);
            userMapper.update(user);  用户积分增加
    
            // 记录积分变动
            PointLog log = new PointLog();
            log.setUserId(userId);
            log.setPoints(points);
            log.setReason("积分充值");
            log.setTransactionType("ADD");
    
订单状态
  • 状态码含义条件
    0待处理用户点击兑换前
    1已下单用户下单,系统扣积分、减库存
    2已取消用户取消订单,系统返还积分与库存
    3已发货商家点击发货
    4已收货用户点击“确认收货”
    5已完成系统检测订单状态为“已收货”后更新

商家发货:


if (order.getStatus() == 1) { // 已下单
    order.setStatus(3); // 更新为已发货
    order.setUpdateTime(LocalDateTime.now());
    orderMapper.update(order);
}

用户确认收货:


if (order.getStatus() == 3) { // 已发货
    order.setStatus(4); // 更新为已收货
    order.setUpdateTime(LocalDateTime.now());
    orderMapper.update(order);
}

系统自动处理已完成订单:

// 用户确认收货后Status=4,系统判断后将状态更新为订单完成
if (order.getStatus() == 4) { // 已收货
    order.setStatus(5); // 订单完成
    order.setUpdateTime(LocalDateTime.now());
    orderMapper.update(order);

    	// 商品店家订单已完成后获取积分
        user.setPoints(user.getPoints() + points);
        userMapper.update(user);  店家积分增加
points.serTransactionType(Transfer) 积分类型
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值