package com.uas.report.controller;

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

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.multipart.MultipartFile;

import com.uas.report.service.FileService;
import com.uas.report.util.ArrayUtils;
import com.uas.report.util.StringUtils;

/**
 * 文件操作请求
 * 
 * @author sunyj
 * @since 2016年11月2日 下午2:05:04
 */
@Controller
@RequestMapping("/file")
public class FileController {

	@Autowired
	private FileService fileService;

	@RequestMapping("/autoDeploy")
	@ResponseBody
	public String autoDeploy(String sourceUserName, String destinationUserNames) throws IOException {
		return fileService.autoDeploy(sourceUserName, destinationUserNames);
	}

	@RequestMapping("/upload")
	@ResponseBody
	public String upload(String userName, String fileType, String filePath, Boolean isAbsolutePath,
			@RequestParam("file") MultipartFile[] files) throws IOException {
		userName = userName == null ? null : userName.toUpperCase();
		// 未指定上传文件路径，则按照账套名称上传
		// 该情况下，不会上传多个文件的
		if (StringUtils.isEmpty(filePath)) {
			return fileService.upload(userName, fileType, ArrayUtils.isEmpty(files) ? null : files[0]);
		} else {
			return fileService.upload(filePath, isAbsolutePath, files);
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @param isAbsolutePath
	 *            文件路径是否为绝对路径
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/download")
	public void download(String filePath, Boolean isAbsolutePath, HttpServletResponse response) throws IOException {
		fileService.download(filePath, isAbsolutePath, response);
	}

	/**
	 * 下载账套下的所有资源
	 * 
	 * @param userName
	 *            账套名
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/download/zip")
	public void downloadZip(String userName, HttpServletResponse response) throws IOException {
		userName = userName == null ? null : userName.toUpperCase();
		fileService.downloadZip(userName, response);
	}

	/**
	 * 下载模板
	 * 
	 * @param userName
	 *            账套名称
	 * @param reportName
	 *            模板名称
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/download/jrxml")
	public void downloadJrxml(String userName, String reportName, HttpServletResponse response) throws IOException {
		userName = userName == null ? null : userName.toUpperCase();
		fileService.download(fileService.getJrxmlFilePath(userName, reportName), true, response);
	}

	/**
	 * 下载标准模板
	 * 
	 * @param onlyData
	 *            是否只获取数据
	 * @throws IOException
	 */
	@RequestMapping("/standardJrxmls")
	@ResponseBody
	public Map<String, Object> standardJrxmls(String userName, String onlyData, HttpServletResponse response)
			throws IOException {
		userName = userName == null ? null : userName.toUpperCase();
		// onlyData为真，只返回字节数据（自动部署时只获取标准模板字节数据），否则下载标准模板zip
		if (!StringUtils.isEmpty(onlyData) && (onlyData.equals("1") || onlyData.equals("true"))) {
			Map<String, Object> result = new HashMap<>();
			// 不直接返回byte[]，是因为客户端请求数据时，该请求可能会抛出异常，
			// 之后客户端获取byte[]数据，进行处理时难以判断数据是文件数据还是异常信息
			result.put("data", fileService.getStandardJrxmls(userName));
			return result;
		} else {
			fileService.downloadStandardJrxmls(userName, response);
			return null;
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String filePath, Boolean isAbsolutePath) throws IOException {
		return "Deleted... " + fileService.delete(filePath, isAbsolutePath);
	}

	@RequestMapping("/listFiles")
	@ResponseBody
	public List<Map<String, Object>> listFiles(String filePath, Boolean isAbsolutePath) throws IOException {
		return fileService.listFiles(filePath, isAbsolutePath);
	}
}
