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.ReadExcel;
import com.zhiwei.util.StringUtil;
import com.zw.entity.ExcelsUrl;

@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";
	}

	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();
		}
	}

	@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";
	}

	@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";
	}
	
	@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";
	}
	
	@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";
	}

	@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";
	}

	@RequestMapping(value = "/getAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAll() {
		return JSON.toJSONString(service.findAll());
	}

}
