SpringBoot15 sell02 订单模块

 

1 订单模块

  1.1 MySQL数据表

    订单模块涉及到两个数据表:

      订单表:主要存储订单相关的基本信息

DROP TABLE IF EXISTS `order_master`;
CREATE TABLE `order_master` (
  `order_id` varchar(32) NOT NULL,
  `buyer_name` varchar(32) NOT NULL COMMENT '买家名字',
  `buyer_phone` varchar(32) NOT NULL COMMENT '买家电话',
  `buyer_address` varchar(128) NOT NULL COMMENT '买家地址',
  `buyer_openid` varchar(64) NOT NULL COMMENT '买家微信openid',
  `order_amount` decimal(8,2) NOT NULL COMMENT '订单总金额',
  `order_status` tinyint(3) NOT NULL DEFAULT '0' COMMENT '订单状态, 默认为新下单',
  `pay_status` tinyint(3) NOT NULL DEFAULT '0' COMMENT '支付状态, 默认未支付',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`order_id`),
  KEY `idx_buyer_openid` (`buyer_openid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
order_master

      订单详情表:主要存放和订单相关的shaping详细信息

DROP TABLE IF EXISTS `order_detail`;
CREATE TABLE `order_detail` (
  `detail_id` varchar(32) NOT NULL,
  `order_id` varchar(32) NOT NULL,
  `product_id` varchar(32) NOT NULL,
  `product_name` varchar(64) NOT NULL COMMENT '商品名称',
  `product_price` decimal(8,2) NOT NULL COMMENT '当前价格,单位分',
  `product_quantity` int(11) NOT NULL COMMENT '数量',
  `product_icon` varchar(512) DEFAULT NULL COMMENT '小图',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`detail_id`),
  KEY `idx_order_id` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
order_detail

  1.2 实体类

    1.2.1 持久实体类

package cn.xiangxu.demo.sell_demo.entity.entityPO;

import com.baomidou.mybatisplus.annotations.TableField;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 王杨帅
 * @create 2018-04-21 13:33
 * @desc 订单实体类
 **/
@Table(name = "order_master")
@Data
@Entity
public class OrderMasterPO {

    /**
     * 订单ID
     */
    @Id
    @Column(name = "order_id")
    private String orderId;

    /**
     * 买家名字
     */
    @Column(name = "buyer_name")
    private String buyerName;
    /**
     * 买家电话
     */
    @Column(name = "buyer_phone")
    private String buyerPhone;
    /**
     * 买家地址
     */
    @Column(name = "buyer_address")
    private String buyerAddress;
    /**
     * 买家微信openid
     */
    @Column(name = "buyer_openid")
    private String buyerOpenid;
    /**
     * 订单总金额
     */
    @Column(name = "order_amount")
    private BigDecimal orderAmount;
    /**
     * 订单状态, 默认为新下单
     */
    @Column(name = "order_status")
    private Integer orderStatus;
    /**
     * 支付状态, 默认未支付
     */
    @Column(name = "pay_status")
    private Integer payStatus;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;
    /**
     * 修改时间
     */
    @Column(name = "update_time")
    private Date updateTime;
}
OrderMasterPO.java
package cn.xiangxu.demo.sell_demo.entity.entityPO;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 王杨帅
 * @create 2018-04-21 13:35
 * @desc 订单详情实体类
 **/
@Table(name = "order_detail")
@Data
@Entity
public class OrderDetailPO {

    /**
     * 订单详情ID
     */
    @Id
    @Column(name = "detail_id")
    private String detailId;
    /**
     * 订单ID
     */
    @Column(name = "order_id")
    private String orderId;
    /**
     * 产品ID
     */
    @Column(name = "product_id")
    private String productId;
    /**
     * 商品名称
     */
    @Column(name = "product_name")
    private String productName;
    /**
     * 当前价格,单位分
     */
    @Column(name = "product_price")
    private BigDecimal productPrice;
    /**
     * 数量
     */
    @Column(name = "product_quantity")
    private Integer productQuantity;
    /**
     * 小图
     */
    @Column(name = "product_icon")
    private String productIcon;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;
    /**
     * 修改时间
     */
    @Column(name = "update_time")
    private Date updateTime;
}
OrderDetailPO.java

    1.2.2 传输实体类

package cn.xiangxu.demo.sell_demo.entity.entityDTO;

import cn.xiangxu.demo.sell_demo.entity.entityPO.OrderDetailPO;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 商品订单和订单详情组合成的数据传输对象
 */
@Data
//@JsonInclude(JsonInclude.Include.NON_NULL) // 数据为null时就不返回
public class OrderDTO {
    /** 订单ID */
    private String orderId;
    /** 买家名字 */
    private String buyerName;
    /** 买家电话 */
    private String buyerPhone;
    /** 买家地址 */
    private String buyerAddress;
    /** 买家微信OpenId */
    private String buyerOpenid;
    /** 订单金额 */
    private BigDecimal orderAmount;
    /** 订单状态,默认为0:新下单 */
    private Integer orderStatus;
    /** 支付状态,默认为0:未支付 */
    private Integer payStatus;
    /** 创建时间 */
//    @JsonSerialize(using = Date2Long.class)  // 让毫秒变成秒
    private Date createTime;
    /** 更新时间 */
//    @JsonSerialize(using = Date2Long.class)
    private Date updateTime;
    /** 订单商品列表 */
    List<OrderDetailPO> orderDetailPOList;
}
OrderDTO.java
package cn.xiangxu.demo.sell_demo.entity.entityDTO;

import lombok.Data;

@Data
public class CarDTO {

    /** 商品ID */
    private String productId;
    /** 商品数量 */
    private Integer productQuantity;

    public CarDTO() {
    }

    public CarDTO(String productId, Integer productQuantity) {
        this.productId = productId;
        this.productQuantity = productQuantity;
    }
}
CarDTO.java

  1.3 持久层

    1.3.1 订单-master

package cn.xiangxu.demo.sell_demo.dao;

import cn.xiangxu.demo.sell_demo.entity.entityPO.OrderMasterPO;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author 王杨帅
 * @create 2018-04-21 13:42
 * @desc 订单持久层接口
 **/
public interface OrderMasterDao extends JpaRepository<OrderMasterPO, String> {

    /**
     * 根据买家的微信OpenId查询订单信息
     * @param openId
     * @return
     */
    List<OrderMasterPO> findByBuyerOpenid(String openId);
}
OrderMasterDao.java
package cn.xiangxu.demo.sell_demo.dao;

import cn.xiangxu.demo.sell_demo.entity.entityPO.OrderMasterPO;
import cn.xiangxu.demo.sell_demo.enums.OrderStatusEnum;
import cn.xiangxu.demo.sell_demo.enums.PayStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class OrderMasterDaoTest {

    private final String className = getClass().getName();

    @Autowired
    private OrderMasterDao orderMasterDao;

    @Test
    public void create() {

        // 01 构建订单对象
        OrderMasterPO orderMasterPO = new OrderMasterPO();
        orderMasterPO.setOrderId("20180001");
        orderMasterPO.setBuyerName("王杨帅");
        orderMasterPO.setBuyerPhone("13272885616");
        orderMasterPO.setBuyerAddress("广东工业大学");
        orderMasterPO.setBuyerOpenid("123456");
        orderMasterPO.setOrderAmount(new BigDecimal(333));
        orderMasterPO.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMasterPO.setPayStatus(PayStatusEnum.WAIT.getCode());

        // 02 调用持久层对象进行新增操作
        OrderMasterPO result = orderMasterDao.save(orderMasterPO);

        // 03 打印新增结果数据
        log.info("===/" + className + "/create===更新后返回的结果为:{}", result);
        Assert.assertNotEquals(null, result);

    }

    @Test
    public void findByBuyerOpenid() throws Exception {
        // 01 模拟一个微信openId
        String openId = "123456";

        // 02 根据微信OpenId查询数据
        List<OrderMasterPO> result = orderMasterDao.findByBuyerOpenid(openId);

        // 03 打印查询到的数据
        log.info("===/" + className + "/findByBuyerOpenid===根据微信openId查询到的数据为:{}", result);
    }

}
OrderMasterDaoTest.java

    1.3.2 订单-detail

package cn.xiangxu.demo.sell_demo.dao;

import cn.xiangxu.demo.sell_demo.entity.entityPO.OrderDetailPO;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author 王杨帅
 * @create 2018-04-21 13:41
 * @desc 订单详情持久层接口
 **/
public interface OrderDetailDao extends JpaRepository<OrderDetailPO, String> {

    /** 根据订单ID查询订单详情信息 */
    List<OrderDetailPO> findByOrOrderId(String orderId);
}
OrderDetailDao.java
package cn.xiangxu.demo.sell_demo.dao;

import cn.xiangxu.demo.sell_demo.entity.entityPO.OrderDetailPO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class OrderDetailDaoTest {

    private final String className = getClass().getName();

    @Autowired
    private OrderDetailDao orderDetailDao;

    @Test
    public void create() {
        // 01 模拟一个订单详情实例
        OrderDetailPO orderDetailPO = new OrderDetailPO();
        orderDetailPO.setDetailId("2");
        orderDetailPO.setOrderId("20180001");
        orderDetailPO.setProductId("20180001");
        orderDetailPO.setProductName("皮蛋粥");
        orderDetailPO.setProductPrice(new BigDecimal(3.5));
        orderDetailPO.setProductQuantity(3);
        orderDetailPO.setProductIcon("http://xxasd.com");

        // 02 执行新增操作
        OrderDetailPO result = orderDetailDao.save(orderDetailPO);

        // 03 返回结果
        log.info("===/" + className + "/create===新增后的返回数据为:{}", result);
        Assert.assertNotEquals(null, result);
    }

    @Test
    public void findByOrOrderId() throws Exception {
        // 模拟一个订单ID
        String orderId = "20180001";

        // 根据订单ID进行查询
        List<OrderDetailPO> result = orderDetailDao.findByOrOrderId(orderId);

        // 记录查询结果
        log.info("===/" + className + "/findByOrderId===根据订单ID查询到的数据为:{}", result);
        Assert.assertNotEquals(0, result.size());
    }

}
OrderDetailDaoTest.java

 

  

      

 

posted @ 2018-04-23 20:29  寻渝记  阅读(426)  评论(0编辑  收藏  举报