|
@@ -2,7 +2,11 @@ package com.yonge.cooleshow.portal.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
+import com.yonge.cooleshow.common.exception.BizException;
|
|
|
+import com.yonge.cooleshow.enums.OrderStatusEnum;
|
|
|
+import com.yonge.cooleshow.enums.PayStatusEnum;
|
|
|
import com.yonge.cooleshow.mall.common.api.CommonPage;
|
|
|
import com.yonge.cooleshow.mall.common.exception.Asserts;
|
|
|
import com.yonge.cooleshow.mall.common.service.RedisService;
|
|
@@ -14,15 +18,23 @@ import com.yonge.cooleshow.portal.dao.PortalOrderItemDao;
|
|
|
import com.yonge.cooleshow.portal.dao.SmsCouponHistoryDao;
|
|
|
import com.yonge.cooleshow.portal.domain.*;
|
|
|
import com.yonge.cooleshow.portal.service.*;
|
|
|
+import com.yonge.cooleshow.sdk.PaymentSdk;
|
|
|
+import com.yonge.cooleshow.sdk.req.OrderPayReq;
|
|
|
+import com.yonge.cooleshow.sdk.res.OrderPayRes;
|
|
|
+import com.yonge.cooleshow.service.PaymentService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.Consumer;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -31,6 +43,8 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(OmsPortalOrderService.class);
|
|
|
@Autowired
|
|
|
private UmsMemberService memberService;
|
|
|
@Autowired
|
|
@@ -64,6 +78,15 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
|
|
|
@Autowired
|
|
|
private CancelOrderSender cancelOrderSender;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserOrderPaymentService userOrderPaymentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaymentService adapayPaymentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaymentSdk paymentSdk;
|
|
|
+
|
|
|
@Override
|
|
|
public ConfirmOrderResult generateConfirmOrder(List<Long> cartIds) {
|
|
|
ConfirmOrderResult result = new ConfirmOrderResult();
|
|
@@ -288,7 +311,10 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
|
|
|
public void cancelOrder(Long orderId) {
|
|
|
//查询未付款的取消订单
|
|
|
OmsOrderExample example = new OmsOrderExample();
|
|
|
- example.createCriteria().andIdEqualTo(orderId).andStatusEqualTo(0).andDeleteStatusEqualTo(0);
|
|
|
+ List<Integer> list = new ArrayList<>();
|
|
|
+ list.add(0);
|
|
|
+ list.add(6);
|
|
|
+ example.createCriteria().andIdEqualTo(orderId).andStatusIn(list).andDeleteStatusEqualTo(0);
|
|
|
List<OmsOrder> cancelOrderList = orderMapper.selectByExample(example);
|
|
|
if (CollectionUtils.isEmpty(cancelOrderList)) {
|
|
|
return;
|
|
@@ -313,6 +339,14 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
|
|
|
memberService.updateIntegration(cancelOrder.getMemberId(), member.getIntegration() + cancelOrder.getUseIntegration());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ UserOrderPayment orderPayment = userOrderPaymentService.getByOrderNo(cancelOrder.getOrderSn());
|
|
|
+ if (null != orderPayment) {
|
|
|
+ //更新付款单
|
|
|
+ orderPayment.setStatus(OrderStatusEnum.FAIL.getCode());
|
|
|
+ orderPayment.setPayFailMsg("交易取消");
|
|
|
+ userOrderPaymentService.saveOrUpdate(orderPayment);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -395,6 +429,25 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
|
|
|
return orderDetail;
|
|
|
}
|
|
|
|
|
|
+ public OmsOrderDetail detail(String orderSn) {
|
|
|
+ OmsOrderExample orderExample = new OmsOrderExample();
|
|
|
+ orderExample.createCriteria().andOrderSnEqualTo(orderSn);
|
|
|
+ List<OmsOrder> omsOrders = orderMapper.selectByExample(orderExample);
|
|
|
+ if (CollUtil.isEmpty(omsOrders)) {
|
|
|
+ throw new BizException("订单信息错误");
|
|
|
+ } else if (omsOrders.size() != 1) {
|
|
|
+ throw new BizException("订单号重复");
|
|
|
+ }
|
|
|
+ OmsOrder omsOrder = omsOrders.get(0);
|
|
|
+ OmsOrderItemExample example = new OmsOrderItemExample();
|
|
|
+ example.createCriteria().andOrderIdEqualTo(omsOrder.getId());
|
|
|
+ List<OmsOrderItem> orderItemList = orderItemMapper.selectByExample(example);
|
|
|
+ OmsOrderDetail orderDetail = new OmsOrderDetail();
|
|
|
+ BeanUtil.copyProperties(omsOrder,orderDetail);
|
|
|
+ orderDetail.setOrderItemList(orderItemList);
|
|
|
+ return orderDetail;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void deleteOrder(Long orderId) {
|
|
|
UmsMember member = memberService.getCurrentMember();
|
|
@@ -410,6 +463,138 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public OrderPayRes orderPay(OrderPayReq payReq) {
|
|
|
+ OrderPayRes orderPayRes = new OrderPayRes();
|
|
|
+ OmsOrderDetail detail = detail(payReq.getOrderNo());
|
|
|
+ if (detail == null) {
|
|
|
+ orderPayRes.setStatus(false);
|
|
|
+ orderPayRes.setMessage("订单不存在");
|
|
|
+ return orderPayRes;
|
|
|
+ }
|
|
|
+ UserOrderPayment userOrderPayment = userOrderPaymentService.getByOrderNo(detail.getOrderSn());
|
|
|
+ payReq.orderStatus(detail.getStatus());
|
|
|
+ payReq.setOrderNo(detail.getOrderSn());
|
|
|
+ payReq.setOrderPrice(detail.getPayAmount());
|
|
|
+ payReq.setGoodTitle("");
|
|
|
+ payReq.setGoodDesc("");
|
|
|
+ if (userOrderPayment != null) {
|
|
|
+ payReq.setPayInfo(userOrderPayment.getPayInfo());
|
|
|
+ }
|
|
|
+ // 支付中的返回拉起信息
|
|
|
+ orderPayRes = adapayPaymentService.orderPay(payReq);
|
|
|
+ // 订单状态没变直接返回
|
|
|
+ if (payReq.getOrderStatus().getCode().equals(orderPayRes.getOrderStatus().getCode())) {
|
|
|
+ return orderPayRes;
|
|
|
+ }
|
|
|
+ // 订单状态变了, 改支付状态 和订单支付表
|
|
|
+ OmsOrder omsOrder = new OmsOrder();
|
|
|
+ omsOrder.setId(detail.getId());
|
|
|
+ if (orderPayRes.getOrderStatus().getCode().equals(OrderStatusEnum.PAYING.getCode())) {
|
|
|
+ omsOrder.setStatus(6);
|
|
|
+ }else {
|
|
|
+ omsOrder.setStatus(7);
|
|
|
+ }
|
|
|
+ orderMapper.updateByPrimaryKey(omsOrder);
|
|
|
+
|
|
|
+ // 保存订单支付表
|
|
|
+ if (userOrderPayment == null) {
|
|
|
+ userOrderPayment = new UserOrderPayment();
|
|
|
+ }
|
|
|
+ userOrderPayment.setOrderNo(orderPayRes.getOrderNo());
|
|
|
+ userOrderPayment.setPayChannel(orderPayRes.getPayChannel());
|
|
|
+ userOrderPayment.setTransNo(orderPayRes.getTransNo());
|
|
|
+ userOrderPayment.setPayAmt(new BigDecimal(orderPayRes.getPay_amt()));
|
|
|
+ userOrderPayment.setPayInfo(orderPayRes.getPay_info());
|
|
|
+ userOrderPayment.setStatus(orderPayRes.getOrderStatus().getCode());
|
|
|
+ userOrderPayment.setPayFailMsg(orderPayRes.getMessage());
|
|
|
+ userOrderPayment.setCreateTime(new Date());
|
|
|
+ userOrderPayment.setUpdateTime(new Date());
|
|
|
+ userOrderPaymentService.saveOrUpdate(userOrderPayment);
|
|
|
+
|
|
|
+ return orderPayRes;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void orderCallback(String data) {
|
|
|
+ JSONObject hfRes = JSONObject.parseObject(data);
|
|
|
+ if (PayStatusEnum.succeeded.getCode().equals(hfRes.getString("status"))) {
|
|
|
+ //订单完成
|
|
|
+ OmsOrderDetail orderDetail = detail(hfRes.getString("order_no"));
|
|
|
+ if (null == orderDetail) {
|
|
|
+ log.error("汇付支付回调,订单未找到。 req is {}", data);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (orderDetail.getStatus() == 0
|
|
|
+ && orderDetail.getStatus() == 6
|
|
|
+ && orderDetail.getStatus() == 7) {
|
|
|
+ orderSuccess(orderDetail, hfRes);
|
|
|
+ } else {
|
|
|
+ log.error("汇付支付回调,订单状态异常。 req is {}", data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /***
|
|
|
+ * 处理支付中订单
|
|
|
+ * @author liweifan
|
|
|
+ * @param: userOrder
|
|
|
+ * @updateTime 2022/4/13 16:51
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ void payingOrderHandle(OmsOrder userOrder) {
|
|
|
+ //判断汇付订单状态
|
|
|
+ UserOrderPayment orderPayment = userOrderPaymentService.getByOrderNo(userOrder.getOrderSn());
|
|
|
+ if (null == orderPayment) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Map<String, Object> resMap = paymentSdk.queryPayment(orderPayment.getTransNo());
|
|
|
+ //支付失败
|
|
|
+ if (PayStatusEnum.failed.getCode().equals(resMap.get("status").toString())) {
|
|
|
+ cancelOrder(userOrder.getId());
|
|
|
+ }
|
|
|
+ //支付成功
|
|
|
+ if (PayStatusEnum.succeeded.getCode().equals(resMap.get("status").toString())) {
|
|
|
+ orderSuccess(userOrder,null);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("轮询处理支付中订单异常,异常参数: {}", JSONObject.toJSONString(userOrder));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单完成
|
|
|
+ *
|
|
|
+ * @author liweifan
|
|
|
+ * @param: detail
|
|
|
+ * @updateTime 2022/4/13 17:17
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ void orderSuccess(OmsOrder detail, JSONObject hfRes) {
|
|
|
+ //更新付款单
|
|
|
+ UserOrderPayment orderPayment = userOrderPaymentService.getByOrderNo(detail.getOrderSn());
|
|
|
+ if (orderPayment.getPayChannel().equals("alipay")) {
|
|
|
+ paySuccess(detail.getId(),1);
|
|
|
+ } else if (orderPayment.getPayChannel().equals("wx_lite")) {
|
|
|
+ paySuccess(detail.getId(),2);
|
|
|
+ }
|
|
|
+ orderPayment.setStatus(OrderStatusEnum.PAID.getCode());
|
|
|
+ orderPayment.setArrivalTime(new Date());
|
|
|
+ if (null != hfRes) {
|
|
|
+ try {
|
|
|
+ orderPayment.setPayAmt(new BigDecimal(hfRes.getString("pay_amt")));
|
|
|
+ orderPayment.setFeeAmt(new BigDecimal(hfRes.getString("fee_amt")));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("完成订单,付款单金额格式化失败,参数{}", hfRes.toJSONString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userOrderPaymentService.saveOrUpdate(orderPayment);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 生成18位订单编号:8位日期+2位平台号码+2位支付方式+6位以上自增id
|
|
|
*/
|