完善了订单类

This commit is contained in:
chen-xin-zhi 2024-11-02 18:10:57 +08:00
parent a647a08e36
commit 3ff932ea7e
14 changed files with 719 additions and 3 deletions

View File

@ -0,0 +1,110 @@
package com.cultural.heritage.controller.good;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.cart.CartRecordAddRequest;
import com.cultural.heritage.model.dto.cart.CartRecordUpdateRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.vo.CartRecordVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
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 java.util.List;
@RestController
@RequestMapping("/cart")
@Slf4j
@Tag(name = "购物车接口")
public class CartRecordController {
@Resource
private CartRecordService cartRecordService;
/**
* 用户添加商品至购物车
* @param cartRecordAddRequest 购物车记录添加请求体
* @return 是否添加成功
*/
@PostMapping("/add")
public BaseResponse<Boolean> addCartRecord(@RequestBody CartRecordAddRequest cartRecordAddRequest) {
if (cartRecordAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = cartRecordService.dealAlreadyExistGood(cartRecordAddRequest);
return ResultUtils.success(result);
}
/**
* 用户更新商品至购物车
* @param cartRecordUpdateRequestList 购物车记录列表更新请求体
* @return 是否更新成功
*/
@PostMapping("/update")
public BaseResponse<Boolean> updateCartRecord(@RequestBody List<CartRecordUpdateRequest> cartRecordUpdateRequestList) {
if (CollectionUtils.isEmpty(cartRecordUpdateRequestList)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
List<CartRecord> cartRecordList = cartRecordUpdateRequestList.stream().map(cartRecordUpdateRequest -> {
CartRecord cartRecord = new CartRecord();
BeanUtils.copyProperties(cartRecordUpdateRequest, cartRecord);
return cartRecord;
}).toList();
boolean result = cartRecordService.updateBatchById(cartRecordList);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
/**
* 用户删除购物车的物品
* @param idList 删除的商品id列表
* @return 是否删除成功
*/
@PostMapping("/delete")
public BaseResponse<Boolean> deleteCart(@RequestBody List<Integer> idList) {
if (CollectionUtils.isEmpty(idList)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = cartRecordService.removeBatchByIds(idList);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
/**
* 展示用户购物车中的商品
* @param userIdRequest 用户id请求体
* @return
*/
@PostMapping("/list")
public BaseResponse<List<CartRecordVO>> listUserCartRecord(@RequestBody CommonRequest userIdRequest) {
if (userIdRequest == null || userIdRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 根据用户id查询购物车记录
Long userId = userIdRequest.getId();
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId);
List<CartRecord> list = cartRecordService.list(queryWrapper);
List<CartRecordVO> cartRecordVOList = cartRecordService.transformToCartRecordVOList(list);
return ResultUtils.success(cartRecordVOList);
}
}

View File

@ -0,0 +1,36 @@
package com.cultural.heritage.model.dto.cart;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class CartRecordAddRequest implements Serializable {
/**
* 用户id
*/
private Long userId;
/**
* 商品id
*/
private Long goodId;
/**
* 数量
*/
private Integer quantity;
/**
* 小计
*/
private Double subtotal;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,41 @@
package com.cultural.heritage.model.dto.cart;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class CartRecordUpdateRequest implements Serializable {
/**
* 购物车记录id
*/
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 商品id
*/
private Long goodId;
/**
* 数量
*/
private Integer quantity;
/**
* 小计
*/
private Double subtotal;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,42 @@
package com.cultural.heritage.model.dto.snapshot;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
@Data
public class AddressSnapshot implements Serializable {
/**
* 联系人
*/
private String name;
/**
* 手机号
*/
private String phone;
/**
* 地区
*/
private String region;
/**
* 详细地址
*/
private String detailAddress;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,27 @@
package com.cultural.heritage.model.dto.snapshot;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class ContactsSnapshot implements Serializable {
/**
* 联系人姓名
*/
private String name;
/**
* 联系人手机号
*/
private String phone;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,57 @@
package com.cultural.heritage.model.dto.snapshot;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
@Data
public class CouponSnapshot implements Serializable {
/**
* 优惠券名称
*/
private String name;
/**
* 满减金额
*/
private Double conditionAmount;
/**
* 优惠券简介
*/
private String intro;
/**
* 优惠券图片
*/
private String image;
/**
* 有效开始日期
*/
private Date startTime;
/**
* 有效截止日期
*/
private Date endTime;
/**
* 作用范围
*/
private String useScope;
/**
* 使用说明
*/
private String description;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,75 @@
package com.cultural.heritage.model.dto.snapshot;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
@Data
public class GoodSnapshot implements Serializable {
/**
* 商品名
*/
private String name;
/**
* 商品类型
*/
private String type;
/**
* 商品价格
*/
private Double price;
/**
* 商品图片
*/
private String goodImg;
/**
* 商品简介
*/
private String intro;
/**
* 商品详情简介
*/
private String introDetail;
/**
* 商品详情图片
*/
private String detailImg;
/**
* 商品标签
*/
private String label;
/**
* 是否是商品类型
*/
private Integer isGoodType;
/**
* 节日限定序号
*/
private Integer festivalOrder;
/**
* 预约日期
*/
private String reserveDate;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -5,7 +5,6 @@ import lombok.Data;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
/** /**
@ -40,7 +39,7 @@ public class CartRecord implements Serializable {
/** /**
* 小计 * 小计
*/ */
private BigDecimal subtotal; private Double subtotal;
/** /**

View File

@ -0,0 +1,89 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cultural.heritage.model.dto.snapshot.AddressSnapshot;
import com.cultural.heritage.model.dto.snapshot.ContactsSnapshot;
import com.cultural.heritage.model.dto.snapshot.CouponSnapshot;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
/**
* 订单
* @TableName order_total
*/
@Data
@TableName("order_total")
public class Order implements Serializable {
/**
* 订单id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 地址信息快照
*/
private AddressSnapshot addressSnapshot;
/**
* 联系人信息快照
*/
private ContactsSnapshot contactsSnapshot;
/**
* 优惠券信息快照
*/
private CouponSnapshot couponSnapshot;
/**
* 订单总金额
*/
private Double totalAmount;
/**
* 订单状态
*/
private String orderStatus;
/**
* 创建时间
*/
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
/**
* 更新时间
*/
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
/**
* 是否删除
*/
private Integer isDelete;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,57 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* 订单明细
* @TableName order_item
*/
@Data
public class OrderItem implements Serializable {
/**
* 订单明细id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 关联的订单id
*/
private Long orderId;
/**
* 商品信息快照
*/
private GoodSnapshot goodSnapshot;
/**
* 商品单价快照
*/
private Double priceSnapshot;
/**
* 购买数量
*/
private Integer quantity;
/**
* 订单项金额单价 * 数量
*/
private Double itemTotalAmount;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,48 @@
package com.cultural.heritage.model.vo;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Data
public class CartRecordVO implements Serializable {
/**
* 购物车记录id
*/
private Long id;
/**
* 用户id
*/
private Long userId;
/**
* 商品id
*/
private Long goodId;
/**
* 数量
*/
private Integer quantity;
/**
* 小计
*/
private Double subtotal;
/**
* 购物车商品详情信息
*/
private GoodVO goodVO;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,49 @@
package com.cultural.heritage.model.vo;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class GoodVO implements Serializable {
/**
* 商品编号
*/
private Long id;
/**
* 商品名
*/
private String name;
/**
* 商品类型
*/
private String type;
/**
* 商品价格
*/
private Double price;
/**
* 商品图片
*/
private String goodImg;
/**
* 商品标签
*/
private String label;
/**
* 预约日期
*/
private String reserveDate;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -1,8 +1,22 @@
package com.cultural.heritage.service.good; package com.cultural.heritage.service.good;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.dto.cart.CartRecordAddRequest;
import com.cultural.heritage.model.entity.CartRecord; import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.vo.CartRecordVO;
import java.util.List;
public interface CartRecordService extends IService<CartRecord> { public interface CartRecordService extends IService<CartRecord> {
/**
* 处理用户购物车已存在该商品
*/
boolean dealAlreadyExistGood(CartRecordAddRequest cartRecordAddRequest);
/**
* 封装用户购物车中的商品信息
*/
List<CartRecordVO> transformToCartRecordVOList(List<CartRecord> list);
} }

View File

@ -1,4 +1,76 @@
package com.cultural.heritage.service.good.impl; package com.cultural.heritage.service.good.impl;
public class CartRecordServiceImpl { import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.CartRecordMapper;
import com.cultural.heritage.mapper.GoodMapper;
import com.cultural.heritage.model.dto.cart.CartRecordAddRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.vo.CartRecordVO;
import com.cultural.heritage.model.vo.GoodVO;
import com.cultural.heritage.service.good.CartRecordService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
@Service
public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRecord> implements CartRecordService {
@Resource
private GoodMapper goodMapper;
/**
* 处理用户购物车已存在该商品
*/
@Override
public boolean dealAlreadyExistGood(CartRecordAddRequest cartRecordAddRequest) {
Long userId = cartRecordAddRequest.getUserId();
Long goodId = cartRecordAddRequest.getGoodId();
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId);
queryWrapper.eq("goodId", goodId);
CartRecord cartRecord = this.baseMapper.selectOne(queryWrapper);
if (cartRecord != null) {
cartRecord.setQuantity(cartRecord.getQuantity() + cartRecordAddRequest.getQuantity());
cartRecord.setSubtotal(cartRecord.getSubtotal() + cartRecordAddRequest.getSubtotal());
boolean result = this.updateById(cartRecord);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
} else {
BeanUtils.copyProperties(cartRecordAddRequest, cartRecord);
boolean result = this.save(cartRecord);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
}
return true;
}
/**
* 封装用户购物车中的商品信息
*/
@Override
public List<CartRecordVO> transformToCartRecordVOList(List<CartRecord> list) {
return list.stream().map(cartRecord -> {
// 封装购物车记录VO
CartRecordVO cartRecordVO = new CartRecordVO();
BeanUtils.copyProperties(cartRecord, cartRecordVO);
// 封装商品类GoodVO
Long goodId = cartRecord.getGoodId();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", goodId);
Good good = goodMapper.selectOne(queryWrapper);
GoodVO goodVO = new GoodVO();
BeanUtils.copyProperties(good, goodVO);
cartRecordVO.setGoodVO(goodVO);
return cartRecordVO;
}).toList();
}
} }