/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workevaluationroom.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.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workevaluationroom.entity.WorkEvaluationRoom;
import com.jeeplus.modules.workevaluationroom.service.WorkEvaluationRoomService;
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 2017-12-28
 */
@Controller
@RequestMapping(value = "${adminPath}/workevaluationroom/workEvaluationRoom")
public class WorkEvaluationRoomController extends BaseController {

	@Autowired
	private WorkEvaluationRoomService workEvaluationRoomService;
	
	@ModelAttribute
	public WorkEvaluationRoom get(@RequestParam(required=false) String id) {
		WorkEvaluationRoom entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workEvaluationRoomService.get(id);
		}
		if (entity == null){
			entity = new WorkEvaluationRoom();
		}
		return entity;
	}
	
	/**
	 * 评标室管理列表页面
	 */
	@RequiresPermissions("workevaluationroom:workEvaluationRoom:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkEvaluationRoom workEvaluationRoom, HttpServletRequest request, HttpServletResponse response, Model model) {
		Role role = UserUtils.getSelectRole().get(0);
		Office office = UserUtils.getSelectOffice();
		Page<WorkEvaluationRoom> page = new Page<>();
		if(!office.getName().contains("招标")){
			page.setPageNo(1);
			page.setPageSize(10);
			model.addAttribute("page", page);
			model.addAttribute("addBtn","addBtn");
			return "modules/workevaluationroom/workEvaluationRoomList";
		}
		page = workEvaluationRoomService.findPage(new Page<WorkEvaluationRoom>(request, response), workEvaluationRoom);
		model.addAttribute("page", page);
		return "modules/workevaluationroom/workEvaluationRoomList";
	}

	/**
	 * 查看，增加，编辑评标室管理表单页面
	 */
	@RequiresPermissions(value={"workevaluationroom:workEvaluationRoom:view","workevaluationroom:workEvaluationRoom:add","workevaluationroom:workEvaluationRoom:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkEvaluationRoom workEvaluationRoom, Model model) {
		model.addAttribute("workEvaluationRoom", workEvaluationRoom);
		if(StringUtils.isNotBlank(workEvaluationRoom.getName()) && workEvaluationRoom.getName().equals("view")){
			return "modules/workevaluationroom/workEvaluationRoomView";
		}
		return "modules/workevaluationroom/workEvaluationRoomForm";
	}

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

	/**
	 * 导入Excel数据

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

}