package com.sandu.ximon.admin.service; import com.alibaba.fastjson.JSON; import com.sandu.common.execption.BusinessException; import com.sandu.common.service.impl.BaseServiceImpl; import com.sandu.ximon.admin.config.C3mRedisConfig; 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.security.order.OrderScanType; 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.domain.C3mCharging; import com.sandu.ximon.dao.domain.C3mOrder; import com.sandu.ximon.dao.domain.Pole; import com.sandu.ximon.dao.enums.C3mRedisConstant; 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 java.util.Date; /** * @Author liuhaonan * @Date 2022/3/10 14:24 * @Version 1.0 */ @Service @AllArgsConstructor public class C3mOrderService extends BaseServiceImpl { private final RedisUtils redisUtils; private final PoleService poleService; private final C3mOrderMapper c3mOrderMapper; private final WxFastPayService fastPayService; private final OrderQueryListener orderQueryListener; public boolean orderRefund(String outTradeNo, Double refundAmount/*, Long userId, String username*/) { Long userId = SecurityUtils.getUserId(); String username = SecurityUtils.getUsername(); C3mOrder orderByOutTradeNo = c3mOrderMapper.getOrderByOutTradeNo(outTradeNo); if (orderByOutTradeNo.getTotalAmount() < refundAmount) { throw new BusinessException("退款金额错误,不能大于付款金额"); } if (null == orderByOutTradeNo) { return false; } else { orderByOutTradeNo.setRefundAmount(refundAmount); return c3mOrderRefund(orderByOutTradeNo, "页面API退款", userId, username); } } private boolean c3mOrderRefund(C3mOrder C3mOrder, String msg, Long userId, String username) { // 进行退款,设置订单状态为已退款 boolean b = false; if (C3mOrder.getOrderType().equals(OrderType.ALIPAY.getCode())) { b = AliPayUtils.alipayrefund( C3mOrder.getPoleId(), C3mOrder.getOutTradeNo(), C3mOrder.getRefundAmount()); } else if (C3mOrder.getOrderType().equals(OrderType.WXPAY.getCode())) { // 进行微信退款 b = fastPayService.refund( C3mOrder.getTotalAmount(), C3mOrder.getRefundAmount(), C3mOrder.getOutTradeNo(), C3mOrder.getPoleId() ); } C3mOrder.setRefundTimestamp(new Date().getTime()); 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()+"}");*/ 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()+"}");*/ return false; } } public C3mOrder advancePayOrder(Long streetlightId, C3mCharging c3m, String orderType, Double totalAmount, Integer subscribeChargingCapacity) { // 判断该充电桩是否存在正在进行中的订单 String chargingJson = redisUtils.get(C3mRedisConstant.C3_CHARGING_ORDER.getCode() + c3m.getC3Mac()); if (null != chargingJson) { return null; } Pole pole = poleService.getById(streetlightId); if (null == pole) { return null; } // 生成订单,并加载到redis缓存,设置超时时间为5分钟 C3mOrder c3mOrderEntity = new C3mOrderVO().generateOrder( streetlightId, pole.getDeviceCode(), c3m.getC3Mac(), OrderType.getOrderType(orderType), totalAmount, subscribeChargingCapacity ); // 加载到redis缓存中, 查询模块自动查询状态并处理 boolean b = redisUtils.set( C3mRedisConstant.C3_NO_PAY_ORDER.getCode() + c3m.getC3Mac() + c3mOrderEntity.getOutTradeNo(), JSON.toJSONString(c3mOrderEntity), C3mRedisConfig.ORDER_MAX_TIME ); // 推送到自动查询模块,进行扫描启动 orderQueryListener.startScan(OrderScanType.C3M.getType()); return b ? c3mOrderEntity : null; } }