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

import java.util.List;

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

import com.jeeplus.modules.sys.utils.UserUtils;
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.workexampleimplog.entity.WorkExampleImplog;
import com.jeeplus.modules.workexampleimplog.service.WorkExampleImplogService;

/**
 * 案例导入日志Controller
 * @author ssrh
 * @version 2018-09-03
 */
@Controller
@RequestMapping(value = "${adminPath}/workexampleimplog/workExampleImplog")
public class WorkExampleImplogController extends BaseController {

	@Autowired
	private WorkExampleImplogService workExampleImplogService;
	
	@ModelAttribute
	public WorkExampleImplog get(@RequestParam(required=false) String id) {
		WorkExampleImplog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workExampleImplogService.get(id);
		}
		if (entity == null){
			entity = new WorkExampleImplog();
		}
		return entity;
	}
	
	/**
	 * 案例导入日志列表页面
	 */
	/*@RequiresPermissions("workexampleimplog:workExampleImplog:list")*/
	@RequestMapping(value = {"list", ""})
	public String list(WorkExampleImplog workExampleImplog, HttpServletRequest request, HttpServletResponse response, Model model) {
		workExampleImplog.setCompanyId(UserUtils.getSelectCompany().getId());
		Page<WorkExampleImplog> page = workExampleImplogService.findPage(new Page<WorkExampleImplog>(request, response), workExampleImplog); 
		model.addAttribute("page", page);
		return "modules/workexampleimplog/workExampleImplogList";
	}

	/**
	 * 查看，增加，编辑案例导入日志表单页面
	 */
	@RequiresPermissions(value={"workexampleimplog:workExampleImplog:view","workexampleimplog:workExampleImplog:add","workexampleimplog:workExampleImplog:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkExampleImplog workExampleImplog, Model model) {
		model.addAttribute("workExampleImplog", workExampleImplog);
		return "modules/workexampleimplog/workExampleImplogForm";
	}

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

	/**
	 * 导入Excel数据

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

}