package com.zjty.autotest.service.impl;

import com.zjty.autotest.pojo.sjq.FileBean;
import com.zjty.autotest.pojo.sjq.common.AppHttpCodeEnum;
import com.zjty.autotest.pojo.sjq.common.ResponseResult;
import com.zjty.autotest.service.UploadCodeService;
import com.zjty.autotest.util.FileHtmlUtil;
import com.zjty.autotest.util.IdWorker;
import com.zjty.autotest.util.UnZipRarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class UploadCodeServiceImpl implements UploadCodeService {
    @Value("${uploadFile}")
    private String uploadFile;
    @Autowired
    private IdWorker idWorker;
    @Value("${uploadUnFile}")
    private String uploadUnFile;
    @Override
    public ResponseResult uploadCodeFile(MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseResult.errorResult(AppHttpCodeEnum.File_Empty);
            }
            String fileName = file.getOriginalFilename();
            String fileType = fileName.substring(0,fileName.lastIndexOf("."));
            File dest = new File(uploadFile +File.separator+idWorker.nextId()+File.separator+fileType);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            String toUrl=uploadUnFile+idWorker.nextId()+File.separator+fileName;
            String s = UnZipRarUtil.zipRarToFile(fileName, dest.getPath(), toUrl);

            FileBean bean=new FileBean(s,fileName);

            return ResponseResult.okResult(bean);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.File_UPLOAD_ERROR,e.getMessage());
        }


    }


}
