8.13 来单提醒

This commit is contained in:
zhangsan 2024-08-13 09:29:47 +08:00
parent 1aca8cfc24
commit 6fd41837ae
16 changed files with 487 additions and 0 deletions

View File

@ -4,12 +4,14 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理 @EnableTransactionManagement //开启注解方式的事务管理
@Slf4j @Slf4j
@EnableCaching @EnableCaching
@EnableScheduling
public class SkyApplication { public class SkyApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SkyApplication.class, args); SpringApplication.run(SkyApplication.class, args);

View File

@ -0,0 +1,18 @@
package com.sky.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* WebSocket配置类用于注册WebSocket的Bean
*/
@Configuration
public class WebSocketConfiguration {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

View File

@ -2,10 +2,12 @@ package com.sky.controller.user;
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.Result; import com.sky.result.Result;
import com.sky.service.OrderService; import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO; import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -40,4 +42,36 @@ public class OrderController {
orderService.paySuccess(ordersPaymentDTO.getOrderNumber()); orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
return Result.success(orderPaymentVO); 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();
}
} }

View File

@ -1,6 +1,7 @@
package com.sky.mapper; package com.sky.mapper;
import com.sky.entity.OrderDetail; import com.sky.entity.OrderDetail;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.List; import java.util.List;
@ -8,4 +9,8 @@ import java.util.List;
@Mapper @Mapper
public interface OrderDetailMapper { public interface OrderDetailMapper {
void insertBatch(List<OrderDetail> orderDetailList); void insertBatch(List<OrderDetail> orderDetailList);
List<OrderDetail> getByOrderId(Long orderId);
} }

View File

@ -1,9 +1,15 @@
package com.sky.mapper; package com.sky.mapper;
import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders; import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.List;
@Mapper @Mapper
public interface OrderMapper { public interface OrderMapper {
void insert(Orders order); void insert(Orders order);
@ -19,4 +25,13 @@ public interface OrderMapper {
* @param orders * @param orders
*/ */
void update(Orders orders); 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);
@Select("select * from orders where status = #{status} and order_time < #{orderTime}")
List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime orderTime);
} }

View File

@ -18,4 +18,6 @@ public interface ShoppingCartMapper {
void deleteByUserId(Long currentId); void deleteByUserId(Long currentId);
@Delete("delete from shopping_cart where id = #{id}") @Delete("delete from shopping_cart where id = #{id}")
void deleteById(Long id); void deleteById(Long id);
void insertBatch(List<ShoppingCart> shoppingCartList);
} }

View File

@ -2,8 +2,10 @@ package com.sky.service;
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.vo.OrderPaymentVO; import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
public interface OrderService { public interface OrderService {
OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO); OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO);
@ -15,4 +17,12 @@ public interface OrderService {
* @param outTradeNo * @param outTradeNo
*/ */
void paySuccess(String outTradeNo); 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);
} }

View File

@ -1,8 +1,12 @@
package com.sky.service.impl; package com.sky.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant; import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext; import com.sky.context.BaseContext;
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.entity.*; import com.sky.entity.*;
@ -10,10 +14,13 @@ 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.result.PageResult;
import com.sky.service.OrderService; import com.sky.service.OrderService;
import com.sky.utils.WeChatPayUtil; import com.sky.utils.WeChatPayUtil;
import com.sky.vo.OrderPaymentVO; import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO; import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,7 +29,10 @@ import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service @Service
public class OrderServiceImpl implements OrderService { public class OrderServiceImpl implements OrderService {
@ -38,6 +48,8 @@ public class OrderServiceImpl implements OrderService {
private WeChatPayUtil weChatPayUtil; private WeChatPayUtil weChatPayUtil;
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;
@Autowired
private WebSocketServer webSocketServer;
@Override @Override
@Transactional @Transactional
public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) { public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {
@ -136,5 +148,125 @@ public class OrderServiceImpl implements OrderService {
.build(); .build();
orderMapper.update(orders); 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);
}
}

View File

@ -0,0 +1,17 @@
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

@ -0,0 +1,53 @@
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 * * * * ? ")
public void processTimeoutOrder(){
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);
if(ordersList != null && ordersList.size() > 0){
ordersList.forEach(order -> {
order.setStatus(Orders.CANCELLED);
order.setCancelReason("支付超时,自动取消");
order.setCancelTime(LocalDateTime.now());
orderMapper.update(order);
});
}
}
@Scheduled(cron = "0 0 1 * * ?")
public void processDeliveryOrder() {
log.info("处理派送中订单:{}", new Date());
// select * from orders where status = 4 and order_time < 当前时间-1小时
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);
});
}
}
}

