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.Finance;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.service.Finance2Service;

@Controller
public class Finance2Controller {

	@Autowired
	private Finance2Service finance2Service;

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

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

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

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

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

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