package com.zcloud.controller.onlinexxks;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.onlinexxks.TeacherCertificateService;
import com.zcloud.util.*;
import org.apache.commons.lang.StringUtils;
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.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
 * 时间：2022-03-04
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/teachercertificate")
public class TeacherCertificateController extends BaseController {

	@Autowired
	private TeacherCertificateService teachercertificateService;

	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	@RequiresPermissions(value = {"teachercertificate:add", "teacher:add"}, logical = Logical.OR)
	@ResponseBody
	public Object add(
			@RequestParam(value="certificateFile") MultipartFile certificateFile) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("TEACHERCERTIFICATE_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("ISDELETE", "0");	//是否删除(0:有效 1：删除)
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		this.handleTeacherCertificateFile(pd, certificateFile);
		teachercertificateService.save(pd);
		map.put("result", errInfo);
		return map;
	}

	/**删除
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@RequiresPermissions(value = {"teachercertificate:del", "teacher:del"}, logical = Logical.OR)
	@ResponseBody
	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()));	//修改时间
//		PageData old = teachercertificateService.findById(pd);
//		if (StringUtils.isNotBlank(old.getString("CERTIFICATE_FILEPATH"))) {
//			Smb.deleteFile(old.getString("CERTIFICATE_FILEPATH"));
//		}
		teachercertificateService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	@RequiresPermissions(value = {"teachercertificate:edit", "teacher:edit"}, logical = Logical.OR)
	@ResponseBody
	public Object edit(
			@RequestParam(value="certificateFile",required=false) MultipartFile certificateFile) 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()));	//修改时间
		PageData old = teachercertificateService.findById(pd);
		this.deleteServerFile(old, certificateFile);
		this.handleTeacherCertificateFile(pd, certificateFile);
		teachercertificateService.edit(pd);
		map.put("result", errInfo);
		return map;
	}

	/**
	 *	新增修改时删除原有附件文件
	 * @param old	数据对象
	 * @param certificateFile		资格证照片
	 */
	private void deleteServerFile(PageData old, MultipartFile certificateFile) throws Exception {
		if (StringUtils.isNotBlank(old.getString("CERTIFICATE_FILEPATH")) && null != certificateFile && !certificateFile.isEmpty()) {
			Smb.delete(old.getString("CERTIFICATE_FILEPATH"));
		}
	}

	/**
	 *
	 * @param pd	数据对象
	 * @param certificateFile		资格证照片
	 */
	private void handleTeacherCertificateFile(PageData pd, MultipartFile certificateFile) throws Exception {
		if (null != certificateFile && !certificateFile.isEmpty()) {	 // 资格证照片
			String  ffile = DateUtil.getDays();
			//服务器
			String fileName = this.get32UUID()+certificateFile.getOriginalFilename().substring(certificateFile.getOriginalFilename().lastIndexOf("."));
			Smb.sshSftp(certificateFile, fileName, Const.FILEPATHFILE + Jurisdiction.getCORPINFO_ID() + "/" + ffile);
			pd.put("CERTIFICATE_FILEPATH", Const.FILEPATHFILE + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
		}
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@RequiresPermissions(value = {"teachercertificate:list", "teacher:list"}, logical = Logical.OR)
	@ResponseBody
	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());
		page.setPd(pd);
		List<PageData>	varList = teachercertificateService.list(page);	//列出TeacherCertificate列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	@RequiresPermissions(value = {"teachercertificate:edit", "teacher:edit"}, logical = Logical.OR)
	@ResponseBody
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = teachercertificateService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	@RequiresPermissions(value = {"teachercertificate:del", "teacher:del"}, logical = Logical.OR)
	@ResponseBody
	public Object deleteAll() 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()));	//修改时间
		String DATA_IDS = pd.getString("DATA_IDS");
		if(Tools.notEmpty(DATA_IDS)){
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			pd.put("ArrayDATA_IDS", ArrayDATA_IDS);	//待删除ids
//			List<PageData> pdList = teachercertificateService.findByIds(pd);
//			for (PageData d : pdList) {
//				if (StringUtils.isNotBlank(d.getString("CERTIFICATE_FILEPATH"))) {
//					Smb.deleteFile(d.getString("CERTIFICATE_FILEPATH"));
//				}
//			}
			teachercertificateService.deleteAll(pd);
			errInfo = "success";
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	 /**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/excel")
	@RequiresPermissions("toExcel")
	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("教师ID");	//1
		titles.add("资质类型");	//2
		titles.add("职业证书编号");	//3
		titles.add("是否永久有效");	//4
		titles.add("证书有效期（开始）");	//5
		titles.add("证书有效期（结束）");	//6
		titles.add("证书照片");	//7
		titles.add("是否删除（0.正常 1.删除）");	//8
		titles.add("添加人");	//9
		titles.add("添加时间");	//10
		titles.add("修改人");	//11
		titles.add("修改时间");	//12
		titles.add("企业ID");	//13
		dataMap.put("titles", titles);
		List<PageData> varOList = teachercertificateService.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("TEACHER_ID"));	    //1
			vpd.put("var2", varOList.get(i).getString("QUALIFICATION_TYPE"));	    //2
			vpd.put("var3", varOList.get(i).getString("VOCATIONAL_CERTIFICATE_NUMBER"));	    //3
			vpd.put("var4", varOList.get(i).getString("IS_PERMANENT_VALID"));	    //4
			vpd.put("var5", varOList.get(i).getString("CERTIFICATE_DATE_BEGIN"));	    //5
			vpd.put("var6", varOList.get(i).getString("CERTIFICATE_DATE_END"));	    //6
			vpd.put("var7", varOList.get(i).getString("CERTIFICATE_FILEPATH"));	    //7
			vpd.put("var8", varOList.get(i).getString("ISDELETE"));	    //8
			vpd.put("var9", varOList.get(i).getString("CREATOR"));	    //9
			vpd.put("var10", varOList.get(i).getString("CREATTIME"));	    //10
			vpd.put("var11", varOList.get(i).getString("OPERATOR"));	    //11
			vpd.put("var12", varOList.get(i).getString("OPERATTIME"));	    //12
			vpd.put("var13", varOList.get(i).getString("CORPINFO_ID"));	    //13
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
