/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.projectapproval.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.projectapproval.entity.ProjectApproval;
import com.jeeplus.modules.projectapproval.service.ProjectApprovalService;
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.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 项目报批Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/projectapproval/projectApproval")
public class ProjectApprovalController extends BaseController {

	@Autowired
	private ProjectApprovalService projectApprovalService;
	
	@ModelAttribute
	public ProjectApproval get(@RequestParam(required=false) String id) {
		ProjectApproval entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectApprovalService.get(id);
		}
		if (entity == null){
			entity = new ProjectApproval();
		}
		return entity;
	}
	
	/**
	 * 列表页面
	 */
	@RequiresPermissions("projectapproval:projectApproval:list")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectApproval projectApproval, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectApproval> page = projectApprovalService.findPage(new Page<ProjectApproval>(request, response), projectApproval); 
		model.addAttribute("page", page);
		return "modules/projectapproval/projectApprovalList";
	}

	/**
	 * 查看，增加，编辑依据性资料表单页面
	 */
	@RequiresPermissions(value={"projectapproval:projectApproval:view","projectapproval:projectApproval:add","projectapproval:projectApproval:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(ProjectApproval projectApproval,HttpServletRequest request, Model model) {
		String tab = request.getParameter("tab");
		model.addAttribute("projectApproval", projectApproval);
		if(StringUtils.isNotBlank(tab)){
			return "modules/projectapproval/projectApprovalView";
		}
		return "modules/projectapproval/projectApprovalForm";
	}

	/**
	 * 保存
	 */
	@RequiresPermissions(value={"projectapproval:projectApproval:add","projectapproval:projectApproval:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(ProjectApproval projectApproval,HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, projectApproval)){
			return form(projectApproval,request, model);
		}
		if(!projectApproval.getIsNewRecord()){//编辑表单保存
			ProjectApproval t = projectApprovalService.get(projectApproval.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(projectApproval, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			projectApprovalService.save(t);//保存
		}else{//新增表单保存
			projectApprovalService.save(projectApproval);//保存
		}
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/projectapproval/projectApproval/?repage";
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("projectapproval:projectApproval:del")
	@RequestMapping(value = "delete")
	public String delete(ProjectApproval projectApproval, RedirectAttributes redirectAttributes) {
		projectApprovalService.delete(projectApproval);
		addMessage(redirectAttributes, "删除依据性资料成功");
		return "redirect:"+Global.getAdminPath()+"/projectapproval/projectApproval/?repage";
	}
	
	/**
	 * 批量删除
	 */
	@RequiresPermissions("projectapproval:projectApproval:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			projectApprovalService.delete(projectApprovalService.get(id));
		}
		addMessage(redirectAttributes, "删除依据性资料成功");
		return "redirect:"+Global.getAdminPath()+"/projectapproval/projectApproval/?repage";
	}
	


//    @RequestMapping("selectList")
//    public String gridSelect(ProjectApproval projectApproval,Model model,String searchLabel,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
//        Page<ProjectApproval> page = projectApprovalService.findPage(new Page<ProjectApproval>(request,response), projectApproval);
//		String searchLabel1 = URLDecoder.decode(searchLabel,"utf-8");
//        model.addAttribute("obj", projectApproval);
//        model.addAttribute("page", page);
//        model.addAttribute("searchLabel",searchLabel1);
//        return "modules/sys/gridselectProjbasedata";
//    }

}