package com.example.demo.service.impl;

import com.example.demo.model.StrAndEmail;
import com.example.demo.service.UseService;
import com.example.demo.utils.DoPerlUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UseServiceImpl implements UseService {


    @Autowired
    RedissonClient redissonClient;


    @Async("userAccessThreadPool")
    @Override
    public CompletableFuture<String> postTxt(StrAndEmail strAndEmail) {


        int last = strAndEmail.getStr().lastIndexOf("/");
        String filename = strAndEmail.getStr().substring(last + 1);

        String folderPath = strAndEmail.getStr().substring(0, last);

        log.info("截取提交文件路径" + folderPath);

        log.info("截取生成的文件名为: " + filename);
        int i = filename.indexOf(".");
        String uuid = filename.substring(0, i);

        StringBuffer resultPath = new StringBuffer();
        resultPath.append("/home/ubuntu/Bio-server/workplace_");
        resultPath.append(filename);
        resultPath.append("/out_result.zip");

        log.info("生成文件位置：" + resultPath.toString());
//        filePath:输入信息的文件地址。
//        把文件地址作为perl脚本的输入参数

//        结果为输出文件的绝对路径


        RBucket<String> rBucket = redissonClient.getBucket("file_uuid" + uuid);


        rBucket.set(resultPath.toString());
        rBucket.expire(7, TimeUnit.DAYS);


        DoPerlUtil.doPerFile(strAndEmail);

        DoPerlUtil.deleteUploadFileByPath(strAndEmail.getStr());


//        .fa的文件Java会判断为是文件夹
//        File folder = new File(folderPath);
//        if (folder.exists()) {
//            FileUtils.deleteSpecificFiles(folder, filename);
//            System.out.println("指定文件已成功删除！");
//        } else {
//            System.out.println("文件夹不存在！");
//        }


        return CompletableFuture.completedFuture(null);
    }
}
