/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workreimbursementback.web;

import java.text.ParseException;
import java.util.List;

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

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.workreimbursementback.entity.WorkReimbursementBack;
import com.jeeplus.modules.workreimbursementback.service.WorkReimbursementBackService;

/**
 * 报销还款Controller
 * @author ssrh
 * @version 2018-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/workreimbursementback/workReimbursementBack")
public class WorkReimbursementBackController extends BaseController {

	@Autowired
	private WorkReimbursementBackService workReimbursementBackService;
	
	@ModelAttribute
	public WorkReimbursementBack get(@RequestParam(required=false) String id) {
		WorkReimbursementBack entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workReimbursementBackService.get(id);
		}
		if (entity == null){
			entity = new WorkReimbursementBack();
		}
		return entity;
	}
	
	/**
	 * 报销还款列表页面
	 */
	@RequiresPermissions("workreimbursementback:workReimbursementBack:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkReimbursementBack workReimbursementBack, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WorkReimbursementBack> page = workReimbursementBackService.findPage(new Page<WorkReimbursementBack>(request, response), workReimbursementBack); 
		model.addAttribute("page", page);
		return "modules/workreimbursementback/workReimbursementBackList";
	}

	/**
	 * 查看，增加，编辑报销还款表单页面
	 */
	@RequiresPermissions(value={"workreimbursementback:workReimbursementBack:view","workreimbursementback:workReimbursementBack:add","workreimbursementback:workReimbursementBack:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkReimbursementBack workReimbursementBack, Model model) {
		model.addAttribute("workReimbursementBack", workReimbursementBack);
		return "modules/workreimbursementback/workReimbursementBackForm";
	}

	/**
	 * 保存报销还款
	 */
	@RequiresPermissions(value={"workreimbursementback:workReimbursementBack:add","workreimbursementback:workReimbursementBack:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(WorkReimbursementBack workReimbursementBack, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, workReimbursementBack)){
			return form(workReimbursementBack, model);
		}
		if(!workReimbursementBack.getIsNewRecord()){//编辑表单保存
			WorkReimbursementBack t = workReimbursementBackService.get(workReimbursementBack.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(workReimbursementBack, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			workReimbursementBackService.save(t);//保存
		}else{//新增表单保存
			workReimbursementBackService.save(workReimbursementBack);//保存
		}
		addMessage(redirectAttributes, "保存报销还款成功");
		return "redirect:"+Global.getAdminPath()+"/workreimbursementback/workReimbursementBack/?repage";
	}
	
	/**
	 * 删除报销还款
	 */
	@RequiresPermissions("workreimbursementback:workReimbursementBack:del")
	@RequestMapping(value = "delete")
	public String delete(WorkReimbursementBack workReimbursementBack, RedirectAttributes redirectAttributes) {
		workReimbursementBackService.delete(workReimbursementBack);
		addMessage(redirectAttributes, "删除报销还款成功");
		return "redirect:"+Global.getAdminPath()+"/workreimbursementback/workReimbursementBack/?repage";
	}
	
	/**
	 * 批量删除报销还款
	 */
	@RequiresPermissions("workreimbursementback:workReimbursementBack:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			workReimbursementBackService.delete(workReimbursementBackService.get(id));
		}
		addMessage(redirectAttributes, "删除报销还款成功");
		return "redirect:"+Global.getAdminPath()+"/workreimbursementback/workReimbursementBack/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workreimbursementback:workReimbursementBack:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(WorkReimbursementBack workReimbursementBack, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "报销还款"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<WorkReimbursementBack> page = workReimbursementBackService.findPage(new Page<WorkReimbursementBack>(request, response, -1), workReimbursementBack);
    		new ExportExcel("报销还款", WorkReimbursementBack.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出报销还款记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workreimbursementback/workReimbursementBack/?repage";
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("workreimbursementback:workReimbursementBack:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<WorkReimbursementBack> list = ei.getDataList(WorkReimbursementBack.class);
			for (WorkReimbursementBack workReimbursementBack : list){
				try{
					workReimbursementBackService.save(workReimbursementBack);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条报销还款记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条报销还款记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入报销还款失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workreimbursementback/workReimbursementBack/?repage";
    }
	
	/**
	 * 下载导入报销还款数据模板
	 */
	@RequiresPermissions("workreimbursementback:workReimbursementBack:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "报销还款数据导入模板.xlsx";
    		List<WorkReimbursementBack> list = Lists.newArrayList(); 
    		new ExportExcel("报销还款数据", WorkReimbursementBack.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workreimbursementback/workReimbursementBack/?repage";
    }


	@ResponseBody
	@RequestMapping(value = "getLoan")
	public List<WorkReimbursementBack> getLoan(String id) throws ParseException {
		List<WorkReimbursementBack> backList= workReimbursementBackService.findLoanList(id);
		return  backList;
	}

}