View File

@ -0,0 +1,22 @@
package com.sky.task;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class WebSocketTask {
@Autowired
private WebSocketServer webSocketServer;
/**
* 通过WebSocket每隔5秒向客户端发送消息
*/
@Scheduled(cron = "0/5 * * * * ?")
public void sendMessageToClient() {
webSocketServer.sendToAllClient("这是来自服务端的消息:" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
}
}

View File

@ -0,0 +1,71 @@
package com.sky.websocket;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* WebSocket服务
*/
@Component
@ServerEndpoint("/ws/{sid}")
public class WebSocketServer {
//存放会话对象
private static Map<String, Session> sessionMap = new HashMap();
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session, @PathParam("sid") String sid) {
System.out.println("客户端:" + sid + "建立连接");
sessionMap.put(sid, session);
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, @PathParam("sid") String sid) {
System.out.println("收到来自客户端:" + sid + "的信息:" + message);
}
/**
* 连接关闭调用的方法
*
* @param sid
*/
@OnClose
public void onClose(@PathParam("sid") String sid) {
System.out.println("连接断开:" + sid);
sessionMap.remove(sid);
}
/**
* 群发
*
* @param message
*/
public void sendToAllClient(String message) {
Collection<Session> sessions = sessionMap.values();
for (Session session : sessions) {
try {
//服务器向客户端发送消息,这里客户端是指商家后台管理的浏览器界面
session.getBasicRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,68 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket Demo</title>
</head>
<body>
<input id="text" type="text" />
<button onclick="send()">发送消息</button>
<button onclick="closeWebSocket()">关闭连接</button>
<div id="message">
</div>
</body>
<script type="text/javascript">
var websocket = null;
var clientId = Math.random().toString(36).substr(2);
//判断当前浏览器是否支持WebSocket
if('WebSocket' in window){
//连接WebSocket节点
websocket = new WebSocket("ws://localhost:8080/ws/"+clientId);
}
else{
alert('Not support websocket')
}
//连接发生错误的回调方法
websocket.onerror = function(){
setMessageInnerHTML("error");
};
//连接成功建立的回调方法
websocket.onopen = function(){
setMessageInnerHTML("连接成功");
}
//接收到消息的回调方法
websocket.onmessage = function(event){
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function(){
setMessageInnerHTML("close");
}
//监听窗口关闭事件当窗口关闭时主动去关闭websocket连接防止连接还没断开就关闭窗口server端会抛异常。
window.onbeforeunload = function(){
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML){
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//发送消息
function send(){
var message = document.getElementById('text').value;
websocket.send(message);
}
//关闭连接
function closeWebSocket() {
websocket.close();
}
</script>
</html>

View File

@ -12,4 +12,10 @@
#{od.number},#{od.amount},#{od.image}) #{od.number},#{od.amount},#{od.image})
</foreach> </foreach>
</insert> </insert>
<select id="getByOrderId" resultType="com.sky.entity.OrderDetail">
select * from order_detail
<where>
order_id=#{orderId}
</where>
</select>
</mapper> </mapper>

View File

@ -42,4 +42,28 @@
</set> </set>
where id = #{id} where id = #{id}
</update> </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 &gt;= #{beginTime}
</if>
<if test="endTime != null">
and order_time &lt;= #{endTime}
</if>
</where>
order by order_time desc
</select>
</mapper> </mapper>

View File

@ -6,6 +6,14 @@
insert into shopping_cart (name, user_id, dish_id, setmeal_id, dish_flavor, number, amount, image, create_time) insert into shopping_cart (name, user_id, dish_id, setmeal_id, dish_flavor, number, amount, image, create_time)
values (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime}) values (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime})
</insert> </insert>
<insert id="insertBatch" parameterType="list">
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>
<update id="updateNumberById"> <update id="updateNumberById">
update shopping_cart set number = #{number} where id = #{id} update shopping_cart set number = #{number} where id = #{id}
</update> </update>