package com.platform.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.BaseController;
import com.platform.entities.MyFilesEntity;
import com.platform.form.ScriptForm;
import com.platform.service.IScriptMakeService;
import com.platform.utils.Configs;
import com.platform.utils.ZipCompressUtils;

@Controller
@RequestMapping("/filePackage")
public class FilePackageController extends BaseController {
	
	@SuppressWarnings("static-access")
	public static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(FilePackageController.class);
	
	@Resource(name = "scriptMakeService")
	private IScriptMakeService scriptMakeService;

	@RequestMapping("/download")
	public ResponseEntity<byte[]> downloadFile(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.info("---------/download--- ");
		scriptMakeService.makeCfg();
		scriptMakeService.makeXml();
		String filePath = Configs.PACKAGE_DOWNLOAD_PATH;
		System.out.println(filePath + Configs.PACKAGE_NAME);
		ZipCompressUtils.zip(new File(filePath + Configs.PACKAGE_NAME),
				filePath + "package.zip"); // 产生压缩文件
		File file = new File(filePath + "package.zip");
		if (file.exists()) {
			HttpHeaders headers = new HttpHeaders();
			String fileName = new String(file.getName());// 为了解决中文名称乱码问题
			System.out.println(fileName);
			headers.setContentDispositionFormData("attachment", fileName);
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(
					FileUtils.readFileToByteArray(file), headers,
					HttpStatus.CREATED);
			file.delete(); //
			return responseEntity;
		} else {
			response.setStatus(500);
			return null;
		}
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	public ModelMap findAll() throws Exception {
		log.info("---------/findAll--- ");
		ModelMap modelMap = new ModelMap();
		List<MyFilesEntity> result = scriptMakeService.FindAllFiles();
		modelMap.addAttribute("data", result);
		modelMap.addAttribute("length", result.size());
		return modelMap;
	}

	@RequestMapping(value = "/readSqlFile", method = RequestMethod.POST)
	@ResponseBody
	public Object getSqlFile(/*@PathVariable*/@RequestParam("type")  String type, @RequestBody MyFilesEntity data,
			HttpServletRequest res, HttpServletResponse req) throws Exception {
		log.info("---------/readSqlFile------------------");
		ModelMap modelMap = new ModelMap();
		Map<String, Object> result = scriptMakeService.readSql(type, data);
		// StringBuilder sb = new StringBuilder();
		// sb.append("查看相应日志").append("\n").append("看到了");
//		modelMap.addAttribute("data1", result.get("data1").toString());
//		modelMap.addAttribute("data2", result.get("data2").toString());
		modelMap.addAllAttributes(result);
		return modelMap;
	}
	
	@RequestMapping(value = "/handleSqlFile", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap handleSqlFile(@RequestParam("type") String type, @RequestParam("opt") String opt, @RequestBody ScriptForm form,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		log.info("---------/handleSqlFile--- " + type +"  "+ opt + "  ");
		ModelMap modelMap = new ModelMap();
		Map<String, Object> result  = scriptMakeService.handleSqlFile(type, opt, form.getContent(), form.getItem());
		modelMap.addAllAttributes(result);
		List<MyFilesEntity> results = scriptMakeService.FindAllFiles();
		modelMap.addAttribute("data", results);
		modelMap.addAttribute("length", results.size());
		return modelMap;
	}
}
