package com.zhiwei.servlet;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.zhiwei.service.Services;
import com.zhiwei.util.Config;
import com.zhiwei.util.ExcelUtil;
import com.zhiwei.util.StringUtil;
import com.zw.entity.ExcelsUrl;

/**
 * 文件更新控制器
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/finance")
public class FianceController {

	@Resource
	private Services service;

	private static final Log log = LogFactory.getLog(FianceController.class);
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping(value = "/upExcel")
	public String upFinance() {
		return "showfinance";
	}

	/**
	 * 金融需要的文件存储
	 * 
	 * @param file
	 * @return
	 */
	public void fileSave(MultipartFile file, String path) {
		File targetFile = new File(path);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// 保存
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 更新金融需要的金融关键词文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upFinanceKey")
	public String upFinanceKey(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
			String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
			log.info(path);
			String beforeUrl = service.getPath("key");
			if (!StringUtil.isEmpty(beforeUrl)) {
				ExcelUtil.delete(beforeUrl);
			}
			fileSave(file, path);
			ExcelsUrl eUrl = new ExcelsUrl();
			eUrl.setType("关键词");
			eUrl.setName("key");
			eUrl.setUrl(path);
			service.insert(eUrl);
		} else {
			return "excelError";
		}
		return "showfinance";
	}

	/**
	 * 更新金融需要的长来源文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upFinanceLong")
	public String upFinanceLong(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
			String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
			log.info(path);
			String beforeUrl = service.getPath("longSource");
			if (!StringUtil.isEmpty(beforeUrl)) {
				ExcelUtil.delete(beforeUrl);
			}
			fileSave(file, path);
			ExcelsUrl eUrl = new ExcelsUrl();
			eUrl.setType("来源多的文件");
			eUrl.setName("longSource");
			eUrl.setUrl(path);
			service.insert(eUrl);
		} else {
			return "excelError";
		}
		return "showfinance";
	}

	/**
	 * 更新金融需要的微信文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upFinanceWechat")
	public String upFinanceWechat(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
			String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
			log.info(path);
			String beforeUrl = service.getPath("wechat");
			if (!StringUtil.isEmpty(beforeUrl)) {
				ExcelUtil.delete(beforeUrl);
			}
			fileSave(file, path);
			ExcelsUrl eUrl = new ExcelsUrl();
			eUrl.setType("微信来源");
			eUrl.setName("wechat");
			eUrl.setUrl(path);
			service.insert(eUrl);
		} else {
			return "excelError";
		}
		return "showfinance";
	}

	/**
	 * 更新金融需要的微博文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upFinanceWeibo")
	public String upFinanceWeibo(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
			String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
			log.info(path);
			String beforeUrl = service.getPath("weibo");
			if (!StringUtil.isEmpty(beforeUrl)) {
				ExcelUtil.delete(beforeUrl);
			}
			fileSave(file, path);
			ExcelsUrl eUrl = new ExcelsUrl();
			eUrl.setType("微博来源");
			eUrl.setName("weibo");
			eUrl.setUrl(path);
			service.insert(eUrl);
		} else {
			return "excelError";
		}
		return "showfinance";
	}

	/**
	 * 更新金融需要的短来源文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upFinanceLess")
	public String upFinanceLess(MultipartFile file) {
		String fileName = file.getOriginalFilename();
		if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
			String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
			log.info(path);
			String beforeUrl = service.getPath("lessSource");
			if (!StringUtil.isEmpty(beforeUrl)) {
				ExcelUtil.delete(beforeUrl);
			}
			fileSave(file, path);
			ExcelsUrl eUrl = new ExcelsUrl();
			eUrl.setType("来源较少的文件");
			eUrl.setName("lessSource");
			eUrl.setUrl(path);
			service.insert(eUrl);
		} else {
			return "excelError";
		}
		return "redirect:upExcel";
	}

	/**
	 * 获取所有文件
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/getAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAll() {
		return JSON.toJSONString(service.findAll());
	}

}
