Procházet zdrojové kódy

fix: 优化支付相关接口

xdd před 4 týdny
rodič
revize
4a0036becc

+ 108 - 0
fs-service/src/main/java/com/fs/hisStore/constants/ErpTypeEnum.java

@@ -0,0 +1,108 @@
+package com.fs.hisStore.constants;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+/**
+ * ERP系统类型枚举
+ *
+ * @author YourName
+ */
+public enum ErpTypeEnum {
+
+    /**
+     * 管易
+     */
+    GUANYI(1, "管易"),
+
+    /**
+     * 旺店通
+     */
+    WANGDIANGTONG(2, "旺店通"),
+
+    /**
+     * 瀚智OMS
+     */
+    HANZHIOMS(3, "瀚智OMS"),
+
+    /**
+     * 代服
+     */
+    DAIFU(4, "代服"),
+
+    /**
+     * 聚水潭
+     */
+    JUSHUITAN(5, "聚水潭"),
+
+    /**
+     * 金博
+     */
+    JINBO(6, "金博");
+
+    /**
+     * ERP系统类型编码
+     */
+    private final Integer code;
+
+    /**
+     * ERP系统类型描述
+     */
+    private final String desc;
+
+    /**
+     * 构造方法
+     *
+     * @param code ERP系统类型编码
+     * @param desc ERP系统类型描述
+     */
+    ErpTypeEnum(Integer code, String desc) {
+        this.code = code;
+        this.desc = desc;
+    }
+
+    /**
+     * 根据编码获取枚举
+     *
+     * @param code ERP系统类型编码
+     * @return 对应的枚举实例,如果未找到返回null
+     */
+    public static ErpTypeEnum getByCode(Integer code) {
+        if (code == null) {
+            return null;
+        }
+
+        return Arrays.stream(ErpTypeEnum.values())
+                .filter(erpType -> Objects.equals(erpType.getCode(), code))
+                .findFirst()
+                .orElse(null);
+    }
+
+    /**
+     * 判断编码是否有效
+     *
+     * @param code ERP系统类型编码
+     * @return 是否有效
+     */
+    public static boolean isValidCode(Integer code) {
+        return getByCode(code) != null;
+    }
+
+    /**
+     * 获取ERP系统类型编码
+     *
+     * @return ERP系统类型编码
+     */
+    public Integer getCode() {
+        return code;
+    }
+
+    /**
+     * 获取ERP系统类型描述
+     *
+     * @return ERP系统类型描述
+     */
+    public String getDesc() {
+        return desc;
+    }
+}

+ 24 - 0
fs-service/src/main/java/com/fs/hisStore/constants/UserAppsLockConstant.java

@@ -0,0 +1,24 @@
+package com.fs.hisStore.constants;
+
+public interface UserAppsLockConstant {
+    /**
+     * APP商城-支付
+     */
+    public static final String LOCK_KEY_PAY = "payment:lock:%d";
+
+    /**
+     * APP商城-支付尾款
+     */
+    public static final String LOCK_KEY_PAY_REMAIN = "payment:remain:lock:%d";
+
+
+    /**
+     * APP商城-取消支付
+     */
+    public static final String LOCK_KEY_CANCEL = "order:cancel:%d";
+
+    /**
+     * 修改支付类型
+     */
+    public static final String LOCK_KEY_EDIT_PAY_TYPE = "payment:paytype:lock:%d";
+}

+ 8 - 0
fs-service/src/main/java/com/fs/hisStore/service/IFsStoreOrderScrmService.java

@@ -286,4 +286,12 @@ public interface IFsStoreOrderScrmService
     R pay(FsStoreOrderPayParam param);
 
     void cancelPay(FsStoreOrderPayParam param);
+
+    R otherPaymentRemain(FsStoreOrderOtherPayParam param);
+
+    R otherPayment(FsStoreOrderOtherPayParam param);
+
+    R payRemain(FsStoreOrderPayParam param);
+
+    R editPayType(FsStoreOrderPayParam param);
 }

+ 425 - 15
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreOrderScrmServiceImpl.java

@@ -1,5 +1,6 @@
 package com.fs.hisStore.service.impl;
 
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.hutool.core.date.DateTime;
 import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.hutool.core.net.URLDecoder;
@@ -24,6 +25,7 @@ import com.fs.common.event.TemplateListenEnum;
 import com.fs.common.exception.CustomException;
 import com.fs.common.exception.ServiceException;
 import com.fs.common.utils.DateUtils;
+import com.fs.common.utils.IpUtil;
 import com.fs.common.utils.ServletUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.ip.IpUtils;
@@ -77,6 +79,7 @@ import com.fs.huifuPay.domain.HuiFuRefundResult;
 import com.fs.huifuPay.domain.HuifuCreateOrderResult;
 import com.fs.huifuPay.sdk.opps.core.request.V2TradePaymentScanpayRefundRequest;
 import com.fs.huifuPay.service.HuiFuService;
+import com.fs.pay.pay.dto.OrderQueryDTO;
 import com.fs.pay.pay.dto.RefundDTO;
 import com.fs.pay.service.IPayService;
 import com.fs.hisStore.config.StoreConfig;
@@ -86,6 +89,8 @@ import com.fs.hisStore.domain.*;
 import com.fs.hisStore.enums.*;
 import com.fs.hisStore.service.*;
 import com.fs.system.service.ISysConfigService;
+import com.fs.wx.miniapp.config.WxMaProperties;
+import com.fs.ybPay.domain.OrderResult;
 import com.fs.ybPay.domain.RefundResult;
 import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
 import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
@@ -97,6 +102,8 @@ import com.github.binarywang.wxpay.exception.WxPayException;
 import com.github.binarywang.wxpay.service.WxPayService;
 import lombok.Synchronized;
 import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang.ObjectUtils;
 import org.apache.http.util.Asserts;
 import org.slf4j.Logger;
@@ -168,7 +175,7 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
     private IFsUserBillScrmService billService;
 
     @Autowired
-    RedisCache redisCache;
+    private RedisCache redisCache;
 
     @Autowired
     private IFsStoreCartScrmService cartService;
@@ -199,13 +206,13 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
     @Autowired
     private ConfigUtil configUtil;
     @Autowired
-    IFsStorePaymentScrmService paymentService;
+    private IFsStorePaymentScrmService paymentService;
 
     @Autowired
-    IFsStoreProductPackageScrmService productPackageService;
+    private IFsStoreProductPackageScrmService productPackageService;
 
     @Autowired
-    ICompanyUserService companyUserService;
+    private ICompanyUserService companyUserService;
     @Autowired
     private IFsStoreProductAttrValueScrmService attrValueService;
     @Autowired
@@ -234,15 +241,15 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
     private FsStorePaymentScrmMapper fsStorePaymentMapper;
 
     @Autowired
-    ICompanyDeptService companyDeptService;
+    private ICompanyDeptService companyDeptService;
     @Autowired
