From cd0fa3261b49dbab89bb01dfa94dc3ebdb08dec4 Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期四, 07 七月 2022 16:32:11 +0800
Subject: [PATCH] changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java |  129 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 103 insertions(+), 26 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 49b621b..a77cb19 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
@@ -9,6 +9,7 @@
 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;
@@ -27,7 +28,10 @@
 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;
 
@@ -47,16 +51,19 @@
     private final OrderQueryListener orderQueryListener;
     private final Snowflake snowflake;
 
+    private final C3ChargingService c3ChargingService;
+
     /**
      * 閫�娆�
+     *
      * @param outTradeNo
-     * @param refundAmount
      * @return
      */
-    public boolean orderRefund(String outTradeNo, Double refundAmount/*, Long userId, String username*/) {
+    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("閫�娆鹃噾棰濋敊璇�,涓嶈兘澶т簬浠樻閲戦");
         }
@@ -98,11 +105,12 @@
                     C3mOrder.getPoleId()
             );
         }
-        C3mOrder.setRefundTimestamp(new Date().getTime());
+        C3mOrder.setRefundTimestamp(System.currentTimeMillis());
         C3mOrder.setRefundMsg(msg);
         if (b) {
             C3mOrder.setOrderStatus(OrderStatus.REFUND.getStatus());
-            c3mOrderMapper.updateRefundOrder(C3mOrder);
+            c3mOrderMapper.updateRefundOrder(C3mOrder);  // TODO: 閫�娆炬棩蹇�
+
            /* logService.log(
                     userId,
                     ServerTask.SERVER_TASK,
@@ -139,13 +147,24 @@
      * @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) {
-            return null;
+            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) {
@@ -175,6 +194,7 @@
         c3mOrderEntity.setC3Mac(c3m.getC3Mac());
         c3mOrderEntity.setOrderStatus(0);
         c3mOrderEntity.setUserCode(wxCode);
+        c3mOrderEntity.setC3Udid(c3m.getMcuUdid());
 
 
         //  鎺ㄩ�佸埌鑷姩鏌ヨ妯″潡锛岃繘琛屾壂鎻忓惎鍔�
@@ -192,7 +212,6 @@
         WxPayOrderNotifyResult wxPayOrderNotifyResult = fastPayService.parseOrderNotifyResult(xmlData);
         String orderSn = wxPayOrderNotifyResult.getOutTradeNo();
         String transactionId = wxPayOrderNotifyResult.getOutTradeNo();
-        Integer totalFee = wxPayOrderNotifyResult.getTotalFee();
         C3mOrder userOrder = getByOrderSn(orderSn);
         if (userOrder == null) {
             return WxPayNotifyResponse.fail("璁㈠崟涓嶅瓨鍦� sn=" + orderSn);
@@ -205,13 +224,18 @@
         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("鏇存柊鏁版嵁宸插け鏁�");
         }
-        /**
-         * 璋冭捣寮�濮嬪厖鐢垫帴鍙�  // TODO
-         */
-
 
         return WxPayNotifyResponse.success("鏇存柊鏁版嵁鎴愬姛");
     }
@@ -228,10 +252,10 @@
     }
 
     /**
-     * 鏍规嵁鐏潌id鑾峰彇鏈�杩戜竴鏉¤鍗�
+     * 鏍规嵁c3Mac鑾峰彇鏈�杩戜竴鏉¤鍗�
      */
-    public C3mOrder getLastOrderByPoleId(Long poleId) {
-        LambdaQueryWrapper<C3mOrder> wrapper = Wrappers.lambdaQuery(C3mOrder.class).eq(C3mOrder::getPoleId, poleId).last("limit 1");
+    public C3mOrder getLastOrderByC3Mac(String c3Mac) {
+        LambdaQueryWrapper<C3mOrder> wrapper = Wrappers.lambdaQuery(C3mOrder.class).eq(C3mOrder::getC3Mac, c3Mac).last("limit 1");
         return getOne(wrapper);
     }
 
@@ -240,16 +264,19 @@
      * 鏍规嵁蹇冭烦鍖呮洿鏂拌鍗曠姸鎬�
      */
     public void updateOrderStatusByHeartbeat(A5C3HeartbeatReportInnerFrame.HeartBeatDataPackage dataPackage) {
-        C3mOrder c3mOrder = getOne(Wrappers.lambdaQuery(C3mOrder.class).eq(C3mOrder::getC3Mac, dataPackage.getC3Mac())
-                .eq(C3mOrder::getChargingStates,1).last("limit 1"));
-        if (c3mOrder == null) {
-            return;
+        //鍏呯數涓殑蹇冭烦鍖呴渶瑕佸幓鏇存柊璁㈠崟鏁版嵁锛屽惁鍒欎笉澶勭悊锛�
+        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);
         }
-        //鏇存柊鍏呯數鎬婚噺
-        c3mOrder.setActualChargingCapacityHide(Double.parseDouble(dataPackage.getChargedCapacity()));
-        //鏇存柊鍓╀綑閲戦
-        c3mOrder.setSurplusAmount(Double.parseDouble(dataPackage.getRemainingAmount()));
-        updateById(c3mOrder);
     }
 
 
@@ -259,10 +286,38 @@
      * @param baseConditionVO
      * @return
      */
-    public List<C3mOrderBo> orderList(BaseConditionVO baseConditionVO) {
+    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);
+        List<C3mOrderBo> list = baseMapper.orderList(userId, keyword, orderStatus, startTimeStamp, nowTimeStamp);
         return list;
     }
 
@@ -270,11 +325,33 @@
     /**
      * 鑾峰彇鍒涘缓鏃堕棿瓒呰繃10鍒嗛挓鐨勮鍗� 骞朵笖娌℃湁鏀粯鐨勮鍗�
      */
-    public void getOrderListByCreateTime() {
+    public void deleteOrderListByCreateTime() {
         // 鑾峰彇褰撳墠鏃堕棿鎴�
-        long time = new Date().getTime();
+        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