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.SqlFileInfoEntity;
import com.platform.form.ScriptForm;
import com.platform.service.IScriptMakeService;
import com.platform.utils.Configs;
import com.platform.utils.ZipCompressUtils;

/** 信息系统--脚本管理
 * @author chen
 *
 */
@Controller
@RequestMapping("/filePackage")
public class FilePackageController extends BaseController {
	
	public static Logger log = Logger.getLogger(FilePackageController.class);
	
	@Resource(name = "scriptMakeService")
	private IScriptMakeService scriptMakeService;

	/**  信息系统--脚本管理--文件包导出
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@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;
		log.info(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());// 为了解决中文名称乱码问题
			log.info(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;
		}
	}
	/**
	 * 信息系统--脚本管理--查看所有系统的 脚本及系统
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findAll")
	public ModelMap findAll() throws Exception {
		log.info("---------/findAll--- ");
		ModelMap modelMap = new ModelMap();
		List<SqlFileInfoEntity> result = scriptMakeService.findAllFiles();
		modelMap.addAttribute("data", result);
		modelMap.addAttribute("length", result.size());
		return modelMap;
	}

	/**
	 * 信息系统--脚本管理--读取 脚本内容、查看
	 * @param type
	 * @param data
	 * @param res
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/readSqlFile", method = RequestMethod.POST)
	@ResponseBody
	public Object getSqlFile(/*@PathVariable*/@RequestParam("type")  String type, @RequestBody SqlFileInfoEntity data,
			HttpServletRequest res, HttpServletResponse req) throws Exception {
		log.info("---------/readSqlFile------------------");
		ModelMap modelMap = new ModelMap();
		Map<String, Object> result = scriptMakeService.readSql(type, data);
		modelMap.addAllAttributes(result);
		return modelMap;
	}
	
	/**
	 * 信息系统--脚本管理--脚本 的 查看、审核、归档、上传
	 * @param type
	 * @param opt
	 * @param form
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@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<SqlFileInfoEntity> results = scriptMakeService.findAllFiles();
		modelMap.addAttribute("data", results);
		modelMap.addAttribute("length", results.size());
		return modelMap;
	}
	
	/**
	 * 信息系统--脚本管理--一键归档
	 * @param type
	 * @param opt
	 * @param form
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/totalOnholeSqlFile", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap totalOnholeSqlFile(@RequestBody List<SqlFileInfoEntity> forms,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		log.info("--------- /totalOnholeSqlFile --- " + forms +"  "+ forms.size());
		ModelMap modelMap = new ModelMap();
		if (forms.size() > 0) {
			Map<String, Object> result  = scriptMakeService.totalOnholeSqlFile(forms);
			if (result.containsKey("nosqlfile")) {
				modelMap.addAllAttributes(result);
			}
		}
		List<SqlFileInfoEntity> results = scriptMakeService.findAllFiles();
		modelMap.addAttribute("data", results);
		modelMap.addAttribute("length", results.size());
		return modelMap;
	}
	
	/**
	 * 信息系统--脚本管理--一键审核
	 * @param type
	 * @param opt
	 * @param form
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/totalVerifySqlFile", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap totalVerifySqlFile(@RequestBody List<SqlFileInfoEntity> forms,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		log.info("--------- /totalVerifySqlFile --- " + forms +"  "+ forms.size());
		ModelMap modelMap = new ModelMap();
		if (forms.size() > 0) {
			Map<String, Object> result  = scriptMakeService.totalVerifySqlFile(forms);
			if (result.containsKey("nosqlfile")) {
				modelMap.addAllAttributes(result);
				res.setStatus(500);
			}
		}
		List<SqlFileInfoEntity> results = scriptMakeService.findAllFiles();
		modelMap.addAttribute("data", results);
		modelMap.addAttribute("length", results.size());
		return modelMap;
	}
}
