//package com.bonus.common.houqin.utils.id;
//
//import cn.hutool.core.text.CharSequenceUtil;
//import org.apache.commons.io.FileUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.util.CollectionUtils;
//
//import java.io.File;
//import java.io.IOException;
//import java.io.RandomAccessFile;
//import java.nio.channels.FileChannel;
//import java.nio.channels.FileLock;
//import java.nio.charset.StandardCharsets;
//import java.util.Collection;
//import java.util.UUID;
//
//@Component
//@ConditionalOnClass({StringRedisTemplate.class})
//@EnableConfigurationProperties({IdProperties.class})
//@ConditionalOnProperty(
//   prefix = "id",
//   name = {"enabled"},
//   havingValue = "true",
//   matchIfMissing = true
//)
//public class IdWorkConfiguration {
//   private static final Logger log = LoggerFactory.getLogger(IdWorkConfiguration.class);
//   private final IdProperties idProperties;
//
//   public IdWorkConfiguration(StringRedisTemplate redisTemplate, IdProperties idProperties) throws IOException {
//      this.idProperties = idProperties;
//      Long workerId = this.getLocalWorkId();
//
//      try {
//         while(workerId == null) {
//            Long newWorkId = redisTemplate.opsForValue().increment(idProperties.getKey(), 1L);
//            this.saveLocalWorkId(String.valueOf(newWorkId));
//            workerId = this.getLocalWorkId();
//         }
//      } catch (Throwable var5) {
//         log.error("获取workID失败", var5);
//         throw var5;
//      }
//
//      if (workerId > 1024L) {
//         throw new RuntimeException("超过最大启动实例");
//      } else {
//         Id.WORKER_ID = workerId;
//      }
//   }
//
//   private void saveLocalWorkId(String workId) throws IOException {
//      File workIdHome = this.getWorkIdHome();
//      String var10002 = String.valueOf(workIdHome.getAbsoluteFile());
//      FileUtils.writeStringToFile(new File(var10002 + "/" + String.valueOf(UUID.randomUUID()) + ".lock"), workId, StandardCharsets.UTF_8);
//   }
//
//   private File getWorkIdHome() {
//      String workHome = this.idProperties.getWorkspace();
//      if (CharSequenceUtil.isBlank(workHome)) {
//         workHome = FileUtils.getUserDirectoryPath() + "/.workId/";
//      }
//
//      String var10002 = CharSequenceUtil.removeSuffix(workHome, "/");
//      return new File(var10002 + "/" + this.idProperties.getKey());
//   }
//
//   private Long getLocalWorkId() throws IOException {
//      File workIdHome = this.getWorkIdHome();
//      if (!workIdHome.exists()) {
//         return null;
//      }
//
//      Collection<File> files = FileUtils.listFiles(workIdHome, new String[]{"lock"}, false);
//      if (CollectionUtils.isEmpty(files)) {
//         return null;
//      }
//
//      for (File file : files) {
//         FileChannel channel = null;
//         FileLock fileLock = null;
//
//         try {
//            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
//            channel = randomAccessFile.getChannel();
//            fileLock = channel.tryLock();
//
//            if (fileLock != null) { // 成功获取文件锁
//               Long workId = Long.valueOf(randomAccessFile.readLine());
//               releaseResourcesOnShutdown(channel);
//               return workId;
//            }
//         } catch (IOException e) {
//            log.error("Error accessing workId file", e);
//         } finally {
//            if (fileLock == null && channel != null) {
//               try {
//                  channel.close();
//               } catch (IOException e) {
//                  log.error("Failed to close file channel", e);
//               }
//            }
//         }
//      }
//      return null;
//   }
//
//   private void releaseResourcesOnShutdown(FileChannel channel) {
//      Runtime.getRuntime().addShutdownHook(new Thread(() -> {
//         try {
//            channel.close();
//         } catch (IOException e) {
//            log.error("Release WorkId file lock error", e);
//         }
//      }));
//   }
//}
