package com.zcloud.controller.archives;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.archives.ArchivesFilesService;
import com.zcloud.util.*;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：档案文件
 * 时间：2021-12-14
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/archivesfiles")
public class ArchivesFilesController extends BaseController {

	@Autowired
	private ArchivesFilesService archivesfilesService;
	@Autowired
	private Smb smb;
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "档案管理",instructionsOperate = "一期一档",instructionsType = "新增")
	public Object add(
			@RequestParam(value="FFILE",required=false) MultipartFile[] files,
			@RequestParam(value="TYPE",required=false) String TYPE,
			@RequestParam(value="YEAR",required=false) String YEAR,
			@RequestParam(value="STUDYTASK_ID",required=false) String STUDYTASK_ID
	) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd.put("ARCHIVESFILES_ID", this.get32UUID());	//主键
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业
		pd.put("TYPE", TYPE);
		pd.put("YEAR", YEAR);
		pd.put("STUDYTASK_ID", STUDYTASK_ID);
		for(MultipartFile file : files){
			String filename = file.getOriginalFilename();
			pd.put("FILE_NAME", filename);
//			File tempFile = new File(file.getOriginalFilename());
//			FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);

			String  ffile = DateUtil.getDays();
			String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			smb.sshSftp(file, fileName, Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile);

			pd.put("FILE_PATH",  Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName);
		}

		archivesfilesService.save(pd);
		map.put("result", errInfo);
		return map;
	}
	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@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();
		smb.deleteFile(pd.getString("FILE_PATH"));
		archivesfilesService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("archivesfiles:edit")
	@ResponseBody
	public Object edit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		archivesfilesService.edit(pd);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "档案管理",instructionsOperate = "一期一档",instructionsType = "列表")
	public Object list() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业
		List<PageData> varList = archivesfilesService.listAll(pd);	//列出ArchivesFiles列表
		map.put("varList", varList);
		map.put("result", errInfo);
		return map;
	}

	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	// @RequiresPermissions("archivesfiles:edit")
	@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 = archivesfilesService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("archivesfiles:del")
	@ResponseBody
	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(",");
			archivesfilesService.deleteAll(ArrayDATA_IDS);
			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
		dataMap.put("titles", titles);
		List<PageData> varOList = archivesfilesService.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("CORPINFO_ID"));	    //1
			vpd.put("var2", varOList.get(i).getString("YEAR"));	    //2
			vpd.put("var3", varOList.get(i).getString("FILE_PATH"));	    //3
			vpd.put("var4", varOList.get(i).getString("FILE_NAME"));	    //4
			vpd.put("var5", varOList.get(i).get("TYPE").toString());	//5
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
