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

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

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

import com.jeeplus.modules.act.entity.Act;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportRecord;
import com.jeeplus.modules.projectcontentinfo.entity.Projectcontentinfo;
import com.jeeplus.modules.projectcontentinfo.service.ProjectReportChangeService;
import com.jeeplus.modules.projectcontentinfo.service.ProjectcontentinfoService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workreview.entity.WorkReviewAudit;
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.projectcontentinfo.entity.ProjectReportData;
import com.jeeplus.modules.projectcontentinfo.service.ProjectReportDataService;

/**
 * 报告详情Controller
 * @author yangfan
 * @version 2018-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/projectcontentinfo/projectReportData")
public class ProjectReportDataController extends BaseController {

	@Autowired
	private ProjectReportDataService projectReportDataService;
	@Autowired
	private ProjectReportChangeService projectReportChangeService;
	@Autowired
	private ProjectcontentinfoService projectcontentinfoService;

	@ModelAttribute
	public ProjectReportData get(@RequestParam(required=false) String id) {
		ProjectReportData entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectReportDataService.get(id);
		}
		if (entity == null){
			entity = new ProjectReportData();
		}
		return entity;
	}
	
	/**
	 * 报告详情列表页面
	 */
	@RequiresPermissions("projectcontentinfo:projectReportData:list")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectReportData projectReportData, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectReportData> page = projectReportDataService.findInfoPage(new Page<ProjectReportData>(request, response), projectReportData);
		model.addAttribute("page", page);
		return "modules/projectcontentinfo/projectReportDataList";
	}

	/**
	 * 查看，增加，编辑报告详情表单页面
	 */
	@RequestMapping(value = "form")
	public String form(ProjectReportData reportData, Model model) {
		String id = reportData.getId();
		ProjectReportData projectReportData;
		Projectcontentinfo projectcontentinfo;
		String view = "reportAudit";
		if (StringUtils.isNotBlank(projectReportDataService.getChangeId(id))){
			id = projectReportDataService.getChangeId(id);
			projectReportData = projectReportChangeService.get(id);
			List<Projectcontentinfo> projectcontentinfos = projectcontentinfoService.getByChangeInfoId(projectReportData.getId());
			projectcontentinfo = projectcontentinfos.get(0);
			view = "reportChangeAudit";
		}else {
			projectReportData = projectReportDataService.get(id);
			projectcontentinfo = projectcontentinfoService.getByInfoId(projectReportData.getId());
		}
		projectcontentinfo.setProjectReportData(projectReportData);
		model.addAttribute("projectId", projectcontentinfo.getProject().getId());
		model.addAttribute("id", projectcontentinfo.getId());
		model.addAttribute("projectcontentinfo", projectcontentinfo);
		return "modules/projectcontentinfo/"+view;
	}

	/**
	 * 保存报告详情
	 */
	@RequiresPermissions(value={"projectcontentinfo:projectReportData:add","projectcontentinfo:projectReportData:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(ProjectReportData projectReportData, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, projectReportData)){
			return form(projectReportData, model);
		}
		if(!projectReportData.getIsNewRecord()){//编辑表单保存
			ProjectReportData t = projectReportDataService.get(projectReportData.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(projectReportData, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			projectReportDataService.save(t);//保存
		}else{//新增表单保存
			projectReportDataService.save(projectReportData);//保存
		}
		addMessage(redirectAttributes, "保存报告详情成功");
		return "redirect:"+Global.getAdminPath()+"/projectcontentinfo/projectReportData/?repage";
	}

	@RequestMapping("selectreportData")
	public String gridSelectData(ProjectReportData projectReportData, String searchLabel , HttpServletRequest request, HttpServletResponse response, Model model) throws UnsupportedEncodingException {
		searchLabel = URLDecoder.decode(searchLabel, "UTF-8");
		projectReportData.setFileStatus("1");
		Page<ProjectReportData> page = projectReportDataService.findInfoPageByStatus(new Page<ProjectReportData>(request, response), projectReportData);
		model.addAttribute("obj", projectReportData);
		model.addAttribute("page", page);
		model.addAttribute("searchLabel",searchLabel);
		return "modules/sys/gridselectreportrecord";
	}
	
	/**
	 * 删除报告详情
	 */
	@RequiresPermissions("projectcontentinfo:projectReportData:del")
	@RequestMapping(value = "delete")
	public String delete(ProjectReportData projectReportData, RedirectAttributes redirectAttributes) {
		projectReportDataService.delete(projectReportData);
		addMessage(redirectAttributes, "删除报告详情成功");
		return "redirect:"+Global.getAdminPath()+"/projectcontentinfo/projectReportData/?repage";
	}
	
	/**
	 * 批量删除报告详情
	 */
	@RequiresPermissions("projectcontentinfo:projectReportData:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			projectReportDataService.delete(projectReportDataService.get(id));
		}
		addMessage(redirectAttributes, "删除报告详情成功");
		return "redirect:"+Global.getAdminPath()+"/projectcontentinfo/projectReportData/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("projectcontentinfo:projectReportData:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(ProjectReportData projectReportData, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "报告明细"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<ProjectReportData> page = projectReportDataService.findInfoPage(new Page<ProjectReportData>(request, response, -1), projectReportData);
    		new ExportExcel("报告明细", ProjectReportData.class).setDataList(page.getList()).write(response, fileName,request.getHeader("USER-AGENT")).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出报告详情记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/projectcontentinfo/projectReportData/?repage";
    }

	/**
	 * 导入Excel数据

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

}