From 77edfb324184d304ae7763aae3ab609f2b495da4 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期五, 22 七月 2022 17:31:47 +0800
Subject: [PATCH] fix
---
ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java | 290 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 267 insertions(+), 23 deletions(-)
diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java
index eb60f18..afe5064 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java
@@ -1,18 +1,39 @@
package com.sandu.ximon.admin.service;
+import cn.hutool.core.lang.Snowflake;
+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.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.util.SpringContextHolder;
+import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5C3HeartbeatReportInnerFrame;
+import com.sandu.ximon.admin.manager.iot.rrpc.enums.C3mRedisConstant;
+import com.sandu.ximon.admin.pay.OrderStatusEnums;
import com.sandu.ximon.admin.pay.wx.WxFastPayService;
import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.security.order.OrderQueryListener;
import com.sandu.ximon.admin.utils.AliPayUtils;
+import com.sandu.ximon.admin.utils.RedisUtils;
+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.enums.OrderStatus;
import com.sandu.ximon.dao.enums.OrderType;
import com.sandu.ximon.dao.mapper.C3mOrderMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
import java.util.Date;
+import java.util.List;
/**
* @Author liuhaonan
@@ -23,27 +44,50 @@
@AllArgsConstructor
public class C3mOrderService extends BaseServiceImpl<C3mOrderMapper, C3mOrder> {
+ private final RedisUtils redisUtils;
+ private final PoleService poleService;
private final C3mOrderMapper c3mOrderMapper;
private final WxFastPayService fastPayService;
+ private final OrderQueryListener orderQueryListener;
+ private final Snowflake snowflake;
- public boolean orderRefund(String outTradeNo, Double refundAmount/*, Long userId, String username*/) {
+ private final C3ChargingService c3ChargingService;
+
+ /**
+ * 閫�娆�
+ *
+ * @param outTradeNo
+ * @return
+ */
+ public boolean orderRefund(String outTradeNo, Double amt) {
Long userId = SecurityUtils.getUserId();
String username = SecurityUtils.getUsername();
C3mOrder orderByOutTradeNo = c3mOrderMapper.getOrderByOutTradeNo(outTradeNo);
+ Double refundAmount = orderByOutTradeNo.getSurplusAmount();
if (orderByOutTradeNo.getTotalAmount() < refundAmount) {
throw new BusinessException("閫�娆鹃噾棰濋敊璇�,涓嶈兘澶т簬浠樻閲戦");
}
- if (null == orderByOutTradeNo) {
- return false;
+ if (orderByOutTradeNo == null) {
+ throw new BusinessException("鏈壘鍒拌鍗�");
} else {
orderByOutTradeNo.setRefundAmount(refundAmount);
- return c3mOrderRefund(orderByOutTradeNo, "椤甸潰API閫�娆�", userId, username);
+ 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;
@@ -61,34 +105,234 @@
C3mOrder.getPoleId()
);
}
- 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 wxCode) {
+
+ // 鍒ゆ柇璇ュ厖鐢垫々鏄惁瀛樺湪姝e湪杩涜涓殑璁㈠崟
+ String chargingJson = redisUtils.get(C3mRedisConstant.C3_CHARGING_ORDER.getCode() + c3m.getC3Mac());
+ if (null != chargingJson) {
+ throw new BusinessException("璇ュ厖鐢垫々宸插瓨鍦ㄦ鍦ㄨ繘琛屼腑鐨勮鍗�");
+ }
+ //鏌ヨ鍏呯數妗╂槸鍚﹀瓨鍦ㄦ鍦ㄨ繘琛屼腑鐨勮鍗�
+ C3mOrder c3mOrder = getOne(Wrappers.lambdaQuery(C3mOrder.class)
+ .eq(C3mOrder::getC3Udid, c3m.getMcuUdid()).eq(C3mOrder::getChargingStates, 0).last("limit 1"));
+
+ if (c3mOrder != null && !c3mOrder.getUserCode().equals(wxCode)) {
+ throw new BusinessException("璇ュ厖鐢垫々姝e湪琚埆浜轰娇鐢�");
+ }
+ if (c3mOrder != null) {
+ throw new BusinessException("璇ュ厖鐢垫々宸插瓨鍦ㄦ鍦ㄨ繘琛屼腑鐨勮鍗�");
+ }
+ Pole pole = poleService.getById(streetlightId);
+ if (null == pole) {
+ return null;
+ }
+ // 鐢熸垚璁㈠崟锛屽苟鍔犺浇鍒皉edis缂撳瓨锛岃缃秴鏃舵椂闂翠负5鍒嗛挓
+ C3mOrder c3mOrderEntity = new C3mOrderVO().generateOrder(
+ streetlightId,
+ pole.getDeviceCode(),
+ c3m.getC3Mac(),
+ OrderType.getOrderType(orderType),
+ totalAmount,
+ subscribeChargingCapacity
+ );
+ boolean b = true;
+ c3mOrderEntity.setOrderId(snowflake.nextId());
+ c3mOrderEntity.setC3Mac(c3m.getC3Name());
+ c3mOrderEntity.setPoleId(streetlightId);
+ c3mOrderEntity.setPoleMac(pole.getDeviceCode());
+ c3mOrderEntity.setPoleName(pole.getPoleName());
+ c3mOrderEntity.setC3Mac(c3m.getC3Mac());
+ c3mOrderEntity.setOrderStatus(0);
+ c3mOrderEntity.setUserCode(wxCode);
+ c3mOrderEntity.setC3Udid(c3m.getMcuUdid());
+
+ if (b) {
+ this.save(c3mOrderEntity);
+ }
+ return b ? c3mOrderEntity : null;
+ }
+
+ /**
+ * 寰俊鏀粯鍥炶皟
+ */
+ public Object payWechatNotify(String xmlData) {
+ WxPayOrderNotifyResult wxPayOrderNotifyResult = fastPayService.parseOrderNotifyResult(xmlData);
+ String orderSn = wxPayOrderNotifyResult.getOutTradeNo();
+ String transactionId = wxPayOrderNotifyResult.getOutTradeNo();
+ C3mOrder userOrder = getByOrderSn(orderSn);
+ 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) {
+ throw new BusinessException("寮�鍚厖鐢靛け璐�");
+ }
+ //寮�鍚厖鐢垫垚鍔熷悗锛屾洿鏂拌鍗曠姸鎬佷负鍏呯數涓�
+ userOrder.setChargingStates(0);
+ 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("璁㈠崟鐘舵�佷笉姝g‘");
+ }
+ 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鍒嗛挓鏈粯娆剧殑璁㈠崟
+ remove(Wrappers.lambdaQuery(C3mOrder.class).lt(C3mOrder::getCreateTimestamp, time).eq(C3mOrder::getOrderStatus, 0));
+ }
+
+ /**
+ * 鎭㈠缁х画鍏呯數
+ *
+ * @param c3Mac
+ * @param mcuUdid
+ */
+ public void recoverContinueCharing(String c3Mac, String mcuUdid) {
+ C3mOrder c3mOrder = getOne(Wrappers.lambdaQuery(C3mOrder.class).
+ eq(C3mOrder::getC3Mac, c3Mac).eq(C3mOrder::getC3Udid, mcuUdid).eq(C3mOrder::getChargingStates, 0).last("limit 1"));
+ if (c3mOrder != null) {
+ c3mOrder.setActualChargingCapacity(c3mOrder.getActualChargingCapacityHide());
+ c3mOrder.setActualChargingCapacityHide(0.00);
+
+ //鍐嶆鍏呯數鐢甸噺
+ double electricity = c3mOrder.getSubscribeChargingCapacity() - c3mOrder.getActualChargingCapacity();
+ Integer chargingCapacity = Integer.valueOf(String.valueOf(Math.floor(electricity)));
+ //鍐嶆鍏呯數閲戦
+ double chargeAmount = c3mOrder.getSurplusAmount();
+ SpringContextHolder.getBean(C3ChargingService.class).startCharging(c3Mac, chargingCapacity, chargeAmount);
+ }
+ }
+
}
--
Gitblit v1.9.3