package lingtao.net.controller;

import java.util.List;

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

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.FinanceExtract;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.service.FinanceExtractService;

@Controller
public class FinanceExtractController {

	@Autowired
	private FinanceExtractService financeExtractService;

	// 用户跳转页面
	@RequestMapping("/finance/extract/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.sendRedirect(request.getContextPath() + "/views/system/finance/extract/extract.jsp");
	}

	/**
	 * 根据条件查询数据
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getFinance_extract")
	public PageInfo<FinanceExtract> getFinanceExtract(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, FinanceExtract financeExtract) {
		PageHelper.startPage(page, limit);
		List<FinanceExtract> financeList = financeExtractService.getFinanceExtract(financeExtract);
		PageInfo<FinanceExtract> pageInfo = new PageInfo<FinanceExtract>(financeList);
		return pageInfo;
	}

	/**
	 * 获取自己上传过的文件名(用于导出文件)
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getFilename_extract")
	public List<String> getFilename_extract() {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		List<String> FilenameList = financeExtractService.getFilename_extract(user.getRealname());
		return FilenameList;
	}

	/**
	 * 根据文件名删除自己导入过的文件
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteDataByFilename2")
	public Msg deleteDataByFilename(@RequestParam(value = "filename") String filename) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		return financeExtractService.deleteDataByFilename(filename, user.getRealname());
	}

	/**
	 * 
	 * 文件上传
	 */
	@ResponseBody
	@RequestMapping(value = "/ajaxUpload_2")
	public Msg uploadExcel(@RequestParam("file") MultipartFile file) throws Exception {
		synchronized (this) {
			return financeExtractService.ajaxUploadExcel(file);
		}
	}

	/**
	 * 导出
	 * 
	 * @param response
	 * @param request
	 * @param finance
	 * @throws Exception
	 */
	@RequestMapping("/excel_extract")
	public void excel(HttpServletResponse response, FinanceExtract financeExtract) throws Exception {
		if (StringUtils.isEmpty(financeExtract.getFilename())) {
			return;
		}
		financeExtractService.excel(response, financeExtract);
	}
}
