package com.lion.util;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.multipart.MultipartFile;

import com.lion.dao.CommentDao;

import jodd.util.StringUtil;

public class FileUtil {

	/**
	 * 
	 * 方法描述  上传附件
	 * @author mt.W
	 * @since 2020年12月23日
	 * @param path 路径
	 * @param folderName  文件夹名称
	 * @param file 附件数组
	 * @return List<Map<String,Object>> Map中包含filepath以及pdfpath
	 */
	public List<Map<String,Object>> uploadFile(String path,String folderName,MultipartFile[] file){
		File f = new File(path+folderName);
		//先判断文件夹是否存在，不存在进行创建文件夹
		if(!f.exists()&&!f .isDirectory()){
			f.mkdir();
		}
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < file.length; i++) {// 遍历上传附件个数
			String nowetime = TimeUtil.getNowTimeTwo();
			String nowTime = nowetime + i;
			// 获取上传的完成文件名
			String fileOldName = file[i].getOriginalFilename();
			// 获取上传文件的后缀名
			if (StringUtil.isNotEmpty(fileOldName)) {
				String suffix = fileOldName.substring(fileOldName.lastIndexOf("."));
				String fileUploadPtah = "fileUpload/";
				String newfile = nowTime + suffix;// 新附件名称
				fileUploadPtah = fileUploadPtah + folderName+"/";// 附件存贮路径
				try {
					String pdfPath = "";
					file[i].transferTo(new File(path + fileUploadPtah + newfile));
					if (suffix.equalsIgnoreCase(".doc") || suffix.equalsIgnoreCase(".docx")) {
						pdfPath = "../" + fileUploadPtah + nowTime + ".pdf";
						WordUtil.doc2pdf(path + fileUploadPtah + newfile,path + fileUploadPtah + nowTime + ".pdf");
					} else if (suffix.equalsIgnoreCase(".pdf")) {
						pdfPath = "../" + fileUploadPtah + newfile;
					} else if (suffix.equalsIgnoreCase(".xls") || suffix.equalsIgnoreCase(".xlsx")) {
						pdfPath = "../" + fileUploadPtah + nowTime + ".pdf";
						ExcelUtil.excel2pdf(path + fileUploadPtah + newfile,path + fileUploadPtah + nowTime + ".pdf");
					}
					Map<String,Object> map = new HashMap<String, Object>();
					map.put("filepath", "../"+fileUploadPtah + newfile);
					map.put("pdfPath", pdfPath);
					list.add(map);
				} catch (Exception e) {
					e.printStackTrace();
					return new ArrayList<Map<String,Object>>();
				}
			}
		}
		return list;
	}
	/**
	 * 
	 * 方法描述  查询回显附件
	 * @author mt.W
	 * @since 2020年12月23日
	 * @param tableName 附件表名称
	 * @param colName id列名
	 * @param id id
	 * @param deletepath 附件删除接口地址(../xxx/xx.service)
	 * @param comDao 
	 * @return
	 * @throws IOException
	 */
	public Map<String,Object> getFileToShow(String tableName,String colName,String id,CommentDao comDao,String deletepath) throws IOException{
		List<Map<String,Object>> file = comDao.getFileToShow(tableName, colName, id,deletepath+"?id=");
		List<String> listPicture = new ArrayList<String>();
		for(Map<String,Object> map:file){
			String url = "";
			String downUrl = "";
			if(StringUtil.isNotEmpty(map.get("pdf").toString())){
				String pdfpath = map.get("pdf").toString();
				String filetype = pdfpath.substring(pdfpath.length()-3,pdfpath.length());
				if(filetype.equals("pdf")){
					map.put("type", "pdf");
				}
				url = map.get("pdf").toString();
			}else{
				url = map.get("filepath").toString();
			}
			downUrl =  map.get("filepath").toString();
			map.put("downloadUrl", downUrl);
			listPicture.add(url);// [.../img,....]
		}
		Map<String,Object> map = Util.responseMap("true", file);
		map.put("listPicture", listPicture);
		return map;
	}
	/**
	 * 
	 * 方法描述  删除附件
	 * @author mt.W
	 * @since 2020年12月23日
	 * @param req
	 * @param session
	 * @param id 附件id
	 * @param tableName 附件表名
	 * @param url 附件路径
	 * @param comDao
	 * @return
	 */
	public String deleteFiles(HttpServletRequest req,HttpSession session,String id,String tableName,String url,CommentDao comDao){
		int count = comDao.deleteFiles(tableName, id);
		if(count>0){
			if(StringUtil.isNotEmpty(url)){
				String path = req.getSession().getServletContext().getRealPath("/");
				url = path+url.substring(2,url.length());
				File file = new File(url);
				if(file.exists()){
					file.delete();
				}
			}
			return Util.responseString("true", "操作成功");
		}		
		return Util.responseString("false", "操作失败");
	}
}
