后端创建订单接口完成;删除service层interface
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.cxyxiaomo.epp.common.enums;
|
||||
|
||||
public enum OrderStatus {
|
||||
PENDING("Pending"),
|
||||
PROCESSING("Processing"),
|
||||
SHIPPED("Shipped"),
|
||||
DELIVERED("Delivered"),
|
||||
CANCELLED("Cancelled");
|
||||
|
||||
private final String value;
|
||||
|
||||
private OrderStatus(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ public class Good {
|
||||
String gallery;
|
||||
String brief;
|
||||
Boolean isOnSale;
|
||||
Integer sortOrder;
|
||||
String picUrl;
|
||||
Integer type;
|
||||
String unit;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.cxyxiaomo.epp.common.pojo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.enums.OrderStatus;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
public class Order {
|
||||
private Long id;
|
||||
private Integer userId;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.cxyxiaomo.epp.common.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
public class OrderDetail {
|
||||
private Long id;
|
||||
private Long orderId;
|
||||
private Long goodId;
|
||||
private Integer goodCount;
|
||||
private Double unitPrice;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.cxyxiaomo.epp.common.utils;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class SnowflakeManager {
|
||||
private static final long EPOCH_STAMP = 1262275200000L;
|
||||
private static final long SEQUENCE_BIT = 12L;
|
||||
private static final long MACHINE_BIT = 5L;
|
||||
private static final long DATA_CENTER_BIT = 5L;
|
||||
private static final long MAX_SEQUENCE_NUM = -1L ^ (-1L << SEQUENCE_BIT);
|
||||
private static final long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
|
||||
private static final long MAX_DATA_CENTER_NUM = -1L ^ (-1L << DATA_CENTER_BIT);
|
||||
private static final long MACHINE_LEFT = SEQUENCE_BIT;
|
||||
private static final long DATA_CENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
|
||||
private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT + DATA_CENTER_BIT;
|
||||
private final long machineId;
|
||||
private final long dataCenterId;
|
||||
private long sequence = 0L;
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
public SnowflakeManager(long machineId, long dataCenterId) {
|
||||
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
|
||||
throw new IllegalArgumentException(String.format("machine id can't be greater than %d or less than 0", MAX_MACHINE_NUM));
|
||||
}
|
||||
if (dataCenterId > MAX_DATA_CENTER_NUM || dataCenterId < 0) {
|
||||
throw new IllegalArgumentException(String.format("data center id can't be greater than %d or less than 0", MAX_DATA_CENTER_NUM));
|
||||
}
|
||||
this.machineId = machineId;
|
||||
this.dataCenterId = dataCenterId;
|
||||
}
|
||||
|
||||
public synchronized long nextValue() throws Exception {
|
||||
String os = System.getProperty("os.name");
|
||||
SecureRandom secureRandom;
|
||||
if (os.toLowerCase().startsWith("win")) {
|
||||
// windows机器用
|
||||
secureRandom = new SecureRandom(); // SecureRandom.getInstanceStrong();
|
||||
} else {
|
||||
// linux机器用
|
||||
secureRandom = SecureRandom.getInstance("NativePRNGNonBlocking");
|
||||
}
|
||||
//SecureRandom secureRandom = SecureRandom.getInstanceStrong();
|
||||
long currentTimeMillis = this.currentTimeMillis();
|
||||
if(currentTimeMillis < this.lastTimestamp) {
|
||||
throw new Exception(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", (this.lastTimestamp-currentTimeMillis)));
|
||||
}
|
||||
|
||||
if(this.lastTimestamp == currentTimeMillis) {
|
||||
this.sequence = (this.sequence+1) & MAX_SEQUENCE_NUM;
|
||||
if (this.sequence == 0) {
|
||||
this.sequence = secureRandom.nextInt(Long.valueOf(SEQUENCE_BIT).intValue());
|
||||
currentTimeMillis = this.tilNextMillis(this.lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
this.sequence = secureRandom.nextInt(Long.valueOf(SEQUENCE_BIT).intValue());
|
||||
}
|
||||
this.lastTimestamp = currentTimeMillis;
|
||||
|
||||
// 64 Bit ID (42(Millis)+5(Data Center ID)+5(Machine ID)+12(Repeat Sequence Summation))
|
||||
long nextId = ((currentTimeMillis-EPOCH_STAMP) << TIMESTAMP_LEFT)
|
||||
| (this.dataCenterId << DATA_CENTER_LEFT)
|
||||
| (this.machineId << MACHINE_LEFT)
|
||||
| this.sequence;
|
||||
|
||||
return nextId;
|
||||
}
|
||||
|
||||
private long tilNextMillis(long lastTimestamp) {
|
||||
long currentTimeMillis = this.currentTimeMillis();
|
||||
while (currentTimeMillis <= lastTimestamp) {
|
||||
currentTimeMillis = this.currentTimeMillis();
|
||||
}
|
||||
return currentTimeMillis;
|
||||
}
|
||||
|
||||
private long currentTimeMillis() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SnowflakeManager snowflakeManager = new SnowflakeManager(0L,0L);
|
||||
long l = snowflakeManager.nextValue();
|
||||
System.out.println(l);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ public class GoodVO implements Serializable {
|
||||
String gallery;
|
||||
String brief;
|
||||
Boolean isOnSale;
|
||||
Integer sortOrder;
|
||||
String picUrl;
|
||||
Integer type;
|
||||
String unit;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.cxyxiaomo.epp.common.vo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// 数据库关系映射
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class OrderDetailVO implements Serializable {
|
||||
private Long id;
|
||||
private Long orderId;
|
||||
private Long goodId;
|
||||
private Integer goodCount;
|
||||
private Double unitPrice;
|
||||
|
||||
public static OrderDetailVO convertFrom(OrderDetail orderDetail) {
|
||||
if (orderDetail == null) {
|
||||
return null;
|
||||
}
|
||||
OrderDetailVO orderDetailVO = new OrderDetailVO();
|
||||
// BeanUtils.copyProperties(orderDetail, orderDetailVO);
|
||||
return orderDetailVO;
|
||||
}
|
||||
|
||||
public static List<OrderDetailVO> convertFrom(List<OrderDetail> orderDetailList) {
|
||||
if (orderDetailList == null) {
|
||||
return null;
|
||||
}
|
||||
return orderDetailList.stream().map(OrderDetailVO::convertFrom).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.cxyxiaomo.epp.common.vo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.enums.OrderStatus;
|
||||
import com.cxyxiaomo.epp.common.pojo.Order;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// 数据库关系映射
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class OrderVO implements Serializable {
|
||||
private Long id;
|
||||
private Integer userId;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
|
||||
public static OrderVO convertFrom(Order order) {
|
||||
if (order == null) {
|
||||
return null;
|
||||
}
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(order, orderVO);
|
||||
return orderVO;
|
||||
}
|
||||
|
||||
public static List<OrderVO> convertFrom(List<Order> orderList) {
|
||||
if (orderList == null) {
|
||||
return null;
|
||||
}
|
||||
List<OrderVO> goodsOrdersVOList = orderList.stream().map(OrderVO::convertFrom).collect(Collectors.toList());
|
||||
return goodsOrdersVOList;
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.cxyxiaomo.epp.shop.controller;
|
||||
import com.cxyxiaomo.epp.common.response.Res;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
import com.cxyxiaomo.epp.shop.service.GoodsServiceImpl;
|
||||
import com.cxyxiaomo.epp.shop.service.GoodsService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -17,7 +17,7 @@ import java.util.List;
|
||||
public class GoodsController {
|
||||
|
||||
@Resource
|
||||
GoodsServiceImpl goodsService;
|
||||
GoodsService goodsService;
|
||||
|
||||
/**
|
||||
* 小程序端商品列表
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.cxyxiaomo.epp.shop.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import com.cxyxiaomo.epp.common.response.Res;
|
||||
import com.cxyxiaomo.epp.shop.service.OrderService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/shop/order")
|
||||
public class OrderController {
|
||||
|
||||
@Resource
|
||||
OrderService orderService;
|
||||
|
||||
/**
|
||||
* 小程序端创建订单
|
||||
*
|
||||
* @param userId 下单用户
|
||||
* @param orderList 下单商品 [ { goodId, count }, ... ]
|
||||
* @param orderList 下单商品 [ { goodId, count }, ... ]
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/miniprogram/createOrder")
|
||||
@ResponseBody
|
||||
public Res createOrder(@RequestBody JSONObject params) {
|
||||
Integer userId = params.getInteger("userId");
|
||||
JSONArray orderList = params.getJSONArray("orderList");
|
||||
if (userId == null || orderList == null) {
|
||||
return Res.error("参数错误");
|
||||
}
|
||||
|
||||
LinkedList<OrderDetail> orderDetailList = new LinkedList<>();
|
||||
for (Object o : orderList) {
|
||||
if (o == null || o instanceof JSONObject) {
|
||||
return Res.error("参数错误");
|
||||
}
|
||||
JSONObject order = new JSONObject((Map) o);
|
||||
Long goodId = order.getLong("goodId");
|
||||
Integer count = order.getInteger("count");
|
||||
if (goodId == null || goodId < 1 || count == null || count < 1) {
|
||||
return Res.error("参数错误");
|
||||
}
|
||||
OrderDetail orderDetail = new OrderDetail();
|
||||
orderDetail.setGoodId(goodId);
|
||||
orderDetail.setGoodCount(count);
|
||||
orderDetailList.add(orderDetail);
|
||||
}
|
||||
|
||||
if (orderDetailList.size() == 0) {
|
||||
return Res.error("订单中不包含商品,不能创建空订单");
|
||||
}
|
||||
|
||||
try {
|
||||
long orderId = orderService.createOrder(userId, orderDetailList);
|
||||
return Res.success(orderId);
|
||||
} catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
if (message.contains("Exception")) {
|
||||
// 内部异常不打印出去
|
||||
return Res.error("创建订单失败,请重试");
|
||||
} else {
|
||||
return Res.error(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,4 +14,12 @@ public interface GoodsDao {
|
||||
List<Good> list(@Param("cateId") Integer cateId, @Param("searchText") String searchText);
|
||||
|
||||
Good getById(Long id);
|
||||
|
||||
Good selectById(Long id);
|
||||
|
||||
Integer insert(Good good);
|
||||
|
||||
Integer update(Good good);
|
||||
|
||||
Integer deleteById(Integer id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.cxyxiaomo.epp.shop.dao;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Order;
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface OrderDao {
|
||||
|
||||
/**
|
||||
* 根据订单 ID 查询订单信息
|
||||
*
|
||||
* @param orderId
|
||||
* @return
|
||||
*/
|
||||
Order getOrderById(Integer orderId);
|
||||
|
||||
/**
|
||||
* 根据用户 ID 查询订单列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<Order> listOrdersByUserId(Integer userId);
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
*
|
||||
* @param order
|
||||
* @return
|
||||
*/
|
||||
int insertOrder(Order order);
|
||||
|
||||
/**
|
||||
* 批量插入订单详情
|
||||
*
|
||||
* @param details
|
||||
* @return
|
||||
*/
|
||||
int batchInsertOrderDetail(List<OrderDetail> details);
|
||||
|
||||
// 更新订单状态
|
||||
int updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") String status);
|
||||
|
||||
// 根据订单 ID 删除订单信息及订单详情信息
|
||||
int deleteOrderById(Integer orderId);
|
||||
}
|
||||
@@ -1,15 +1,40 @@
|
||||
package com.cxyxiaomo.epp.shop.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Good;
|
||||
import com.cxyxiaomo.epp.common.pojo.GoodCategory;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsCategoryDao;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
public interface GoodsService {
|
||||
@Service
|
||||
public class GoodsService {
|
||||
|
||||
List<GoodVO> list(Integer cateId, String searchText);
|
||||
@Resource
|
||||
GoodsDao goodsDao;
|
||||
|
||||
GoodVO getById(Long id);
|
||||
@Resource
|
||||
GoodsCategoryDao goodsCategoryDao;
|
||||
|
||||
List<GoodCategoryVO> cateList();
|
||||
public List<GoodVO> list(Integer cateId, String searchText) {
|
||||
List<Good> list = goodsDao.list(cateId, searchText);
|
||||
List<GoodVO> goodVOS = GoodVO.convertFrom(list);
|
||||
return goodVOS;
|
||||
}
|
||||
|
||||
public GoodVO getById(Long id) {
|
||||
Good good = goodsDao.getById(id);
|
||||
GoodVO goodVO = GoodVO.convertFrom(good);
|
||||
return goodVO;
|
||||
}
|
||||
|
||||
public List<GoodCategoryVO> cateList() {
|
||||
List<GoodCategory> list = goodsCategoryDao.list();
|
||||
List<GoodCategoryVO> goodCategoryVOList = GoodCategoryVO.convertFrom(list);
|
||||
return goodCategoryVOList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.cxyxiaomo.epp.shop.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.pojo.Good;
|
||||
import com.cxyxiaomo.epp.common.pojo.GoodCategory;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsCategoryDao;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GoodsServiceImpl implements GoodsService {
|
||||
|
||||
@Resource
|
||||
GoodsDao goodsDao;
|
||||
|
||||
@Resource
|
||||
GoodsCategoryDao goodsCategoryDao;
|
||||
|
||||
@Override
|
||||
public List<GoodVO> list(Integer cateId, String searchText) {
|
||||
List<Good> list = goodsDao.list(cateId, searchText);
|
||||
List<GoodVO> goodVOS = GoodVO.convertFrom(list);
|
||||
return goodVOS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GoodVO getById(Long id) {
|
||||
Good good = goodsDao.getById(id);
|
||||
GoodVO goodVO = GoodVO.convertFrom(good);
|
||||
return goodVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GoodCategoryVO> cateList() {
|
||||
List<GoodCategory> list = goodsCategoryDao.list();
|
||||
List<GoodCategoryVO> goodCategoryVOList = GoodCategoryVO.convertFrom(list);
|
||||
return goodCategoryVOList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.cxyxiaomo.epp.shop.service;
|
||||
|
||||
import com.cxyxiaomo.epp.common.enums.OrderStatus;
|
||||
import com.cxyxiaomo.epp.common.pojo.Good;
|
||||
import com.cxyxiaomo.epp.common.pojo.Order;
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import com.cxyxiaomo.epp.common.utils.SnowflakeManager;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsDao;
|
||||
import com.cxyxiaomo.epp.shop.dao.OrderDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
|
||||
@Resource
|
||||
private GoodsDao goodsDao;
|
||||
|
||||
@Resource
|
||||
private OrderDao orderDao;
|
||||
|
||||
/**
|
||||
* 创建订单,返回订单编号
|
||||
*
|
||||
* @param userId
|
||||
* @param orderItemList
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public long createOrder(int userId, List<OrderDetail> orderItemList) throws Exception {
|
||||
// 创建订单编号
|
||||
SnowflakeManager snowflakeManager = new SnowflakeManager(1L, 1L);
|
||||
long orderId = snowflakeManager.nextValue();
|
||||
|
||||
// 创建订单
|
||||
Order order = new Order();
|
||||
order.setId(orderId);
|
||||
order.setUserId(userId);
|
||||
order.setOrderDate(LocalDateTime.now());
|
||||
order.setOrderStatus(OrderStatus.PENDING);
|
||||
orderDao.insertOrder(order);
|
||||
|
||||
// 创建订单详情
|
||||
for (OrderDetail orderItem : orderItemList) {
|
||||
if (orderItem == null || orderItem.getGoodId() == null || orderItem.getGoodCount() == null) {
|
||||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||||
throw new IllegalArgumentException("商品不存在");
|
||||
}
|
||||
|
||||
// 查询商品价格
|
||||
Good good = goodsDao.selectById(orderItem.getGoodId());
|
||||
if (good == null || good.getDeleted() || good.getRetailPrice() == null) {
|
||||
throw new IllegalArgumentException("商品已删除或状态异常,无法下单");
|
||||
}
|
||||
Double retailPrice = good.getRetailPrice();
|
||||
|
||||
// 手动塞入信息
|
||||
orderItem.setId(null);
|
||||
orderItem.setOrderId(orderId);
|
||||
orderItem.setUnitPrice(retailPrice);
|
||||
}
|
||||
orderDao.batchInsertOrderDetail(orderItemList);
|
||||
return orderId;
|
||||
}
|
||||
}
|
||||
@@ -21,3 +21,8 @@ spring:
|
||||
url: jdbc:mysql://127.0.0.1:3306/epp?useUnicode=true&characterEncoding=utf8&useSSL=false
|
||||
username: root
|
||||
password: root
|
||||
|
||||
logging:
|
||||
level:
|
||||
org.mybatis.spring.SqlSessionFactoryBean: DEBUG
|
||||
# org.mybatis.spring.SqlSessionFactoryBean: TRACE
|
||||
|
||||
@@ -3,6 +3,24 @@
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cxyxiaomo.epp.shop.dao.GoodsDao">
|
||||
<resultMap id="GoodsResultMap" type="com.cxyxiaomo.epp.common.pojo.Good">
|
||||
<id column="id" property="id" />
|
||||
<result column="goods_name" property="goodsName" />
|
||||
<result column="category_id" property="categoryId" />
|
||||
<result column="brand" property="brand" />
|
||||
<result column="gallery" property="gallery" />
|
||||
<result column="brief" property="brief" />
|
||||
<result column="is_on_sale" property="isOnSale" />
|
||||
<result column="sort_order" property="sortOrder" />
|
||||
<result column="pic_url" property="picUrl" />
|
||||
<result column="type" property="type" />
|
||||
<result column="unit" property="unit" />
|
||||
<result column="counter_price" property="counterPrice" />
|
||||
<result column="retail_price" property="retailPrice" />
|
||||
<result column="detail" property="detail" />
|
||||
<result column="deleted" property="deleted" />
|
||||
</resultMap>
|
||||
|
||||
<!--<insert id="insert" parameterType="com.cxyxiaomo.epp.common.pojo.Good">-->
|
||||
<!-- INSERT INTO report (`user_id`, `name`, `address`, `time`, `temperature`)-->
|
||||
<!-- VALUES (#{userId}, #{name}, #{address}, #{time}, #{temperature})-->
|
||||
@@ -25,4 +43,50 @@
|
||||
WHERE id = #{id}
|
||||
order by `sort_order` asc
|
||||
</select>
|
||||
|
||||
<select id="selectById" resultMap="GoodsResultMap">
|
||||
SELECT *
|
||||
FROM goods
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
<!--<select id="selectByCategoryId" resultMap="GoodsResultMap">-->
|
||||
<!-- SELECT *-->
|
||||
<!-- FROM goods-->
|
||||
<!-- WHERE category_id = #{categoryId}-->
|
||||
<!--</select>-->
|
||||
|
||||
<!--<select id="selectAll" resultMap="GoodsResultMap">-->
|
||||
<!-- SELECT *-->
|
||||
<!-- FROM goods-->
|
||||
<!--</select>-->
|
||||
|
||||
<insert id="insert" parameterType="com.cxyxiaomo.epp.common.pojo.Good" useGeneratedKeys="true" keyProperty="id">
|
||||
INSERT INTO goods (goods_name, category_id, brand, gallery, brief, is_on_sale, sort_order, pic_url, type, unit, counter_price, retail_price, detail, deleted)
|
||||
VALUES (#{goodsName}, #{categoryId}, #{brand}, #{gallery}, #{brief}, #{isOnSale}, #{sortOrder}, #{picUrl}, #{type}, #{unit}, #{counterPrice}, #{retailPrice}, #{detail}, #{deleted})
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="com.cxyxiaomo.epp.common.pojo.Good">
|
||||
UPDATE goods
|
||||
SET goods_name = #{goodsName},
|
||||
category_id = #{categoryId},
|
||||
brand = #{brand},
|
||||
gallery = #{gallery},
|
||||
brief = #{brief},
|
||||
is_on_sale = #{isOnSale},
|
||||
sort_order = #{sortOrder},
|
||||
pic_url = #{picUrl},
|
||||
type = #{type},
|
||||
unit = #{unit},
|
||||
counter_price = #{counterPrice},
|
||||
retail_price = #{retailPrice},
|
||||
detail = #{detail},
|
||||
deleted = #{deleted}
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
<delete id="deleteById">
|
||||
DELETE FROM goods
|
||||
WHERE id = #{id}
|
||||
</delete>
|
||||
</mapper>
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cxyxiaomo.epp.shop.dao.OrderDao">
|
||||
|
||||
<resultMap id="OrderResultMap" type="com.cxyxiaomo.epp.common.pojo.Order">
|
||||
<id column="id" property="id"/>
|
||||
<result column="user_id" property="userId"/>
|
||||
<result column="order_date" property="orderDate"/>
|
||||
<result column="order_status" property="orderStatus"/>
|
||||
</resultMap>
|
||||
<resultMap id="OrderDetailResultMap" type="com.cxyxiaomo.epp.common.pojo.OrderDetail">
|
||||
<id property="id" column="id" />
|
||||
<result property="orderId" column="order_id" />
|
||||
<result property="goodId" column="good_id" />
|
||||
<result property="goodCount" column="good_count" />
|
||||
<result property="unitPrice" column="unit_price" />
|
||||
</resultMap>
|
||||
|
||||
<!-- 根据订单 ID 查询订单信息 -->
|
||||
<select id="getOrderById" resultMap="OrderResultMap">
|
||||
SELECT * FROM order WHERE order_id=#{orderId}
|
||||
</select>
|
||||
|
||||
<!-- 根据用户 ID 查询订单列表 -->
|
||||
<select id="listOrdersByUserId" resultType="com.cxyxiaomo.epp.common.pojo.Order">
|
||||
SELECT *
|
||||
FROM order
|
||||
WHERE user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<!-- 创建订单 -->
|
||||
<insert id="insertOrder" parameterType="com.cxyxiaomo.epp.common.pojo.Order" useGeneratedKeys="true" keyProperty="orderId">
|
||||
INSERT INTO `order` (id, user_id, order_date, order_status) VALUES
|
||||
(#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus})
|
||||
</insert>
|
||||
|
||||
<!-- 批量插入订单详情 -->
|
||||
<insert id="batchInsertOrderDetail" parameterType="java.util.List">
|
||||
INSERT INTO order_detail (order_id, good_id, good_count, unit_price)
|
||||
VALUES
|
||||
<if test="list != null and list.size() > 0">
|
||||
<foreach collection="list" item="detail" separator=",">
|
||||
(#{detail.orderId}, #{detail.goodId}, #{detail.goodCount}, #{detail.unitPrice})
|
||||
</foreach>
|
||||
</if>
|
||||
<if test="list == null or list.size() == 0">
|
||||
()
|
||||
</if>
|
||||
</insert>
|
||||
|
||||
<update id="updateOrderStatus" parameterType="com.cxyxiaomo.epp.common.pojo.Order">
|
||||
UPDATE order SET order_status=#{orderStatus} WHERE order_id=#{orderId}
|
||||
</update>
|
||||
|
||||
<!-- 根据订单 ID 删除订单信息及订单详情信息 -->
|
||||
<delete id="deleteOrderById" parameterType="java.lang.Integer">
|
||||
DELETE FROM order WHERE order_id = #{orderId};
|
||||
DELETE FROM goods_order_details WHERE order_id = #{orderId};
|
||||
</delete>
|
||||
</mapper>
|
||||
@@ -9,5 +9,9 @@
|
||||
|
||||
<!--下划线转小驼峰-->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
|
||||
<!-- 打印 SQL 日志 -->
|
||||
<!-- 设置logImpl为STDOUT_LOGGING,表示使用标准输出打印SQL日志 -->
|
||||
<setting name="logImpl" value="STDOUT_LOGGING"/>
|
||||
</settings>
|
||||
</configuration>
|
||||
|
||||
Reference in New Issue
Block a user