<parent>
<artifactId>common-serviceartifactId>
<groupId>com.southwindgroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
<version>4.7.0version>
dependency>
<dependency>
<groupId>com.southwindgroupId>
<artifactId>common-serviceartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.southwindgroupId>
<artifactId>repository-serviceartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
2、application.yml
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/uushop
username: root
password: 123456
application:
name: order-service
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: com/southwind/mapper/xml/*.xml
# order服务 向mq中存消息
rocketmq:
name-server: 192.168.248.138:9876
producer:
group: order
3、entity 实体类
OrderDetail (订单详情)
@Data
@EqualsAndHashCode(callSuper = false)
public class OrderDetail implements Serializable {
private static final long serialVersionUID=1L;
@TableId(type = IdType.ASSIGN_UUID)
private String detailId;
private String orderId;
private Integer productId;
/**
* 商品名称
*/
private String productName;
/**
* 商品单价
*/
private BigDecimal productPrice;
/**
* 商品数量
*/
private Integer productQuantity;
/**
* 商品小图
*/
private String productIcon;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
OrderMaster
@Data
@EqualsAndHashCode(callSuper = false)
public class OrderMaster implements Serializable {
private static final long serialVersionUID=1L;
@TableId(type = IdType.ASSIGN_UUID)
private String orderId;
/**
* 买家名字
*/
private String buyerName;
/**
* 买家电话
*/
private String buyerPhone;
/**
* 买家地址
*/
private String buyerAddress;
/**
* 买家id
*/
private Integer buyerOpenid;
/**
* 订单总金额
*/
private BigDecimal orderAmount;
/**
* 订单状态,默认0新下单,1完成,2取消
*/
private Integer orderStatus;
/**
* 支付状态,默认0未支付,1已支付
*/
private Integer payStatus;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
买家端
1、创建订单:前端把数据传过来
form–>OrderForm (接收前端传过来的数据)
基于注解校验
@Data
public class OrderForm {
@NotEmpty(message = "买家姓名不能为空")
private String name;
@NotEmpty(message = "买家电话不能为空")
private String phone;
@NotEmpty(message = "收货地址不能为空")
private String address;
//ID---NotNull
@NotNull(message = "买家ID不能为空")
private Integer id;
@NotEmpty(message = "商品信息不能空")
private List<ProductForm> items;
}
ProductForm
@Data
public class ProductForm {
private Integer productId;
private Integer productQuantity;
}
controller 层
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
@Autowired
private OrderMasterService orderMasterService;
//@RequestBody 接收前端传过来的数据
@PostMapping("/create")
public ResultVO create(@Valid @RequestBody OrderForm orderForm, BindingResult bindingResult){
if(bindingResult.hasErrors()){
throw new ShopException(ResponseEnum.ORDER_CREATE_ERROR.getMsg());
}
String orderId = this.orderMasterService.create(orderForm);
if(orderId!=null) {
Map<String,String> map = new HashMap<>();
map.put("orderId",orderId);
return ResultVOUtil.success(map);
}
return ResultVOUtil.fail(null);
}
}
service
public interface OrderMasterService extends IService<OrderMaster> {
//创建订单
public String create(OrderForm orderForm);
}
service–>Impl
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements OrderMasterService {
@Autowired
private OrderMasterMapper orderMasterMapper;
@Autowired
private OrderDetailMapper orderDetailMapper;
@Autowired
private ProductFeign productFeign;
@Autowired
private RocketMQTemplate rocketMQTemplate;
//添加到数据库(orderForm-->给orderMaster)
@Override
public String create(OrderForm orderForm) {
//实体类
OrderMaster orderMaster = new OrderMaster();
orderMaster.setBuyerName(orderForm.getName());
orderMaster.setBuyerPhone(orderForm.getPhone());
orderMaster.setBuyerAddress(orderForm.getAddress());
orderMaster.setBuyerOpenid(orderForm.getId());
//0:数据库中的状态
orderMaster.setOrderStatus(0);
orderMaster.setPayStatus(0);
//计算订单总价()
// order 调用 pro单价*数量dunct(feign)List items = orderForm.getItems();
//amount 计算总价
BigDecimal amount = new BigDecimal(0);
for (ProductForm item : items) {
Integer productId = item.getProductId();
//feign 调用
BigDecimal pr000ice = this.productFeign.findPriceById(productId);
// 单价*数量
BigDecimal multiply = price.multiply(new BigDecimal(item.getProductQuantity()));
amount = amount.add(multiply);
}
//总价
orderMaster.setOrderAmount(amount);
//从表--存储订单详情
int insert = this.orderMasterMapper.insert(orderMaster);
items = orderForm.getItems();
for (ProductForm item : items) {
//存储订单详情
OrderDetail orderDetail = new OrderDetail();
orderDetail.setProductQuantity(item.getProductQuantity());
//feign 调用
ProductInfo productInfo = this.productFeign.findById(item.getProductId());
orderDetail.setProductIcon(productInfo.getProductIcon());
orderDetail.setProductPrice(productInfo.getProductPrice());
orderDetail.setProductName(productInfo.getProductName());
orderDetail.setProductId(productInfo.getProductId());
orderDetail.setOrderId(orderMaster.getOrderId());
this.orderDetailMapper.insert(orderDetail);
}
//存入MQ
this.rocketMQTemplate.convertAndSend("myTop","有新的订单");
return orderMaster.getOrderId();
}
}
Feign 接口调用 :计算订单总价(单价*数量)
【order-service 调用 produnct-service(单价)】
Feign—》ProductFeign
@FeignClient("product-service")
public interface ProductFeign {
//product-service
@GetMapping("/buyer/product/findPriceById/{id}")
public BigDecimal findPriceById(@PathVariable("id")Integer id);
@GetMapping("/buyer/product/findById/{id}")
public ProductInfo findById(@PathVariable("id")Integer id);
}
注入到service层
启动类加:@EnableFeignClients
productInfo 实体类
@Data
@EqualsAndHashCode(callSuper = false)
public class ProductInfo implements Serializable {
private static final long serialVersionUID=1L;
@TableId(value = "product_id", type = IdType.AUTO)
private Integer productId;
/**
* 商品名称
*/
private String productName;
/**
* 商品单价
*/
private BigDecimal productPrice;
/**
* 库存
*/
private Integer productStock;
/**
* 描述
*/
private String productDescription;
/**
* 小图
*/
private String productIcon;
/**
* 类目编号
*/
private Integer categoryType;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 商品状态,1正常0下架
*/
private Integer productStatus;
}
2、 通过Id查询订单列表(分页)
controller
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
@Autowired
private OrderMasterService orderMasterService;
@GetMapping("/list/{buyerId}/{page}/{size}")
public ResultVO list(
@PathVariable("buyerId") Integer buyerId,
@PathVariable("page") Integer page,
@PathVariable("size") Integer size
){
//分页——page(mybatis 提供)
Page<OrderMaster> page1 = new Page<>(page, size);
QueryWrapper<OrderMaster> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("buyer_openid", buyerId);
Page<OrderMaster> page2 = this.orderMasterService.page(page1, queryWrapper);
// page2.getRecords()数据
return ResultVOUtil.success(page2.getRecords());
}
}
3、 查询订单详情
vo
@Data
public class OrderMasterVO {
private String orderId;
private String buyerName;
private String buyerPhone;
private String buyerAddress;
private Integer buyerOpenid;
private BigDecimal orderAmount;
private Integer orderStatus;
private Integer payStatus;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private List<OrderDetailVO> orderDetailList;
}
@Data
public class OrderDetailVO {
private String detailId;
private String orderId;
private Integer productId;
private String productName;
private BigDecimal productPrice;
private Integer productQuantity;
private String productIcon;
}
controller
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
@Autowired
private OrderMasterService orderMasterService;
@Autowired
private OrderDetailService orderDetailService;
@GetMapping("/detail/{buyerId}/{orderId}")
public ResultVO detail(
@PathVariable("buyerId") Integer buyerId,
@PathVariable("orderId") String orderId
){
//条件查询 QueryWrapper
QueryWrapper<OrderMaster> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("buyer_openid", buyerId);
queryWrapper.eq("order_id", orderId);
OrderMaster one = this.orderMasterService.getOne(queryWrapper);
//封装成 OrderMasterVO
OrderMasterVO orderMasterVO = new OrderMasterVO();
// one中的数据复制到 OrderMasterVO
BeanUtils.copyProperties(one, orderMasterVO);
//条件查询 OrderDetail
QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("order_id", orderId);
List<OrderDetail> list = this.orderDetailService.list(queryWrapper1);
List<OrderDetailVO> orderDetailVOList = new ArrayList<>();
for (OrderDetail orderDetail : list) {
OrderDetailVO orderDetailVO = new OrderDetailVO();
BeanUtils.copyProperties(orderDetail, orderDetailVO);
orderDetailVOList.add(orderDetailVO);
}
orderMasterVO.setOrderDetailList(orderDetailVOList);
return ResultVOUtil.success(orderMasterVO);
}
4 、取消订单
controller
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
@Autowired
private OrderMasterMapper orderMasterMapper;
@PutMapping("/cancel/{buyerId}/{orderId}")
public ResultVO cancel(
@PathVariable("buyerId") Integer buyerId,
@PathVariable("orderId") String orderId
){
Boolean cancel = this.orderMasterMapper.cancel(buyerId, orderId);
if(cancel) return ResultVOUtil.success(null);
return ResultVOUtil.fail(null);
}
mapper 接口
public interface OrderMasterMapper extends BaseMapper<OrderMaster> {
public Boolean cancel(Integer buyerId,String orderId);
}
xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.mapper.OrderMasterMapper">
<update id="cancel">
update order_master set order_status = 2 where buyer_openid=#{param1} and order_id=#{param2}
update>
mapper>
5、完成订单
controller
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
@Autowired
private OrderMasterMapper orderMasterMapper;
@PutMapping("/finish/{orderId}")
public ResultVO finish(
@PathVariable("orderId") String orderId
){
Boolean cancel = this.orderMasterMapper.finish( orderId);
if(cancel) return ResultVOUtil.success(null);
return ResultVOUtil.fail(null);
}
mapper 接口
public interface OrderMasterMapper extends BaseMapper<OrderMaster> {
public Boolean finish(String orderId);
}
xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.mapper.OrderMasterMapper">
<update id="finish">
update order_master set order_status = 1 where order_id=#{orderId}
update>
mapper>
6 、支付订单
controller
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
@Autowired
private OrderMasterMapper orderMasterMapper;
@PutMapping("/pay/{buyerId}/{orderId}")
public ResultVO pay(
@PathVariable("buyerId") Integer buyerId,
@PathVariable("orderId") String orderId
){
Boolean cancel = this.orderMasterMapper.pay(buyerId, orderId);
if(cancel) return ResultVOUtil.success(null);
return ResultVOUtil.fail(null);
}
mapper 接口
public interface OrderMasterMapper extends BaseMapper<OrderMaster> {
public Boolean finish(String orderId);
}
xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.mapper.OrderMasterMapper">
<update id="pay">
update order_master set pay_status = 1 where buyer_openid=#{param1} and order_id=#{param2}
update>
mapper>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)