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

import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workprocesstype.entity.WorkProcessType;
import com.jeeplus.modules.workprocesstype.service.WorkProcessTypeService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * 工程控制类型Controller
 * @author 马鹏波
 * @version 2018-06-07
 */
@Controller
@RequestMapping(value = "${adminPath}/workprocesstype/workProcessType")
public class WorkProcessTypeController extends BaseController {

	@Autowired
	private WorkProcessTypeService workProcessTypeService;
	
	@ModelAttribute
	public WorkProcessType get(@RequestParam(required=false) String id) {
		WorkProcessType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workProcessTypeService.get(id);
		}
		if (entity == null){
			entity = new WorkProcessType();
		}
		return entity;
	}
	
	/**
	 * 过程控制类型列表页面
	 */
	@RequiresPermissions("workprocesstype:workProcessType:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkProcessType workProcessType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WorkProcessType> page = workProcessTypeService.findPage(new Page<WorkProcessType>(request, response), workProcessType); 
		model.addAttribute("page", page);
		return "modules/workprocesstype/workProcessTypeList";
	}

	/**
	 * 查看，增加，编辑过程控制类型表单页面
	 */
	@RequiresPermissions(value={"workprocesstype:workProcessType:view","workprocesstype:workProcessType:add","workprocesstype:workProcessType:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkProcessType workProcessType, Model model) {
		model.addAttribute("workProcessType", workProcessType);
		return "modules/workprocesstype/workProcessTypeForm";
	}

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

	/**
	 * 导入Excel数据

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

	/**
	 * 保存过程控制类型
	 */
	@RequiresPermissions("workprocesstype:workProcessType:saveState")
	@RequestMapping(value = "saveState")
	public String saveState(WorkProcessType workProcessType, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, workProcessType)){
			return form(workProcessType, model);
		}
		workProcessType.setCompanyId(UserUtils.getSelectCompany().getId());
		if(!workProcessType.getIsNewRecord()){//编辑表单保存
			WorkProcessType t = workProcessTypeService.get(workProcessType.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(workProcessType, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			workProcessTypeService.save(t);//保存
		}else{//新增表单保存
			workProcessTypeService.save(workProcessType);//保存
		}
		addMessage(redirectAttributes, "保存过程控制类型成功");
		return "redirect:"+Global.getAdminPath()+"/workprocesstype/workProcessDetail/?repage";
	}
	

}