2025.5.6 商家订单处理:接单 取消、拒单等,接入百度api判断送餐地址范围
This commit is contained in:
parent
30d33fcd89
commit
ff94f3f75d
@ -0,0 +1,21 @@
|
|||||||
|
package com.sky.constant;
|
||||||
|
|
||||||
|
public class OrdersConstant {
|
||||||
|
/**
|
||||||
|
* 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 静态成员变量
|
||||||
|
*/
|
||||||
|
public static final Integer PENDING_PAYMENT = 1;
|
||||||
|
public static final Integer TO_BE_CONFIRMED = 2;
|
||||||
|
public static final Integer CONFIRMED = 3;
|
||||||
|
public static final Integer DELIVERY_IN_PROGRESS = 4;
|
||||||
|
public static final Integer COMPLETED = 5;
|
||||||
|
public static final Integer CANCELLED = 6;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支付状态 0未支付 1已支付 2退款
|
||||||
|
*/
|
||||||
|
public static final Integer UN_PAID = 0;
|
||||||
|
public static final Integer PAID = 1;
|
||||||
|
public static final Integer REFUND = 2;
|
||||||
|
|
||||||
|
}
|
@ -8,10 +8,8 @@ import org.springframework.stereotype.Component;
|
|||||||
@ConfigurationProperties(prefix = "sky.alioss")
|
@ConfigurationProperties(prefix = "sky.alioss")
|
||||||
@Data
|
@Data
|
||||||
public class AliOssProperties {
|
public class AliOssProperties {
|
||||||
|
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
private String accessKeyId;
|
private String accessKeyId;
|
||||||
private String accessKeySecret;
|
private String accessKeySecret;
|
||||||
private String bucketName;
|
private String bucketName;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.sky.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Data
|
||||||
|
public class BaiduAdressProperties {
|
||||||
|
@Value("${sky.shop.address}")
|
||||||
|
private String shopAddress;
|
||||||
|
|
||||||
|
@Value("${sky.baidu.ak}")
|
||||||
|
private String ak;
|
||||||
|
}
|
@ -4,22 +4,10 @@ import com.aliyun.oss.ClientException;
|
|||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSClientBuilder;
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
import com.aliyun.oss.OSSException;
|
import com.aliyun.oss.OSSException;
|
||||||
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
|
|
||||||
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
|
|
||||||
import com.aliyun.oss.model.PutObjectRequest;
|
|
||||||
import com.aliyun.oss.model.PutObjectResult;
|
|
||||||
import com.sky.properties.AliOssProperties;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@ -137,7 +137,7 @@ public class WeChatPayUtil {
|
|||||||
jsonObject.put("mchid", weChatProperties.getMchid());
|
jsonObject.put("mchid", weChatProperties.getMchid());
|
||||||
jsonObject.put("description", description);
|
jsonObject.put("description", description);
|
||||||
jsonObject.put("out_trade_no", orderNum);
|
jsonObject.put("out_trade_no", orderNum);
|
||||||
jsonObject.put("notify_url", weChatProperties.getNotifyUrl());
|
jsonObject.put("notify_url", weChatProperties.getNotifyUrl()); //回调地址
|
||||||
|
|
||||||
JSONObject amount = new JSONObject();
|
JSONObject amount = new JSONObject();
|
||||||
amount.put("total", total.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue());
|
amount.put("total", total.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue());
|
||||||
|
@ -18,23 +18,6 @@ import java.time.LocalDateTime;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Orders implements Serializable {
|
public class Orders implements Serializable {
|
||||||
|
|
||||||
/**
|
|
||||||
* 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
|
|
||||||
*/
|
|
||||||
public static final Integer PENDING_PAYMENT = 1;
|
|
||||||
public static final Integer TO_BE_CONFIRMED = 2;
|
|
||||||
public static final Integer CONFIRMED = 3;
|
|
||||||
public static final Integer DELIVERY_IN_PROGRESS = 4;
|
|
||||||
public static final Integer COMPLETED = 5;
|
|
||||||
public static final Integer CANCELLED = 6;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 支付状态 0未支付 1已支付 2退款
|
|
||||||
*/
|
|
||||||
public static final Integer UN_PAID = 0;
|
|
||||||
public static final Integer PAID = 1;
|
|
||||||
public static final Integer REFUND = 2;
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
@ -11,6 +11,7 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
//注意,这里是继承!
|
||||||
public class OrderVO extends Orders implements Serializable {
|
public class OrderVO extends Orders implements Serializable {
|
||||||
|
|
||||||
//订单菜品信息
|
//订单菜品信息
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.sky.controller.user;
|
package com.sky.controller.user;
|
||||||
|
|
||||||
|
import com.sky.dto.OrdersPageQueryDTO;
|
||||||
import com.sky.dto.OrdersPaymentDTO;
|
import com.sky.dto.OrdersPaymentDTO;
|
||||||
import com.sky.dto.OrdersSubmitDTO;
|
import com.sky.dto.OrdersSubmitDTO;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
@ -54,4 +55,65 @@ public class OrderController {
|
|||||||
log.info("生成预支付交易单:{}", orderPaymentVO);
|
log.info("生成预支付交易单:{}", orderPaymentVO);
|
||||||
return Result.success(orderPaymentVO);
|
return Result.success(orderPaymentVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 历史订单查询
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/historyOrders")
|
||||||
|
@ApiOperation("历史订单查询")
|
||||||
|
public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO) {
|
||||||
|
PageResult pageResult = orderService.pageQuery4User(ordersPageQueryDTO);
|
||||||
|
return Result.success(pageResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询订单详情
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/orderDetail/{id}")
|
||||||
|
@ApiOperation("查询订单详情")
|
||||||
|
public Result<OrderVO> details(@PathVariable("id") Long id) {
|
||||||
|
OrderVO orderVO = orderService.details(id);
|
||||||
|
return Result.success(orderVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户取消订单
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PutMapping("/cancel/{id}")
|
||||||
|
@ApiOperation("取消订单")
|
||||||
|
public Result cancel(@PathVariable("id") Long id) throws Exception {
|
||||||
|
orderService.userCancelById(id);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 再来一单
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/repetition/{id}")
|
||||||
|
@ApiOperation("再来一单")
|
||||||
|
public Result repetition(@PathVariable Long id) {
|
||||||
|
orderService.repetition(id);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 客户催单
|
||||||
|
// * @param id
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// @GetMapping("/reminder/{id}")
|
||||||
|
// @ApiOperation("客户催单")
|
||||||
|
// public Result reminder(@PathVariable("id") Long id){
|
||||||
|
// orderService.reminder(id);
|
||||||
|
// return Result.success();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
@ -39,4 +39,24 @@ public interface OrderMapper {
|
|||||||
*/
|
*/
|
||||||
@Select("select * from orders where status = #{status} and order_time < #{orderTime}")
|
@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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页条件查询并按下单时间排序
|
||||||
|
* @param ordersPageQueryDTO
|
||||||
|
*/
|
||||||
|
Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查询订单
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Select("select * from orders where id=#{id}")
|
||||||
|
Orders getById(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据状态统计订单数量
|
||||||
|
* @param status
|
||||||
|
*/
|
||||||
|
@Select("select count(id) from orders where status = #{status}")
|
||||||
|
Integer countStatus(Integer status);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.sky.service;
|
package com.sky.service;
|
||||||
|
|
||||||
import com.sky.dto.*;
|
import com.sky.dto.*;
|
||||||
|
import com.sky.result.PageResult;
|
||||||
import com.sky.vo.*;
|
import com.sky.vo.*;
|
||||||
|
|
||||||
public interface OrderService {
|
public interface OrderService {
|
||||||
@ -25,4 +26,27 @@ public interface OrderService {
|
|||||||
*/
|
*/
|
||||||
void paySuccess(String outTradeNo);
|
void paySuccess(String outTradeNo);
|
||||||
|
|
||||||
|
PageResult pageQuery4User(OrdersPageQueryDTO ordersPageQueryDTO);
|
||||||
|
|
||||||
|
OrderVO details(Long id);
|
||||||
|
|
||||||
|
void userCancelById(Long id) throws Exception;
|
||||||
|
|
||||||
|
void repetition(Long id);
|
||||||
|
|
||||||
|
PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);
|
||||||
|
|
||||||
|
OrderStatisticsVO statistics();
|
||||||
|
|
||||||
|
void confirm(OrdersConfirmDTO ordersConfirmDTO);
|
||||||
|
|
||||||
|
void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception;
|
||||||
|
|
||||||
|
void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception;
|
||||||
|
|
||||||
|
void delivery(Long id);
|
||||||
|
|
||||||
|
void complete(Long id);
|
||||||
|
|
||||||
|
// void reminder(Long id);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.sky.service.impl;
|
package com.sky.service.impl;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.github.pagehelper.Page;
|
import com.github.pagehelper.Page;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import com.sky.constant.MessageConstant;
|
import com.sky.constant.MessageConstant;
|
||||||
|
import com.sky.constant.OrdersConstant;
|
||||||
import com.sky.context.BaseContext;
|
import com.sky.context.BaseContext;
|
||||||
import com.sky.dto.*;
|
import com.sky.dto.*;
|
||||||
import com.sky.entity.*;
|
import com.sky.entity.*;
|
||||||
@ -12,8 +14,10 @@ import com.sky.exception.AddressBookBusinessException;
|
|||||||
import com.sky.exception.OrderBusinessException;
|
import com.sky.exception.OrderBusinessException;
|
||||||
import com.sky.exception.ShoppingCartBusinessException;
|
import com.sky.exception.ShoppingCartBusinessException;
|
||||||
import com.sky.mapper.*;
|
import com.sky.mapper.*;
|
||||||
|
import com.sky.properties.BaiduAdressProperties;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
import com.sky.service.OrderService;
|
import com.sky.service.OrderService;
|
||||||
|
import com.sky.utils.HttpClientUtil;
|
||||||
import com.sky.utils.WeChatPayUtil;
|
import com.sky.utils.WeChatPayUtil;
|
||||||
import com.sky.vo.OrderPaymentVO;
|
import com.sky.vo.OrderPaymentVO;
|
||||||
import com.sky.vo.OrderStatisticsVO;
|
import com.sky.vo.OrderStatisticsVO;
|
||||||
@ -51,7 +55,72 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
private AddressBookMapper addressBookMapper;
|
private AddressBookMapper addressBookMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
private WeChatPayUtil weChatPayUtil;
|
private WeChatPayUtil weChatPayUtil;
|
||||||
|
@Autowired
|
||||||
|
private BaiduAdressProperties props;
|
||||||
|
/**
|
||||||
|
* 检查客户的收货地址是否超出配送范围
|
||||||
|
* @param address
|
||||||
|
*/
|
||||||
|
|
||||||
|
private void checkOutOfRange(String address) {
|
||||||
|
Map map = new HashMap();
|
||||||
|
map.put("address",props.getShopAddress());
|
||||||
|
map.put("output","json");
|
||||||
|
map.put("ak",props.getAk());
|
||||||
|
|
||||||
|
//获取店铺的经纬度坐标
|
||||||
|
String shopCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);
|
||||||
|
|
||||||
|
JSONObject jsonObject = JSON.parseObject(shopCoordinate);
|
||||||
|
if(!jsonObject.getString("status").equals("0")){
|
||||||
|
throw new OrderBusinessException("店铺地址解析失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
//数据解析
|
||||||
|
JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
|
||||||
|
String lat = location.getString("lat");
|
||||||
|
String lng = location.getString("lng");
|
||||||
|
//店铺经纬度坐标
|
||||||
|
String shopLngLat = lat + "," + lng;
|
||||||
|
|
||||||
|
map.put("address",address);
|
||||||
|
//获取用户收货地址的经纬度坐标
|
||||||
|
String userCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);
|
||||||
|
|
||||||
|
jsonObject = JSON.parseObject(userCoordinate);
|
||||||
|
if(!jsonObject.getString("status").equals("0")){
|
||||||
|
throw new OrderBusinessException("收货地址解析失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
//数据解析
|
||||||
|
location = jsonObject.getJSONObject("result").getJSONObject("location");
|
||||||
|
lat = location.getString("lat");
|
||||||
|
lng = location.getString("lng");
|
||||||
|
//用户收货地址经纬度坐标
|
||||||
|
String userLngLat = lat + "," + lng;
|
||||||
|
|
||||||
|
map.put("origin",shopLngLat);
|
||||||
|
map.put("destination",userLngLat);
|
||||||
|
map.put("steps_info","0");
|
||||||
|
|
||||||
|
//路线规划
|
||||||
|
String json = HttpClientUtil.doGet("https://api.map.baidu.com/directionlite/v1/driving", map);
|
||||||
|
|
||||||
|
jsonObject = JSON.parseObject(json);
|
||||||
|
if(!jsonObject.getString("status").equals("0")){
|
||||||
|
throw new OrderBusinessException("配送路线规划失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
//数据解析
|
||||||
|
JSONObject result = jsonObject.getJSONObject("result");
|
||||||
|
JSONArray jsonArray = (JSONArray) result.get("routes");
|
||||||
|
Integer distance = (Integer) ((JSONObject) jsonArray.get(0)).get("distance");
|
||||||
|
|
||||||
|
if(distance > 5000){
|
||||||
|
//配送距离超过5000米
|
||||||
|
throw new OrderBusinessException("超出配送范围");
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 用户下单
|
* 用户下单
|
||||||
*
|
*
|
||||||
@ -66,11 +135,14 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
|
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//检查用户的收货地址是否超出配送范围
|
||||||
|
checkOutOfRange(addressBook.getCityName() + addressBook.getDistrictName() + addressBook.getDetail());
|
||||||
|
|
||||||
|
//查询当前用户的购物车数据
|
||||||
Long userId = BaseContext.getCurrentId();
|
Long userId = BaseContext.getCurrentId();
|
||||||
ShoppingCart shoppingCart = new ShoppingCart();
|
ShoppingCart shoppingCart = new ShoppingCart();
|
||||||
shoppingCart.setUserId(userId);
|
shoppingCart.setUserId(userId);
|
||||||
|
|
||||||
//查询当前用户的购物车数据
|
|
||||||
List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
|
List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
|
||||||
if (shoppingCartList == null || shoppingCartList.size() == 0) {
|
if (shoppingCartList == null || shoppingCartList.size() == 0) {
|
||||||
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
|
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
|
||||||
@ -84,8 +156,8 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
order.setConsignee(addressBook.getConsignee());
|
order.setConsignee(addressBook.getConsignee());
|
||||||
order.setNumber(String.valueOf(System.currentTimeMillis()));
|
order.setNumber(String.valueOf(System.currentTimeMillis()));
|
||||||
order.setUserId(userId);
|
order.setUserId(userId);
|
||||||
order.setStatus(Orders.PENDING_PAYMENT);
|
order.setStatus(OrdersConstant.PENDING_PAYMENT);
|
||||||
order.setPayStatus(Orders.UN_PAID);
|
order.setPayStatus(OrdersConstant.UN_PAID);
|
||||||
order.setOrderTime(LocalDateTime.now());
|
order.setOrderTime(LocalDateTime.now());
|
||||||
|
|
||||||
//向订单表插入1条数据
|
//向订单表插入1条数据
|
||||||
@ -132,7 +204,7 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
// JSONObject jsonObject = weChatPayUtil.pay(
|
// JSONObject jsonObject = weChatPayUtil.pay(
|
||||||
// ordersPaymentDTO.getOrderNumber(), //商户订单号
|
// ordersPaymentDTO.getOrderNumber(), //商户订单号
|
||||||
// new BigDecimal(0.01), //支付金额,单位 元
|
// new BigDecimal(0.01), //支付金额,单位 元
|
||||||
// "苍穹外卖订单", //商品描述
|
// "理工外卖订单", //商品描述
|
||||||
// user.getOpenid() //微信用户的openid
|
// user.getOpenid() //微信用户的openid
|
||||||
// );
|
// );
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
@ -142,7 +214,7 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
|
|
||||||
OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
|
OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
|
||||||
vo.setPackageStr(jsonObject.getString("package"));
|
vo.setPackageStr(jsonObject.getString("package"));
|
||||||
|
paySuccess(ordersPaymentDTO.getOrderNumber()); //模拟微信调用回调函数
|
||||||
return vo;
|
return vo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,12 +231,340 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
// 根据订单id更新订单的状态、支付方式、支付状态、结账时间
|
// 根据订单id更新订单的状态、支付方式、支付状态、结账时间
|
||||||
Orders orders = Orders.builder()
|
Orders orders = Orders.builder()
|
||||||
.id(ordersDB.getId())
|
.id(ordersDB.getId())
|
||||||
.status(Orders.TO_BE_CONFIRMED)
|
.status(OrdersConstant.TO_BE_CONFIRMED)
|
||||||
.payStatus(Orders.PAID)
|
.payStatus(OrdersConstant.PAID)
|
||||||
.checkoutTime(LocalDateTime.now())
|
.checkoutTime(LocalDateTime.now())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
orderMapper.update(orders);
|
orderMapper.update(orders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户端订单分页查询
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public PageResult pageQuery4User(OrdersPageQueryDTO dto) {
|
||||||
|
return pageQuery(dto, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询订单详情
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OrderVO details(Long 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户取消订单
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
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已取消
|
||||||
|
if (ordersDB.getStatus() > 2) {
|
||||||
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
Orders orders = new Orders();
|
||||||
|
orders.setId(ordersDB.getId());
|
||||||
|
|
||||||
|
// 订单处于待接单状态下取消,需要进行退款
|
||||||
|
if (ordersDB.getStatus().equals(OrdersConstant.TO_BE_CONFIRMED)) {
|
||||||
|
//调用微信支付退款接口
|
||||||
|
weChatPayUtil.refund(
|
||||||
|
ordersDB.getNumber(), //商户订单号
|
||||||
|
ordersDB.getNumber(), //商户退款单号
|
||||||
|
new BigDecimal(0.01),//退款金额,单位 元
|
||||||
|
new BigDecimal(0.01));//原订单金额
|
||||||
|
|
||||||
|
//支付状态修改为 退款
|
||||||
|
orders.setPayStatus(OrdersConstant.REFUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新订单状态、取消原因、取消时间
|
||||||
|
orders.setStatus(OrdersConstant.CANCELLED);
|
||||||
|
orders.setCancelReason("用户取消");
|
||||||
|
orders.setCancelTime(LocalDateTime.now());
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 再来一单
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void repetition(Long id) {
|
||||||
|
// 查询当前用户id
|
||||||
|
Long userId = BaseContext.getCurrentId();
|
||||||
|
|
||||||
|
// 根据订单id查询当前订单详情
|
||||||
|
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(id);
|
||||||
|
|
||||||
|
// 将订单详情对象转换为购物车对象
|
||||||
|
List<ShoppingCart> shoppingCartList = new ArrayList<>();
|
||||||
|
for (OrderDetail x : orderDetailList) {
|
||||||
|
ShoppingCart shoppingCart = new ShoppingCart();
|
||||||
|
BeanUtils.copyProperties(x, shoppingCart, "id");
|
||||||
|
shoppingCart.setUserId(userId);
|
||||||
|
shoppingCart.setCreateTime(LocalDateTime.now());
|
||||||
|
shoppingCartList.add(shoppingCart);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将购物车对象批量添加到数据库
|
||||||
|
shoppingCartMapper.insertBatch(shoppingCartList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单搜索
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public PageResult conditionSearch(OrdersPageQueryDTO dto) {
|
||||||
|
return pageQuery(dto, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 各个状态的订单数量统计
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public OrderStatisticsVO statistics() {
|
||||||
|
// 根据状态,分别查询出待接单、待派送、派送中的订单数量
|
||||||
|
Integer toBeConfirmed = orderMapper.countStatus(OrdersConstant.TO_BE_CONFIRMED);
|
||||||
|
Integer confirmed = orderMapper.countStatus(OrdersConstant.CONFIRMED);
|
||||||
|
Integer deliveryInProgress = orderMapper.countStatus(OrdersConstant.DELIVERY_IN_PROGRESS);
|
||||||
|
|
||||||
|
// 将查询出的数据封装到orderStatisticsVO中响应
|
||||||
|
OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
|
||||||
|
orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
|
||||||
|
orderStatisticsVO.setConfirmed(confirmed);
|
||||||
|
orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
|
||||||
|
return orderStatisticsVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接单
|
||||||
|
*
|
||||||
|
* @param ordersConfirmDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
|
||||||
|
Orders orders = Orders.builder()
|
||||||
|
.id(ordersConfirmDTO.getId())
|
||||||
|
.status(OrdersConstant.CONFIRMED)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拒单
|
||||||
|
*
|
||||||
|
* @param ordersRejectionDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception {
|
||||||
|
// 根据id查询订单
|
||||||
|
Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId());
|
||||||
|
|
||||||
|
// 订单只有存在且状态为2(待接单)才可以拒单
|
||||||
|
if (ordersDB == null || !ordersDB.getStatus().equals(OrdersConstant.TO_BE_CONFIRMED)) {
|
||||||
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
//支付状态
|
||||||
|
Integer payStatus = ordersDB.getPayStatus();
|
||||||
|
if (payStatus == OrdersConstant.PAID) {
|
||||||
|
//用户已支付,需要退款
|
||||||
|
String refund = weChatPayUtil.refund(
|
||||||
|
ordersDB.getNumber(),
|
||||||
|
ordersDB.getNumber(),
|
||||||
|
new BigDecimal(0.01),
|
||||||
|
new BigDecimal(0.01));
|
||||||
|
log.info("申请退款:{}", refund);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 拒单需要退款,根据订单id更新订单状态、拒单原因、取消时间
|
||||||
|
Orders orders = new Orders();
|
||||||
|
orders.setId(ordersDB.getId());
|
||||||
|
orders.setStatus(OrdersConstant.CANCELLED);
|
||||||
|
orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
|
||||||
|
orders.setCancelTime(LocalDateTime.now());
|
||||||
|
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消订单
|
||||||
|
*
|
||||||
|
* @param ordersCancelDTO
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception {
|
||||||
|
// 根据id查询订单
|
||||||
|
Orders ordersDB = orderMapper.getById(ordersCancelDTO.getId());
|
||||||
|
|
||||||
|
//支付状态
|
||||||
|
Integer payStatus = ordersDB.getPayStatus();
|
||||||
|
if (payStatus == 1) {
|
||||||
|
//用户已支付,需要退款
|
||||||
|
String refund = weChatPayUtil.refund(
|
||||||
|
ordersDB.getNumber(),
|
||||||
|
ordersDB.getNumber(),
|
||||||
|
new BigDecimal(0.01),
|
||||||
|
new BigDecimal(0.01));
|
||||||
|
log.info("申请退款:{}", refund);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 管理端取消订单需要退款,根据订单id更新订单状态、取消原因、取消时间
|
||||||
|
Orders orders = new Orders();
|
||||||
|
orders.setId(ordersCancelDTO.getId());
|
||||||
|
orders.setStatus(OrdersConstant.CANCELLED);
|
||||||
|
orders.setCancelReason(ordersCancelDTO.getCancelReason());
|
||||||
|
orders.setCancelTime(LocalDateTime.now());
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 派送订单
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void delivery(Long id) {
|
||||||
|
// 根据id查询订单
|
||||||
|
Orders ordersDB = orderMapper.getById(id);
|
||||||
|
|
||||||
|
// 校验订单是否存在,并且状态为3
|
||||||
|
if (ordersDB == null || !ordersDB.getStatus().equals(OrdersConstant.CONFIRMED)) {
|
||||||
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
Orders orders = new Orders();
|
||||||
|
orders.setId(ordersDB.getId());
|
||||||
|
// 更新订单状态,状态转为派送中
|
||||||
|
orders.setStatus(OrdersConstant.DELIVERY_IN_PROGRESS);
|
||||||
|
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 完成订单
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void complete(Long id) {
|
||||||
|
// 根据id查询订单
|
||||||
|
Orders ordersDB = orderMapper.getById(id);
|
||||||
|
|
||||||
|
// 校验订单是否存在,并且状态为4
|
||||||
|
if (ordersDB == null || !ordersDB.getStatus().equals(OrdersConstant.DELIVERY_IN_PROGRESS)) {
|
||||||
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
Orders orders = new Orders();
|
||||||
|
orders.setId(ordersDB.getId());
|
||||||
|
// 更新订单状态,状态转为完成
|
||||||
|
orders.setStatus(OrdersConstant.COMPLETED);
|
||||||
|
orders.setDeliveryTime(LocalDateTime.now());
|
||||||
|
|
||||||
|
orderMapper.update(orders);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用分页+转换 VO 方法
|
||||||
|
*
|
||||||
|
* @param dto 查询参数
|
||||||
|
* @param includeDetails true:返回 OrderDetail 列表;false:返回 orderDishes 字符串
|
||||||
|
*/
|
||||||
|
private PageResult pageQuery(OrdersPageQueryDTO dto, boolean includeDetails) {
|
||||||
|
// 1. 分页
|
||||||
|
PageHelper.startPage(dto.getPage(), dto.getPageSize());
|
||||||
|
// 2. 查询
|
||||||
|
Page<Orders> page = orderMapper.pageQuery(dto);
|
||||||
|
// 3. 转 VO
|
||||||
|
List<OrderVO> voList = new ArrayList<>();
|
||||||
|
for (Orders orders : page) {
|
||||||
|
OrderVO vo = new OrderVO();
|
||||||
|
BeanUtils.copyProperties(orders, vo);
|
||||||
|
if (includeDetails) {
|
||||||
|
// 用户端查询:塞明细列表
|
||||||
|
List<OrderDetail> details = orderDetailMapper.getByOrderId(orders.getId());
|
||||||
|
vo.setOrderDetailList(details);
|
||||||
|
} else {
|
||||||
|
// 管理端查询:拼菜品字符串
|
||||||
|
vo.setOrderDishes(getOrderDishesStr(orders));
|
||||||
|
}
|
||||||
|
voList.add(vo);
|
||||||
|
}
|
||||||
|
// 4. 返回
|
||||||
|
return new PageResult(page.getTotal(), voList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据订单 id 拼接菜品字符串
|
||||||
|
*/
|
||||||
|
private String getOrderDishesStr(Orders orders) {
|
||||||
|
List<OrderDetail> detailList = orderDetailMapper.getByOrderId(orders.getId());
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (OrderDetail d : detailList) {
|
||||||
|
sb.append(d.getName())
|
||||||
|
.append('*')
|
||||||
|
.append(d.getNumber())
|
||||||
|
.append(';');
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 客户催单
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
// @Override
|
||||||
|
// public void reminder(Long id) {
|
||||||
|
// // 根据id查询订单
|
||||||
|
// Orders ordersDB = orderMapper.getById(id);
|
||||||
|
//
|
||||||
|
// // 校验订单是否存在
|
||||||
|
// if (ordersDB == null) {
|
||||||
|
// throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Map map = new HashMap();
|
||||||
|
// map.put("type",2); //1表示来单提醒 2表示客户催单
|
||||||
|
// map.put("orderId",id);
|
||||||
|
// map.put("content","订单号:" + ordersDB.getNumber());
|
||||||
|
//
|
||||||
|
// //通过websocket向客户端浏览器推送消息
|
||||||
|
// webSocketServer.sendToAllClient(JSON.toJSONString(map));
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.sky.task;
|
package com.sky.task;
|
||||||
|
|
||||||
|
import com.sky.constant.OrdersConstant;
|
||||||
import com.sky.entity.Orders;
|
import com.sky.entity.Orders;
|
||||||
import com.sky.mapper.OrderMapper;
|
import com.sky.mapper.OrderMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -29,11 +30,11 @@ public class OrderTask {
|
|||||||
LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
|
||||||
|
|
||||||
// select * from orders where status = ? and order_time < (当前时间 - 15分钟)
|
// select * from orders where status = ? and order_time < (当前时间 - 15分钟)
|
||||||
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT, time);
|
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(OrdersConstant.PENDING_PAYMENT, time);
|
||||||
|
|
||||||
if(ordersList != null && ordersList.size() > 0){
|
if(ordersList != null && ordersList.size() > 0){
|
||||||
for (Orders orders : ordersList) {
|
for (Orders orders : ordersList) {
|
||||||
orders.setStatus(Orders.CANCELLED);
|
orders.setStatus(OrdersConstant.CANCELLED);
|
||||||
orders.setCancelReason("订单超时,自动取消");
|
orders.setCancelReason("订单超时,自动取消");
|
||||||
orders.setCancelTime(LocalDateTime.now());
|
orders.setCancelTime(LocalDateTime.now());
|
||||||
orderMapper.update(orders);
|
orderMapper.update(orders);
|
||||||
@ -50,11 +51,11 @@ public class OrderTask {
|
|||||||
|
|
||||||
LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
|
LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
|
||||||
|
|
||||||
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS, time);
|
List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeLT(OrdersConstant.DELIVERY_IN_PROGRESS, time);
|
||||||
|
|
||||||
if(ordersList != null && ordersList.size() > 0){
|
if(ordersList != null && ordersList.size() > 0){
|
||||||
for (Orders orders : ordersList) {
|
for (Orders orders : ordersList) {
|
||||||
orders.setStatus(Orders.COMPLETED);
|
orders.setStatus(OrdersConstant.COMPLETED);
|
||||||
orderMapper.update(orders);
|
orderMapper.update(orders);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,7 @@ sky:
|
|||||||
apiV3Key: CZBK51236435wxpay435434323FFDuv3
|
apiV3Key: CZBK51236435wxpay435434323FFDuv3
|
||||||
weChatPayCertFilePath: D:\pay\wechatpay_166D96F876F45C7D07CE98952A96EC980368ACFC.pem
|
weChatPayCertFilePath: D:\pay\wechatpay_166D96F876F45C7D07CE98952A96EC980368ACFC.pem
|
||||||
notifyUrl: https://58869fb.r2.cpolar.top/notify/paySuccess
|
notifyUrl: https://58869fb.r2.cpolar.top/notify/paySuccess
|
||||||
refundNotifyUrl: https://58869fb.r2.cpolar.top/notify/refundSuccess
|
refundNotifyUrl: https://58869fb.r2.cpolar.top/notify/refundSuccess
|
||||||
|
|
||||||
|
baidu:
|
||||||
|
ak: tcCwJsRZj872u23133ELtE1WMBY5lojW
|
||||||
|
@ -73,4 +73,6 @@ sky:
|
|||||||
refundNotifyUrl: ${sky.wechat.refundNotifyUrl}
|
refundNotifyUrl: ${sky.wechat.refundNotifyUrl}
|
||||||
|
|
||||||
shop:
|
shop:
|
||||||
address: 武汉理工大学南湖
|
address: 武汉理工大学鉴湖校区
|
||||||
|
baidu:
|
||||||
|
ak: ${sky.baidu.ak}
|
@ -42,5 +42,28 @@
|
|||||||
</set>
|
</set>
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
<select id="pageQuery" resultType="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