2025.4.24 下单 支付
This commit is contained in:
parent
1497a29c75
commit
30d33fcd89
@ -10,82 +10,82 @@ import com.sky.vo.DishVO;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RestController("adminDishController")
|
||||
@RequestMapping("/admin/dish")
|
||||
@Slf4j
|
||||
public class DishController {
|
||||
|
||||
@Autowired
|
||||
private DishService dishService;
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation("新增菜品")
|
||||
public Result addDish(@RequestBody DishDTO dishDTO){
|
||||
log.info("新增菜品");
|
||||
// 新增后清除当前分类的缓存 ,注意这里缓存的是菜品分类,每个分类中包括多个菜品
|
||||
@CacheEvict(value = "dishCache", key = "'dish_' + #dishDTO.categoryId")
|
||||
public Result<Void> save(@RequestBody DishDTO dishDTO){
|
||||
log.info("新增菜品: {}", dishDTO);
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
|
||||
//清理缓存数据
|
||||
String key = "dish_" + dishDTO.getCategoryId();
|
||||
cleanCache(key);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("菜品分类查询")
|
||||
@ApiOperation("菜品分页查询")
|
||||
public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
|
||||
PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
|
||||
PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
@DeleteMapping
|
||||
@ApiOperation("删除菜品")
|
||||
public Result deleteDish(@RequestParam List<Long> ids){
|
||||
log.info("批量删除菜品");
|
||||
dishService.deleteBatch(ids);
|
||||
|
||||
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||
cleanCache("dish_*");
|
||||
@DeleteMapping
|
||||
@ApiOperation("菜品批量删除")
|
||||
// 删除后清除所有分类下的菜品缓存
|
||||
@CacheEvict(value = "dishCache", allEntries = true)
|
||||
public Result<Void> delete(@RequestParam List<Long> ids){
|
||||
log.info("菜品批量删除:{}", ids);
|
||||
dishService.deleteBatch(ids);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ApiOperation("根据id查询菜品")
|
||||
@ApiOperation("根据id查询菜品及口味信息")
|
||||
public Result<DishVO> queryByIdWithFlavor(@PathVariable Long id){
|
||||
DishVO dishVO=dishService.getByIdWithFlavor(id);
|
||||
DishVO dishVO = dishService.getByIdWithFlavor(id);
|
||||
return Result.success(dishVO);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@ApiOperation("更新菜品")
|
||||
public Result update(@RequestBody DishDTO dishDTO){
|
||||
// 更新后清除所有分类下的菜品缓存,保证下一次查询是最新数据
|
||||
@CacheEvict(value = "dishCache", allEntries = true)
|
||||
public Result<Void> update(@RequestBody DishDTO dishDTO){
|
||||
log.info("更新菜品:{}", dishDTO);
|
||||
dishService.updateWithFlavor(dishDTO);
|
||||
|
||||
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||
cleanCache("dish_*");
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@PostMapping("/status/{status}")
|
||||
@ApiOperation("菜品起售/停售")
|
||||
public Result startOrStop(@PathVariable("status") Integer status, Long id){
|
||||
dishService.startOrStop(status,id);
|
||||
|
||||
//将所有的菜品缓存数据清理掉,所有以dish_开头的key
|
||||
cleanCache("dish_*");
|
||||
// 改变状态后也清空所有分类缓存
|
||||
@CacheEvict(value = "dishCache", allEntries = true)
|
||||
public Result<Void> startOrStop(@PathVariable("status") Integer status,
|
||||
@RequestParam Long id){
|
||||
dishService.startOrStop(status, id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询菜品")
|
||||
public Result<List<Dish>> list(Long categoryId) {
|
||||
@Cacheable(value = "dishCache", key = "'dish_' + #categoryId")
|
||||
public Result<List<Dish>> list(@RequestParam("categoryId") Long categoryId) {
|
||||
// Spring Cache 会自动:
|
||||
// 1. 先查缓存 dishCache::dish_{categoryId}
|
||||
// 2. 没有才执行方法体,并把返回值缓存
|
||||
List<Dish> list = dishService.list(categoryId);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
private void cleanCache(String pattern){
|
||||
Set keys = redisTemplate.keys(pattern);
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,127 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
import com.sky.dto.OrdersCancelDTO;
|
||||
import com.sky.dto.OrdersConfirmDTO;
|
||||
import com.sky.dto.OrdersPageQueryDTO;
|
||||
import com.sky.dto.OrdersRejectionDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.OrderService;
|
||||
import com.sky.vo.OrderStatisticsVO;
|
||||
import com.sky.vo.OrderVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 订单管理
|
||||
*/
|
||||
@RestController("adminOrderController")
|
||||
@RequestMapping("/admin/order")
|
||||
@Slf4j
|
||||
@Api(tags = "订单管理接口")
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
/**
|
||||
* 订单搜索
|
||||
*
|
||||
* @param ordersPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/conditionSearch")
|
||||
@ApiOperation("订单搜索")
|
||||
public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
|
||||
PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 各个状态的订单数量统计
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/statistics")
|
||||
@ApiOperation("各个状态的订单数量统计")
|
||||
public Result<OrderStatisticsVO> statistics() {
|
||||
OrderStatisticsVO orderStatisticsVO = orderService.statistics();
|
||||
return Result.success(orderStatisticsVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/details/{id}")
|
||||
@ApiOperation("查询订单详情")
|
||||
public Result<OrderVO> details(@PathVariable("id") Long id) {
|
||||
OrderVO orderVO = orderService.details(id);
|
||||
return Result.success(orderVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/confirm")
|
||||
@ApiOperation("接单")
|
||||
public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
|
||||
orderService.confirm(ordersConfirmDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 拒单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/rejection")
|
||||
@ApiOperation("拒单")
|
||||
public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
|
||||
orderService.rejection(ordersRejectionDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/cancel")
|
||||
@ApiOperation("取消订单")
|
||||
public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
|
||||
orderService.cancel(ordersCancelDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 派送订单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/delivery/{id}")
|
||||
@ApiOperation("派送订单")
|
||||
public Result delivery(@PathVariable("id") Long id) {
|
||||
orderService.delivery(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成订单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/complete/{id}")
|
||||
@ApiOperation("完成订单")
|
||||
public Result complete(@PathVariable("id") Long id) {
|
||||
orderService.complete(id);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
@ -23,6 +23,12 @@ public class SetmealController {
|
||||
@Autowired
|
||||
private SetmealService setmealService;
|
||||
|
||||
/**
|
||||
* 新增套餐
|
||||
*
|
||||
* @param setmealDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增套餐")
|
||||
@CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
|
||||
@ -30,6 +36,13 @@ public class SetmealController {
|
||||
setmealService.saveWithDish(setmealDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
*
|
||||
* @param setmealPageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("套餐分页查询")
|
||||
public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
|
||||
@ -43,6 +56,7 @@ public class SetmealController {
|
||||
setmealService.deleteBatch(ids);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ApiOperation("根据id查询套餐")
|
||||
public Result<SetmealVO> getById(@PathVariable Long id) {
|
||||
|
@ -9,6 +9,7 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -31,27 +32,15 @@ public class DishController {
|
||||
* @param categoryId
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(value = "dishCache", key = "'dish_' + #categoryId")
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询菜品")
|
||||
public Result<List<DishVO>> list(Long categoryId) {
|
||||
//构造key:dish_分类id
|
||||
String key="dish_"+categoryId;
|
||||
|
||||
//查询redis中是否存在菜品数据
|
||||
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
|
||||
if(list!=null &&list.size()>0)
|
||||
{
|
||||
//如果存在,直接返回,无须查询数据库
|
||||
log.info("查缓存");
|
||||
return Result.success(list);
|
||||
}
|
||||
//如果不存在,查询数据库并将数据放入redis中
|
||||
// 只管业务:查询数据库
|
||||
Dish dish = new Dish();
|
||||
dish.setCategoryId(categoryId);
|
||||
dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
|
||||
|
||||
list = dishService.listWithFlavor(dish);
|
||||
redisTemplate.opsForValue().set(key,list);
|
||||
dish.setStatus(StatusConstant.ENABLE);
|
||||
List<DishVO> list = dishService.listWithFlavor(dish);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
@ -14,19 +14,32 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@Api("用户订单相关接口")
|
||||
@Slf4j
|
||||
/**
|
||||
* 订单
|
||||
*/
|
||||
@RestController("userOrderController")
|
||||
@RequestMapping("/user/order")
|
||||
@Slf4j
|
||||
@Api(tags = "C端订单接口")
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
/**
|
||||
* 用户下单
|
||||
*
|
||||
* @param ordersSubmitDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/submit")
|
||||
@ApiOperation("提交订单")
|
||||
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
|
||||
OrderSubmitVO orderSubmitVO=orderService.submit(ordersSubmitDTO);
|
||||
@ApiOperation("用户下单")
|
||||
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
|
||||
log.info("用户下单:{}", ordersSubmitDTO);
|
||||
OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
|
||||
return Result.success(orderSubmitVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单支付
|
||||
*
|
||||
@ -39,39 +52,6 @@ public class OrderController {
|
||||
log.info("订单支付:{}", ordersPaymentDTO);
|
||||
OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
|
||||
log.info("生成预支付交易单:{}", orderPaymentVO);
|
||||
orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
|
||||
return Result.success(orderPaymentVO);
|
||||
}
|
||||
|
||||
@GetMapping("/historyOrders")
|
||||
@ApiOperation("查询历史订单")
|
||||
public Result<PageResult> page(int page,int pageSize,int status){
|
||||
PageResult pageResult = orderService.pageQuery4User(page, pageSize, status);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
@GetMapping("/orderDetail/{id}")
|
||||
@ApiOperation("查询订单详情")
|
||||
public Result<OrderVO> details(@PathVariable Integer id){
|
||||
OrderVO orderVO=orderService.details(id);
|
||||
return Result.success(orderVO);
|
||||
}
|
||||
@PutMapping("/cancel/{id}")
|
||||
@ApiOperation("取消订单")
|
||||
public Result cancel(@PathVariable Integer id) throws Exception {
|
||||
orderService.cancel(id);
|
||||
return Result.success();
|
||||
}
|
||||
/**
|
||||
* 再来一单
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/repetition/{id}")
|
||||
@ApiOperation("再来一单")
|
||||
public Result repetition(@PathVariable Long id) {
|
||||
orderService.repetition(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import java.util.List;
|
||||
public class ShoppingCartController {
|
||||
@Autowired
|
||||
private ShoppingCartService shoppingCartService;
|
||||
|
||||
@PostMapping("/add")
|
||||
@ApiOperation("添加购物车")
|
||||
public Result addCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
|
||||
|
@ -1,16 +1,24 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.OrderDetail;
|
||||
import com.sky.vo.OrderVO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
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,18 +1,23 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.dto.GoodsSalesDTO;
|
||||
import com.sky.dto.OrdersPageQueryDTO;
|
||||
import com.sky.entity.Orders;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface OrderMapper {
|
||||
/**
|
||||
* 插入订单数据
|
||||
* @param order
|
||||
*/
|
||||
void insert(Orders order);
|
||||
|
||||
/**
|
||||
* 根据订单号查询订单
|
||||
* @param orderNumber
|
||||
@ -26,12 +31,12 @@ public interface OrderMapper {
|
||||
*/
|
||||
void update(Orders orders);
|
||||
|
||||
|
||||
List<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);
|
||||
@Select("select * from orders where id=#{id}")
|
||||
Orders getById(Integer id);
|
||||
@Delete("delete form orders where id=#{id}")
|
||||
void deleteById(Integer id);
|
||||
/**
|
||||
* 根据订单状态和下单时间查询订单
|
||||
* @param status
|
||||
* @param orderTime
|
||||
* @return
|
||||
*/
|
||||
@Select("select * from orders where status = #{status} and order_time < #{orderTime}")
|
||||
List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime orderTime);
|
||||
List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);
|
||||
}
|
||||
|
@ -1,15 +1,22 @@
|
||||
package com.sky.service;
|
||||
|
||||
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;
|
||||
import com.sky.dto.*;
|
||||
import com.sky.vo.*;
|
||||
|
||||
public interface OrderService {
|
||||
OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO);
|
||||
|
||||
/**
|
||||
* 用户下单
|
||||
* @param ordersSubmitDTO
|
||||
* @return
|
||||
*/
|
||||
OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO);
|
||||
|
||||
/**
|
||||
* 订单支付
|
||||
* @param ordersPaymentDTO
|
||||
* @return
|
||||
*/
|
||||
OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception;
|
||||
|
||||
/**
|
||||
@ -18,11 +25,4 @@ public interface OrderService {
|
||||
*/
|
||||
void paySuccess(String outTradeNo);
|
||||
|
||||
PageResult pageQuery4User(int page, int pageSize, int status);
|
||||
|
||||
OrderVO details(Integer id);
|
||||
|
||||
void cancel(Integer id) throws Exception;
|
||||
|
||||
void repetition(Long id);
|
||||
}
|
||||
|
@ -6,9 +6,7 @@ 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.dto.*;
|
||||
import com.sky.entity.*;
|
||||
import com.sky.exception.AddressBookBusinessException;
|
||||
import com.sky.exception.OrderBusinessException;
|
||||
@ -18,14 +16,15 @@ import com.sky.result.PageResult;
|
||||
import com.sky.service.OrderService;
|
||||
import com.sky.utils.WeChatPayUtil;
|
||||
import com.sky.vo.OrderPaymentVO;
|
||||
import com.sky.vo.OrderStatisticsVO;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
import com.sky.vo.OrderVO;
|
||||
import com.sky.websocket.WebSocketServer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
@ -34,26 +33,34 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 订单
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
@Autowired
|
||||
private AddressBookMapper addressBookMapper;
|
||||
@Autowired
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
@Autowired
|
||||
private OrderDetailMapper orderDetailMapper;
|
||||
@Autowired
|
||||
private WeChatPayUtil weChatPayUtil;
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
@Autowired
|
||||
private WebSocketServer webSocketServer;
|
||||
@Override
|
||||
private AddressBookMapper addressBookMapper;
|
||||
@Autowired
|
||||
private WeChatPayUtil weChatPayUtil;
|
||||
|
||||
/**
|
||||
* 用户下单
|
||||
*
|
||||
* @param ordersSubmitDTO
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {
|
||||
//异常情况的处理(收货地址为空、超出配送范围、购物车为空)
|
||||
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
|
||||
//异常情况的处理(收货地址为空、购物车为空)
|
||||
AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
|
||||
if (addressBook == null) {
|
||||
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
|
||||
@ -68,18 +75,22 @@ public class OrderServiceImpl implements OrderService {
|
||||
if (shoppingCartList == null || shoppingCartList.size() == 0) {
|
||||
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
|
||||
}
|
||||
|
||||
//构造订单数据
|
||||
Orders order = new Orders();
|
||||
BeanUtils.copyProperties(ordersSubmitDTO,order);
|
||||
order.setPhone(addressBook.getPhone());
|
||||
order.setAddress(addressBook.getDetail());
|
||||
order.setConsignee(addressBook.getConsignee());
|
||||
order.setNumber(String.valueOf(System.currentTimeMillis())); //订单号
|
||||
order.setNumber(String.valueOf(System.currentTimeMillis()));
|
||||
order.setUserId(userId);
|
||||
order.setStatus(Orders.PENDING_PAYMENT);
|
||||
order.setPayStatus(Orders.UN_PAID);
|
||||
order.setOrderTime(LocalDateTime.now());
|
||||
|
||||
//向订单表插入1条数据
|
||||
orderMapper.insert(order);
|
||||
|
||||
//订单明细数据
|
||||
List<OrderDetail> orderDetailList = new ArrayList<>();
|
||||
for (ShoppingCart cart : shoppingCartList) {
|
||||
@ -88,8 +99,10 @@ public class OrderServiceImpl implements OrderService {
|
||||
orderDetail.setOrderId(order.getId());
|
||||
orderDetailList.add(orderDetail);
|
||||
}
|
||||
|
||||
//向明细表插入n条数据
|
||||
orderDetailMapper.insertBatch(orderDetailList);
|
||||
|
||||
//清理购物车中的数据
|
||||
shoppingCartMapper.deleteByUserId(userId);
|
||||
|
||||
@ -104,22 +117,25 @@ public class OrderServiceImpl implements OrderService {
|
||||
return orderSubmitVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* 订单支付
|
||||
*
|
||||
* @param ordersPaymentDTO
|
||||
* @return
|
||||
*/
|
||||
public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception {
|
||||
// 当前登录用户id
|
||||
Long userId = BaseContext.getCurrentId();
|
||||
User user = userMapper.getById(userId);
|
||||
|
||||
//调用微信支付接口,生成预支付交易单
|
||||
//取消调用微信支付
|
||||
// //调用微信支付接口,生成预支付交易单
|
||||
// JSONObject jsonObject = weChatPayUtil.pay(
|
||||
// ordersPaymentDTO.getOrderNumber(), //商户订单号
|
||||
// new BigDecimal(0.01), //支付金额,单位 元
|
||||
// "苍穹外卖订单", //商品描述
|
||||
// user.getOpenid() //微信用户的openid
|
||||
// );
|
||||
JSONObject jsonObject=new JSONObject();
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("ORDERPAID")) {
|
||||
throw new OrderBusinessException("该订单已支付");
|
||||
}
|
||||
@ -129,6 +145,7 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付成功,修改订单状态
|
||||
*
|
||||
@ -148,125 +165,6 @@ public class OrderServiceImpl implements OrderService {
|
||||
.build();
|
||||
|
||||
orderMapper.update(orders);
|
||||
Map map=new HashMap();
|
||||
map.put("type",1); //1表示来单提醒 2表示客户催单
|
||||
map.put("orderId",ordersDB.getId());
|
||||
map.put("content","订单号:"+outTradeNo);
|
||||
String json=JSON.toJSONString(map);
|
||||
webSocketServer.sendToAllClient(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult pageQuery4User(int pageNum, int pageSize, int status) {
|
||||
// 设置分页
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
|
||||
OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
|
||||
ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
|
||||
ordersPageQueryDTO.setStatus(status);
|
||||
|
||||
// 分页条件查询
|
||||
// 执行分页查询
|
||||
List<Orders> ordersList = orderMapper.pageQuery(ordersPageQueryDTO);
|
||||
Page<Orders> page= (Page<Orders>) ordersList;
|
||||
// 获取分页结果
|
||||
// Page<Orders> page = orderMapper.pageQuery(ordersPageQueryDTO);
|
||||
|
||||
List<OrderVO> list = new ArrayList();
|
||||
|
||||
// 查询出订单明细,并封装入OrderVO进行响应
|
||||
if (page != null && page.getTotal() > 0) {
|
||||
for (Orders orders : page) {
|
||||
Long orderId = orders.getId();// 订单id
|
||||
|
||||
// 查询订单明细
|
||||
List<OrderDetail> orderDetails = orderDetailMapper.getByOrderId(orderId);
|
||||
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(orders, orderVO);
|
||||
orderVO.setOrderDetailList(orderDetails);
|
||||
|
||||
list.add(orderVO);
|
||||
}
|
||||
}
|
||||
return new PageResult(page.getTotal(), list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrderVO details(Integer id) {
|
||||
// 根据id查询订单
|
||||
Orders orders = orderMapper.getById(id);
|
||||
|
||||
// 查询该订单对应的菜品/套餐明细
|
||||
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orders.getId());
|
||||
|
||||
// 将该订单及其详情封装到OrderVO并返回
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(orders, orderVO);
|
||||
orderVO.setOrderDetailList(orderDetailList);
|
||||
|
||||
return orderVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel(Integer 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已取消
|
||||
if (ordersDB.getStatus() > 2) {
|
||||
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||
}
|
||||
Orders orders = new Orders();
|
||||
orders.setId(ordersDB.getId());
|
||||
|
||||
// 订单处于待接单状态下取消,需要进行退款
|
||||
if (ordersDB.getStatus().equals(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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void repetition(Long id) {
|
||||
// 查询当前用户id
|
||||
Long userId = BaseContext.getCurrentId();
|
||||
|
||||
// 根据订单id查询当前订单详情
|
||||
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(id);
|
||||
|
||||
// 将订单详情对象转换为购物车对象
|
||||
List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(x -> {
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
|
||||
// 将原订单详情里面的菜品信息重新复制到购物车对象中
|
||||
BeanUtils.copyProperties(x, shoppingCart, "id");
|
||||
shoppingCart.setUserId(userId);
|
||||
shoppingCart.setCreateTime(LocalDateTime.now());
|
||||
|
||||
return shoppingCart;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
// 将购物车对象批量添加到数据库
|
||||
shoppingCartMapper.insertBatch(shoppingCartList);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,12 +27,15 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
|
||||
|
||||
@Override
|
||||
public void addCart(ShoppingCartDTO shoppingCartDTO) {
|
||||
//判断当前加入到购物车中的商品是否已经存在了
|
||||
ShoppingCart shoppingCart=new ShoppingCart();
|
||||
BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
|
||||
shoppingCart.setUserId(BaseContext.getCurrentId());
|
||||
|
||||
List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
|
||||
|
||||
//如果已经存在,就更新数量,数量加1
|
||||
if (shoppingCartList != null && shoppingCartList.size() == 1) {
|
||||
//如果已经存在,就更新数量,数量加1
|
||||
shoppingCart = shoppingCartList.get(0);
|
||||
shoppingCart.setNumber(shoppingCart.getNumber() + 1);
|
||||
shoppingCartMapper.updateNumberById(shoppingCart);
|
||||
|
@ -2,52 +2,61 @@ package com.sky.task;
|
||||
|
||||
import com.sky.entity.Orders;
|
||||
import com.sky.mapper.OrderMapper;
|
||||
import lombok.extern.java.Log;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 定时任务类,定时处理订单状态
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class OrderTask {
|
||||
/**
|
||||
* 处理下单之后未15分组内支付的超时订单
|
||||
*/
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Scheduled(cron = "0 * * * * ? ")
|
||||
/**
|
||||
* 处理超时订单的方法
|
||||
*/
|
||||
@Scheduled(cron = "0 * * * * ? ") //每分钟触发一次
|
||||
public void processTimeoutOrder(){
|
||||
log.info("定时处理支付超时订单:{}", LocalDateTime.now());
|
||||
log.info("定时处理超时订单:{}", LocalDateTime.now());
|
||||
|
||||
LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
|
||||
|
||||
// select * from orders where status = 1 and order_time < 当前时间-15分钟
|
||||
List<Orders> ordersList = orderMapper.getByStatusAndOrdertimeLT(Orders.PENDING_PAYMENT, time);
|
||||
// select * from orders where status = ? and order_time < (当前时间 - 15分钟)
|
||||
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT, time);
|
||||
|
||||
if(ordersList != null && ordersList.size() > 0){
|
||||
ordersList.forEach(order -> {
|
||||
order.setStatus(Orders.CANCELLED);
|
||||
order.setCancelReason("支付超时,自动取消");
|
||||
order.setCancelTime(LocalDateTime.now());
|
||||
orderMapper.update(order);
|
||||
});
|
||||
for (Orders orders : ordersList) {
|
||||
orders.setStatus(Orders.CANCELLED);
|
||||
orders.setCancelReason("订单超时,自动取消");
|
||||
orders.setCancelTime(LocalDateTime.now());
|
||||
orderMapper.update(orders);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Scheduled(cron = "0 0 1 * * ?")
|
||||
public void processDeliveryOrder() {
|
||||
log.info("处理派送中订单:{}", new Date());
|
||||
// select * from orders where status = 4 and order_time < 当前时间-1小时
|
||||
|
||||
/**
|
||||
* 处理一直处于派送中状态的订单
|
||||
*/
|
||||
@Scheduled(cron = "0 0 1 * * ?") //每天凌晨1点触发一次
|
||||
public void processDeliveryOrder(){
|
||||
log.info("定时处理处于派送中的订单:{}",LocalDateTime.now());
|
||||
|
||||
LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
|
||||
List<Orders> ordersList = orderMapper.getByStatusAndOrdertimeLT(Orders.DELIVERY_IN_PROGRESS, time);
|
||||
if (ordersList != null && ordersList.size() > 0) {
|
||||
ordersList.forEach(order -> {
|
||||
order.setStatus(Orders.COMPLETED);
|
||||
orderMapper.update(order);
|
||||
});
|
||||
|
||||
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS, time);
|
||||
|
||||
if(ordersList != null && ordersList.size() > 0){
|
||||
for (Orders orders : ordersList) {
|
||||
orders.setStatus(Orders.COMPLETED);
|
||||
orderMapper.update(orders);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,3 +25,10 @@ sky:
|
||||
wechat:
|
||||
appid: wxa3b6f70e4ffb92cd
|
||||
secret: ef08b2be0e89a4ff71105c28416e9f42
|
||||
mchid : 1561414331
|
||||
mchSerialNo: 4B3B3DC35414AD50B1B755BAF8DE9CC7CF407606
|
||||
privateKeyFilePath: D:\pay\apiclient_key.pem
|
||||
apiV3Key: CZBK51236435wxpay435434323FFDuv3
|
||||
weChatPayCertFilePath: D:\pay\wechatpay_166D96F876F45C7D07CE98952A96EC980368ACFC.pem
|
||||
notifyUrl: https://58869fb.r2.cpolar.top/notify/paySuccess
|
||||
refundNotifyUrl: https://58869fb.r2.cpolar.top/notify/refundSuccess
|
@ -64,3 +64,13 @@ sky:
|
||||
wechat:
|
||||
appid: ${sky.wechat.appid}
|
||||
secret: ${sky.wechat.secret}
|
||||
mchid : ${sky.wechat.mchid}
|
||||
mchSerialNo: ${sky.wechat.mchSerialNo}
|
||||
privateKeyFilePath: ${sky.wechat.privateKeyFilePath}
|
||||
apiV3Key: ${sky.wechat.apiV3Key}
|
||||
weChatPayCertFilePath: ${sky.wechat.weChatPayCertFilePath}
|
||||
notifyUrl: ${sky.wechat.notifyUrl}
|
||||
refundNotifyUrl: ${sky.wechat.refundNotifyUrl}
|
||||
|
||||
shop:
|
||||
address: 武汉理工大学南湖
|
@ -3,19 +3,11 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.OrderDetailMapper">
|
||||
|
||||
<insert id="insertBatch" parameterType="list">
|
||||
insert into order_detail
|
||||
(name, order_id, dish_id, setmeal_id, dish_flavor, number, amount, image)
|
||||
<insert id="insertBatch">
|
||||
insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount)
|
||||
values
|
||||
<foreach collection="orderDetailList" item="od" separator=",">
|
||||
(#{od.name},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},
|
||||
#{od.number},#{od.amount},#{od.image})
|
||||
(#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
|
||||
</foreach>
|
||||
</insert>
|
||||
<select id="getByOrderId" resultType="com.sky.entity.OrderDetail">
|
||||
select * from order_detail
|
||||
<where>
|
||||
order_id=#{orderId}
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
</mapper>
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?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" >
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.OrderMapper">
|
||||
|
||||
<insert id="insert" parameterType="Orders" useGeneratedKeys="true" keyProperty="id">
|
||||
@ -12,6 +11,7 @@
|
||||
#{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},
|
||||
#{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="com.sky.entity.Orders">
|
||||
update orders
|
||||
<set>
|
||||
@ -42,28 +42,5 @@
|
||||
</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>
|
||||
|
||||
</mapper>
|
||||
|
Loading…
x
Reference in New Issue
Block a user