package com.fs.core.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.fs.common.config.FSConfig;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.spring.SpringUtils;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
import com.fs.common.vo.SmsSendVO;
import com.fs.core.vo.OrderCodeVO;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class OrderCodeUtils {

    public static String genOrderSn(){

        RedissonClient redissonClient =SpringUtils.getBean(RedissonClient.class);
        RLock lock = redissonClient.getLock("myLock");
        try {
            // 尝试获取锁，最多等待100秒，锁定之后最多持有锁10秒
            boolean isLocked = lock.tryLock(100, 10, TimeUnit.SECONDS);
            if (isLocked) {
                // 业务逻辑
                return IdUtil.getSnowflake(0, 0).nextIdStr();
            } else {
                return null;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }


    }
    public static String getOrderSn(){
        String url= FSConfig.getCommonApi()+ "/app/common/genOrderCode";
//        String url= "42.194.245.189:8010/app/common/genOrderCode";
        String json = HttpRequest.get(url)
                .execute().body();
        OrderCodeVO vo= JSONUtil.toBean(json, OrderCodeVO.class);
        if(vo.getCode()==200){
            return vo.getOrderCode();
        }
        else return null;
        //return OrderCodeUtils.genOrderSn();

    }

    public static String getH5RedPacketOrderSn(){
        String url= FSConfig.getH5CommonApi()+ "/app/common/genOrderCode";
        String json = HttpRequest.get(url)
                .execute().body();
        OrderCodeVO vo= JSONUtil.toBean(json, OrderCodeVO.class);
        if(vo.getCode()==200){
            return vo.getOrderCode();
        }
        else return null;

    }

//    public static String genOrderSn() {
//
//        JedisPool jedisPool = SpringUtils.getBean(JedisPool.class);
//        try (Jedis jedis = jedisPool.getResource()) {
//            String lockKey = "myLock";
//            String lockValue = UUID.randomUUID().toString();
//
//            // 尝试获取锁，设置锁的超时时间防止死锁
////            try {
////                Thread.sleep(10000); // 10秒等待
////            } catch (InterruptedException e) {
////                Thread.currentThread().interrupt();
////            }
//            SetParams params = new SetParams().nx().px(100);
//            String result = jedis.set(lockKey, lockValue, params);
//
//            if ("OK".equals(result)) {
//                try {
//                    // 业务逻辑
//                    return String.valueOf(IdUtil.getSnowflake(0, 0).nextId());
//                } finally {
//                    jedis.del(lockKey); // 释放锁
//                }
//            } else {
//                return null;
//            }
//        } catch (JedisConnectionException e) {
//            // 处理连接异常，例如重试或记录日志
//            e.printStackTrace();
//            return null;
//        }
//    }
}
