2025.5.8 spring task实现付款超时自动取消订单 每日凌晨自动设置完成配送中的订单 websocket来单提醒

This commit is contained in:
zhangsan 2025-05-08 14:38:28 +08:00
parent ff94f3f75d
commit 2dd3f86132
6 changed files with 96 additions and 57 deletions

View File

@ -105,15 +105,15 @@ public class OrderController {
return Result.success(); return Result.success();
} }
// /** /**
// * 客户催单 * 客户催单
// * @param id * @param id
// * @return * @return
// */ */
// @GetMapping("/reminder/{id}") @GetMapping("/reminder/{id}")
// @ApiOperation("客户催单") @ApiOperation("客户催单")
// public Result reminder(@PathVariable("id") Long id){ public Result reminder(@PathVariable("id") Long id){
// orderService.reminder(id); orderService.reminder(id);
// return Result.success(); return Result.success();
// } }
} }

View File

@ -48,5 +48,7 @@ public interface OrderService {
void complete(Long id); void complete(Long id);
void reminder(Long id);
// void reminder(Long id); // void reminder(Long id);
} }

View File

@ -23,6 +23,7 @@ import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderStatisticsVO; import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO; import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -31,10 +32,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -57,6 +55,9 @@ public class OrderServiceImpl implements OrderService {
private WeChatPayUtil weChatPayUtil; private WeChatPayUtil weChatPayUtil;
@Autowired @Autowired
private BaiduAdressProperties props; private BaiduAdressProperties props;
@Autowired
private WebSocketServer webSocketServer;
/** /**
* 检查客户的收货地址是否超出配送范围 * 检查客户的收货地址是否超出配送范围
* @param address * @param address
@ -179,14 +180,13 @@ public class OrderServiceImpl implements OrderService {
shoppingCartMapper.deleteByUserId(userId); shoppingCartMapper.deleteByUserId(userId);
//封装返回结果 //封装返回结果
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
return OrderSubmitVO.builder()
.id(order.getId()) .id(order.getId())
.orderNumber(order.getNumber()) .orderNumber(order.getNumber())
.orderAmount(order.getAmount()) .orderAmount(order.getAmount())
.orderTime(order.getOrderTime()) .orderTime(order.getOrderTime())
.build(); .build();
return orderSubmitVO;
} }
/** /**
@ -237,6 +237,15 @@ public class OrderServiceImpl implements OrderService {
.build(); .build();
orderMapper.update(orders); orderMapper.update(orders);
//来单提醒
Map map = new HashMap();
map.put("type", 1);//消息类型1表示来单提醒
map.put("orderId", orders.getId());
map.put("content", "订单号:" + outTradeNo);
//通过WebSocket实现来单提醒向客户端浏览器推送消息
webSocketServer.sendToAllClient(JSON.toJSONString(map));
} }
/** /**
@ -296,11 +305,11 @@ public class OrderServiceImpl implements OrderService {
// 订单处于待接单状态下取消需要进行退款 // 订单处于待接单状态下取消需要进行退款
if (ordersDB.getStatus().equals(OrdersConstant.TO_BE_CONFIRMED)) { if (ordersDB.getStatus().equals(OrdersConstant.TO_BE_CONFIRMED)) {
//调用微信支付退款接口 //调用微信支付退款接口
weChatPayUtil.refund( // weChatPayUtil.refund(
ordersDB.getNumber(), //商户订单号 // ordersDB.getNumber(), //商户订单号
ordersDB.getNumber(), //商户退款单号 // ordersDB.getNumber(), //商户退款单号
new BigDecimal(0.01),//退款金额单位 // new BigDecimal(0.01),//退款金额单位
new BigDecimal(0.01));//原订单金额 // new BigDecimal(0.01));//原订单金额
//支付状态修改为 退款 //支付状态修改为 退款
orders.setPayStatus(OrdersConstant.REFUND); orders.setPayStatus(OrdersConstant.REFUND);
@ -391,6 +400,7 @@ public class OrderServiceImpl implements OrderService {
*/ */
@Override @Override
public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception { public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception {
log.info(ordersRejectionDTO.toString());
// 根据id查询订单 // 根据id查询订单
Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId()); Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId());
@ -401,14 +411,15 @@ public class OrderServiceImpl implements OrderService {
//支付状态 //支付状态
Integer payStatus = ordersDB.getPayStatus(); Integer payStatus = ordersDB.getPayStatus();
if (payStatus == OrdersConstant.PAID) { if (Objects.equals(payStatus, OrdersConstant.PAID)) {
//用户已支付需要退款 // //用户已支付需要退款
String refund = weChatPayUtil.refund( // String refund = weChatPayUtil.refund(
ordersDB.getNumber(), // ordersDB.getNumber(),
ordersDB.getNumber(), // ordersDB.getNumber(),
new BigDecimal(0.01), // new BigDecimal(0.01),
new BigDecimal(0.01)); // new BigDecimal(0.01));
log.info("申请退款:{}", refund); // log.info("申请退款:{}", refund);
log.info("拒单,退款给用户");
} }
// 拒单需要退款根据订单id更新订单状态拒单原因取消时间 // 拒单需要退款根据订单id更新订单状态拒单原因取消时间
@ -433,14 +444,15 @@ public class OrderServiceImpl implements OrderService {
//支付状态 //支付状态
Integer payStatus = ordersDB.getPayStatus(); Integer payStatus = ordersDB.getPayStatus();
if (payStatus == 1) { if (Objects.equals(payStatus, OrdersConstant.PAID)) {
//用户已支付需要退款 // //用户已支付需要退款
String refund = weChatPayUtil.refund( // String refund = weChatPayUtil.refund(
ordersDB.getNumber(), // ordersDB.getNumber(),
ordersDB.getNumber(), // ordersDB.getNumber(),
new BigDecimal(0.01), // new BigDecimal(0.01),
new BigDecimal(0.01)); // new BigDecimal(0.01));
log.info("申请退款:{}", refund); // log.info("申请退款:{}", refund);
log.info("取消订单,退款给用户");
} }
// 管理端取消订单需要退款根据订单id更新订单状态取消原因取消时间 // 管理端取消订单需要退款根据订单id更新订单状态取消原因取消时间
@ -499,6 +511,27 @@ public class OrderServiceImpl implements OrderService {
orderMapper.update(orders); orderMapper.update(orders);
} }
/**
* 用户催单
*
* @param id
*/
@Override
public void reminder(Long id) {
// 查询订单是否存在
Orders orders = orderMapper.getById(id);
if (orders == null) {
throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
}
//基于WebSocket实现催单
Map map = new HashMap();
map.put("type", 2);//2代表用户催单
map.put("orderId", id);
map.put("content", "订单号:" + orders.getNumber());
webSocketServer.sendToAllClient(JSON.toJSONString(map));
}
/** /**
* 通用分页+转换 VO 方法 * 通用分页+转换 VO 方法
* *

View File

@ -1,17 +0,0 @@
package com.sky.task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Slf4j
@Component
public class MyTask {
@Scheduled(cron = "0/5 * * * * ?")
public void executed(){
log.info("定時任務開始執行:{}",new Date());
}
}

View File

@ -73,6 +73,6 @@ sky:
refundNotifyUrl: ${sky.wechat.refundNotifyUrl} refundNotifyUrl: ${sky.wechat.refundNotifyUrl}
shop: shop:
address: 武汉理工大学鉴湖校区 address: 湖北省武汉市洪山区武汉理工大学鉴湖校区
baidu: baidu:
ak: ${sky.baidu.ak} ak: ${sky.baidu.ak}

View File

@ -20,6 +20,9 @@
<update id="update" parameterType="addressBook"> <update id="update" parameterType="addressBook">
update address_book update address_book
<set> <set>
<if test="userId != null">
user_id = #{userId},
</if>
<if test="consignee != null"> <if test="consignee != null">
consignee = #{consignee}, consignee = #{consignee},
</if> </if>
@ -29,6 +32,24 @@
<if test="phone != null"> <if test="phone != null">
phone = #{phone}, phone = #{phone},
</if> </if>
<if test="provinceCode != null">
province_code = #{provinceCode},
</if>
<if test="provinceName != null">
province_name = #{provinceName},
</if>
<if test="cityCode != null">
city_code = #{cityCode},
</if>
<if test="cityName != null">
city_name = #{cityName},
</if>
<if test="districtCode != null">
district_code = #{districtCode},
</if>
<if test="districtName != null">
district_name = #{districtName},
</if>
<if test="detail != null"> <if test="detail != null">
detail = #{detail}, detail = #{detail},
</if> </if>