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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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.workfullmanage.entity.WorkFullSurvey;
import com.jeeplus.modules.workfullmanage.service.WorkFullSurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
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.HashMap;
import java.util.List;

/**
 * 施工监理设计单位联系人Controller
 * @author mapengbo
 * @version 2017-10-25
 */
@Controller
@RequestMapping(value = "${adminPath}/workfullmanage/workFullSurvey")
public class WorkFullSurveyController extends BaseController {

	@Autowired
	private WorkFullSurveyService workFullSurveyService;

	@Autowired
	private HttpServletRequest request;

	@ModelAttribute
	public WorkFullSurvey get(@RequestParam(required=false) String id) {
		WorkFullSurvey entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workFullSurveyService.get(id);
		}
		if (entity == null){
			entity = new WorkFullSurvey();
		}
		return entity;
	}
	
	/**
	 * 施工监理设计单位联系人列表页面
	 */
//	@RequiresPermissions("workfullmanage:workFullSurvey:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkFullSurvey workFullSurvey, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WorkFullSurvey> page = workFullSurveyService.findPage(new Page<WorkFullSurvey>(request, response), workFullSurvey);
		String index = request.getParameter("index");
		model.addAttribute("indexf",index);
		model.addAttribute("page", page);
		return "modules/workfullmanage/workFullSurveyList";
	}

	/**
	 * 查看，增加，编辑施工监理设计单位联系人表单页面
	 */
//	@RequiresPermissions(value={"workfullmanage:workFullSurvey:view","workfullmanage:workFullSurvey:add","workfullmanage:workFullSurvey:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkFullSurvey workFullSurvey, Model model) {
		String tabId = request.getParameter("tabId");
		String view = "workFullSurveyForm";
		if("1".equals(tabId)){
			view = "workFullSurveyFormDetail";
		}
		model.addAttribute("workFull", workFullSurvey);
		return "modules/workfullmanage/"+view;
	}

	/**
	 * 保存施工监理设计单位联系人
	 */
//	@RequiresPermissions(value={"workfullmanage:workFullSurvey:add","workfullmanage:workFullSurvey:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	@ResponseBody
	public HashMap<Object, Object> save(WorkFullSurvey workFullSurvey, Model model, RedirectAttributes redirectAttributes) throws Exception{
		HashMap<Object, Object> map = Maps.newHashMap();

		if(!workFullSurvey.getIsNewRecord()){//编辑表单保存
			WorkFullSurvey t = workFullSurveyService.get(workFullSurvey.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(workFullSurvey, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			workFullSurveyService.save(t);//保存
			map.put("str","修改成功");
		}else{//新增表单保存
			WorkFullSurvey unit_name = workFullSurveyService.findUniqueByProperty("unit_name", workFullSurvey.getUnitName());
			if(unit_name != null){
				map.put("msg","snounique");
				return map;
			}
			workFullSurveyService.save(workFullSurvey);//保存
			map.put("str","新增成功");
		}
		map.put("id",workFullSurvey.getId());
		map.put("unitName",workFullSurvey.getUnitName());
		map.put("unitLinkMan",workFullSurvey.getUnitLinkMan());
		map.put("linkPhone",workFullSurvey.getLinkPhone());
		map.put("msg","sunique");
		return  map;
	}
	
	/**
	 * 删除施工监理设计单位联系人
	 */
//	@RequiresPermissions("workfullmanage:workFullSurvey:del")
	@RequestMapping(value = "delete")
	@ResponseBody
	public HashMap<Object, Object> delete(WorkFullSurvey workFullSurvey, RedirectAttributes redirectAttributes) {
		HashMap<Object, Object> map = Maps.newHashMap();
		workFullSurveyService.delete(workFullSurvey);
		map.put("str","success");
		return map;
//		addMessage(redirectAttributes, "删除施工监理设计单位联系人成功");
//		return "redirect:"+Global.getAdminPath()+"/workfullmanage/workFullSurvey/?repage";
	}
	
	/**
	 * 批量删除施工监理设计单位联系人
	 */
//	@RequiresPermissions("workfullmanage:workFullSurvey:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			workFullSurveyService.delete(workFullSurveyService.get(id));
		}
		addMessage(redirectAttributes, "删除施工监理设计单位联系人成功");
		return "redirect:"+Global.getAdminPath()+"/workfullmanage/workFullSurvey/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
//	@RequiresPermissions("workfullmanage:workFullSurvey:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(WorkFullSurvey workFullSurvey, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "施工监理设计单位联系人"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<WorkFullSurvey> page = workFullSurveyService.findPage(new Page<WorkFullSurvey>(request, response, -1), workFullSurvey);
    		new ExportExcel("施工监理设计单位联系人", WorkFullSurvey.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出施工监理设计单位联系人记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workfullmanage/workFullSurvey/?repage";
    }

	/**
	 * 导入Excel数据

	 */
//	@RequiresPermissions("workfullmanage:workFullSurvey: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<WorkFullSurvey> list = ei.getDataList(WorkFullSurvey.class);
			for (WorkFullSurvey workFullSurvey : list){
				try{
					workFullSurveyService.save(workFullSurvey);
					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()+"/workfullmanage/workFullSurvey/?repage";
    }
	
	/**
	 * 下载导入施工监理设计单位联系人数据模板
	 */
//	@RequiresPermissions("workfullmanage:workFullSurvey:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "施工监理设计单位联系人数据导入模板.xlsx";
    		List<WorkFullSurvey> list = Lists.newArrayList(); 
    		new ExportExcel("施工监理设计单位联系人数据", WorkFullSurvey.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workfullmanage/workFullSurvey/?repage";
    }
	
	
	

}