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

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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
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.project.entity.Archivedirectory;
import com.jeeplus.modules.project.service.ArchivedirectoryService;

/**
 * 归档目录Controller
 * @author 姚儒彬
 * @version 2017-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/archivedirectory")
public class ArchivedirectoryController extends BaseController {

	@Autowired
	private ArchivedirectoryService archivedirectoryService;
	
	@ModelAttribute
	public Archivedirectory get(@RequestParam(required=false) String id) {
		Archivedirectory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = archivedirectoryService.get(id);
		}
		if (entity == null){
			entity = new Archivedirectory();
		}
		return entity;
	}
	
	/**
	 * 归档目录列表页面
	 */
	@RequiresPermissions("project:archivedirectory:list")
	@RequestMapping(value = {"list", ""})
	public String list(Archivedirectory archivedirectory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Archivedirectory> page = archivedirectoryService.findPage(new Page<Archivedirectory>(request, response), archivedirectory); 
		model.addAttribute("page", page);
		return "modules/project/archivedirectoryList";
	}

	/**
	 * 查看，增加，编辑归档目录表单页面
	 */
	@RequiresPermissions(value={"project:archivedirectory:view","project:archivedirectory:add","project:archivedirectory:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(Archivedirectory archivedirectory, Model model,String bidInformationId) {
		archivedirectory.setBidInformationId(bidInformationId);
		model.addAttribute("archivedirectory", archivedirectory);
		return "modules/project/archivedirectoryForm";
	}

	/**
	 * 查看归档目录表单页面
	 */
	@RequiresPermissions(value={"project:archivedirectory:view"},logical=Logical.OR)
	@RequestMapping(value = "forms")
	public String forms(Archivedirectory archivedirectory, Model model,String bidInformationId) {
		archivedirectory.setBidInformationId(bidInformationId);
		model.addAttribute("archivedirectory", archivedirectory);
		return "modules/project/archivedirectoryForms";
	}

	/**
	 * 保存归档目录
	 */
	@RequiresPermissions(value={"project:archivedirectory:add","project:archivedirectory:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Archivedirectory archivedirectory, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, archivedirectory)){
			return form(archivedirectory, model,"");
		}
		archivedirectoryService.save(archivedirectory);//保存
		addMessage(redirectAttributes, "保存归档目录成功");
		return "redirect:"+Global.getAdminPath()+"/project/archivedirectory/?repage";
	}
	
	/**
	 * 删除归档目录
	 */
	@RequiresPermissions("project:archivedirectory:del")
	@RequestMapping(value = "delete")
	public String delete(Archivedirectory archivedirectory, RedirectAttributes redirectAttributes) {
		archivedirectoryService.delete(archivedirectory);
		addMessage(redirectAttributes, "删除归档目录成功");
		return "redirect:"+Global.getAdminPath()+"/project/archivedirectory/?repage";
	}
	
	/**
	 * 批量删除归档目录
	 */
	@RequiresPermissions("project:archivedirectory:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			archivedirectoryService.delete(archivedirectoryService.get(id));
		}
		addMessage(redirectAttributes, "删除归档目录成功");
		return "redirect:"+Global.getAdminPath()+"/project/archivedirectory/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("project:archivedirectory:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(Archivedirectory archivedirectory, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "归档目录"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<Archivedirectory> page = archivedirectoryService.findPage(new Page<Archivedirectory>(request, response, -1), archivedirectory);
    		new ExportExcel("归档目录", Archivedirectory.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出归档目录记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/project/archivedirectory/?repage";
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("project:archivedirectory: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<Archivedirectory> list = ei.getDataList(Archivedirectory.class);
			for (Archivedirectory archivedirectory : list){
				try{
					archivedirectoryService.save(archivedirectory);
					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()+"/project/archivedirectory/?repage";
    }
	
	/**
	 * 下载导入归档目录数据模板
	 */
	@RequiresPermissions("project:archivedirectory:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "归档目录数据导入模板.xlsx";
    		List<Archivedirectory> list = Lists.newArrayList(); 
    		new ExportExcel("归档目录数据", Archivedirectory.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/project/archivedirectory/?repage";
    }
	
	
	

}