-    IFsPrescribeScrmService prescribeService;
+    private IFsPrescribeScrmService prescribeService;
 
     @Autowired
-    IPayService ybPayService;
+    private IPayService ybPayService;
 
     @Autowired
-    HuiFuService huiFuService;
+    private HuiFuService huiFuService;
     @Autowired
     private WxPayService wxPayService;
     @Autowired
@@ -325,7 +332,8 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
 
     @Autowired
     private FsPackageOrderMapper fsPackageOrderMapper;
-
+    @Autowired
+    private WxMaProperties properties;
     /**
      * 查询订单
      *
@@ -3760,13 +3768,10 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
                     }
                     order.setPayMoney(payMoney);
                     order.setPayDelivery(order.getPayPrice().subtract(payMoney) );
-//                    order.setPayMoney(BigDecimal.ZERO);
                 }
                 this.updateFsStoreOrder(order);
             }
             String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
-//            order.setOrderCode(orderCode);
-//            if(order.getPayType().equals("1")||order.getPayType().equals("2")){
             if((order.getPayType().equals("1")||order.getPayType().equals("2")||order.getPayType().equals("3")) && order.getPayMoney().compareTo(new BigDecimal(0))>0){
                 String json = configService.selectConfigByKey(STORE_PAY_CONF);
                 FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
@@ -3826,7 +3831,6 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
                     orderRequest.setBody("商城订单支付");
                     orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
                     orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
-                    //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
                     orderRequest.setTradeType("JSAPI");
                     orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
                     //调用统一下单接口,获取"预支付交易会话标识"
@@ -3839,10 +3843,10 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
                     }
                 }
             }
-//            else if(order.getPayType().equals("3")){
             else if(order.getPayType().equals("3") && order.getPayMoney().compareTo(new BigDecimal(0))<=0){
                 //货到付款
-                this.payConfirm(2,order.getId(),null,null,null,null);
+                IFsStoreOrderScrmService fsStoreOrderScrmService = (IFsStoreOrderScrmService) AopContext.currentProxy();
+                fsStoreOrderScrmService.payConfirm(2,order.getId(),null,null,null,null);
                 return R.ok().put("payType",param.getPayType());
             }
             return R.error();
@@ -3857,6 +3861,412 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
         redisCache.deleteObject("isPaying:"+param.getOrderId());
     }
 
+    @Override
+    @Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
+    public R otherPaymentRemain(FsStoreOrderOtherPayParam param) {
+        final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
+        try {
+            String ip = IpUtil.getRequestIp();
+            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
+            FsUserScrm user=userService.selectFsUserByMaOpenId(session.getOpenid());
+            if(user==null){
+                //创建
+                user=new FsUserScrm();
+                user.setUsername("");
+                user.setNickname("微信用户");
+                user.setStatus(1);
+                user.setMaOpenId(session.getOpenid());
+                user.setUnionId(session.getUnionid());
+                user.setIsWeixinAuth(0);
+                user.setLastIp(ip);
+                user.setCreateTime(new Date());
+                userService.insertFsUser(user);
+            }
+            FsStoreOrderScrm order=this.selectFsStoreOrderById(param.getOrderId());
+            if(order==null){
+                return R.error("订单不存在");
+            }
+            if(order.getStatus()!= OrderInfoEnum.STATUS_2.getValue()){
+                return R.error("待收货订单可申请支付尾款");
+            }
+            if(order.getPayType().equals(1)){
+                return R.error("此订单已支付");
+            }
+            //只有顺风才可以付尾
+            if(!order.getDeliverySn().trim().equals("SF")){
+                return R.error("只有顺丰物流支持尾款支付");
+            }
+
+            if(!order.getIsPayRemain().equals(0)){
+                return R.error("此订单已支付");
+            }
+            String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
+            String json = configService.selectConfigByKey(STORE_PAY_CONF);
+            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
+            FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
+            storePayment.setCompanyId(order.getCompanyId());
+            storePayment.setCompanyUserId(order.getCompanyUserId());
+            storePayment.setStatus(0);
+            storePayment.setPayMode(fsPayConfig.getType());
+            storePayment.setPayCode(payCode);
+            storePayment.setPayMoney(order.getPayDelivery());
+            storePayment.setCreateTime(new Date());
+            storePayment.setPayTypeCode("weixin");
+            storePayment.setBusinessType(2);
+            storePayment.setRemark("商城订单尾款支付");
+            storePayment.setOpenId(user.getRealName());
+            storePayment.setUserId(user.getUserId());
+            storePayment.setBusinessOrderId(order.getId().toString());
+            storePayment.setOrderId(order.getId());
+            storePayment.setIsPayRemain(1);
+            fsStorePaymentMapper.insertFsStorePayment(storePayment);
+
+            if (fsPayConfig.getType().equals("hf")){
+                HuiFuCreateOrder o = new HuiFuCreateOrder();
+                o.setTradeType("T_MINIAPP");
+                o.setOpenid(user.getMaOpenId());
+                o.setReqSeqId("store-"+storePayment.getPayCode());
+                o.setTransAmt(storePayment.getPayMoney().toString());
+                o.setGoodsDesc("商城订单支付");
+                HuifuCreateOrderResult result = huiFuService.createOrder(o);
+                if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
+
+                    FsStorePaymentScrm mt=new FsStorePaymentScrm();
+                    mt.setPaymentId(storePayment.getPaymentId());
+                    mt.setTradeNo(result.getHf_seq_id());
+                    fsStorePaymentMapper.updateFsStorePayment(mt);
+                    redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
+                    Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
+                    String s = (String) resultMap.get("package");
+                    resultMap.put("packageValue",s);
+                    return R.ok().put("result",resultMap);
+                }
+                else{
+                    return R.error(result.getResp_desc());
+                }
+            }else  if (fsPayConfig.getType().equals("wx")){
+                //创建微信订单
+                WxPayConfig payConfig = new WxPayConfig();
+                payConfig.setAppId(fsPayConfig.getAppId());
+                payConfig.setMchId(fsPayConfig.getWxMchId());
+                payConfig.setMchKey(fsPayConfig.getWxMchKey());
+                payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
+                payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
+                payConfig.setKeyPath(null);
+                payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
+                wxPayService.setConfig(payConfig);
+                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
+                orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
+                orderRequest.setBody("商城订单支付");
+                orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
+                orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
+                //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
+                orderRequest.setTradeType("JSAPI");
+                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
+                //调用统一下单接口,获取"预支付交易会话标识"
+                try {
+                    WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
+                    return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0);
+                } catch (WxPayException e) {
+                    e.printStackTrace();
+                    throw new CustomException("支付失败" + e.getMessage());
+                }
+            }
+        } catch (WxErrorException e) {
+            e.printStackTrace();
+            return R.error(e.getMessage());
+        }
+        return R.error("无支付类型");
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
+    public R otherPayment(FsStoreOrderOtherPayParam param) {
+        final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
+        try {
+            String ip = IpUtil.getRequestIp();
+            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
+            FsUserScrm user=userService.selectFsUserByMaOpenId(session.getOpenid());
+            if(user==null){
+                //创建
+                user=new FsUserScrm();
+                user.setUsername("");
+                user.setNickname("微信用户");
+                user.setStatus(1);
+                user.setMaOpenId(session.getOpenid());
+                user.setUnionId(session.getUnionid());
+                user.setIsWeixinAuth(0);
+                user.setLastIp(ip);
+                user.setCreateTime(new Date());
+                userService.insertFsUser(user);
+            }
+            FsStoreOrderScrm order=selectFsStoreOrderById(param.getOrderId());
+            if(order==null){
+                return R.error("订单不存在");
+            }
+            if(!Objects.equals(order.getStatus(), OrderInfoEnum.STATUS_0.getValue())){
+                return R.error("此订单不能支付");
+            }
+            if(order.getPayMoney().compareTo(new BigDecimal(0))<1){
+                return R.error("此订单没有可支付的金额");
+            }
+
+            String orderId=redisCache.getCacheObject("isPaying:"+order.getId());
+            if(StringUtils.isNotEmpty(orderId)&&orderId.equals(order.getId().toString())){
+                return R.error("正在支付中...");
+            }
+
+            String json = configService.selectConfigByKey(STORE_PAY_CONF);
+            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
+            String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
+            //易宝支付
+            FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
+            storePayment.setCompanyId(order.getCompanyId());
+            storePayment.setCompanyUserId(order.getCompanyUserId());
+            storePayment.setStatus(0);
+            storePayment.setPayCode(payCode);
+            storePayment.setPayMode(fsPayConfig.getType());
+            storePayment.setPayMoney(order.getPayMoney());
+            storePayment.setCreateTime(new Date());
+            storePayment.setPayTypeCode("weixin");
+            storePayment.setBusinessType(2);
+            storePayment.setRemark("商城订单支付");
+            storePayment.setOpenId(session.getOpenid());
+            storePayment.setUserId(user.getUserId());
+            storePayment.setBusinessOrderId(order.getId().toString());
+            storePayment.setOrderId(order.getId());
+            fsStorePaymentMapper.insertFsStorePayment(storePayment);
+
+            if (fsPayConfig.getType().equals("hf")){
+                HuiFuCreateOrder o = new HuiFuCreateOrder();
+                o.setTradeType("T_MINIAPP");
+                o.setOpenid(user.getMaOpenId());
+                o.setReqSeqId("store-"+storePayment.getPayCode());
+                o.setTransAmt(storePayment.getPayMoney().toString());
+                o.setGoodsDesc("商城订单支付");
+                HuifuCreateOrderResult result = huiFuService.createOrder(o);
+                if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
+
+                    FsStorePaymentScrm mt=new FsStorePaymentScrm();
+                    mt.setPaymentId(storePayment.getPaymentId());
+                    mt.setTradeNo(result.getHf_seq_id());
+                    fsStorePaymentMapper.updateFsStorePayment(mt);
+                    redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
+                    Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
+                    String s = (String) resultMap.get("package");
+                    resultMap.put("packageValue",s);
+                    return R.ok().put("result",resultMap);
+                }
+                else{
+                    return R.error(result.getResp_desc());
+                }
+            }else  if (fsPayConfig.getType().equals("wx")){
+                //创建微信订单
+                WxPayConfig payConfig = new WxPayConfig();
+                payConfig.setAppId(fsPayConfig.getAppId());
+                payConfig.setMchId(fsPayConfig.getWxMchId());
+                payConfig.setMchKey(fsPayConfig.getWxMchKey());
+                payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
+                payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
+                payConfig.setKeyPath(null);
+                payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
+                wxPayService.setConfig(payConfig);
+                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
+                orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
+                orderRequest.setBody("商城订单支付");
+                orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
+                orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
+                orderRequest.setTradeType("JSAPI");
+                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
+                //调用统一下单接口,获取"预支付交易会话标识"
+                try {
+                    WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
+                    return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0);
+                } catch (WxPayException e) {
+                    e.printStackTrace();
+                    throw new CustomException("支付失败" + e.getMessage());
+                }
+            }
+        } catch (WxErrorException e) {
+            e.printStackTrace();
+            return R.error(e.getMessage());
+        }
+        return R.error("无支付类型");
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
+    public R payRemain(FsStoreOrderPayParam param) {
+        FsStoreOrderScrm order=selectFsStoreOrderById(param.getOrderId());
+        if(order==null){
+            return R.error("订单不存在");
+        }
+        if(order.getStatus()!= OrderInfoEnum.STATUS_2.getValue()){
+            return R.error("待收货订单可申请支付尾款");
+        }
+        if(order.getPayType().equals(1)){
+            return R.error("此订单已支付");
+        }
+        //只有顺风才可以付尾
+        if(!order.getDeliverySn().trim().equals("SF")){
+            return R.error("只有顺丰物流支持尾款支付");
+        }
+
+        if(!order.getIsPayRemain().equals(0)){
+            return R.error("此订单已支付");
+        }
+        FsUserScrm user=userService.selectFsUserById(order.getUserId());
+        if(user!=null){
+            String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
+            String json = configService.selectConfigByKey(STORE_PAY_CONF);
+            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
+            FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
+            storePayment.setCompanyId(order.getCompanyId());
+            storePayment.setCompanyUserId(order.getCompanyUserId());
+            storePayment.setStatus(0);
+            storePayment.setPayMode(fsPayConfig.getType());
+            storePayment.setPayCode(payCode);
+            storePayment.setPayMoney(order.getPayDelivery());
+            storePayment.setCreateTime(new Date());
+            storePayment.setPayTypeCode("weixin");
+            storePayment.setBusinessType(2);
+            storePayment.setRemark("商城订单尾款支付");
+            storePayment.setOpenId(user.getRealName());
+            storePayment.setUserId(user.getUserId());
+            storePayment.setBusinessOrderId(order.getId().toString());
+            storePayment.setOrderId(order.getId());
+            storePayment.setIsPayRemain(1);
+            fsStorePaymentMapper.insertFsStorePayment(storePayment);
+
+            if (fsPayConfig.getType().equals("hf")){
+                HuiFuCreateOrder o = new HuiFuCreateOrder();
+                o.setTradeType("T_MINIAPP");
+                o.setOpenid(user.getMaOpenId());
+                o.setReqSeqId("store_remain-"+storePayment.getPayCode());
+                o.setTransAmt(storePayment.getPayMoney().toString());
+                o.setGoodsDesc("商城订单尾款支付");
+                HuifuCreateOrderResult result = huiFuService.createOrder(o);
+                //创建订单
+                if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
+
+                    FsStorePaymentScrm mt=new FsStorePaymentScrm();
+                    mt.setPaymentId(storePayment.getPaymentId());
+                    mt.setTradeNo(result.getHf_seq_id());
+                    fsStorePaymentMapper.updateFsStorePayment(mt);
+                    redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
+                    Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
+                    String s = (String) resultMap.get("package");
+                    resultMap.put("packageValue",s);
+                    return R.ok().put("payType",param.getPayType()).put("result",resultMap);
+                }
+                else{
+                    return R.error(result.getResp_desc());
+                }
+            }else if(fsPayConfig.getType().equals("wx")) {
+                WxPayConfig payConfig = new WxPayConfig();
+                payConfig.setAppId(fsPayConfig.getAppId());
+                payConfig.setMchId(fsPayConfig.getWxMchId());
+                payConfig.setMchKey(fsPayConfig.getWxMchKey());
+                payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
+                payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
+                payConfig.setKeyPath(null);
+                payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
+                wxPayService.setConfig(payConfig);
+                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
+                orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
+                orderRequest.setBody("商城订单支付");
+                orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
+                orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
+                //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
+                orderRequest.setTradeType("JSAPI");
+                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
+                //调用统一下单接口,获取"预支付交易会话标识"
+                try {
+                    WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
+                    return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0);
+                } catch (WxPayException e) {
+                    e.printStackTrace();
+                    throw new CustomException("支付失败" + e.getMessage());
+                }
+            }
+        }
+        else{
+            return R.error("用户OPENID不存在");
+        }
+        return R.error("无支付类型");
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public R editPayType(FsStoreOrderPayParam param) {
+        FsStoreOrderScrm order=this.selectFsStoreOrderById(param.getOrderId());
+        if(order==null){
+            return R.error("订单不存在");
+        }
+        if(!Objects.equals(order.getStatus(), OrderInfoEnum.STATUS_0.getValue())){
+            return R.error("订单状态不正确");
+        }
+        String orderId=redisCache.getCacheObject("isPaying:"+order.getId());
+        if(StringUtils.isNotEmpty(orderId)&&orderId.equals(order.getId().toString())){
+            return R.error("正在支付中...");
+        }
+        List<FsStorePaymentScrm>  payments=fsStorePaymentMapper.selectFsStorePaymentByOrder(order.getId());
+        if(CollectionUtils.isNotEmpty(payments)){
+            for(FsStorePaymentScrm payment : payments){
+                OrderQueryDTO orderQueryDTO = new OrderQueryDTO();
+                orderQueryDTO.setLowOrderId("store-"+payment.getPayCode());
+                OrderResult orderResult = ybPayService.getOrder(orderQueryDTO);
+                if(orderResult.getStatus()!= null&&orderResult.getStatus().equals("100")){
+                    if(orderResult.getState()!=null&&orderResult.getState().equals("0")){
+                        return R.error("订单已支付");
+                    }
+
+                }
+            }
+        }
+        FsUserScrm user=userService.selectFsUserById(order.getUserId());
+        if(user!=null){
+            //已改价处理
+            if(order.getIsEditMoney()!=null&&order.getIsEditMoney()==1){
+                //改过价不做处理
+            }
+            else{
+                String config=configService.selectConfigByKey("his.store");
+                com.fs.store.config.StoreConfig storeConfig= JSONUtil.toBean(config, com.fs.store.config.StoreConfig.class);
+                if(param.getPayType().equals(1)){
+                    order.setPayType("1");
+                    order.setPayMoney(order.getPayPrice());
+                    order.setPayDelivery(BigDecimal.ZERO);
+                }
+                else if(param.getPayType().equals(2)){
+
+                    order.setPayType("2");
+                    BigDecimal payMoney=order.getPayPrice().multiply(new BigDecimal(storeConfig.getPayRate())).divide(new BigDecimal(100));
+                    payMoney=new BigDecimal(payMoney.setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
+                    order.setPayDelivery(order.getPayPrice().subtract(payMoney));
+                    order.setPayMoney(payMoney);
+                }
+                else if(param.getPayType().equals(3)){
+                    //货到付款
+                    order.setPayType("3");
+                    BigDecimal amount=redisCache.getCacheObject("orderAmount:"+order.getId());
+                    BigDecimal payMoney = BigDecimal.ZERO;
+                    if (amount != null){
+                        payMoney=amount;
+                    }
+                    order.setPayMoney(payMoney);
+                    order.setPayDelivery(order.getPayPrice().subtract(payMoney) );
+                }
+                IFsStoreOrderScrmService fsStoreOrderScrmService = (IFsStoreOrderScrmService) AopContext.currentProxy();
+                fsStoreOrderScrmService.updateFsStoreOrder(order);
+            }
+            return R.ok().put("order",order);
+        }
+        else{
+            return R.error("用户OPENID不存在");
+        }
+    }
+
     private static final DateTimeFormatter CST_FORMATTER = DateTimeFormatter
             .ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US)
             .withZone(ZoneId.of("Asia/Shanghai"));

+ 157 - 467
fs-user-app/src/main/java/com/fs/app/controller/store/StoreOrderScrmController.java

@@ -64,40 +64,25 @@ import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 
+import static com.fs.hisStore.constants.UserAppsLockConstant.*;
+
 
 @Api("商城接口")
 @RestController
 @RequestMapping(value="/store/app/storeOrder")
 public class StoreOrderScrmController extends AppBaseController {
 
-    private static final String STORE_PAY_CONF = "his.pay";
-
     Logger logger= LoggerFactory.getLogger(getClass());
 
-    @Autowired
-    private WxMaProperties properties;
-
-    @Autowired
-    private WxPayService wxPayService;
     @Autowired
     private IFsStoreOrderScrmService orderService;
-    @Autowired
-    private FsStorePaymentScrmMapper fsStorePaymentMapper;
 
     @Autowired
     private IFsStoreOrderItemScrmService itemService;
 
-    @Autowired
-    private IFsUserScrmService userService;
-
     @Autowired
     private IFsPrescribeScrmService prescribeService;
-    @Autowired
-    private IPayService payService;
-    @Autowired
-    private FSSysConfig sysConfig;
-    @Autowired
-    private IErpOrderService erpOrderService;
+
     @Autowired
     private ISysConfigService configService;
     @Autowired
@@ -105,68 +90,8 @@ public class StoreOrderScrmController extends AppBaseController {
     @Autowired
     private IFsStoreProductPackageScrmService productPackageService;
     @Autowired
-    IPayService ybPayService;
-    @Autowired
-    private HuiFuService huiFuService;
-    @Autowired
-    @Qualifier("erpOrderServiceImpl")
-    private IErpOrderService gyOrderService;
-
-    @Autowired
-    @Qualifier("wdtErpOrderServiceImpl")
-    private IErpOrderService wdtOrderService;
-
-    @Autowired
-    @Qualifier("k9OrderScrmServiceImpl")
-    private IErpOrderService k9OrderService;
-    @Autowired
     private RedissonClient redissonClient;
 
-    @Autowired
-    private ConfigUtil configUtil;
-    //TODO 应该没用到
-    private IErpOrderService getErpService(){
-        //判断是否开启erp
-        IErpOrderService erpOrderService = null;
-        FsErpConfig erpConfig = configUtil.getErpConfig();
-        Integer erpOpen = erpConfig.getErpOpen();
-        if (erpOpen != null && erpOpen == 1) {
-            //判断erp类型
-            Integer erpType = erpConfig.getErpType();
-            if (erpType != null) {
-                if (erpType == 1) {
-                    //管易
-                    erpOrderService = gyOrderService;
-                } else if (erpType == 2) {
-                    //旺店通
-                    erpOrderService = wdtOrderService;
-                } else if (erpType == 3) {
-                    //金博
-                    erpOrderService = k9OrderService;
-                }
-            }
-        }
-        return erpOrderService;
-    }
-
-
-    @ApiOperation("测试ERP")
-    @GetMapping("/test")
-    public R test(){
-        IErpOrderService erpOrderService = getErpService();
-//        erpOrderService.addOmsOrder(172L);
-//        k9OrderService.refundOmsOrder(172L);
-        return R.ok();
-    }
-
-
-    @ApiOperation("测试ERP")
-    @GetMapping("/test2")
-    public R test2(){
-//        k9OrderService.refundOmsOrder(172L);
-//        k9OrderService.refundOmsOrder(172L);
-        return R.ok();
-    }
 
 
     @Login
@@ -287,7 +212,7 @@ public class StoreOrderScrmController extends AppBaseController {
         Long orderId = param.getOrderId();
         logger.info("开始处理支付请求, 订单号: {}, 支付类型: {}", orderId, param.getPayType());
 
-        RLock lock = redissonClient.getLock("payment:lock:" + orderId);
+        RLock lock = redissonClient.getLock(String.format(LOCK_KEY_PAY,orderId));
         R result = null;
 
         try {
@@ -308,7 +233,6 @@ public class StoreOrderScrmController extends AppBaseController {
             logger.error("支付过程中发生异常, 订单号: {}", orderId, e);
             throw e;
         } finally {
-            // 确保锁被释放
             if (lock.isHeldByCurrentThread()) {
                 lock.unlock();
                 logger.debug("支付锁已释放, 订单号: {}", orderId);
@@ -322,309 +246,137 @@ public class StoreOrderScrmController extends AppBaseController {
     @ApiOperation("取消支付")
     @PostMapping("/cancelPay")
     public R cancelPay(@Validated @RequestBody FsStoreOrderPayParam param) {
+        Long orderId = param.getOrderId();
+
         logger.info("用户取消支付 订单号: {},支付类型",param.getOrderId());
-        orderService.cancelPay(param);
-        return R.ok();
+        RLock lock = redissonClient.getLock(String.format(LOCK_KEY_PAY,orderId));
+        try {
+            boolean locked = lock.tryLock(100, 30000, TimeUnit.MILLISECONDS);
+
+            if (!locked) {
+                logger.warn("订单正在处理中,获取锁失败, 订单号: {}", orderId);
+                return R.error("订单正在处理中,请稍后再试");
+            }
+
+            orderService.cancelPay(param);
+            return R.ok();
+
+        } catch (InterruptedException e) {
+            logger.error("获取取消支付锁的过程被中断, 订单号: {}", orderId, e);
+            Thread.currentThread().interrupt();
+            return R.error("取消支付处理被中断,请稍后重试");
+        } catch (Throwable e) {
+            logger.error("取消支付过程中发生异常, 订单号: {}", orderId, e);
+            throw e;
+        } finally {
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
+                logger.debug("取消支付锁已释放, 订单号: {}", orderId);
+            }
+        }
     }
 
 
     @Login
     @ApiOperation("修改支付类型")
     @PostMapping("/editPayType")
-    @Transactional
     public R editPayType(HttpServletRequest request, @Validated @RequestBody FsStoreOrderPayParam param) {
-        FsStoreOrderScrm order=orderService.selectFsStoreOrderById(param.getOrderId());
-        if(order==null){
-            return R.error("订单不存在");
-        }
-        if(order.getStatus()!= OrderInfoEnum.STATUS_0.getValue()){
-            return R.error("订单状态不正确");
-        }
-        String orderId=redisCache.getCacheObject("isPaying:"+order.getId());
-        if(StringUtils.isNotEmpty(orderId)&&orderId.equals(order.getId().toString())){
-            return R.error("正在支付中...");
-        }
-        List<FsStorePaymentScrm>  payments=fsStorePaymentMapper.selectFsStorePaymentByOrder(order.getId());
-        if(payments.size()>0){
-            for(FsStorePaymentScrm payment : payments){
-                OrderQueryDTO orderQueryDTO = new OrderQueryDTO();
-                orderQueryDTO.setLowOrderId("store-"+payment.getPayCode());
-                OrderResult orderResult = ybPayService.getOrder(orderQueryDTO);
-                if(orderResult.getStatus()!= null&&orderResult.getStatus().equals("100")){
-                    if(orderResult.getState()!=null&&orderResult.getState().equals("0")){
-                        return R.error("订单已支付");
-                    }
-
-                }
-            }
-        }
-        FsUserScrm user=userService.selectFsUserById(order.getUserId());
-        if(user!=null){
-            //已改价处理
-            if(order.getIsEditMoney()!=null&&order.getIsEditMoney()==1){
-                //改过价不做处理
+        Long orderId = param.getOrderId();
+        logger.info("开始处理修改支付类型请求, 订单号: {}, 新支付类型: {}", orderId, param.getPayType());
+        RLock lock = redissonClient.getLock(String.format(LOCK_KEY_EDIT_PAY_TYPE,orderId));
+        R result = null;
+        try {
+            boolean locked = lock.tryLock(100, 10000, TimeUnit.MILLISECONDS);
+            if (!locked) {
+                logger.warn("订单支付类型正在修改中,获取锁失败, 订单号: {}", orderId);
+                return R.error("订单正在处理中,请勿重复提交");
             }
-            else{
-                String config=configService.selectConfigByKey("his.store");
-                StoreConfig storeConfig= JSONUtil.toBean(config,StoreConfig.class);
-                if(param.getPayType().equals(1)){
-                    order.setPayType("1");
-                    order.setPayMoney(order.getPayPrice());
-                    order.setPayDelivery(BigDecimal.ZERO);
-                }
-                else if(param.getPayType().equals(2)){
-
-                    order.setPayType("2");
-                    BigDecimal payMoney=order.getPayPrice().multiply(new BigDecimal(storeConfig.getPayRate())).divide(new BigDecimal(100));
-                    payMoney=new BigDecimal(payMoney.setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
-                    order.setPayDelivery(order.getPayPrice().subtract(payMoney));
-                    order.setPayMoney(payMoney);
-                }
-                else if(param.getPayType().equals(3)){
-                    //货到付款
-                    order.setPayType("3");
-//                    order.setPayDelivery(order.getPayPrice() );
-//                    order.setPayMoney(BigDecimal.ZERO);
-                    BigDecimal amount=redisCache.getCacheObject("orderAmount:"+order.getId());
-                    BigDecimal payMoney = BigDecimal.ZERO;
-                    if (amount != null){
-                        payMoney=amount;
-                    }
-                    order.setPayMoney(payMoney);
-                    order.setPayDelivery(order.getPayPrice().subtract(payMoney) );
-                }
-                orderService.updateFsStoreOrder(order);
+            result = orderService.editPayType(param);
+        } catch (InterruptedException e) {
+            logger.error("获取修改支付类型锁的过程被中断, 订单号: {}", orderId, e);
+            Thread.currentThread().interrupt();
+            return R.error("修改支付类型处理被中断,请稍后重试");
+        } catch (Throwable e) {
+            logger.error("修改支付类型过程中发生异常, 订单号: {}", orderId, e);
+            throw e;
+        } finally {
+            if (lock != null && lock.isHeldByCurrentThread()) {
+                lock.unlock();
+                logger.debug("修改支付类型锁已释放, 订单号: {}", orderId);
             }
-            return R.ok().put("order",order);
-        }
-        else{
-            return R.error("用户OPENID不存在");
         }
-
+        return result;
     }
 
 
     @Login
     @ApiOperation("支付尾款")
     @PostMapping("/payRemain")
-    @Transactional
     public R payRemain(HttpServletRequest request, @Validated @RequestBody FsStoreOrderPayParam param) {
-        FsStoreOrderScrm order=orderService.selectFsStoreOrderById(param.getOrderId());
-        if(order==null){
-            return R.error("订单不存在");
-        }
-        if(order.getStatus()!= OrderInfoEnum.STATUS_2.getValue()){
-            return R.error("待收货订单可申请支付尾款");
-        }
-        if(order.getPayType().equals(1)){
-            return R.error("此订单已支付");
-        }
-        //只有顺风才可以付尾
-        if(!order.getDeliverySn().trim().equals("SF")){
-            return R.error("只有顺丰物流支持尾款支付");
-        }
-
-        if(!order.getIsPayRemain().equals(0)){
-            return R.error("此订单已支付");
-        }
-        FsUserScrm user=userService.selectFsUserById(order.getUserId());
-        if(user!=null){
-            String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
-            String json = configService.selectConfigByKey(STORE_PAY_CONF);
-            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
-            FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
-            storePayment.setCompanyId(order.getCompanyId());
-            storePayment.setCompanyUserId(order.getCompanyUserId());
-            storePayment.setStatus(0);
-            storePayment.setPayMode(fsPayConfig.getType());
-            storePayment.setPayCode(payCode);
-            storePayment.setPayMoney(order.getPayDelivery());
-            storePayment.setCreateTime(new Date());
-            storePayment.setPayTypeCode("weixin");
-            storePayment.setBusinessType(2);
-            storePayment.setRemark("商城订单尾款支付");
-            storePayment.setOpenId(user.getRealName());
-            storePayment.setUserId(user.getUserId());
-            storePayment.setBusinessOrderId(order.getId().toString());
-            storePayment.setOrderId(order.getId());
-            storePayment.setIsPayRemain(1);
-            fsStorePaymentMapper.insertFsStorePayment(storePayment);
-
-            if (fsPayConfig.getType().equals("hf")){
-                HuiFuCreateOrder o = new HuiFuCreateOrder();
-                o.setTradeType("T_MINIAPP");
-                o.setOpenid(user.getMaOpenId());
-                o.setReqSeqId("store_remain-"+storePayment.getPayCode());
-                o.setTransAmt(storePayment.getPayMoney().toString());
-                o.setGoodsDesc("商城订单尾款支付");
-                HuifuCreateOrderResult result = huiFuService.createOrder(o);
-                //创建订单
-                if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
-
-                    FsStorePaymentScrm mt=new FsStorePaymentScrm();
-                    mt.setPaymentId(storePayment.getPaymentId());
-                    mt.setTradeNo(result.getHf_seq_id());
-                    fsStorePaymentMapper.updateFsStorePayment(mt);
-                    redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
-                    Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
-                    String s = (String) resultMap.get("package");
-                    resultMap.put("packageValue",s);
-                    return R.ok().put("payType",param.getPayType()).put("result",resultMap);
-                }
-                else{
-                    return R.error(result.getResp_desc());
-                }
-            }else if(fsPayConfig.getType().equals("wx")) {
-                WxPayConfig payConfig = new WxPayConfig();
-                payConfig.setAppId(fsPayConfig.getAppId());
-                payConfig.setMchId(fsPayConfig.getWxMchId());
-                payConfig.setMchKey(fsPayConfig.getWxMchKey());
-                payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
-                payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
-                payConfig.setKeyPath(null);
-                payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
-                wxPayService.setConfig(payConfig);
-                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
-                orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
-                orderRequest.setBody("商城订单支付");
-                orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
-                orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
-                //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
-                orderRequest.setTradeType("JSAPI");
-                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
-                //调用统一下单接口,获取"预支付交易会话标识"
-                try {
-                    WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
-                    return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0);
-                } catch (WxPayException e) {
-                    e.printStackTrace();
-                    throw new CustomException("支付失败" + e.getMessage());
-                }
+        Long orderId = param.getOrderId();
+        logger.info("开始处理支付尾款请求, 订单号: {}, 支付类型: {}", orderId, param.getPayType());
+        RLock lock = redissonClient.getLock(String.format(LOCK_KEY_PAY_REMAIN,orderId));
+        R result = null;
+        try {
+            boolean locked = lock.tryLock(100, 30000, TimeUnit.MILLISECONDS);
+            if (!locked) {
+                logger.warn("订单尾款正在处理中,获取锁失败, 订单号: {}", orderId);
+                return R.error("订单尾款正在处理中,请勿重复提交");
+            }
+            result = orderService.payRemain(param);
+        } catch (InterruptedException e) {
+            logger.error("获取支付尾款锁的过程被中断, 订单号: {}", orderId, e);
+            Thread.currentThread().interrupt();
+            return R.error("支付尾款处理被中断,请稍后重试");
+        } catch (Throwable e) {
+            logger.error("支付尾款过程中发生异常, 订单号: {}", orderId, e);
+            throw e;
+        } finally {
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
+                logger.debug("支付尾款锁已释放, 订单号: {}", orderId);
             }
         }
-        else{
-            return R.error("用户OPENID不存在");
-        }
-        return R.error("无支付类型");
+        return result;
     }
 
 
     @Login
     @ApiOperation("亲友支付")
     @PostMapping("/otherPayment")
-    @Transactional
     public R otherPayment(@Validated @RequestBody FsStoreOrderOtherPayParam param, HttpServletRequest request){
-        final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
+        Long orderId = param.getOrderId();
+        logger.info("开始处理亲友支付请求, 订单号: {}", orderId);
+
+        String lockKey = String.format(LOCK_KEY_PAY,orderId);
+        RLock lock = redissonClient.getLock(lockKey);
+        R result = null;
+
         try {
-            String ip = IpUtil.getRequestIp();
-            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
-            FsUserScrm user=userService.selectFsUserByMaOpenId(session.getOpenid());
-            if(user==null){
-                //创建
-                user=new FsUserScrm();
-                user.setUsername("");
-                user.setNickname("微信用户");
-                user.setStatus(1);
-                user.setMaOpenId(session.getOpenid());
-                user.setUnionId(session.getUnionid());
-                user.setIsWeixinAuth(0);
-                user.setLastIp(ip);
-                user.setCreateTime(new Date());
-                userService.insertFsUser(user);
-            }
-            FsStoreOrderScrm order=orderService.selectFsStoreOrderById(param.getOrderId());
-            if(order==null){
-                return R.error("订单不存在");
-            }
-            if(order.getStatus()!= OrderInfoEnum.STATUS_0.getValue()){
-                return R.error("此订单不能支付");
-            }
-            if(order.getPayMoney().compareTo(new BigDecimal(0))<1){
-                return R.error("此订单没有可支付的金额");
-            }
+            boolean locked = lock.tryLock(100, 30000, TimeUnit.MILLISECONDS);
 
-            String orderId=redisCache.getCacheObject("isPaying:"+order.getId());
-            if(StringUtils.isNotEmpty(orderId)&&orderId.equals(order.getId().toString())){
-                return R.error("正在支付中...");
+            if (!locked) {
+                logger.warn("订单正在处理中,获取锁失败, 订单号: {}", orderId);
+                return R.error("订单正在处理中,请勿重复提交");
             }
 
-            String json = configService.selectConfigByKey(STORE_PAY_CONF);
-            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
-            String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
-            //易宝支付
-            FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
-            storePayment.setCompanyId(order.getCompanyId());
-            storePayment.setCompanyUserId(order.getCompanyUserId());
-            storePayment.setStatus(0);
-            storePayment.setPayCode(payCode);
-            storePayment.setPayMode(fsPayConfig.getType());
-            storePayment.setPayMoney(order.getPayMoney());
-            storePayment.setCreateTime(new Date());
-            storePayment.setPayTypeCode("weixin");
-            storePayment.setBusinessType(2);
-            storePayment.setRemark("商城订单支付");
-            storePayment.setOpenId(session.getOpenid());
-            storePayment.setUserId(user.getUserId());
-            storePayment.setBusinessOrderId(order.getId().toString());
-            storePayment.setOrderId(order.getId());
-            fsStorePaymentMapper.insertFsStorePayment(storePayment);
-
-            if (fsPayConfig.getType().equals("hf")){
-                HuiFuCreateOrder o = new HuiFuCreateOrder();
-                o.setTradeType("T_MINIAPP");
-                o.setOpenid(user.getMaOpenId());
-                o.setReqSeqId("store-"+storePayment.getPayCode());
-                o.setTransAmt(storePayment.getPayMoney().toString());
-                o.setGoodsDesc("商城订单支付");
-                HuifuCreateOrderResult result = huiFuService.createOrder(o);
-                if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
-
-                    FsStorePaymentScrm mt=new FsStorePaymentScrm();
-                    mt.setPaymentId(storePayment.getPaymentId());
-                    mt.setTradeNo(result.getHf_seq_id());
-                    fsStorePaymentMapper.updateFsStorePayment(mt);
-                    redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
-                    Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
-                    String s = (String) resultMap.get("package");
-                    resultMap.put("packageValue",s);
-                    return R.ok().put("result",resultMap);
-                }
-                else{
-                    return R.error(result.getResp_desc());
-                }
-            }else  if (fsPayConfig.getType().equals("wx")){
-                //创建微信订单
-                WxPayConfig payConfig = new WxPayConfig();
-                payConfig.setAppId(fsPayConfig.getAppId());
-                payConfig.setMchId(fsPayConfig.getWxMchId());
-                payConfig.setMchKey(fsPayConfig.getWxMchKey());
-                payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
-                payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
-                payConfig.setKeyPath(null);
-                payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
-                wxPayService.setConfig(payConfig);
-                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
-                orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
-                orderRequest.setBody("商城订单支付");
-                orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
-                orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
-                //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
-                orderRequest.setTradeType("JSAPI");
-                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
-                //调用统一下单接口,获取"预支付交易会话标识"
-                try {
-                    WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
-                    return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0);
-                } catch (WxPayException e) {
-                    e.printStackTrace();
-                    throw new CustomException("支付失败" + e.getMessage());
-                }
+            result = orderService.otherPayment(param);
+
+        } catch (InterruptedException e) {
+            logger.error("获取支付锁的过程被中断, 订单号: {}", orderId, e);
+            Thread.currentThread().interrupt();
+            return R.error("支付处理被中断,请稍后重试");
+        } catch (Throwable e) {
+            logger.error("亲友支付过程中发生异常, 订单号: {}", orderId, e);
+            throw e;
+        } finally {
+            if (lock != null && lock.isHeldByCurrentThread()) {
+                lock.unlock();
+                logger.debug("支付锁已释放, 订单号: {}", orderId);
             }
-        } catch (WxErrorException e) {
-            e.printStackTrace();
-            return R.error(e.getMessage());
         }
-        return R.error("无支付类型");
+
+        return result;
     }
 
     @Login
@@ -632,119 +384,36 @@ public class StoreOrderScrmController extends AppBaseController {
     @PostMapping("/otherPaymentRemain")
     @Transactional
     public R otherPaymentRemain(@Validated @RequestBody FsStoreOrderOtherPayParam param, HttpServletRequest request){
-        final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
+        Long orderId = param.getOrderId();
+        logger.info("开始处理亲友支付请求, 订单号: {}", orderId);
+        String lockKey = String.format(LOCK_KEY_PAY,orderId);
+        RLock lock = redissonClient.getLock(lockKey);
+        R result = null;
         try {
-            String ip = IpUtil.getRequestIp();
-            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
-            FsUserScrm user=userService.selectFsUserByMaOpenId(session.getOpenid());
-            if(user==null){
-                //创建
-                user=new FsUserScrm();
-                user.setUsername("");
-                user.setNickname("微信用户");
-                user.setStatus(1);
-                user.setMaOpenId(session.getOpenid());
-                user.setUnionId(session.getUnionid());
-                user.setIsWeixinAuth(0);
-                user.setLastIp(ip);
-                user.setCreateTime(new Date());
-                userService.insertFsUser(user);
-            }
-            FsStoreOrderScrm order=orderService.selectFsStoreOrderById(param.getOrderId());
-            if(order==null){
-                return R.error("订单不存在");
-            }
-            if(order.getStatus()!= OrderInfoEnum.STATUS_2.getValue()){
-                return R.error("待收货订单可申请支付尾款");
-            }
-            if(order.getPayType().equals(1)){
-                return R.error("此订单已支付");
-            }
-            //只有顺风才可以付尾
-            if(!order.getDeliverySn().trim().equals("SF")){
-                return R.error("只有顺丰物流支持尾款支付");
-            }
+            boolean locked = lock.tryLock(100, 30000, TimeUnit.MILLISECONDS);
 
-            if(!order.getIsPayRemain().equals(0)){
-                return R.error("此订单已支付");
+            if (!locked) {
+                logger.warn("订单正在处理中,获取锁失败, 订单号: {}", orderId);
+                return R.error("订单正在处理中,请勿重复提交");
             }
-            String payCode = IdUtil.getSnowflake(0, 0).nextIdStr();
-            String json = configService.selectConfigByKey(STORE_PAY_CONF);
-            FsPayConfigScrm fsPayConfig = JSON.parseObject(json, FsPayConfigScrm.class);
-            FsStorePaymentScrm storePayment=new FsStorePaymentScrm();
-            storePayment.setCompanyId(order.getCompanyId());
-            storePayment.setCompanyUserId(order.getCompanyUserId());
-            storePayment.setStatus(0);
-            storePayment.setPayMode(fsPayConfig.getType());
-            storePayment.setPayCode(payCode);
-            storePayment.setPayMoney(order.getPayDelivery());
-            storePayment.setCreateTime(new Date());
-            storePayment.setPayTypeCode("weixin");
-            storePayment.setBusinessType(2);
-            storePayment.setRemark("商城订单尾款支付");
-            storePayment.setOpenId(user.getRealName());
-            storePayment.setUserId(user.getUserId());
-            storePayment.setBusinessOrderId(order.getId().toString());
-            storePayment.setOrderId(order.getId());
-            storePayment.setIsPayRemain(1);
-            fsStorePaymentMapper.insertFsStorePayment(storePayment);
-
-            if (fsPayConfig.getType().equals("hf")){
-                HuiFuCreateOrder o = new HuiFuCreateOrder();
-                o.setTradeType("T_MINIAPP");
-                o.setOpenid(user.getMaOpenId());
-                o.setReqSeqId("store-"+storePayment.getPayCode());
-                o.setTransAmt(storePayment.getPayMoney().toString());
-                o.setGoodsDesc("商城订单支付");
-                HuifuCreateOrderResult result = huiFuService.createOrder(o);
-                if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
-
-                    FsStorePaymentScrm mt=new FsStorePaymentScrm();
-                    mt.setPaymentId(storePayment.getPaymentId());
-                    mt.setTradeNo(result.getHf_seq_id());
-                    fsStorePaymentMapper.updateFsStorePayment(mt);
-                    redisCache.setCacheObject("isPaying:"+order.getId(),order.getId().toString(),1, TimeUnit.MINUTES);
-                    Map<String, Object> resultMap = JSON.parseObject(result.getPay_info(), new TypeReference<Map<String, Object>>() {});
-                    String s = (String) resultMap.get("package");
-                    resultMap.put("packageValue",s);
-                    return R.ok().put("result",resultMap);
-                }
-                else{
-                    return R.error(result.getResp_desc());
-                }
-            }else  if (fsPayConfig.getType().equals("wx")){
-                //创建微信订单
-                WxPayConfig payConfig = new WxPayConfig();
-                payConfig.setAppId(fsPayConfig.getAppId());
-                payConfig.setMchId(fsPayConfig.getWxMchId());
-                payConfig.setMchKey(fsPayConfig.getWxMchKey());
-                payConfig.setSubAppId(org.apache.commons.lang3.StringUtils.trimToNull(null));
-                payConfig.setSubMchId(org.apache.commons.lang3.StringUtils.trimToNull(null));
-                payConfig.setKeyPath(null);
-                payConfig.setNotifyUrl(fsPayConfig.getNotifyUrlScrm());
-                wxPayService.setConfig(payConfig);
-                WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
-                orderRequest.setOpenid(user.getMaOpenId());//公众号支付提供用户openid
-                orderRequest.setBody("商城订单支付");
-                orderRequest.setOutTradeNo("store-" + storePayment.getPayCode());
-                orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(storePayment.getPayMoney().toString()));//测试
-                //orderRequest.setTotalFee(WxPayUnifiedOrderRequest.yuanToFen(money));//测试
-                orderRequest.setTradeType("JSAPI");
-                orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
-                //调用统一下单接口,获取"预支付交易会话标识"
-                try {
-                    WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
-                    return R.ok().put("result", orderResult).put("type", "wx").put("isPay", 0);
-                } catch (WxPayException e) {
-                    e.printStackTrace();
-                    throw new CustomException("支付失败" + e.getMessage());
-                }
+
+            result = orderService.otherPaymentRemain(param);
+
+        } catch (InterruptedException e) {
+            logger.error("获取亲友支付锁的过程被中断, 订单号: {}", orderId, e);
+            Thread.currentThread().interrupt();
+            return R.error("支付处理被中断,请稍后重试");
+        } catch (Throwable e) {
+            logger.error("亲友支付过程中发生异常, 订单号: {}", orderId, e);
+            throw e;
+        } finally {
+            if (lock != null && lock.isHeldByCurrentThread()) {
+                lock.unlock();
+                logger.debug("亲友支付锁已释放, 订单号: {}", orderId);
             }
-        } catch (WxErrorException e) {
-            e.printStackTrace();
-            return R.error(e.getMessage());
         }
-        return R.error("无支付类型");
+
+        return result;
     }
 
 
@@ -752,16 +421,37 @@ public class StoreOrderScrmController extends AppBaseController {
     @ApiOperation("取消订单")
     @PostMapping("/cancelOrder")
     public R cancelOrder( @Validated @RequestBody FsStoreOrderCancelParam param, HttpServletRequest request){
-        FsStoreOrderScrm order=orderService.selectFsStoreOrderById(param.getOrderId());
-        if (ObjectUtil.isNull(order)) {
-            throw new CustomException("订单不存在");
-        }
-        if (order.getStatus() !=0) {
-            throw new CustomException("非法操作");
-        }
-        orderService.cancelOrder(param.getOrderId());
 
-        return R.ok("操作成功");
+        Long orderId = param.getOrderId();
+        logger.info("开始处理取消订单请求, 订单号: {}", orderId);
+
+        RLock lock = redissonClient.getLock(String.format(LOCK_KEY_CANCEL,orderId));
+        R result = null;
+
+        try {
+            boolean locked = lock.tryLock(100, 30000, TimeUnit.MILLISECONDS);
+            if (!locked) {
+                throw new CustomException("订单正在处理中,请勿重复提交");
+            }
+            FsStoreOrderScrm order = orderService.selectFsStoreOrderById(orderId);
+            if (ObjectUtil.isNull(order)) {
+                throw new CustomException("订单不存在");
+            }
+            if (order.getStatus() != 0) {
+                throw new CustomException("非法操作");
+            }
+            orderService.cancelOrder(orderId);
+
+            result = R.ok("操作成功");
+        } catch (Exception e) {
+            logger.error("取消订单失败, 订单号: {}", orderId, e);
+            throw new CustomException("取消订单过程中发生错误");
+        } finally {
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
+            }
+        }
+        return result;
     }
     @Login
     @ApiOperation("物流查询")