|  | @@ -1,6 +1,5 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
	
		
			
				|  | @@ -27,7 +26,6 @@ import com.ym.mec.common.service.IdGeneratorService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.tenant.TenantContextHolder;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.yqpay.DateUtils;
 | 
	
		
			
				|  |  | -import com.ym.mec.thirdparty.yqpay.Msg;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  |  import org.apache.commons.collections.CollectionUtils;
 | 
	
		
			
				|  |  |  import org.apache.commons.lang3.StringUtils;
 | 
	
	
		
			
				|  | @@ -442,12 +440,19 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |       * 机构开通缴费
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public Map<String, Object> tenantOpenPay(Integer tenantId) throws Exception {
 | 
	
		
			
				|  |  | +        TenantOrderRecordEnum tenantEnum = TenantOrderRecordEnum.TENANT_OPEN;
 | 
	
		
			
				|  |  | +        RBucket<Map<String, Object>> cacheOrder = cacheOrder(tenantEnum.getCode() + ":" + tenantId);
 | 
	
		
			
				|  |  | +        Map<String, Object> result = cacheOrder.get();
 | 
	
		
			
				|  |  | +        if (Objects.nonNull(result)) {
 | 
	
		
			
				|  |  | +            return result;
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            result = new HashMap<>();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          TenantProductInfo productInfo = getProductInfo(tenantId);
 | 
	
		
			
				|  |  |          //生成订单编号
 | 
	
		
			
				|  |  |          String orderNo = idGenerator.generatorId("payment") + "";
 | 
	
		
			
				|  |  |          String baseApiUrl = sysConfigDao.findConfigValue("base_api_url");
 | 
	
		
			
				|  |  | -        TenantOrderRecordEnum tenantEnum = TenantOrderRecordEnum.TENANT_OPEN;
 | 
	
		
			
				|  |  | -        Map<String, Object> result = new HashMap<>();
 | 
	
		
			
				|  |  |          int orderState = 0;
 | 
	
		
			
				|  |  |          //消费大于0元则拉起支付
 | 
	
		
			
				|  |  |          if (productInfo.getPayAmount().compareTo(BigDecimal.ZERO) > 0) {
 | 
	
	
		
			
				|  | @@ -462,6 +467,8 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |                      1,//todo 机构开通付款临时写死
 | 
	
		
			
				|  |  |                      tenantEnum.getCode()
 | 
	
		
			
				|  |  |              );
 | 
	
		
			
				|  |  | +            //将数据缓存起来避免重复生成订单数据,如果这次没有支付 3分钟内可以重复支付
 | 
	
		
			
				|  |  | +            cacheOrder.set(result, 3L, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              //已支付
 | 
	
		
			
				|  |  |              orderState = 1;
 | 
	
	
		
			
				|  | @@ -469,9 +476,9 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |              Map<String, Object> payMap = new HashMap<>();
 | 
	
		
			
				|  |  |              payMap.put("orderNo", orderNo);
 | 
	
		
			
				|  |  |              result.put("payMap", payMap);
 | 
	
		
			
				|  |  | -            result.put("amount", productInfo.getPayAmount());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        createOrderRecord(tenantId, productInfo.getPayAmount(), orderNo, tenantEnum, orderState);
 | 
	
		
			
				|  |  | +        result.put("amount", productInfo.getPayAmount());
 | 
	
		
			
				|  |  | +        createOrderRecord(tenantId, productInfo.getPayAmount(), orderNo, tenantEnum, orderState, WrapperUtil.toStr(result, "routingMerNos"));
 | 
	
		
			
				|  |  |          log.info("tenant pay >>>>> {} ", result);
 | 
	
		
			
				|  |  |          return result;
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -483,14 +490,19 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |       * @param val      购买周期
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public Map<String, Object> tenantRenewPay(Integer tenantId, Integer val) throws Exception {
 | 
	
		
			
				|  |  | +        TenantOrderRecordEnum tenantEnum = TenantOrderRecordEnum.TENANT_RENEW;
 | 
	
		
			
				|  |  | +        RBucket<Map<String, Object>> cacheOrder = cacheOrder(tenantEnum.getCode() + ":" + tenantId + ":" + val);
 | 
	
		
			
				|  |  | +        Map<String, Object> result = cacheOrder.get();
 | 
	
		
			
				|  |  | +        if (Objects.nonNull(result)) {
 | 
	
		
			
				|  |  | +            return result;
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            result = new HashMap<>();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          TenantProductInfo productInfo = getProductInfo(tenantId);
 | 
	
		
			
				|  |  |          //续费时 取合同价
 | 
	
		
			
				|  |  |          BigDecimal amount = productInfo.getContractPrice().multiply(new BigDecimal(val));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          //生成订单编号
 | 
	
		
			
				|  |  |          String orderNo = idGenerator.generatorId("payment") + "";
 | 
	
		
			
				|  |  | -        TenantOrderRecordEnum tenantEnum = TenantOrderRecordEnum.TENANT_RENEW;
 | 
	
		
			
				|  |  | -        Map<String, Object> result = new HashMap<>();
 | 
	
		
			
				|  |  |          int orderState = 0;
 | 
	
		
			
				|  |  |          //消费大于0元则拉起支付
 | 
	
		
			
				|  |  |          if (amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
	
		
			
				|  | @@ -507,23 +519,33 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |              );
 | 
	
		
			
				|  |  |              //操作续费信息,把续费周期存起来
 | 
	
		
			
				|  |  |              opsRenewInfo(tenantId).set(val, 1, TimeUnit.HOURS);
 | 
	
		
			
				|  |  | +            //将数据缓存起来避免重复生成订单数据,如果这次没有支付 3分钟内可以重复支付
 | 
	
		
			
				|  |  | +            cacheOrder.set(result, 3L, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              //续费成功
 | 
	
		
			
				|  |  |              renewSuccess(val, productInfo, amount);
 | 
	
		
			
				|  |  |              orderState = 1;
 | 
	
		
			
				|  |  | -            result.put("amount", amount);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        createOrderRecord(tenantId, amount, orderNo, tenantEnum, orderState);
 | 
	
		
			
				|  |  | +        result.put("amount", amount);
 | 
	
		
			
				|  |  | +        createOrderRecord(tenantId, amount, orderNo, tenantEnum, orderState, WrapperUtil.toStr(result, "routingMerNos"));
 | 
	
		
			
				|  |  |          log.info("tenant pay >>>>> {} ", result);
 | 
	
		
			
				|  |  |          return result;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    //操作续费信息
 | 
	
		
			
				|  |  | -    public RBucket<Object> opsRenewInfo(Integer tenantId) {
 | 
	
		
			
				|  |  | -        String key = TenantOrderRecordEnum.TENANT_RENEW.getCode() + ":" + tenantId;
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 缓存订单数据
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param key TenantOrderRecordEnum+机构id+其他
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public RBucket<Map<String, Object>> cacheOrder(String key) {
 | 
	
		
			
				|  |  |          return redissonClient.getBucket(key);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    //操作续费信息
 | 
	
		
			
				|  |  | +    public RBucket<Integer> opsRenewInfo(Integer key) {
 | 
	
		
			
				|  |  | +        return redissonClient.getBucket(TenantOrderRecordEnum.TENANT_RENEW.getCode() + ":" + key);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 续费成功
 | 
	
		
			
				|  |  |       *
 | 
	
	
		
			
				|  | @@ -581,7 +603,7 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      //生成订单
 | 
	
		
			
				|  |  |      private void createOrderRecord(Integer tenantId, BigDecimal payAmount, String orderNo, TenantOrderRecordEnum
 | 
	
		
			
				|  |  | -            tenantEnum, Integer orderState) {
 | 
	
		
			
				|  |  | +            tenantEnum, Integer orderState, String merNos) {
 | 
	
		
			
				|  |  |          TenantOrderRecord record = new TenantOrderRecord();
 | 
	
		
			
				|  |  |          record.setTenantId(tenantId);
 | 
	
		
			
				|  |  |          record.setOrderNo(orderNo);
 | 
	
	
		
			
				|  | @@ -590,6 +612,7 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |          record.setExpectAmount(payAmount);
 | 
	
		
			
				|  |  |          record.setActualAmount(payAmount);
 | 
	
		
			
				|  |  |          record.setOrderState(orderState);
 | 
	
		
			
				|  |  | +        record.setMerNos(merNos);
 | 
	
		
			
				|  |  |          LocalDateTime now = LocalDateTime.now();
 | 
	
		
			
				|  |  |          if (orderState == 1) {
 | 
	
		
			
				|  |  |              record.setPayDate(now.toLocalDate().toDate());
 | 
	
	
		
			
				|  | @@ -600,44 +623,6 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  | -     * 支付消息回调
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @param msg
 | 
	
		
			
				|  |  | -     * @return
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    @Override
 | 
	
		
			
				|  |  | -    public Msg orderNotify(Msg msg) {
 | 
	
		
			
				|  |  | -        log.info("tenant orderNotify >>>>> {}", msg);
 | 
	
		
			
				|  |  | -        msg.setMsg("fail");
 | 
	
		
			
				|  |  | -        Map<String, String> notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
 | 
	
		
			
				|  |  | -        String orderNo = notifyMap.get("merMerOrderNo");
 | 
	
		
			
				|  |  | -        TenantOrderRecord record = tenantOrderRecordService.getOne(new WrapperUtil<TenantOrderRecord>()
 | 
	
		
			
				|  |  | -                .hasEq("order_no_", orderNo).queryWrapper());
 | 
	
		
			
				|  |  | -        String channelType = notifyMap.get("channelType").equals("1") ? "WXPay" : (notifyMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
 | 
	
		
			
				|  |  | -        record.setPayChannel(channelType);
 | 
	
		
			
				|  |  | -        //支付中订单存在,更新状态 tradeState 88就是正确的 其他错误的
 | 
	
		
			
				|  |  | -        if (msg.getResponseType().equals("1") && notifyMap.size() > 0 && msg.getCode().equals("88")) {
 | 
	
		
			
				|  |  | -            // notifyMap.put("totalMoney", notifyMap.get("payAmount"));
 | 
	
		
			
				|  |  | -            LocalDateTime now = LocalDateTime.now();
 | 
	
		
			
				|  |  | -            record.setOrderState(1);
 | 
	
		
			
				|  |  | -            record.setPayDate(now.toLocalDate().toDate());
 | 
	
		
			
				|  |  | -            record.setPayTime(now.toDate());
 | 
	
		
			
				|  |  | -            msg.setCode("000000");
 | 
	
		
			
				|  |  | -            msg.setMsg("success");
 | 
	
		
			
				|  |  | -            if (TenantOrderRecordEnum.TENANT_OPEN.getCode().equals(record.getOrderType())) {
 | 
	
		
			
				|  |  | -                tenantOpen(record.getTenantId());
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        } else {
 | 
	
		
			
				|  |  | -            //支付失败
 | 
	
		
			
				|  |  | -            record.setOrderState(2);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        log.info("tenant orderNotify return>>>>> {}", record);
 | 
	
		
			
				|  |  | -        tenantOrderRecordService.updateById(record);
 | 
	
		
			
				|  |  | -        log.info("tenant orderNotify return>>>>> {}", msg);
 | 
	
		
			
				|  |  | -        return msg;
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  |       * 校验手机号
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      private RBucket<Object> checkPhone(TenantInfoDto dto) {
 | 
	
	
		
			
				|  | @@ -747,10 +732,10 @@ public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoDao, TenantInfo
 | 
	
		
			
				|  |  |                      tenantEnum.getCode()
 | 
	
		
			
				|  |  |              );
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  | -            result.put("amount",amount);
 | 
	
		
			
				|  |  | -            return result;
 | 
	
		
			
				|  |  | +            orderState = 1;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        createOrderRecord(tenantId, amount, orderNo, tenantEnum, orderState);
 | 
	
		
			
				|  |  | +        result.put("amount", amount);
 | 
	
		
			
				|  |  | +        createOrderRecord(tenantId, amount, orderNo, tenantEnum, orderState, WrapperUtil.toStr(result, "routingMerNos"));
 | 
	
		
			
				|  |  |          log.info("tenant pay >>>>> {} ", result);
 | 
	
		
			
				|  |  |          return result;
 | 
	
		
			
				|  |  |      }
 |