package com.dcit.danger.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.dcit.common.enums.ResultEnum;
import com.dcit.common.utils.DateUtil;
import com.dcit.common.utils.KeyUtil;
import com.dcit.common.utils.ResultVOUtil;
import com.dcit.common.vo.ResultVO;
import com.dcit.danger.config.ProjectPath;
import com.dcit.danger.exception.IOCException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value="文件上传")
@RestController
@RequestMapping("file")
@Slf4j
public class FileController {
	
	@Autowired
	private ProjectPath projectPath;
	
	@ApiOperation(value = "上传文件")
	@PostMapping("/uploadFile")
	public ResultVO<Object> upload(@RequestParam("file") MultipartFile upFile) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		String originalName = upFile.getOriginalFilename();
		
		String fileName = DateUtil.formatYYYYMMDDHHMMSS(new Date())+ File.separator +KeyUtil.genUniqueKey() + "." + getFileSuffix(upFile.getOriginalFilename());
		
		try {
			String fileSavePath = getUploadPath(projectPath.getFileRootPath());
			File file =new File(fileSavePath + fileName);
			//判断目录存不存在,不存在得加上
	        if (!file.exists()) {
	            file.mkdirs();
			}
			upFile.transferTo(file);
						
			//图片尺寸不变，压缩图片文件大小outputQuality实现,输出图片的压缩比,这里压缩比为25%，参数1为最高质量
	        //Thumbnails.of(fileSavePath + fileName).scale(1f).outputQuality(0.25f).toFile(fileSavePath + fileName);
			map.put("originalName", originalName);
			map.put("savePath", fileName);
			map.put("viewPath", projectPath.getFileUrlPath() + fileName);
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【上传文件】操作出错，{}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		return ResultVOUtil.success(map);
	}
	
	@ApiOperation(value = "删除上传文件")
	@GetMapping("/deleteFile")
	public ResultVO<Object> deleteFile(@RequestParam String savePath){
		try {
			String fileSavePath = getUploadPath(projectPath.getFileRootPath());
			File file =new File(fileSavePath + savePath);
			//判断目录存不存在,不存在得加上
	        if (file.exists()) {
	            file.delete();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【删除上传文件】操作出错，{}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		return ResultVOUtil.success();
		
	}

	private static String getFileSuffix(String fileWholeName) {
        if (fileWholeName.equals("")||fileWholeName==null) {
            return "none";
        }
        int lastIndexOf = fileWholeName.lastIndexOf(".");
        return fileWholeName.substring(lastIndexOf + 1);
    }
	
	private static String getUploadPath(String pathStr) {
        File file = new File(pathStr);
        //判断目录存不存在,不存在得加上
        if (!file.exists()) {
            file.mkdirs();
        //判断有没有结尾符,没有得加上
        if (!pathStr.endsWith(File.separator)) {
        	pathStr = pathStr + File.separator;
        }
       }
		return pathStr;
	}
}
