OrderController
/*** 接单** @param ordersConfirmDTO* @return*/@PutMapping("/confirm")@ApiOperation("接单")public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {orderService.confirm(ordersConfirmDTO);return Result.success();}/*** 拒单** @param ordersRejectionDTO* @return* @throws Exception*/@PutMapping("/rejection")@ApiOperation("拒单")public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {orderService.rejection(ordersRejectionDTO);return Result.success();}
OrderService
/*** 接单** @param ordersConfirmDTO*/public void confirm(OrdersConfirmDTO ordersConfirmDTO) {// 接单的逻辑其实就是将订单的状态改为CONFIRMEDOrders orders = Orders.builder().id(ordersConfirmDTO.getId()).status(Orders.CONFIRMED).build();orderMapper.update(orders);}/*** 抽取出来的退款方法** @param ordersDelete* @throws Exception*/// TODO 这个退款方法不应该放在此处,应该在util中public void refund(Orders ordersDelete) throws Exception {String refund = weChatPayUtil.refund(ordersDelete.getNumber(),ordersDelete.getNumber(),new BigDecimal(String.valueOf(ordersDelete.getAmount())),new BigDecimal(String.valueOf(ordersDelete.getAmount())));log.info("申请退款:{}", refund);}/*** 拒单** @param ordersRejectionDTO*/@Overridepublic void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception{// 根据id查询订单Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId());// 当订单存在并且状态为2(待接单)才可以拒单if (ordersDB == null) {// 若订单为null,那么无法拒单,抛出异常throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);}if (!ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {// 若订单状态不是待接单,那么也无法拒单,抛出异常throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);}// 查询支付状态,若已支付,那么需要退款Integer payStatus = ordersDB.getPayStatus();if (payStatus.equals(Orders.PAID)) {//用户已支付,需要退款refund(ordersDB);}// 拒单需要退款,根据订单id更新订单状态、拒单原因、取消时间Orders orders = new Orders();orders.setId(ordersDB.getId());orders.setStatus(Orders.CANCELLED);orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());orders.setCancelTime(LocalDateTime.now());orderMapper.update(orders);}
OrderMapper
/*** 修改订单信息* @param orders*/void update(Orders orders);
<update id="update" parameterType="com.sky.entity.Orders">update orders<set><if test="cancelReason != null and cancelReason!='' ">cancel_reason=#{cancelReason},</if><if test="rejectionReason != null and rejectionReason!='' ">rejection_reason=#{rejectionReason},</if><if test="cancelTime != null">cancel_time=#{cancelTime},</if><if test="payStatus != null">pay_status=#{payStatus},</if><if test="payMethod != null">pay_method=#{payMethod},</if><if test="checkoutTime != null">checkout_time=#{checkoutTime},</if><if test="status != null">status = #{status},</if><if test="deliveryTime != null">delivery_time = #{deliveryTime}</if></set>where id = #{id}</update>