| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.lang.Snowflake; |
| | | import cn.hutool.core.util.XmlUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; |
| | | import com.github.binarywang.wxpay.config.WxPayConfig; |
| | | import com.github.binarywang.wxpay.constant.WxPayConstants; |
| | | import com.github.binarywang.wxpay.service.WxPayService; |
| | | import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.common.third.pay.wx.WxFastPayService; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3HeartbeatReportInnerFrame; |
| | | import com.sandu.ximon.admin.pay.OrderStatusEnums; |
| | | import com.sandu.ximon.admin.pay.wx.WxFastPayService; |
| | | import com.sandu.ximon.admin.pay.wxpay.UsrWxPayConfigService; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.admin.utils.AliPayUtils; |
| | | import com.sandu.ximon.admin.vo.C3mOrderVO; |
| | | import com.sandu.ximon.dao.bo.C3mOrderBo; |
| | | import com.sandu.ximon.dao.domain.C3mCharging; |
| | | import com.sandu.ximon.dao.domain.C3mOrder; |
| | | import com.sandu.ximon.dao.domain.Pole; |
| | | import com.sandu.ximon.dao.domain.WxConfigEntity; |
| | | import com.sandu.ximon.dao.enums.OrderStatus; |
| | | import com.sandu.ximon.dao.enums.OrderType; |
| | | import com.sandu.ximon.dao.mapper.C3mOrderMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @Author liuhaonan |
| | |
| | | @AllArgsConstructor |
| | | public class C3mOrderService extends BaseServiceImpl<C3mOrderMapper, C3mOrder> { |
| | | |
| | | private final PoleService poleService; |
| | | private final C3mOrderMapper c3mOrderMapper; |
| | | private final WxFastPayService fastPayService; |
| | | private final Snowflake snowflake; |
| | | |
| | | public boolean orderRefund(String outTradeNo, Double refundAmount, Long userId, String username) { |
| | | private final C3ChargingService c3ChargingService; |
| | | private final UsrWxPayConfigService usrWxPayConfigService; |
| | | |
| | | /** |
| | | * 退款 |
| | | * |
| | | * @param outTradeNo |
| | | * @return |
| | | */ |
| | | public boolean orderRefund(String outTradeNo) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | String username = SecurityUtils.getUsername(); |
| | | C3mOrder orderByOutTradeNo = c3mOrderMapper.getOrderByOutTradeNo(outTradeNo); |
| | | if (null == orderByOutTradeNo) { |
| | | return false; |
| | | |
| | | if(orderByOutTradeNo==null){ |
| | | throw new BusinessException("未找到订单!"); |
| | | } |
| | | //退款前进行状态判断 |
| | | if (orderByOutTradeNo.getOrderStatus().equals(OrderStatus.REFUND.getStatus()) |
| | | || orderByOutTradeNo.getOrderStatus().equals(OrderStatus.CANCEL.getStatus()) |
| | | || orderByOutTradeNo.getOrderStatus().equals(OrderStatus.NO_PAY.getStatus())) { |
| | | throw new BusinessException("订单状态错误,无法退款 !"); |
| | | } |
| | | if (orderByOutTradeNo == null) { |
| | | throw new BusinessException("未找到订单"); |
| | | } else { |
| | | orderByOutTradeNo.setRefundAmount(refundAmount); |
| | | return c3mOrderRefund(orderByOutTradeNo, "页面API退款", userId, username); |
| | | if (orderByOutTradeNo.getTotalAmount() < orderByOutTradeNo.getRefundAmount()) { |
| | | throw new BusinessException("退款金额错误,不能大于付款金额"); |
| | | } |
| | | orderByOutTradeNo.setRefundAmount(orderByOutTradeNo.getSurplusAmount()); |
| | | return c3mOrderRefund(orderByOutTradeNo, "充电桩退款", userId, username); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 退款 |
| | | * |
| | | * @param C3mOrder |
| | | * @param msg |
| | | * @param userId |
| | | * @param username |
| | | * @return |
| | | */ |
| | | // @Transactional(rollbackFor = Exception.class) |
| | | private boolean c3mOrderRefund(C3mOrder C3mOrder, String msg, Long userId, String username) { |
| | | // 进行退款,设置订单状态为已退款 |
| | | boolean b = false; |
| | |
| | | C3mOrder.getRefundAmount()); |
| | | } else if (C3mOrder.getOrderType().equals(OrderType.WXPAY.getCode())) { |
| | | // 进行微信退款 |
| | | // b = WxPayUtils.getBean().wxRefund( |
| | | // C3mOrder.getTotalAmount(), |
| | | // C3mOrder.getRefundAmount(), |
| | | // C3mOrder.getOutTradeNo(), |
| | | // C3mOrder.getPoleId() |
| | | // ); |
| | | b = fastPayService.refund(C3mOrder); |
| | | } |
| | | C3mOrder.setRefundTimestamp(new Date().getTime()); |
| | | C3mOrder.setRefundTimestamp(System.currentTimeMillis()); |
| | | C3mOrder.setRefundMsg(msg); |
| | | if (b) { |
| | | C3mOrder.setOrderStatus(OrderStatus.REFUND.getStatus()); |
| | | c3mOrderMapper.updateRefundOrder(C3mOrder); |
| | | /* logService.log( |
| | | userId, |
| | | ServerTask.SERVER_TASK, |
| | | "C3m订单处理", |
| | | null, |
| | | "{ 操作者("+username+")" + |
| | | "订单退款: "+C3mOrder.getOutTradeNo() |
| | | +",订单总金额:"+C3mOrder.getTotalAmount() |
| | | +",退款金额: "+C3mOrder.getTotalAmount()+"}");*/ |
| | | |
| | | log.error("充电桩退款 " + "{ 操作者(" + username + ")" + |
| | | "订单退款: " + C3mOrder.getOutTradeNo() |
| | | + ",订单总金额:" + C3mOrder.getTotalAmount() |
| | | + ",退款金额: " + C3mOrder.getRefundAmount() + "}"); |
| | | return true; |
| | | } else { |
| | | C3mOrder.setOrderStatus(OrderStatus.REFUND_FAILED.getStatus()); |
| | | c3mOrderMapper.updateRefundOrder(C3mOrder); |
| | | /* logService.log( |
| | | userId, |
| | | ServerTask.SERVER_TASK, |
| | | "C3m订单处理", |
| | | null, |
| | | "{ 操作者("+username+")" + |
| | | "订单退款(失败): "+C3mOrder.getOutTradeNo() |
| | | +",订单总金额:"+C3mOrder.getTotalAmount() |
| | | +",退款金额: "+C3mOrder.getTotalAmount()+"}");*/ |
| | | log.error("充电桩退款 " + "{ 操作者(" + username + ")" + |
| | | "订单退款: " + C3mOrder.getOutTradeNo() |
| | | + ",订单总金额:" + C3mOrder.getTotalAmount() |
| | | + ",退款金额: " + C3mOrder.getRefundAmount() + "}"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建订单 |
| | | * |
| | | * @param streetlightId |
| | | * @param c3m |
| | | * @param orderType |
| | | * @param totalAmount |
| | | * @param subscribeChargingCapacity |
| | | * @return |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public C3mOrder advancePayOrder(Long streetlightId, C3mCharging c3m, String orderType, Double totalAmount, |
| | | Integer subscribeChargingCapacity, String openId) throws InterruptedException { |
| | | //查询充电桩是否存在正在进行中的订单 |
| | | C3mOrder c3mOrder = getOne(Wrappers.lambdaQuery(C3mOrder.class) |
| | | .eq(C3mOrder::getC3Udid, c3m.getMcuUdid()).eq(C3mOrder::getC3Mac, c3m.getC3Mac()).last("limit 1")); |
| | | |
| | | //充电桩正在充电中 |
| | | if (c3mOrder != null && c3mOrder.getChargingStates() != null && c3mOrder.getChargingStates() == 0) { |
| | | throw new BusinessException("充电桩正在被占用!"); |
| | | } |
| | | |
| | | // //存在订单,但是未支付,需要取消订单 |
| | | // if (c3mOrder != null) { |
| | | // c3mOrder.setOrderStatus(OrderStatus.CANCEL.getStatus()); |
| | | // updateById(c3mOrder); |
| | | // } |
| | | |
| | | Pole pole = poleService.getById(streetlightId); |
| | | if (null == pole) { |
| | | throw new BusinessException("充电桩灯杆信息不存在!"); |
| | | } |
| | | // 生成订单 |
| | | C3mOrder c3mOrderEntity = new C3mOrderVO().generateOrder( |
| | | streetlightId, |
| | | pole.getDeviceCode(), |
| | | c3m.getC3Mac(), |
| | | OrderType.getOrderType(orderType), |
| | | totalAmount, |
| | | subscribeChargingCapacity |
| | | ); |
| | | c3mOrderEntity.setOrderId(snowflake.nextId()); |
| | | c3mOrderEntity.setC3Mac(c3m.getC3Name()); |
| | | c3mOrderEntity.setPoleId(streetlightId); |
| | | c3mOrderEntity.setPoleMac(pole.getDeviceCode()); |
| | | c3mOrderEntity.setPoleName(pole.getPoleName()); |
| | | c3mOrderEntity.setC3Mac(c3m.getC3Mac()); |
| | | c3mOrderEntity.setC3Name(c3m.getC3Name()); |
| | | c3mOrderEntity.setOrderStatus(0); |
| | | c3mOrderEntity.setUserCode(openId); |
| | | c3mOrderEntity.setC3Udid(c3m.getMcuUdid()); |
| | | c3mOrderEntity.setRefundAmount(totalAmount); |
| | | c3mOrderEntity.setSurplusAmount(totalAmount); |
| | | |
| | | // //确认充电桩状态 |
| | | // ChargingDto chargingInfo = c3ChargingService.getChargingInfo(streetlightId.toString()); |
| | | // if (chargingInfo.getStatusBit() != 1) { |
| | | // throw new BusinessException("充电桩非空闲状态,无法创建订单!"); |
| | | // } |
| | | // Thread.sleep(500); |
| | | // |
| | | // //同步时间戳 |
| | | // System.out.println("同步时间戳开始"); |
| | | // String date = new SimpleDateFormat("yyMMddHHmmss").format(new Date()); |
| | | // String s = c3ChargingService.SetCalendar(c3m.getC3Mac(), Integer.parseInt(date.substring(0, 2)), Integer.parseInt(date.substring(2, 4)), |
| | | // Integer.parseInt(date.substring(4, 6)), Integer.parseInt(date.substring(6, 8)), |
| | | // Integer.parseInt(date.substring(8, 10)), Integer.parseInt(date.substring(10, 12)), true); |
| | | // log.error("同步时间戳结果 : " + s); |
| | | // if (!"操作成功".equals(s)) { |
| | | // throw new BusinessException(s + "!"); |
| | | // } |
| | | // Thread.sleep(500); |
| | | // |
| | | // //同步费率 |
| | | // System.out.println("同步费率开始"); |
| | | // List<C3mChargingCharge> list = SpringContextHolder.getBean(C3mChargingChargeService.class) |
| | | // .list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3m.getC3Id())); |
| | | // if (list == null && list.size() == 0) { |
| | | // //费率表中没有此充电桩数据时 添加默认费率 |
| | | // SpringContextHolder.getBean(C3mChargingChargeService.class).initCharge((c3m.getC3Id()).intValue()); |
| | | // } |
| | | // String rateState = c3ChargingService.setRate(c3m.getC3Mac(), list, true); |
| | | // log.error("费率设置结果 : " + rateState); |
| | | // if (!"操作成功".equals(rateState)) { |
| | | // throw new BusinessException(s + "!"); |
| | | // } |
| | | |
| | | if (save(c3mOrderEntity)) { |
| | | return c3mOrderEntity; |
| | | } else { |
| | | throw new BusinessException("创建订单失败,请重新扫码!"); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 微信支付回调 小程序 |
| | | */ |
| | | public Object payOrderInstoreNotify(HttpServletRequest request, HttpServletResponse httpResponse) { |
| | | log.error("[-----------------微信支付回调开始-----------------]"); |
| | | String xmlResult = null; |
| | | try { |
| | | xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding()); |
| | | } catch (IOException e) { |
| | | throw new BusinessException(e.getMessage()); |
| | | } |
| | | log.error("request: " + xmlResult); |
| | | |
| | | if (xmlResult == null) { |
| | | return null; |
| | | } |
| | | Map<String, Object> stringObjectMap = XmlUtil.xmlToMap(xmlResult); |
| | | String appId = (String) stringObjectMap.get("appid"); |
| | | WxConfigEntity configByAppId = usrWxPayConfigService.getConfigByAppId(appId); |
| | | WxPayConfig config = new WxPayConfig(); |
| | | config.setAppId(configByAppId.getAppappid()); |
| | | config.setMchId(configByAppId.getMchId()); |
| | | config.setMchKey(configByAppId.getPrivateKey()); |
| | | |
| | | WxPayService wxPayService = new WxPayServiceImpl(); |
| | | wxPayService.setConfig(config); |
| | | |
| | | |
| | | WxPayOrderNotifyResult wxPayOrderNotifyResult = fastPayService.parseOrderNotifyResult(wxPayService, xmlResult); |
| | | if (!WxPayConstants.ResultCode.SUCCESS.equals(wxPayOrderNotifyResult.getResultCode()) |
| | | || !WxPayConstants.ResultCode.SUCCESS.equals(wxPayOrderNotifyResult.getReturnCode())) { |
| | | log.error(xmlResult); |
| | | return WxPayNotifyResponse.fail("微信通知支付失败!"); |
| | | } |
| | | |
| | | String orderSn = wxPayOrderNotifyResult.getOutTradeNo(); |
| | | String transactionId = wxPayOrderNotifyResult.getOutTradeNo(); |
| | | C3mOrder userOrder = getByOrderSn(orderSn); |
| | | updateById(userOrder); |
| | | if (userOrder == null) { |
| | | return WxPayNotifyResponse.fail("订单不存在 sn=" + orderSn); |
| | | } |
| | | |
| | | // 检查这个订单是否已经处理过 |
| | | if (!OrderStatusEnums.UNPAID.getCode().equals(userOrder.getOrderStatus())) { |
| | | return WxPayNotifyResponse.success("订单已经处理成功!"); |
| | | } |
| | | userOrder.setOutTradeNo(transactionId); |
| | | userOrder.setPayTimestamp(new Date().getTime()); |
| | | userOrder.setOrderStatus(OrderStatusEnums.PAID.getCode()); |
| | | /** |
| | | * 调起开始充电接口 |
| | | */ |
| | | String s = c3ChargingService.startCharging(userOrder.getC3Mac(), userOrder.getSubscribeChargingCapacity(), userOrder.getTotalAmount()); |
| | | if (s.isEmpty() || s == null) { |
| | | log.error("[-----------------开启充电失败-----------------]"); |
| | | throw new BusinessException("开启充电失败"); |
| | | } |
| | | //开启充电成功后,更新订单状态为充电中 |
| | | userOrder.setChargingStates(0); |
| | | userOrder.setStartChargingTimestamp(System.currentTimeMillis()); |
| | | log.error("[-----------------微信支付回调结束-----------------]"); |
| | | if (!updateById(userOrder)) { |
| | | return WxPayNotifyResponse.fail("更新数据已失效"); |
| | | } |
| | | |
| | | return WxPayNotifyResponse.success("更新数据成功"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 订单详情 by orderSn |
| | | * |
| | | * @param orderSn |
| | | * @return |
| | | */ |
| | | public C3mOrder getByOrderSn(String orderSn) { |
| | | LambdaQueryWrapper<C3mOrder> wrapper = Wrappers.lambdaQuery(C3mOrder.class).eq(C3mOrder::getOutTradeNo, orderSn).last("limit 1"); |
| | | return getOne(wrapper); |
| | | } |
| | | |
| | | /** |
| | | * 根据c3Mac获取最近一条订单 |
| | | */ |
| | | public C3mOrder getLastOrderByC3Mac(String c3Mac) { |
| | | LambdaQueryWrapper<C3mOrder> wrapper = Wrappers.lambdaQuery(C3mOrder.class).eq(C3mOrder::getC3Mac, c3Mac).last("limit 1"); |
| | | return getOne(wrapper); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据心跳包更新订单状态 |
| | | */ |
| | | public void updateOrderStatusByHeartbeat(A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage dataPackage) { |
| | | //充电中的心跳包需要去更新订单数据,否则不处理! |
| | | if (dataPackage.getStatusBit().equals("02")) { |
| | | C3mOrder c3mOrder = getOne(Wrappers.lambdaQuery(C3mOrder.class).eq(C3mOrder::getC3Mac, dataPackage.getC3Mac()) |
| | | .eq(C3mOrder::getChargingStates, 1).last("limit 1")); |
| | | if (c3mOrder == null) { |
| | | return; |
| | | } |
| | | //更新充电总量 |
| | | c3mOrder.setActualChargingCapacityHide(Double.parseDouble(dataPackage.getChargedCapacity())); |
| | | //更新剩余金额 |
| | | c3mOrder.setSurplusAmount(Double.parseDouble(dataPackage.getRemainingAmount())); |
| | | updateById(c3mOrder); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 订单列表 |
| | | * |
| | | * @param baseConditionVO |
| | | * @return |
| | | */ |
| | | public List<C3mOrderBo> orderList(BaseConditionVO baseConditionVO, String keyword, |
| | | Integer orderStatus, Integer timeType) { |
| | | if (orderStatus != null && (orderStatus > 5 || orderStatus < 0)) { |
| | | throw new BusinessException("订单状态不正确"); |
| | | } |
| | | Long userId = SecurityUtils.getClientId(); |
| | | //查询近一个月的订单 |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | LocalDateTime startTime = null; |
| | | ; |
| | | Long startTimeStamp = null; |
| | | Long nowTimeStamp = null; |
| | | if (timeType != null) { |
| | | if (timeType == 1) { |
| | | //查询近一个月的订单 |
| | | startTime = now.minusMonths(1); |
| | | } else if (timeType == 2) { |
| | | //查询近三个月的订单 |
| | | startTime = now.minusMonths(3); |
| | | } else if (timeType == 3) { |
| | | //查询近半年的订单 |
| | | startTime = now.minusMonths(6); |
| | | } else { |
| | | throw new BusinessException("时间类型不正确"); |
| | | } |
| | | //startTime转换为时间戳 |
| | | startTimeStamp = startTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| | | //endTime转换为时间戳 |
| | | nowTimeStamp = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| | | } |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | List<C3mOrderBo> list = baseMapper.orderList(userId, keyword, orderStatus, startTimeStamp, nowTimeStamp); |
| | | return list; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取创建时间超过10分钟的订单 并且没有支付的订单 |
| | | */ |
| | | public void deleteOrderListByCreateTime() { |
| | | // 获取当前时间戳 |
| | | Long time = System.currentTimeMillis(); |
| | | |
| | | //当前时间减去10分钟 |
| | | Long timeMinus10 = time - 600000; |
| | | |
| | | //删除超过10分钟未付款的订单 |
| | | remove(Wrappers.lambdaQuery(C3mOrder.class).lt(C3mOrder::getCreateTimestamp, timeMinus10).eq(C3mOrder::getOrderStatus, 0)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 充电桩故障情况下,结束故障前正在进行中的订单 |
| | | * |
| | | * @param dataPackage |
| | | */ |
| | | public void stopCharingOrder(A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage dataPackage) { |
| | | if (dataPackage == null) { |
| | | return; |
| | | } |
| | | //获取正在进行中的订单 |
| | | C3mOrder c3mOrder = getOne(Wrappers.lambdaQuery(C3mOrder.class) |
| | | .eq(C3mOrder::getC3Mac, dataPackage.getC3Mac()).eq(C3mOrder::getChargingStates, 0) |
| | | .eq(C3mOrder::getOrderStatus, OrderStatus.PAYED.getStatus()).last("limit 1")); |
| | | //订单未结束,但是充电桩为空闲状态(即充电桩断电/故障情况) 结束该订单 |
| | | if ("1".equals(dataPackage.getStatusBit()) && c3mOrder != null) { |
| | | //设置订单为已完成 |
| | | c3mOrder.setOrderStatus(OrderStatus.COMPLETE.getStatus()); |
| | | //设置充电状态为已结束 |
| | | c3mOrder.setChargingStates(1); |
| | | //设置充电电量 |
| | | c3mOrder.setActualChargingCapacity(Double.parseDouble(dataPackage.getChargedCapacity())); |
| | | //设置退款金额 |
| | | c3mOrder.setRefundAmount(Double.parseDouble(dataPackage.getRemainingAmount())); |
| | | //设置剩余金额 |
| | | c3mOrder.setSurplusAmount(Double.parseDouble(dataPackage.getRemainingAmount())); |
| | | //结束充电(订单)时间 |
| | | c3mOrder.setStopChargingTimestamp(System.currentTimeMillis()); |
| | | //充电时长 |
| | | c3mOrder.setAlreadyChargingTime(dataPackage.getAlreadyChargingTime()); |
| | | updateById(c3mOrder); |
| | | } |
| | | } |
| | | |
| | | } |