2021与蓝度共同重构项目,服务端
liuhaonan
2022-07-07 cd0fa3261b49dbab89bb01dfa94dc3ebdb08dec4
ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mOrderService.java
@@ -28,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;
@@ -48,17 +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("退款金额错误,不能大于付款金额");
        }
@@ -100,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,
@@ -141,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) {
        //  判断该充电桩是否存在正在进行中的订单
        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("该充电桩正在被别人使用");
        }
        if (c3mOrder != null) {
            throw new BusinessException("该充电桩已存在正在进行中的订单");
        }
        Pole pole = poleService.getById(streetlightId);
        if (null == pole) {
@@ -177,6 +194,7 @@
        c3mOrderEntity.setC3Mac(c3m.getC3Mac());
        c3mOrderEntity.setOrderStatus(0);
        c3mOrderEntity.setUserCode(wxCode);
        c3mOrderEntity.setC3Udid(c3m.getMcuUdid());
        //  推送到自动查询模块,进行扫描启动
@@ -194,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);
@@ -207,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("更新数据成功");
    }
@@ -230,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);
    }
@@ -242,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);
    }
@@ -261,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("订单状态不正确");
        }
        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;
    }
@@ -272,9 +325,9 @@
    /**
     * 获取创建时间超过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));
    }