/**
 * 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.WorkFullDesign;
import com.jeeplus.modules.workfullmanage.service.WorkFullDesignService;
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/workFullDesign")
public class WorkFullDesignController extends BaseController {

	@Autowired
	private WorkFullDesignService workFullDesignService;

	@Autowired
	private HttpServletRequest request;

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

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

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

	/**
	 * 导入Excel数据

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

}