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

@Controller
public class FinanceDifferenceController {

	@Autowired
	private FinanceDifferenceService differenceService;

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

	/**
	 * 根据条件查询数据
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getDifference")
	public PageInfo<FinanceDifference> getFinance(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, FinanceDifference difference) {
		PageHelper.startPage(page, limit);
		List<FinanceDifference> differenceList = differenceService.get(difference);
		PageInfo<FinanceDifference> pageInfo = new PageInfo<FinanceDifference>(differenceList);
		return pageInfo;
	}

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

	@ResponseBody
	@RequestMapping("/getFilename")
	public List<String> getAllFilename() {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		List<String> FilenameList = differenceService.getAllFilename(user.getRealname());
		return FilenameList;
	}

	/**
	 * 导出
	 * 
	 * @param response
	 * @param request
	 * @param difference
	 * @throws Exception
	 */
	@RequestMapping("/excel_difference")
	public void excel(HttpServletResponse response, FinanceDifference difference) throws Exception {
		if (StringUtils.isEmpty(difference.getFilename())) {
			return;
		}
		differenceService.excel(response, difference);
	}
}
