kongdeqiang
昨天 973f7a2b420dddb06553489d72d4aa6fdea3c1c0
src/main/java/com/boying/controller/phone/YCPayController.java
@@ -13,6 +13,7 @@
import com.boying.service.*;
import com.boying.util.DateUtilOther;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
@@ -34,6 +35,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
@@ -163,8 +165,11 @@
        if(StringUtils.isNotBlank(outPark.getPayCode()) && outPark.getStatus() == 0){
            return R.ok(1,"订单已提交支付请求");
        }
        if(StringUtils.isNotBlank(outPark.getPayCode()) && outPark.getStatus() == 1){
            return R.failed(0,"订单已缴费");
        }
        if(StringUtils.isBlank(outPark.getPayCode())){
            OutPark by10min = outParkService.findBy10min(outPark.getCarNo(), outPark.getParkId(), outPark.getCreateTime());
            OutPark by10min = outParkService.findBy10min(outPark.getCarNo(), outPark.getParkId(), outPark.getCreateTime(),outPark.getId());
            if(by10min !=null){
                return R.ok(1,"订单已提交支付请求");
            }else {
@@ -710,7 +715,48 @@
                outParkService.saveOrUpdate(outPark);
                //缓存在redis里
                String jsonValue = JSON.toJSONString(outPark);
                redisTemplate.opsForValue().set("outPark-"+outPark.getBarrierId(), jsonValue);
                String redisKey = "outPark-"+outPark.getBarrierId();
                String parkChangeKey = "park_change_in_"+outPark.getParkId();
                int retryCount = 0;
                int maxRetry = 3;
                boolean cacheSuccess = false;
                while(retryCount < maxRetry && !cacheSuccess){
                    try {
                        redisTemplate.opsForValue().set(redisKey, jsonValue);
                        redisTemplate.opsForValue().set(parkChangeKey,"true",1, TimeUnit.DAYS);
                        cacheSuccess = true;
                        String logs = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date())+
                                " 车牌号为:"+outPark.getCarNo()+
                                ",Redis缓存设置成功,道闸ID:"+outPark.getBarrierId()+"\n";
                        writeTxt2(logs);
                    } catch (Exception e) {
                        retryCount++;
                        String errorLogs = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date())+
                                " 车牌号为:"+outPark.getCarNo()+
                                ",Redis缓存设置失败,第"+retryCount+"次重试,错误信息:"+e.getMessage()+"\n";
                        writeTxt2(errorLogs);
                        e.printStackTrace();
                        if(retryCount < maxRetry){
                            try {
                                Thread.sleep(100); // 等待100ms后重试
                            } catch (InterruptedException ie) {
                                Thread.currentThread().interrupt();
                            }
                        }
                    }
                }
                if(!cacheSuccess){
                    String errorLogs = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date())+
                            " 车牌号为:"+outPark.getCarNo()+
                            ",Redis缓存设置失败,已重试"+maxRetry+"次,请检查Redis服务状态!\n";
                    writeTxt2(errorLogs);
                    // 虽然Redis缓存失败,但数据库已更新,心跳接口可以通过数据库查询获取数据
                }
//                redisTemplate.opsForValue().set("outPark-"+outPark.getBarrierId(), jsonValue);
//                redisTemplate.opsForValue().set("park_change_in_"+outPark.getParkId(),"true",1, TimeUnit.DAYS);
            }
        }
    }