Răsfoiți Sursa

feat:为seata-order-micro-servic订单微服务模块编写业务代码

yang yi 1 an în urmă
părinte
comite
2c837dd68d

+ 2 - 1
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/controller/AccountController.java

@@ -2,6 +2,7 @@ package space.anyi.seataAccountMicroService.controller;
 
 import anyi.sapce.common.entity.ResponseResult;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import space.anyi.seataAccountMicroService.service.AccountService;
@@ -17,7 +18,7 @@ import space.anyi.seataAccountMicroService.service.AccountService;
 public class AccountController {
     @Autowired
     private AccountService accountService;
-
+    @PostMapping("/account/reduce")
     public ResponseResult reduce(@RequestParam("userId")Long userId, @RequestParam("money")Integer money){
         accountService.reduce(userId,money);
         return ResponseResult.okResult(200,"扣减账户余额OK");

+ 5 - 0
seata_order_micro_servic/pom.xml

@@ -41,6 +41,11 @@
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
         </dependency>
+        <!--配合OpenFeign的负载均衡组件-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
+        </dependency>
         <!--远程调用openFeign的依赖-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>

+ 22 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/SeataOrderMicroServiceApplication.java

@@ -0,0 +1,22 @@
+package space.anyi.seataOrderMicroService;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: SeataOrderMicroServiceApplication
+ * @Author: 杨逸
+ * @Data:2025/9/21 11:24
+ * @Description:
+ */
+@SpringBootApplication
+@EnableDiscoveryClient
+@EnableFeignClients
+public class SeataOrderMicroServiceApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(SeataOrderMicroServiceApplication.class,args);
+    }
+}

+ 26 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/controller/OrderController.java

@@ -0,0 +1,26 @@
+package space.anyi.seataOrderMicroService.controller;
+
+import anyi.sapce.common.entity.ResponseResult;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+import space.anyi.seataOrderMicroService.domain.Order;
+import space.anyi.seataOrderMicroService.service.OrderService;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: OrderController
+ * @Author: 杨逸
+ * @Data:2025/9/21 11:25
+ * @Description:
+ */
+@RestController
+public class OrderController {
+    @Autowired
+    private OrderService orderService;
+    @GetMapping("/order/save")
+    public ResponseResult save(Order order){
+        orderService.saveOrder(order);
+        return ResponseResult.okResult("订单创建成功",null);
+    }
+}

+ 100 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/domain/Order.java

@@ -0,0 +1,100 @@
+package space.anyi.seataOrderMicroService.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import lombok.Data;
+
+/**
+ *
+ * @TableName order
+ */
+@TableName(value ="order")
+@Data
+public class Order implements Serializable {
+    /**
+     *
+     */
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /**
+     *
+     */
+    private Long userId;
+
+    /**
+     *
+     */
+    private Long productId;
+
+    /**
+     *
+     */
+    private Integer nums;
+
+    /**
+     *
+     */
+    private Integer money;
+
+    /**
+     * 0:创建中;1:已完结
+     */
+    private Integer status;
+
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
+
+    @Override
+    public boolean equals(Object that) {
+        if (this == that) {
+            return true;
+        }
+        if (that == null) {
+            return false;
+        }
+        if (getClass() != that.getClass()) {
+            return false;
+        }
+        Order other = (Order) that;
+        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
+            && (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
+            && (this.getProductId() == null ? other.getProductId() == null : this.getProductId().equals(other.getProductId()))
+            && (this.getNums() == null ? other.getNums() == null : this.getNums().equals(other.getNums()))
+            && (this.getMoney() == null ? other.getMoney() == null : this.getMoney().equals(other.getMoney()))
+            && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()));
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
+        result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
+        result = prime * result + ((getProductId() == null) ? 0 : getProductId().hashCode());
+        result = prime * result + ((getNums() == null) ? 0 : getNums().hashCode());
+        result = prime * result + ((getMoney() == null) ? 0 : getMoney().hashCode());
+        result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append(getClass().getSimpleName());
+        sb.append(" [");
+        sb.append("Hash = ").append(hashCode());
+        sb.append(", id=").append(id);
+        sb.append(", userId=").append(userId);
+        sb.append(", productId=").append(productId);
+        sb.append(", nums=").append(nums);
+        sb.append(", money=").append(money);
+        sb.append(", status=").append(status);
+        sb.append(", serialVersionUID=").append(serialVersionUID);
+        sb.append("]");
+        return sb.toString();
+    }
+}

