购物车和订单中心以及优惠券相关功能初版

This commit is contained in:
xiaowang
2026-02-27 15:57:14 +08:00
parent 6adfd2f025
commit ddba11c08c
16 changed files with 890 additions and 128 deletions
@@ -62,6 +62,7 @@ public class AuthInterceptor implements HandlerInterceptor {
return path.contains("/user/login") ||
path.contains("/user/register") ||
path.contains("/user/refresh-token") ||
path.contains("/user/get/account") ||
path.contains("/shop/list/simple") ||
path.contains("/admin/auth/login") ||
path.contains("/admin/object-storage-file/upload") ||
@@ -108,4 +108,14 @@ public class CouponController {
List<UserCouponVO> availableCoupons = couponService.getAvailableCoupons(userId, productTags, productPrice);
return Result.success(availableCoupons);
}
/**
* 发放优惠券
*/
@PostMapping("/distribute")
@RequirePermission("coupon:assign")
public Result<Boolean> distributeCoupon(@RequestParam Long couponId, @RequestParam List<Long> userIds, @RequestParam Integer quantity, @RequestParam(required = false) Long shopId) {
boolean success = couponService.distributeCoupon(couponId, userIds, quantity, shopId);
return Result.success(success);
}
}
@@ -88,4 +88,14 @@ public interface CouponService extends IService<CouponDO> {
* @return 可用优惠券列表
*/
List<UserCouponVO> getAvailableCoupons(Long userId, String productTags, Double productPrice);
/**
* 发放优惠券
* @param couponId 优惠券ID
* @param userIds 用户ID列表
* @param quantity 发放数量
* @param shopId 店铺ID
* @return 是否成功
*/
boolean distributeCoupon(Long couponId, List<Long> userIds, Integer quantity, Long shopId);
}
@@ -268,6 +268,49 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
return availableCoupons;
}
@Override
@Transactional
public boolean distributeCoupon(Long couponId, List<Long> userIds, Integer quantity, Long shopId) {
// 检查优惠券是否存在
CouponDO couponDO = getById(couponId);
if (couponDO == null || couponDO.getIsDelete() == 1) {
return false;
}
// 检查优惠券是否可用
if (couponDO.getStatus() != 1) {
return false;
}
// 检查优惠券数量是否足够
if (couponDO.getUsedCount() + userIds.size() * quantity > couponDO.getTotalCount()) {
return false;
}
// 发放优惠券
for (Long userId : userIds) {
for (int i = 0; i < quantity; i++) {
// 检查是否已经分配
UserCouponDO existing = userCouponMapper.selectOne(new LambdaQueryWrapper<UserCouponDO>()
.eq(UserCouponDO::getUserId, userId)
.eq(UserCouponDO::getCouponId, couponId)
.eq(UserCouponDO::getIsDelete, 0)
);
if (existing == null) {
// 分配优惠券
UserCouponDO userCouponDO = new UserCouponDO();
userCouponDO.setUserId(userId);
userCouponDO.setCouponId(couponId);
userCouponDO.setStatus(1);
userCouponDO.setObtainTime(LocalDateTime.now());
userCouponMapper.insert(userCouponDO);
}
}
}
return true;
}
/**
* 处理优惠券标签关联
*/
@@ -552,4 +552,108 @@ WHERE u.type = 2
'coupon:edit',
'coupon:delete',
'coupon:assign'
);
-- 购物车表
create table t_cart
(
id bigint unsigned auto_increment comment '购物车唯一主键ID'
primary key comment '购物车主键索引',
user_id bigint unsigned not null comment '关联用户表主键',
product_id bigint unsigned not null comment '关联商品表主键',
quantity int unsigned default 1 not null comment '商品数量',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
constraint uk_user_product
unique (user_id, product_id) comment '用户-商品唯一索引'
) comment '购物车表';
create index idx_user_id
on t_cart (user_id) comment '按用户查询购物车的索引';
-- 订单表
create table t_order
(
id bigint unsigned auto_increment comment '订单唯一主键ID'
primary key comment '订单主键索引',
order_no varchar(50) not null comment '订单编号',
user_id bigint unsigned not null comment '关联用户表主键',
total_amount decimal(10, 2) not null comment '订单总金额',
actual_amount decimal(10, 2) not null comment '实际支付金额',
coupon_id bigint unsigned default 0 null comment '使用的优惠券ID',
status tinyint unsigned default 1 not null comment '订单状态:1-待使用,2-已完成,3-已取消',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
constraint uk_order_no
unique (order_no) comment '订单编号唯一索引'
) comment '订单表';
create index idx_status
on t_order (status) comment '按状态查询订单的索引';
create index idx_user_id
on t_order (user_id) comment '按用户查询订单的索引';
-- 订单商品表
create table t_order_item
(
id bigint unsigned auto_increment comment '订单商品唯一主键ID'
primary key comment '订单商品主键索引',
order_id bigint unsigned not null comment '关联订单表主键',
product_id bigint unsigned not null comment '关联商品表主键',
quantity int unsigned not null comment '商品数量',
price decimal(10, 2) not null comment '商品单价',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删'
) comment '订单商品表';
create index idx_order_id
on t_order_item (order_id) comment '按订单查询商品的索引';
-- 插入订单管理相关权限
INSERT INTO t_permission (permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) VALUES
('order:view', '查看订单', '订单管理', '查看订单列表和详情', NOW(), NOW(), 0),
('order:edit', '编辑订单', '订单管理', '编辑订单信息', NOW(), NOW(), 0),
('order:delete', '删除订单', '订单管理', '删除订单', NOW(), NOW(), 0),
('order:process', '处理订单', '订单管理', '处理订单状态', NOW(), NOW(), 0);
-- 为管理员(type=4)分配订单管理权限
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
SELECT
u.id as user_id,
p.id as permission_id,
NOW() as create_time,
NOW() as update_time
FROM t_user u
CROSS JOIN t_permission p
WHERE u.type = 4
AND p.is_delete = 0
AND p.permission_code IN (
'order:view',
'order:edit',
'order:delete',
'order:process'
);
-- 为店长(type=2)分配订单管理权限
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
SELECT
u.id as user_id,
p.id as permission_id,
NOW() as create_time,
NOW() as update_time
FROM t_user u
CROSS JOIN t_permission p
WHERE u.type = 2
AND p.is_delete = 0
AND p.permission_code IN (
'order:view',
'order:edit',
'order:delete',
'order:process'
);