|  | @@ -3,11 +3,13 @@ package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  | +import com.mysql.cj.protocol.MessageSender;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.CloudTeacherDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.CloudTeacherOrderDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.SysConfigDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.CloudTeacherOrderDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.*;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.enums.MessageTypeEnum;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.PaymentChannelEnum;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.PeriodEnum;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.TenantOrderRecordEnum;
 | 
	
	
		
			
				|  | @@ -17,14 +19,20 @@ import com.ym.mec.biz.service.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.dal.BaseDAO;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.page.PageInfo;
 | 
	
		
			
				|  |  | +import com.ym.mec.common.page.WrapperUtil;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.IdGeneratorService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.impl.BaseServiceImpl;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.adapay.Payment;
 | 
	
		
			
				|  |  | +import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.yqpay.Msg;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.collection.MapUtil;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  |  import jodd.util.StringUtil;
 | 
	
		
			
				|  |  | +import org.apache.commons.collections.CollectionUtils;
 | 
	
		
			
				|  |  | +import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  |  import org.joda.time.LocalDateTime;
 | 
	
		
			
				|  |  | +import org.redisson.api.RBucket;
 | 
	
		
			
				|  |  | +import org.redisson.api.RedissonClient;
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
		
			
				|  |  |  import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
	
		
			
				|  | @@ -33,12 +41,16 @@ import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  |  import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  |  import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import static com.ym.mec.biz.dal.enums.PeriodEnum.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTeacherOrder> implements CloudTeacherOrderService {
 | 
	
		
			
				|  |  | +    //订单不存在
 | 
	
		
			
				|  |  | +    public static final String PAYMENT_ID_NOT_EXISTS = "payment_id_not_exists";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      private static final Logger log = LoggerFactory.getLogger(CloudTeacherOrderServiceImpl.class);
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private CloudTeacherOrderDao cloudTeacherOrderDao;
 | 
	
	
		
			
				|  | @@ -60,6 +72,10 @@ public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTea
 | 
	
		
			
				|  |  |      public BaseDAO<Long, CloudTeacherOrder> getDAO() {
 | 
	
		
			
				|  |  |          return cloudTeacherOrderDao;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private RedissonClient redissonClient;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysMessageService sysMessageService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public boolean addStudents(CloudTeacherAddQueryInfo cloudTeacherAddQueryInfo) {
 | 
	
	
		
			
				|  | @@ -251,6 +267,63 @@ public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTea
 | 
	
		
			
				|  |  |          return msg;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void payCheck(TenantOrderRecord record) {
 | 
	
		
			
				|  |  | +        RBucket<Object> bucket = redissonClient.getBucket("tenant_check_order");
 | 
	
		
			
				|  |  | +        if (!bucket.trySet(1, 3L, TimeUnit.SECONDS)) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        log.info("checkCloudTeacherOrder  checkTenantOrder>>>>>");
 | 
	
		
			
				|  |  | +        checkTransOrderState(record);
 | 
	
		
			
				|  |  | +        if (record.getOrderState() == 1) {
 | 
	
		
			
				|  |  | +            tenantOrderRecordService.updateById(record);
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                this.paySuccess(record); // 充值成功
 | 
	
		
			
				|  |  | +            } catch (Exception e) {
 | 
	
		
			
				|  |  | +                e.printStackTrace();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //TODO 发送短信
 | 
	
		
			
				|  |  | +            // Map<Integer, String> userPhone = new HashMap<>();
 | 
	
		
			
				|  |  | +            // sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, MessageTypeEnum.CLOUD_TEACHER_ORDER_SUCCESS, userPhone, null, 0, null, null);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        bucket.delete();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //主动去第三方查询订单状态
 | 
	
		
			
				|  |  | +    private void checkTransOrderState(TenantOrderRecord orderRecord) {
 | 
	
		
			
				|  |  | +        if (StringUtils.equals(orderRecord.getPaymentChannel(), PaymentChannelEnum.ADAPAY.getCode())) {
 | 
	
		
			
				|  |  | +            Map<String, Object> payment;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                payment = Payment.queryPayment(orderRecord.getTransNo());
 | 
	
		
			
				|  |  | +                log.info("checkCloudTeacherOrder  payment >>>>> {}", payment);
 | 
	
		
			
				|  |  | +                String status = (String) payment.get("status");
 | 
	
		
			
				|  |  | +                //判断状态是成功还是失败
 | 
	
		
			
				|  |  | +                if (status.equals("succeeded")) {
 | 
	
		
			
				|  |  | +                    orderRecord.setOrderState(1);
 | 
	
		
			
				|  |  | +                } else if (Objects.nonNull(payment.get("error_code"))) {
 | 
	
		
			
				|  |  | +                    //若状态是失败则判断code
 | 
	
		
			
				|  |  | +                    if (!PAYMENT_ID_NOT_EXISTS.equals(payment.get("error_code"))) {
 | 
	
		
			
				|  |  | +                        //不等于这个异常都是失败
 | 
	
		
			
				|  |  | +                        orderRecord.setOrderState(2);
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        long orderTime = orderRecord.getCreatedTime().getTime();//订单创建时间
 | 
	
		
			
				|  |  | +                        long nowTime = new Date().getTime();
 | 
	
		
			
				|  |  | +                        //丢弃5分钟之前的订单 300000 = 5 * 60秒 * 1000
 | 
	
		
			
				|  |  | +                        long exTime = 300000L;
 | 
	
		
			
				|  |  | +                        if (nowTime - orderTime > exTime) {
 | 
	
		
			
				|  |  | +                            orderRecord.setOrderState(2);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                log.info("checkCloudTeacherOrder  payment >>>>> {}", payment);
 | 
	
		
			
				|  |  | +                log.info("checkCloudTeacherOrder  orderRecord >>>>> {}", JSON.toJSONString(orderRecord));
 | 
	
		
			
				|  |  | +            } catch (Exception e) {
 | 
	
		
			
				|  |  | +                log.error("checkCloudTeacherOrder  payment error>>>>> {}", JSON.toJSONString(orderRecord));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      private void paySuccess(TenantOrderRecord record) throws Exception {
 | 
	
		
			
				|  |  |          log.info("tenant orderNotify update order status start" + record.getId().toString());
 | 
	
		
			
				|  |  |          if (record.getId() == null) {
 | 
	
	
		
			
				|  | @@ -296,11 +369,6 @@ public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTea
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public Boolean payCheck(String orderNo) throws Exception {
 | 
	
		
			
				|  |  |          TenantOrderRecord record = tenantOrderRecordService.getOne(new QueryWrapper<TenantOrderRecord>().eq("order_no_", orderNo));
 | 
	
		
			
				|  |  | -        Map<String, Object> payment = Payment.queryPayment(record.getTransNo());
 | 
	
		
			
				|  |  | -        if ("succeeded".equals(payment.get("status")) ) {
 | 
	
		
			
				|  |  | -            this.paySuccess(record);
 | 
	
		
			
				|  |  | -            return true;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  |          return record.getOrderState() == 1;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 |