+ 20 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/mapper/OrderMapper.java

@@ -0,0 +1,20 @@
+package space.anyi.seataOrderMicroService.mapper;
+
+import org.apache.ibatis.annotations.Mapper;
+import space.anyi.seataOrderMicroService.domain.Order;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author 杨逸
+* @description 针对表【order】的数据库操作Mapper
+* @createDate 2025-09-21 11:22:55
+* @Entity generator.domain.Order
+*/
+@Mapper
+public interface OrderMapper extends BaseMapper<Order> {
+
+}
+
+
+
+

+ 19 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/service/AccountService.java

@@ -0,0 +1,19 @@
+package space.anyi.seataOrderMicroService.service;
+
+import anyi.sapce.common.entity.ResponseResult;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: AccountService
+ * @Author: 杨逸
+ * @Data:2025/9/21 11:35
+ * @Description:
+ */
+@FeignClient("seata-account-micro-service")
+public interface AccountService {
+    @PostMapping("/account/reduce")
+    public ResponseResult reduce(@RequestParam("userId")Long userId, @RequestParam("money")Integer money);
+}

+ 14 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/service/OrderService.java

@@ -0,0 +1,14 @@
+package space.anyi.seataOrderMicroService.service;
+
+import space.anyi.seataOrderMicroService.domain.Order;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+* @author 杨逸
+* @description 针对表【order】的数据库操作Service
+* @createDate 2025-09-21 11:22:55
+*/
+public interface OrderService extends IService<Order> {
+
+    void saveOrder(Order order);
+}

+ 19 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/service/StorageService.java

@@ -0,0 +1,19 @@
+package space.anyi.seataOrderMicroService.service;
+
+import anyi.sapce.common.entity.ResponseResult;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ * @ProjectName: distributedSystemLearn
+ * @FileName: StorageService
+ * @Author: 杨逸
+ * @Data:2025/9/21 11:36
+ * @Description:
+ */
+@FeignClient("seata-storage-micro-service")
+public interface StorageService {
+    @PostMapping("/storage/reduce")
+    public ResponseResult reduce(@RequestParam("productId") Long productId,@RequestParam("nums") Integer nums);
+}

+ 47 - 0
seata_order_micro_servic/src/main/java/space/anyi/seataOrderMicroService/service/impl/OrderServiceImpl.java

@@ -0,0 +1,47 @@
+package space.anyi.seataOrderMicroService.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import space.anyi.seataOrderMicroService.domain.Order;
+import space.anyi.seataOrderMicroService.service.AccountService;
+import space.anyi.seataOrderMicroService.service.OrderService;
+import space.anyi.seataOrderMicroService.mapper.OrderMapper;
+import org.springframework.stereotype.Service;
+import space.anyi.seataOrderMicroService.service.StorageService;
+
+/**
+* @author 杨逸
+* @description 针对表【order】的数据库操作Service实现
+* @createDate 2025-09-21 11:22:55
+*/
+@Slf4j
+@Service
+public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
+    @Autowired
+    private AccountService accountService;
+    @Autowired
+    private StorageService storageService;
+
+    @Override
+    public void saveOrder(Order order) {
+        log.info("开始创建订单");
+        save(order);
+
+        log.info("减少金额开始");
+        accountService.reduce(order.getUserId(),order.getMoney());
+        log.info("减少金额结束");
+
+        log.info("减少库存开始");
+        storageService.reduce(order.getProductId(),order.getNums());
+        log.info("减少库存结束");
+
+        order.setStatus(0);
+        updateById(order);
+        log.info("订单创建成功");
+    }
+}
+
+
+
+

+ 20 - 0
seata_order_micro_servic/src/main/resources/mapper/OrderMapper.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="space.anyi.seataOrderMicroService.mapper.OrderMapper">
+
+    <resultMap id="BaseResultMap" type="space.anyi.seataOrderMicroService.domain.Order">
+            <id property="id" column="id" jdbcType="BIGINT"/>
+            <result property="userId" column="user_id" jdbcType="BIGINT"/>
+            <result property="productId" column="product_id" jdbcType="BIGINT"/>
+            <result property="nums" column="nums" jdbcType="INTEGER"/>
+            <result property="money" column="money" jdbcType="INTEGER"/>
+            <result property="status" column="status" jdbcType="INTEGER"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,user_id,product_id,
+        nums,money,status
+    </sql>
+</mapper>