| | |
| | | import com.ruoyi.errand.object.vo.sys.UserStatsVO; |
| | | import com.ruoyi.errand.service.OrderService; |
| | | import com.ruoyi.errand.utils.*; |
| | | import com.ruoyi.errand.utils.sms.AliyunSmsService; |
| | | import com.ruoyi.errand.utils.wx.PayResult; |
| | | import com.ruoyi.errand.utils.wx.RefundCallbackResult; |
| | | import com.ruoyi.errand.utils.wx.WechatPayService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | |
| | | @Resource |
| | | private DeliveryWebSocket deliveryWebSocket; |
| | | |
| | | @Autowired |
| | | private WechatPayService wechatPayService; |
| | | |
| | | @Override |
| | | public ConfirmOrderVO confirmOrder(ConfirmOrderDTO confirmOrderDTO) { |
| | | |
| | | ConfirmOrderVO confirmOrderVO = new ConfirmOrderVO(); |
| | | BeanUtils.copyProperties(confirmOrderDTO, confirmOrderVO); |
| | | |
| | | |
| | | |
| | | //小区是否存在 |
| | | Community community = communityMapper.selectById(confirmOrderDTO.getCommodityId()); |
| | |
| | | } |
| | | //将地址簿信息查询出来 |
| | | AddressBook addressBook = addressBookMapper.selectById(confirmOrderDTO.getAddressBookId()); |
| | | //地址簿是否存在 |
| | | if (addressBook==null||addressBook.getDelFlag().equals(DelFlagConstant.DELETE)){ |
| | | throw new ServiceException("地址簿不存在"); |
| | | } |
| | | confirmOrderVO.setRecipientName(addressBook.getRecipientName()); |
| | | confirmOrderVO.setRecipientPhone(addressBook.getRecipientPhone()); |
| | | confirmOrderVO.setAddressDetail(addressBook.getAddressDetail()); |
| | |
| | | }else if (confirmOrderDTO.getPayMethod()==1){ |
| | | confirmOrderVO.setPayMethod(1); |
| | | //会员支付 |
| | | if (appuser.getEndTime().isAfter(LocalDateTime.now())) { |
| | | if (appuser.getEndTime().isBefore(LocalDateTime.now())) { |
| | | //过期了 |
| | | throw new ServiceException("支付错误:会员已到期"); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R orderPayment(ConfirmOrderDTO confirmOrderDTO) { |
| | | Order order = new Order(); |
| | | BeanUtils.copyProperties(confirmOrderDTO, order); |
| | |
| | | |
| | | //将地址簿信息查询出来 |
| | | AddressBook addressBook = addressBookMapper.selectById(confirmOrderDTO.getAddressBookId()); |
| | | //地址簿是否存在 |
| | | if (addressBook==null||addressBook.getDelFlag().equals(DelFlagConstant.DELETE)){ |
| | | throw new ServiceException("地址簿不存在"); |
| | | } |
| | | order.setRecipientName(addressBook.getRecipientName()); |
| | | order.setRecipientPhone(addressBook.getRecipientPhone()); |
| | | order.setAddressDetail(addressBook.getAddressDetail()); |
| | |
| | | if ( BigDecimal.ZERO.compareTo(paymentMoney) < 0){ |
| | | //调起微信支付 |
| | | String agencyMatters = order.getAgencyMatters(); |
| | | UniPayResult uniPayResult = PaymentUtil.uniPay(order.getOrderNumber(), paymentMoney.doubleValue(), "代办事项", |
| | | agencyMatters, "", "/app/order/orderPaymentCallback", appuser.getWxOpenid(), null); |
| | | if(null == uniPayResult || !"100".equals(uniPayResult.getRa_Code())){ |
| | | return R.fail(null == uniPayResult ? "支付失败" : uniPayResult.getRb_CodeMsg()); |
| | | try { |
| | | R r = wechatPayService.unifiedOrder(order.getId().toString(), order.getOrderNumber(), paymentMoney.toString(), agencyMatters,appuser.getWxOpenid(),"/app/order/orderPaymentCallback"); |
| | | if (null == r || 200 != r.getCode()){ |
| | | //支付失败 删除订单 |
| | | order.setDelFlag(1); |
| | | this.updateById(order); |
| | | //返回报错信息 |
| | | return R.fail(null == r ? "支付失败" : r.getMsg()); |
| | | } |
| | | //将支付数据添加到redis队列中,便于定时任务去校验是否完成支付,没有完成支付支付,15分钟后关闭订单。 |
| | | long second = LocalDateTime.now().plusMinutes(15).toEpochSecond(ZoneOffset.UTC); |
| | | redisTemplate.opsForZSet().add("OrderPayment", order.getOrderNumber(), second); |
| | | return r; |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | String rc_result = uniPayResult.getRc_Result(); |
| | | JSONObject jsonObject = JSON.parseObject(rc_result); |
| | | jsonObject.put("orderId", order.getId().toString()); |
| | | //将支付数据添加到redis队列中,便于定时任务去校验是否完成支付,没有完成支付支付,15分钟后关闭订单。 |
| | | long second = LocalDateTime.now().plusMinutes(15).toEpochSecond(ZoneOffset.UTC); |
| | | redisTemplate.opsForZSet().add("OrderPayment", order.getOrderNumber(), second); |
| | | return R.ok(jsonObject.toJSONString()); |
| | | |
| | | } |
| | | |
| | | //会员支付或支付金额为0直接支付成功 |
| | |
| | | order.setOrderTime(LocalDateTime.now());//下单时间 |
| | | this.updateById(order); |
| | | Map<String,Object> courier= appUserMapper.getCourierByCommunityId(order.getCommunityId()); |
| | | if (courier == null || courier.isEmpty()) { |
| | | if (courier == null || courier.isEmpty() ||null == courier.get("id") ) { |
| | | log.warn("未找到社区ID={}对应的骑手", order.getCommunityId()); |
| | | return R.ok(); |
| | | } |
| | | //todo 短信通知 |
| | | if (courier.get("status").equals("0")){ |
| | | log.warn("当前社区骑手离职中,骑手id={}", courier.get("courierId")); |
| | | return R.ok(); |
| | | } |
| | | Integer courierIdLong = (Integer) courier.get("courierId"); |
| | | order.setCourierId(courierIdLong); |
| | | this.updateById(order); |
| | | sendSmsNotificationToCourier(courier,order); |
| | | //小程序弹窗通知 |
| | | notifyDeliveryPerson((Long) courier.get("id"), order,1);//1=新订单 |
| | |
| | | return; |
| | | } |
| | | // String content = String.format("您有新的跑腿订单(订单号:%s),请及时处理!", order.getOrderNumber()); |
| | | //todo 调用短信服务API |
| | | // SMSUtil.sendSms(order.getOrderNumber(),phoneNumber, "", ""); |
| | | AliyunSmsService.sendOrderRemindSms(phoneNumber); |
| | | log.info("已发送短信通知给骑手: {}", phoneNumber); |
| | | } catch (Exception e) { |
| | | log.error("发送短信通知失败", e); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public R orderPaymentCallback(UniPayCallbackResult uniPayCallbackResult) { |
| | | Order order = this.getBaseMapper().selectOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, uniPayCallbackResult.getR2_OrderNo())); |
| | | public R orderPaymentCallback(PayResult payResult) { |
| | | Order order = this.getBaseMapper().selectOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, payResult.getOrderNumber())); |
| | | if(null == order || order.getPayStatus() == 2){ |
| | | return R.ok(); |
| | | } |
| | | order.setOrderStatus(1);//待确认 |
| | | order.setPayStatus(2);//已支付 |
| | | order.setOrderTime(LocalDateTime.now());//下单时间 |
| | | String r7TrxNo = uniPayCallbackResult.getR9_BankTrxNo(); |
| | | String r7TrxNo = payResult.getTransactionId(); |
| | | order.setSerialNumber(r7TrxNo); |
| | | this.updateById(order); |
| | | |
| | |
| | | log.warn("未找到社区ID={}对应的骑手", order.getCommunityId()); |
| | | return R.ok(); |
| | | } |
| | | //todo 短信通知 |
| | | if (courier.get("status").equals("0")){ |
| | | log.warn("当前社区骑手离职中,骑手id={}", courier.get("courierId")); |
| | | return R.ok(); |
| | | } |
| | | Integer courierIdLong = (Integer) courier.get("courierId"); |
| | | order.setCourierId(courierIdLong); |
| | | this.updateById(order); |
| | | |
| | | sendSmsNotificationToCourier(courier,order); |
| | | //小程序弹窗通知 |
| | | notifyDeliveryPerson((Long) courier.get("id"), order,1);//1=新订单 |
| | |
| | | continue; |
| | | } |
| | | //开始执行关闭订单操作 |
| | | CloseOrderResult closeOrderResult = PaymentUtil.closeOrder(order.getOrderNumber()); |
| | | if((null == closeOrderResult || !closeOrderResult.getRa_Status().equals("100")) && |
| | | Arrays.asList("0", "4", "101", "10080000", "10080002", "10083004", "10083005").contains(closeOrderResult.getRb_Code())){ |
| | | //开始执行关闭订单操作 |
| | | Map<String, String> map = wechatPayService.closeOrder(order.getOrderNumber()); |
| | | if((null == map || !map.get("return_code").equals("SUCCESS"))){ |
| | | redisTemplate.opsForZSet().add("OrderPayment", order.getOrderNumber(), 0); |
| | | log.error("关闭订单失败:{}---->{}", order.getOrderNumber(), JSON.toJSONString(closeOrderResult)); |
| | | log.error("关闭订单失败:{}---->{}", order.getOrderNumber(), map.get("return_msg")); |
| | | } |
| | | redisTemplate.opsForZSet().remove("OrderPayment", order.getOrderNumber()); |
| | | //删除订单 |
| | |
| | | ||!Objects.equals(order.getAppUserId(), appuser.getId())){ |
| | | throw new ServiceException("订单id错误"); |
| | | } |
| | | if (order.getOrderStatus()!=1){ |
| | | if (order.getOrderStatus()!=1 && order.getOrderStatus()!=2){ |
| | | throw new ServiceException("订单状态错误"); |
| | | } |
| | | //判断地址簿id是否存在 |
| | | if (setConfirmOrderDTO.getAddressBookId()!=null){ |
| | | AddressBook addressBook = addressBookMapper.selectById(setConfirmOrderDTO.getAddressBookId()); |
| | | if (addressBook==null||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)){ |
| | | if (addressBook==null||addressBook.getDelFlag().equals(DelFlagConstant.DELETE)){ |
| | | throw new ServiceException("该地址簿id不存在"); |
| | | } |
| | | if (!Objects.equals(addressBook.getCommunityId(), order.getCommunityId())){ |
| | |
| | | } |
| | | |
| | | @Override |
| | | public R refundPayMoneyCallback(RefundCallbackResult refundCallbackResult) { |
| | | String code = refundCallbackResult.getR3_RefundOrderNo().substring(1); |
| | | Order order = this.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, code)); |
| | | public R refundPayMoneyCallback(String xmlData) { |
| | | RefundCallbackResult result = wechatPayService.processRefundCallback(xmlData); |
| | | if (!result.isSuccess()) { |
| | | return R.fail(result.getMsg()); |
| | | } |
| | | Order order = this.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, result.getOrderNo())); |
| | | if (null == order || order.getPayStatus() == 1 || order.getOrderStatus() == 6) { |
| | | return R.ok(); |
| | | } |
| | | order.setRefundCode(refundCallbackResult.getR5_RefundTrxNo()); |
| | | order.setRefundCode(result.getRefundNo()); |
| | | order.setRefundStatus(2); |
| | | order.setRefundTime(LocalDateTime.now()); |
| | | this.updateById(order); |
| | |
| | | OrderTopInfoVO orderTopInfoVO = new OrderTopInfoVO(); |
| | | //总金额 |
| | | Map<String, Object> total = this.baseMapper.getOrderTopInfoByDate(communityId,null,null); |
| | | orderTopInfoVO.setTodayOrderNum(Integer.valueOf(String.valueOf(total.get("num"))) ); |
| | | orderTopInfoVO.setTotalOrderNum(Integer.valueOf(String.valueOf(total.get("num"))) ); |
| | | orderTopInfoVO.setTotalOrderAmount(new BigDecimal(String.valueOf(total.get("amount")))); |
| | | |
| | | //今日 |
| | |
| | | public IPage<FinanceStatisticsVO> financeStatistics(FinanceStatisticsDTO dto) { |
| | | //查询出总的数量 |
| | | IPage<FinanceStatisticsVO> page = new Page<>(); |
| | | dto.setPageNum(Math.max(dto.getPageNum() - 1, 0)*dto.getPageSize()); |
| | | page.setTotal(this.getBaseMapper().selectPageTotal(dto)); |
| | | List<FinanceStatisticsVO> list=this.getBaseMapper().financeStatistics(dto); |
| | | page.setRecords(list); |
| | |
| | | */ |
| | | public R refundPayMoney(Order order) { |
| | | //开始退款 |
| | | BigDecimal paymentAmount = order.getPaymentAmount(); |
| | | if (BigDecimal.ZERO.compareTo(order.getPaymentAmount()) < 0) {//支付的金额是否大于0 |
| | | //微信退款 |
| | | RefundResult refund = PaymentUtil.refund(order.getOrderNumber(), "R" + order.getOrderNumber(), paymentAmount.doubleValue(), |
| | | "/app/order/refundPayMoneyCallback"); |
| | | if (!"100".equals(refund.getRa_Status())) { |
| | | return R.fail(refund.getRc_CodeMsg());//退款失败 |
| | | Map<String,String> map = wechatPayService.refund(order.getOrderNumber(), order.getOrderNumber(), order.getPaymentAmount().toString(), order.getPaymentAmount().toString(), "退款", "/order/refundPayMoneyCallback"); |
| | | |
| | | if (!"SUCCESS".equals(map.get("return_code"))) { |
| | | return R.fail(map.get("return_msg"));//退款失败 |
| | | } |
| | | //这里申请成功后先返回 |
| | | order.setRefundStatus(1); |
| | | this.updateById(order); |
| | | return R.ok(); |
| | | } |
| | | return R.ok(); |
| | | } |