苍穹外卖项目学习日记(12) day09
历史订单查询
controller 层OrderController添加历史订单查询方法OrderController.java
@GetMapping ( "/historyOrders" ) @ApiOperation ( "历史订单查询" ) public Result < PageResult > page ( OrdersPageQueryDTO ordersPageQueryDTO) { log. info ( "历史订单查询:{}" , ordersPageQueryDTO) ; PageResult pageResult = orderService. pageQuery ( ordersPageQueryDTO) ; return Result . success ( pageResult) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public PageResult pageQuery ( OrdersPageQueryDTO ordersPageQueryDTO) { PageHelper . startPage ( ordersPageQueryDTO. getPage ( ) , ordersPageQueryDTO. getPageSize ( ) ) ; Page < Orders > page = orderMapper. pageQuery ( ordersPageQueryDTO) ; List < OrderVO > orderVOList = new ArrayList < > ( ) ; if ( page != null && page. size ( ) > 0 ) { for ( Orders orders: page) { Long id = orders. getId ( ) ; List < OrderDetail > orderDetails = OrderDetailMapper . getByOrderId ( id) ; OrderVO orderVO = new OrderVO ( ) ; BeanUtils . copyProperties ( orders, orderVO) ; orderVO. setOrderDetailList ( orderDetails) ; } } return new PageResult ( page. getTotal ( ) , orderVOList) ; }
在mapper层添加相应方法,并且在xml中实现 orderMapper.xml
< 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>
@Select ( "select * from order_detail where order_id = #{orderId}" ) List < OrderDetail > getByOrderId ( Long orderId) ;
查询订单详情
controller 层OrderController添加订单详情方法OrderController.java
@GetMapping ( "/orderDetail/{id}" ) @ApiOperation ( "查询订单详情" ) public Result < OrderVO > details ( @PathVariable ( "id" ) Long id) { OrderVO orderVO = orderService. details ( id) ; return Result . success ( orderVO) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public OrderVO details ( Long id) { OrderVO orderVO = new OrderVO ( ) ; Orders orders = orderMapper. getByOrderId ( id) ; List < OrderDetail > orderDetailList = orderDetailMapper. getByOrderId ( orders. getId ( ) ) ; BeanUtils . copyProperties ( orders, orderVO) ; orderVO. setOrderDetailList ( orderDetailList) ; return orderVO; }
取消订单
controller 层OrderController添加取消订单方法OrderController.java
@PutMapping ( "/cancel/{id}" ) @ApiOperation ( "取消订单" ) public Result cancel ( @PathVariable Long id) { log. info ( "取消订单:{}" , id) ; orderService. cancelById ( id) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public void cancelById ( Long id) { Orders orders = orderMapper. getByOrderId ( id) ; Integer status = orders. getStatus ( ) ; if ( orders == null ) { throw new OrderBusinessException ( MessageConstant . ORDER_NOT_FOUND ) ; } if ( orders. getStatus ( ) > 2 ) { throw new OrderBusinessException ( MessageConstant . ORDER_STATUS_ERROR ) ; } if ( status == Orders . PENDING_PAYMENT || status == Orders . TO_BE_CONFIRMED ) { orders. setStatus ( Orders . CANCELLED ) ; orders. setCancelTime ( LocalDateTime . now ( ) ) ; orders. setCancelReason ( "用户取消" ) ; orderMapper. update ( orders) ; } }
再来一单
controller 层OrderController添加再来一单方法OrderController.java
@PostMapping ( "/repetition/{id}" ) @ApiOperation ( "再来一单" ) public Result repeat ( @PathVariable Long id) { log. info ( "再来一单:{}" , id) ; orderService. repeat ( id) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public void repeat ( Long id) { List < OrderDetail > orderDetailList = orderDetailMapper. getByOrderId ( id) ; List < ShoppingCart > shoppingCartList = new ArrayList < > ( ) ; Long currentId = BaseContext . getCurrentId ( ) ; for ( OrderDetail orderDetail: orderDetailList) { ShoppingCart shoppingCart = new ShoppingCart ( ) ; BeanUtils . copyProperties ( orderDetail, shoppingCart) ; shoppingCart. setUserId ( currentId) ; shoppingCart. setCreateTime ( LocalDateTime . now ( ) ) ; shoppingCartList. add ( shoppingCart) ; } shoppingCartMapper. insertBatch ( shoppingCartList) ; }
在mapper层添加相应方法,并且在xml中实现 shoppingCartMapper.xml
< insert id= "insertBatch" > insert into shopping_cart ( name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount, create_time) values< foreach collection= "shoppingCartList" item= "sc" separator= "," > ( #{ sc. name} , #{ sc. image} , #{ sc. userId} , #{ sc. dishId} , #{ sc. setmealId} , #{ sc. dishFlavor} , #{ sc. number} , #{ sc. amount} , #{ sc. createTime} ) < / foreach> < / insert>
订单搜索
admin的controller层创建OrderController类,并且添加条件搜索方法 OrderController.java
package com. sky. controller. admin ; import com. sky. dto. OrdersPageQueryDTO ;
import com. sky. result. PageResult ;
import com. sky. result. Result ;
import com. sky. service. OrderService ;
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. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ; @RestController ( "adminOrderController" )
@Api ( tags = "订单管理接口" )
@RequestMapping ( "/admin/order" )
@Slf4j
public class OrderController { @Autowired private OrderService orderService; @GetMapping ( "/conditionSearch" ) @ApiOperation ( "订单搜索" ) public Result < PageResult > conditionSearch ( OrdersPageQueryDTO ordersPageQueryDTO) { log. info ( "订单搜索:{}" , ordersPageQueryDTO) ; PageResult pageResult = orderService. conditionSearch ( ordersPageQueryDTO) ; return Result . success ( pageResult) ; }
}
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public PageResult conditionSearch ( OrdersPageQueryDTO ordersPageQueryDTO) { PageHelper . startPage ( ordersPageQueryDTO. getPage ( ) , ordersPageQueryDTO. getPageSize ( ) ) ; Page < Orders > page = orderMapper. pageQuery ( ordersPageQueryDTO) ; List < OrderVO > orderVOList = getOrderVOList ( page) ; return new PageResult ( page. getTotal ( ) , orderVOList) ; } private List < OrderVO > getOrderVOList ( Page < Orders > page) { List < OrderVO > orderVOList = new ArrayList < > ( ) ; List < Orders > ordersList= page. getResult ( ) ; if ( ordersList != null && ordersList. size ( ) > 0 ) { for ( Orders orders : ordersList) { OrderVO orderVO = new OrderVO ( ) ; BeanUtils . copyProperties ( orders, orderVO) ; orderVO. setOrderDishes ( getOrderDishs ( orders) ) ; orderVOList. add ( orderVO) ; } } return orderVOList; } private String getOrderDishs ( Orders orders) { List < OrderDetail > orderDetailList = orderDetailMapper. getByOrderId ( orders. getId ( ) ) ; List < String > orderDishList = orderDetailList. stream ( ) . map ( x -> { String orderDish = x. getName ( ) + "*" + x. getNumber ( ) + ";" ; return orderDish; } ) . collect ( Collectors . toList ( ) ) ; return String . join ( "" , orderDishList) ; }
各个状态的订单数量统计
admin的controller层OrderController类,添加条件订单数量统计方法 OrderController.java
@GetMapping ( "/statistics" ) @ApiOperation ( "订单数量统计" ) public Result < OrderStatisticsVO > statistics ( ) { log. info ( "订单数量统计" ) ; OrderStatisticsVO orderStatisticsVO = orderService. statistics ( ) ; return Result . success ( orderStatisticsVO) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
public OrderStatisticsVO statistics ( ) { OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO ( ) ; OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO ( ) ; ordersPageQueryDTO. setStatus ( Orders . TO_BE_CONFIRMED ) ; orderStatisticsVO. setToBeConfirmed ( orderMapper. pageQuery ( ordersPageQueryDTO) . getResult ( ) . size ( ) ) ; ordersPageQueryDTO. setStatus ( Orders . CONFIRMED ) ; orderStatisticsVO. setConfirmed ( orderMapper. pageQuery ( ordersPageQueryDTO) . getResult ( ) . size ( ) ) ; ordersPageQueryDTO. setStatus ( Orders . DELIVERY_IN_PROGRESS ) ; orderStatisticsVO. setDeliveryInProgress ( orderMapper. pageQuery ( ordersPageQueryDTO) . getResult ( ) . size ( ) ) ; return orderStatisticsVO; }
发现问题:在获取page后,使用getTotal返回值一直为0,猜测可能是pageHelper的配置问题,所以使用先获取了result再获取sizie的方法
查询订单详情
admin的controller层OrderController类,添加查询订单详情方法 OrderController.java
@GetMapping ( "/details/{id}" ) @ApiOperation ( "查询订单详情" ) public Result < OrderVO > details ( @PathVariable ( "id" ) Long id) { OrderVO orderVO = orderService. details ( id) ; return Result . success ( orderVO) ; }
接单
admin的controller层OrderController类,添加接单方法 OrderController.java
@PutMapping ( "/confirm" ) @ApiOperation ( "接单" ) public Result confirm ( @RequestBody OrdersConfirmDTO ordersConfirmDTO) { log. info ( "接单:{}" , ordersConfirmDTO) ; orderService. confirm ( ordersConfirmDTO) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public void confirm ( OrdersConfirmDTO ordersConfirmDTO) { Orders orders = new Orders ( ) ; orders. setStatus ( Orders . CONFIRMED ) ; orders. setId ( ordersConfirmDTO. getId ( ) ) ; orderMapper. update ( orders) ; }
拒单
admin的controller层OrderController类,添加拒单方法 OrderController.java
@PutMapping ( "/rejection" ) @ApiOperation ( "拒单" ) public Result rejection ( @RequestBody OrdersRejectionDTO OrdersRejectionDTO ) { log. info ( "拒单:{}" , OrdersRejectionDTO ) ; orderService. rejection ( OrdersRejectionDTO ) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public void rejection ( OrdersRejectionDTO ordersRejectionDTO) { Orders ordersDB = orderMapper. getByOrderId ( ordersRejectionDTO. getId ( ) ) ; if ( ordersDB == null || ! ordersDB. getStatus ( ) . equals ( Orders . TO_BE_CONFIRMED ) ) { throw new OrderBusinessException ( MessageConstant . ORDER_STATUS_ERROR ) ; } Orders orders = new Orders ( ) ; orders. setStatus ( Orders . CANCELLED ) ; orders. setId ( ordersRejectionDTO. getId ( ) ) ; orders. setRejectionReason ( ordersRejectionDTO. getRejectionReason ( ) ) ; orders. setCancelTime ( LocalDateTime . now ( ) ) ; orderMapper. update ( orders) ; }
取消订单
admin的controller层OrderController类,添加取消订单方法 OrderController.java
@PutMapping ( "/cancel" ) @ApiOperation ( "取消订单" ) public Result cancel ( @RequestBody OrdersCancelDTO ordersCancelDTO) { log. info ( "取消订单:{}" , ordersCancelDTO) ; orderService. cancel ( ordersCancelDTO) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
@Override public void cancel ( OrdersCancelDTO ordersCancelDTO) { Orders orders = new Orders ( ) ; orders. setStatus ( Orders . CANCELLED ) ; orders. setId ( ordersCancelDTO. getId ( ) ) ; orders. setCancelReason ( ordersCancelDTO. getCancelReason ( ) ) ; orders. setCancelTime ( LocalDateTime . now ( ) ) ; orderMapper. update ( orders) ; }
派送订单
admin的controller层OrderController类,添加派送订单方法 OrderController.java
@PutMapping ( "/delivery/{id}" ) @ApiOperation ( "派送订单" ) public Result delivery ( @PathVariable ( "id" ) Long id) { orderService. delivery ( id) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
public void delivery ( Long id) { Orders ordersDB = orderMapper. getByOrderId ( id) ; if ( ordersDB == null || ! ordersDB. getStatus ( ) . equals ( Orders . CONFIRMED ) ) { throw new OrderBusinessException ( MessageConstant . ORDER_STATUS_ERROR ) ; } Orders orders = new Orders ( ) ; orders. setId ( ordersDB. getId ( ) ) ; orders. setStatus ( Orders . DELIVERY_IN_PROGRESS ) ; orderMapper. update ( orders) ; }
完成订单
admin的controller层OrderController类,添加完成订单方法 OrderController.java
@PutMapping ( "/complete/{id}" ) @ApiOperation ( "完成订单" ) public Result complete ( @PathVariable ( "id" ) Long id) { orderService. complete ( id) ; return Result . success ( ) ; }
在OrderService 中添加相应方法,并且在实现类中实现 OrderServiceImpl.java
public void complete ( Long id) { Orders ordersDB = orderMapper. getByOrderId ( id) ; if ( ordersDB == null || ! ordersDB. getStatus ( ) . equals ( Orders . DELIVERY_IN_PROGRESS ) ) { throw new OrderBusinessException ( MessageConstant . ORDER_STATUS_ERROR ) ; } Orders orders = new Orders ( ) ; orders. setId ( ordersDB. getId ( ) ) ; orders. setStatus ( Orders . COMPLETED ) ; orders. setDeliveryTime ( LocalDateTime . now ( ) ) ; orderMapper. update ( orders) ; }
优化用户下单
配置外卖商家店铺地址和百度地图的AK: application.yml
shop: address: 青海省西宁市城北区宁大路235 号青海大学baidu: ak: nbNi7uwUlppkzYC2pXsQafERrglnCf4G
改造OrderServiceImpl ,注入上面的配置项,并且在OrderServiceImpl 中提供校验方法: OrderServiceImpl.java
@Value ( "${sky.shop.address}" ) private String shopAddress; @Value ( "${sky.baidu.ak}" ) private String ak; private void checkOutOfRange ( String address) { Map map = new HashMap ( ) ; map. put ( "address" , shopAddress) ; map. put ( "output" , "json" ) ; map. put ( "ak" , ak) ; 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 ) { throw new OrderBusinessException ( "超出配送范围" ) ; } }
在OrderServiceImpl 的submitOrder方法中调用上面的校验方法:
windwos后台启动程序
进入需要启动的文件目录下,输入powershell 运行Start-Process -WindowStyle hidden 你的程序
即可
前后端数据传参的区别(@RequestParam,@PathVariable和@RequestBody)
http: / / localhost: 8090 / hello? id= 2
public String Demo1 ( @RequestParam String id) { System . out. println ( id) ; return null ;
}
http: / / localhost: 8090 / hello/ 2
@RequestMapping ( value = "/getBook/{id}" , method = RequestMethod . GET )
public String getBook ( @PathVariable Integer id) { System . out. println ( id) ; return null ;
}
@RequestBody 一般是在请求体中,传递比较多参数时使用,可以实现自动封装,即可以以实体类在controller中作为形参