完成用户端订单所有功能
This commit is contained in:
parent
1afbf9e420
commit
2ed7d003b2
@ -1,11 +1,14 @@
|
||||
package com.sky.controller.user;
|
||||
|
||||
import com.sky.dto.OrdersPageQueryDTO;
|
||||
import com.sky.dto.OrdersPaymentDTO;
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.OrderService;
|
||||
import com.sky.vo.OrderPaymentVO;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
import com.sky.vo.OrderVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -61,4 +64,58 @@ public class OrderController {
|
||||
orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 历史订单查询
|
||||
* @param page
|
||||
* @param pageSize
|
||||
* @param status 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/historyOrders")
|
||||
@ApiOperation("分页查询用户订单")
|
||||
public Result<PageResult> page(int page, int pageSize, Integer status) {
|
||||
log.info("分页查询订单:page={}, pageSize={}, status={}", page, pageSize, status);
|
||||
PageResult pageResult = orderService.pageQuery4User(page, pageSize, status);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情查询
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/orderDetail/{id}")
|
||||
@ApiOperation("订单详情查询")
|
||||
public Result<OrderVO> details(@PathVariable Long id) {
|
||||
log.info("订单详情查询:id={}", id);
|
||||
OrderVO orderVO = orderService.details(id);
|
||||
return Result.success(orderVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单取消
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/cancel/{id}")
|
||||
@ApiOperation("订单取消")
|
||||
public Result cancel(@PathVariable Long id) throws Exception {
|
||||
log.info("订单取消:id={}", id);
|
||||
orderService.userCancelById(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 再来一单
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/repetition/{id}")
|
||||
@ApiOperation("再来一单")
|
||||
public Result repetition(@PathVariable Long id) {
|
||||
log.info("再来一单:id={}", id);
|
||||
orderService.repetition(id);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.OrderDetail;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -12,4 +13,12 @@ public interface OrderDetailMapper {
|
||||
* @param orderDetailList
|
||||
*/
|
||||
void insertBatch(List<OrderDetail> orderDetailList);
|
||||
|
||||
/**
|
||||
* 根据订单ID查询订单明细
|
||||
* @param orderId
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from order_detail where order_id = #{orderId}")
|
||||
List<OrderDetail> getByOrderId(Long orderId);
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.dto.OrdersPageQueryDTO;
|
||||
import com.sky.entity.Orders;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
@ -25,4 +27,18 @@ public interface OrderMapper {
|
||||
*/
|
||||
void update(Orders orders);
|
||||
|
||||
/**
|
||||
* 分页查询订单信息
|
||||
* @param ordersPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);
|
||||
|
||||
/**
|
||||
* 根据id查询订单信息
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from orders where id = #{id}")
|
||||
Orders getById(Long id);
|
||||
}
|
||||
|
@ -46,4 +46,10 @@ public interface ShoppingCartMapper {
|
||||
*/
|
||||
@Delete("delete from shopping_cart where id = #{id}")
|
||||
void deleteById(Long id);
|
||||
|
||||
/**
|
||||
* 批量新增购物车商品
|
||||
* @param shoppingCartList
|
||||
*/
|
||||
void insertBatch(List<ShoppingCart> shoppingCartList);
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.OrdersPageQueryDTO;
|
||||
import com.sky.dto.OrdersPaymentDTO;
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.vo.OrderPaymentVO;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
import com.sky.vo.OrderVO;
|
||||
|
||||
public interface OrderService {
|
||||
/**
|
||||
@ -25,4 +28,33 @@ public interface OrderService {
|
||||
* @param outTradeNo
|
||||
*/
|
||||
void paySuccess(String outTradeNo);
|
||||
|
||||
/**
|
||||
* 分页查询订单
|
||||
* @param page
|
||||
* @param pageSize
|
||||
* @param status
|
||||
* @return
|
||||
*/
|
||||
PageResult pageQuery4User(int page, int pageSize, Integer status);
|
||||
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
OrderVO details(Long id);
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
* @param id
|
||||
*/
|
||||
void userCancelById(Long id) throws Exception;
|
||||
|
||||
/**
|
||||
* 再来一单
|
||||
* @param id
|
||||
*/
|
||||
void repetition(Long id);
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.dto.OrdersPageQueryDTO;
|
||||
import com.sky.dto.OrdersPaymentDTO;
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.entity.*;
|
||||
@ -10,10 +13,12 @@ import com.sky.exception.AddressBookBusinessException;
|
||||
import com.sky.exception.OrderBusinessException;
|
||||
import com.sky.exception.ShoppingCartBusinessException;
|
||||
import com.sky.mapper.*;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.OrderService;
|
||||
import com.sky.utils.WeChatPayUtil;
|
||||
import com.sky.vo.OrderPaymentVO;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
import com.sky.vo.OrderVO;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -23,6 +28,7 @@ import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
@ -66,13 +72,14 @@ public class OrderServiceImpl implements OrderService {
|
||||
//向订单插入一条数据
|
||||
Orders orders = new Orders();
|
||||
BeanUtils.copyProperties(ordersSubmitDTO, orders);
|
||||
orders.setOrderTime(LocalDateTime.now());
|
||||
orders.setPayStatus(Orders.UN_PAID);
|
||||
orders.setStatus(Orders.PENDING_PAYMENT);
|
||||
orders.setNumber(String.valueOf(System.currentTimeMillis()));
|
||||
orders.setPhone(addressBook.getPhone());
|
||||
orders.setAddress(addressBook.getDetail());
|
||||
orders.setConsignee(addressBook.getConsignee());
|
||||
orders.setNumber(String.valueOf(System.currentTimeMillis()));
|
||||
orders.setUserId(userId);
|
||||
orders.setStatus(Orders.PENDING_PAYMENT);
|
||||
orders.setPayStatus(Orders.UN_PAID);
|
||||
orders.setOrderTime(LocalDateTime.now());
|
||||
|
||||
orderMapper.insert(orders);
|
||||
|
||||
@ -148,4 +155,132 @@ public class OrderServiceImpl implements OrderService {
|
||||
orderMapper.update(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询订单列表
|
||||
* @param pageNum
|
||||
* @param pageSize
|
||||
* @param status
|
||||
* @return
|
||||
*/
|
||||
public PageResult pageQuery4User(int pageNum, int pageSize, Integer status) {
|
||||
//设置分页
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
|
||||
OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
|
||||
ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
|
||||
ordersPageQueryDTO.setStatus(status);
|
||||
|
||||
//分页条件查询
|
||||
Page<Orders> page = orderMapper.pageQuery(ordersPageQueryDTO);
|
||||
|
||||
List<OrderVO> list = new ArrayList<>();
|
||||
|
||||
//查询出订单明细,并封装如OrderVO对象进行响应
|
||||
if (page != null && page.getTotal() > 0) {
|
||||
page.forEach(orders -> {
|
||||
Long orderId = orders.getId();
|
||||
|
||||
//查询订单详情
|
||||
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orderId);
|
||||
|
||||
//封装OrderVO对象
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(orders, orderVO);
|
||||
orderVO.setOrderDetailList(orderDetailList);
|
||||
|
||||
list.add(orderVO);
|
||||
});
|
||||
}
|
||||
return new PageResult(page.getTotal(), list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public OrderVO details(Long id) {
|
||||
//根据id查询订单
|
||||
Orders orders = orderMapper.getById(id);
|
||||
|
||||
//根据订单id查询订单详情
|
||||
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orders.getId());
|
||||
|
||||
//封装OrderVO对象
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(orders, orderVO);
|
||||
orderVO.setOrderDetailList(orderDetailList);
|
||||
|
||||
return orderVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
* @param id
|
||||
*/
|
||||
public void userCancelById(Long id) throws Exception {
|
||||
//根据id查询订单
|
||||
Orders ordersDB = orderMapper.getById(id);
|
||||
|
||||
//校验订单是否存在
|
||||
if (ordersDB == null) {
|
||||
throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
|
||||
}
|
||||
|
||||
//订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
|
||||
Integer status = ordersDB.getStatus();
|
||||
if (status != Orders.PENDING_PAYMENT && status != Orders.TO_BE_CONFIRMED) {
|
||||
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||
}
|
||||
|
||||
Orders orders = new Orders();
|
||||
orders.setId(ordersDB.getId());
|
||||
|
||||
//订单处于待接单状态,需要退款
|
||||
if (status == Orders.TO_BE_CONFIRMED) {
|
||||
// //调用微信支付退款接口
|
||||
// weChatPayUtil.refund(
|
||||
// ordersDB.getNumber(), //商户订单号
|
||||
// ordersDB.getNumber(), //商户退款单号
|
||||
// new BigDecimal(0.01),//退款金额,单位 元
|
||||
// new BigDecimal(0.01));//原订单金额
|
||||
|
||||
//支付状态修改为 退款
|
||||
orders.setPayStatus(Orders.REFUND);
|
||||
}
|
||||
|
||||
//更新订单状态为 已取消
|
||||
orders.setStatus(Orders.CANCELLED);
|
||||
orders.setCancelReason("用户主动取消订单");
|
||||
orders.setCancelTime(LocalDateTime.now());
|
||||
orderMapper.update(orders);
|
||||
}
|
||||
|
||||
/**】
|
||||
* 再来一单
|
||||
* @param id
|
||||
*/
|
||||
//TODO 再来一单功能可以优化
|
||||
public void repetition(Long id) {
|
||||
//查询当前用户id
|
||||
Long userId = BaseContext.getCurrentId();
|
||||
|
||||
//根据id查询订单详情
|
||||
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(id);
|
||||
|
||||
//将订单详情对象转换为购物车对象
|
||||
List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(orderDetail -> {
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
|
||||
//将原订单详情里面的菜品信息重新复制到购物车对象中
|
||||
BeanUtils.copyProperties(orderDetail, shoppingCart, "id");
|
||||
shoppingCart.setUserId(userId);
|
||||
shoppingCart.setCreateTime(LocalDateTime.now());
|
||||
|
||||
return shoppingCart;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
//将购物车对象的菜品套餐批量插入到数据库
|
||||
shoppingCartMapper.insertBatch(shoppingCartList);
|
||||
}
|
||||
}
|
||||
|
@ -43,4 +43,28 @@
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
<select id="pageQuery" resultType="com.sky.entity.Orders">
|
||||
select * from orders
|
||||
<where>
|
||||
<if test="number != null and number!='' ">
|
||||
and number like concat('%', #{number}, '%')
|
||||
</if>
|
||||
<if test="phone != null and phone!='' ">
|
||||
and phone like concat('%', #{phone}, '%')
|
||||
</if>
|
||||
<if test="userId != null">
|
||||
and user_id = #{userId}
|
||||
</if>
|
||||
<if test="status != null">
|
||||
and status = #{status}
|
||||
</if>
|
||||
<if test="beginTime != null">
|
||||
and order_time >= #{beginTime}
|
||||
</if>
|
||||
<if test="endTime != null">
|
||||
and order_time <= #{endTime}
|
||||
</if>
|
||||
</where>
|
||||
order by order_time desc
|
||||
</select>
|
||||
</mapper>
|
||||
|
@ -2,6 +2,14 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.ShoppingCartMapper">
|
||||
<insert id="insertBatch">
|
||||
insert into shopping_cart
|
||||
(name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount, create_time)
|
||||
values
|
||||
<foreach collection="shoppingCartList" item="sc" separator=",">
|
||||
(#{sc.name},#{sc.image},#{sc.userId},#{sc.dishId},#{sc.setmealId},#{sc.dishFlavor},#{sc.number},#{sc.amount},#{sc.createTime})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
||||
<select id="list" resultType="com.sky.entity.ShoppingCart">
|
||||
|
Loading…
Reference in New Issue
Block a user