package com.zcloud.controller.system;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.system.VersionManagerService;
import com.zcloud.service.system.VersionManagerZdgcService;
import com.zcloud.util.*;
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
 * 时间：2021-02-07
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/versionmanager")
public class AppVersionManagerController extends BaseController {

	@Autowired
	private VersionManagerService versionmanagerService;
	@Autowired
	private VersionManagerZdgcService versionmanagerServiceZdgc;
	@Autowired
	private Smb smb;
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	// @RequiresPermissions("versionmanager:add")
	@ResponseBody
	public Object add(@RequestParam(value="FFILE",required=false) MultipartFile file,
			@RequestParam(value="VERSIONMANAGER_ID",required=false) String VERSIONMANAGER_ID,
			@RequestParam(value="VERSION",required=false) String VERSION,
			@RequestParam(value="FILETYPE",required=false) String FILETYPE,
			@RequestParam(value="ISEFFECTIVE",required=false) String ISEFFECTIVE,
			@RequestParam(value="ISUPDATE",required=false) String ISUPDATE) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		String  ffile = DateUtil.getDays(), fileName = "";
		if (null != file && !file.isEmpty()) {
			//String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + ffile;	//文件上传路径
			//fileName = FileUpload.fileUp(file, filePath, this.get32UUID());				//执行上传
			fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			smb.sshSftp(file, fileName, Const.FILEPATHFILE + ffile);
			pd.put("FILEURL", Const.FILEPATHFILE + ffile + "/" + fileName);			//文件路径
			pd.put("VERSION", VERSION);
			pd.put("FILETYPE", FILETYPE);
			pd.put("ISEFFECTIVE", ISEFFECTIVE);
			pd.put("ISUPDATE", ISUPDATE);
			pd.put("OPUSER", Jurisdiction.getUsername());
			pd.put("OPTIME", DateUtil.date2Str(new Date()));
			pd.put("VERSIONMANAGER_ID", this.get32UUID());	//主键
			versionmanagerService.save(pd);						//存入数据库表
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	// @RequiresPermissions("versionmanager:del")
	@ResponseBody
	public Object delete() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		versionmanagerService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("versionmanager:edit")
	@ResponseBody
	public Object edit(@RequestParam(value="FFILE",required=false) MultipartFile file,
			@RequestParam(value="VERSIONMANAGER_ID",required=false) String VERSIONMANAGER_ID,
			@RequestParam(value="VERSION",required=false) String VERSION,
			@RequestParam(value="FILETYPE",required=false) String FILETYPE,
			@RequestParam(value="ISEFFECTIVE",required=false) String ISEFFECTIVE,
			@RequestParam(value="ISUPDATE",required=false) String ISUPDATE) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		String  ffile = DateUtil.getDays(), fileName = "";
		if (null != file && !file.isEmpty()) {
//			String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + ffile;	//文件上传路径
//			fileName = FileUpload.fileUp(file, filePath, this.get32UUID());				//执行上传
			fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			smb.sshSftp(file, fileName, Const.FILEPATHFILE + ffile);
			pd.put("FILEURL", Const.FILEPATHFILE + ffile + "/" + fileName);			//文件路径
			pd.put("VERSION", VERSION);
			pd.put("FILETYPE", FILETYPE);
			pd.put("ISEFFECTIVE", ISEFFECTIVE);
			pd.put("ISUPDATE", ISUPDATE);
			pd.put("OPUSER", Jurisdiction.getUsername());
			pd.put("OPTIME", DateUtil.date2Str(new Date()));
			pd.put("VERSIONMANAGER_ID",VERSIONMANAGER_ID);	//主键
			versionmanagerService.edit(pd);						//存入数据库表
		}else{
			pd.put("VERSION", VERSION);
			pd.put("FILETYPE", FILETYPE);
			pd.put("ISEFFECTIVE", ISEFFECTIVE);
			pd.put("ISUPDATE", ISUPDATE);
			pd.put("OPUSER", Jurisdiction.getUsername());
			pd.put("OPTIME", DateUtil.date2Str(new Date()));
			pd.put("VERSIONMANAGER_ID",VERSIONMANAGER_ID);	//主键
			versionmanagerService.edit(pd);						//存入数据库表
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("versionmanager:list")
	@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 = versionmanagerService.list(page);	//列出VersionManager列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}
	@RequestMapping(value="/getVersion")
	@ResponseBody
	public Object getVersion() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData>	varList = versionmanagerService.listAll(pd);	//列出VersionManager列表
		map.put("pd", varList.get(0));
		map.put("result", errInfo);
		return map;
	}
	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/getAllAppVersion")
	@ResponseBody
	public ReturnMap getAllAppVersion() throws Exception{
		List<PageData> varList = versionmanagerService.getAllAppVersion();	//列出VersionManager列表
		return ReturnMap.ok().put("data",varList);
	}
	@RequestMapping(value="/getVersionZdgc")
	@ResponseBody
	public Object getVersionZdgc() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData>	varList = versionmanagerServiceZdgc.listAll(pd);	//列出VersionManager列表
		map.put("pd", varList.get(0));
		map.put("result", errInfo);
		return map;
	}
	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	// @RequiresPermissions("versionmanager: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 = versionmanagerService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("versionmanager: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(",");
			versionmanagerService.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("版本");	//1
		titles.add("文件地址");	//2
		titles.add("文件类型");	//3
		titles.add("操作人");	//4
		titles.add("操作日期");	//5
		titles.add("是否有效");	//6
		titles.add("是否更新 1，强制更新，2不强制更新");	//7
		dataMap.put("titles", titles);
		List<PageData> varOList = versionmanagerService.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("VERSION"));	    //1
			vpd.put("var2", varOList.get(i).getString("FILEURL"));	    //2
			vpd.put("var3", varOList.get(i).getString("FILETYPE"));	    //3
			vpd.put("var4", varOList.get(i).getString("OPUSER"));	    //4
			vpd.put("var5", varOList.get(i).getString("OPTIME"));	    //5
			vpd.put("var6", varOList.get(i).getString("ISEFFECTIVE"));	    //6
			vpd.put("var7", varOList.get(i).getString("ISUPDATE"));	    //7
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
