后端创建订单接口完成;删除service层interface
This commit is contained in:
@@ -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