package com.zcloud.controller.education;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.education.PersonObtainEvidenceService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.PostService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.*;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 说明：人员取证情况
 * 作者：luoxiaobao
 * 时间：2020-12-29
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/personobtainevidence")
public class PersonObtainEvidenceController extends BaseController {

	@Autowired
	private PersonObtainEvidenceService personobtainevidenceService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private PostService postService;
	@Autowired
	private Smb smb;
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	// @RequiresPermissions("personobtainevidence:add")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "新增")
	public Object add(@RequestParam(value="file",required=false) MultipartFile file) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("PERSONOBTAINEVIDENCE_ID", this.get32UUID());	//主键
		pd.put("CREATOR", Jurisdiction.getUsername());	//添加人
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		pd.put("ISDELETE", "0");	//是否删除 1-是  0-否
		if (null != file && !file.isEmpty()) {
			String  ffile = DateUtil.getDays();
//			String filePath = PathUtil.getProjectpath() + Const.FILEPATHIMG + ffile;	//文件上传路径
//			String fileName = FileUpload.fileUp(file, filePath, this.get32UUID());				//执行上传
    		String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		 	smb.sshSftp(file, fileName, Const.FILEPATHIMG + Jurisdiction.getCORPINFO_ID() + "/" + ffile);
			pd.put("IMGPATH", Const.FILEPATHIMG + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
		}
		personobtainevidenceService.save(pd);
		map.put("result", errInfo);
		return map;
	}

	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	// @RequiresPermissions("personobtainevidence:del")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "删除")
	public Object delete() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		personobtainevidenceService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("personobtainevidence:edit")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "修改")
	public Object edit(@RequestParam(value="file",required=false) MultipartFile file) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		if (null != file && !file.isEmpty()) {
			String  ffile = DateUtil.getDays();
//			String filePath = PathUtil.getProjectpath() + Const.FILEPATHIMG + ffile;	//文件上传路径
//			String fileName = FileUpload.fileUp(file, filePath, this.get32UUID());				//执行上传
    		String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		 	smb.sshSftp(file, fileName, Const.FILEPATHIMG + Jurisdiction.getCORPINFO_ID() + "/" + ffile);
			pd.put("IMGPATH", Const.FILEPATHIMG + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
		}
		personobtainevidenceService.edit(pd);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("personobtainevidence:list")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "列表")
	public Object list(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		page.setPd(pd);
		List<PageData>	varList = personobtainevidenceService.list(page);	//列出PersonObtainEvidence列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	// @RequiresPermissions("personobtainevidence:edit")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "去修改页面获取数据")
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = personobtainevidenceService.findById(pd);	//根据ID读取
//		//获取部门下拉树 ;
//		List<PageData> zdepartmentPdList = new ArrayList<PageData>();
//		String ZDEPARTMENT_ID = Jurisdiction.getDEPARTMENT_ID();
//		ZDEPARTMENT_ID = "".equals(ZDEPARTMENT_ID)?"0":ZDEPARTMENT_ID;
//		PageData dept = new PageData();
//		dept.put("DEPARTMENT_ID",ZDEPARTMENT_ID);
//		dept=departmentService.findById(dept);


		//获取部门下拉树
		List<PageData> zdepartmentPdList = new ArrayList<PageData>();
		PageData dept = new PageData();
		dept.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		dept=this.departmentService.findByCorpId(dept);
		String ZDEPARTMENT_ID = dept.getString("DEPARTMENT_ID");

		PageData node = new PageData();
		node.put("id", dept.get("DEPARTMENT_ID"));
		node.put("parentId", dept.get("PARENT_ID"));
		node.put("name", dept.get("NAME"));
		node.put("icon", "../../../assets/images/user.gif");
		zdepartmentPdList.add(node);
		JSONArray arr = JSONArray.fromObject(departmentService.listAllDepartmentToSelect(ZDEPARTMENT_ID,zdepartmentPdList));
		map.put("zTreeNodes", (null == arr ?"":"{\"treeNodes\":" + arr.toString() + "}"));

		PageData seletctDept = new PageData();
		seletctDept.put("DEPARTMENT_ID",pd.getString("DEPARTMENT_ID"));
		seletctDept = departmentService.findById(seletctDept);
		String depname = null == seletctDept?"请选择":seletctDept.getString("NAME");
		map.put("depname", null == depname?"请选择":depname);
		//获取岗位下拉
		PageData post = new PageData();
		post.put("DEPARTMENT_ID", pd.getString("DEPARTMENT_ID"));
		post.put("STATUS", 0);
		List<PageData> postList = postService.listAll(post);
		map.put("postList", postList);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}


	 /**获取部门下拉树
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getDept")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "获取部门下拉树")
	public Object getDept() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();

		//获取部门下拉树
		List<PageData> zdepartmentPdList = new ArrayList<PageData>();
		PageData dept = new PageData();
		dept.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		dept=this.departmentService.findByCorpId(dept);
		String ZDEPARTMENT_ID = dept.getString("DEPARTMENT_ID");
//		String ZDEPARTMENT_ID = Jurisdiction.getDEPARTMENT_ID();
//		ZDEPARTMENT_ID = "".equals(ZDEPARTMENT_ID)?"0":ZDEPARTMENT_ID;
//		PageData dept = new PageData();
//		dept.put("DEPARTMENT_ID",ZDEPARTMENT_ID);
//		dept=this.departmentService.findById(dept);

		PageData node = new PageData();
		node.put("id", dept.get("DEPARTMENT_ID"));
		node.put("parentId", dept.get("PARENT_ID"));
		node.put("name", dept.get("NAME"));
		node.put("icon", "../../../assets/images/user.gif");
		zdepartmentPdList.add(node);
		JSONArray arr = JSONArray.fromObject(departmentService.listAllDepartmentToSelect(ZDEPARTMENT_ID,zdepartmentPdList));
		map.put("zTreeNodes", (null == arr ?"":"{\"treeNodes\":" + arr.toString() + "}"));

		PageData loginDept = new PageData();
		loginDept.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		loginDept = departmentService.findById(loginDept);
//		//获取岗位下拉
//		PageData post = new PageData();
//		post.put("DEPARTMENT_ID", ZDEPARTMENT_ID);
//		post.put("STATUS", 0);
//		List<PageData> postList = this.postService.listAll(post);
//		map.put("postList", postList);
		map.put("result", errInfo);

		map.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		map.put("USER_ID", Jurisdiction.getUSER_ID());
		map.put("DEPT_NAME", loginDept.get("NAME"));
		return map;
	}


	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("personobtainevidence:del")
	@ResponseBody
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "批量删除")
	public Object deleteAll() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String DATA_IDS = pd.getString("DATA_IDS");
		if(Tools.notEmpty(DATA_IDS)){
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			personobtainevidenceService.deleteAll(ArrayDATA_IDS);
			errInfo = "success";
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	 /**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/excel")
	// @RequiresPermissions("toExcel")
	@LogAnno(menuType= "综合管理",menuServer= "教育培训",instructionsOperate = "人员取证情况",instructionsType = "导出到excel")
	public ModelAndView exportExcel() throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		Map<String,Object> dataMap = new HashMap<String,Object>();
		List<String> titles = new ArrayList<String>();
		titles.add("部门DI");	//1
		titles.add("职务ID");	//2
		titles.add("类别");	//3
		titles.add("人员姓名");	//4
		titles.add("证书名称");	//5
		titles.add("证书编号");	//6
		titles.add("证书到期日期");	//7
		titles.add("提醒日期");	//8
		titles.add("提醒人员");	//9
		titles.add("证书图片路径");	//10
		titles.add("备注");	//11
		titles.add("企业ID");	//12
		titles.add("添加人");	//13
		titles.add("添加时间");	//14
		titles.add("修改人");	//15
		titles.add("修改时间");	//16
		titles.add("是否删除 1-是  0-否");	//17
		dataMap.put("titles", titles);
		List<PageData> varOList = personobtainevidenceService.listAll(pd);
		List<PageData> varList = new ArrayList<PageData>();
		for(int i=0;i<varOList.size();i++){
			PageData vpd = new PageData();
			vpd.put("var1", varOList.get(i).getString("DEPARTMENT_ID"));	    //1
			vpd.put("var2", varOList.get(i).getString("POST_ID"));	    //2
			vpd.put("var3", varOList.get(i).getString("TYPE"));	    //3
			vpd.put("var4", varOList.get(i).getString("PERSONNAME"));	    //4
			vpd.put("var5", varOList.get(i).getString("CERTIFICATENAME"));	    //5
			vpd.put("var6", varOList.get(i).getString("CERTIFICATENUMBER"));	    //6
			vpd.put("var7", varOList.get(i).getString("EXPIRATIONDATE"));	    //7
			vpd.put("var8", varOList.get(i).getString("REMINDERDATE"));	    //8
			vpd.put("var9", varOList.get(i).getString("REMINDPEOPLE"));	    //9
			vpd.put("var10", varOList.get(i).getString("IMGPATH"));	    //10
			vpd.put("var11", varOList.get(i).getString("DESCR"));	    //11
			vpd.put("var12", varOList.get(i).getString("CORPINFO_ID"));	    //12
			vpd.put("var13", varOList.get(i).getString("CREATOR"));	    //13
			vpd.put("var14", varOList.get(i).getString("CREATTIME"));	    //14
			vpd.put("var15", varOList.get(i).getString("OPERATOR"));	    //15
			vpd.put("var16", varOList.get(i).getString("OPERATTIME"));	    //16
			vpd.put("var17", varOList.get(i).getString("ISDELETE"));	    //17
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
