/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.managementcenter.activiti.web;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.UploadImages;
import com.jeeplus.modules.sg.managementcenter.activiti.service.ConstructionService;
import com.jeeplus.modules.sg.managementcenter.activiti.service.DiscloseService;
import com.jeeplus.modules.sg.managementcenter.activiti.service.UploadImagesService;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project1;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 施工交底Controller
 * @author
 * @version 2019-11-08
 */
@Controller
@RequestMapping(value = "${adminPath}/managementcenter/upload")

public class UploadController extends BaseController {

	@Autowired
	private ConstructionService constructionService;
	@Autowired
	private ActProcessService actProcessService;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private DiscloseService discloseService;
	@Autowired
	private UploadImagesService uploadImagesService;

	@ModelAttribute
	public UploadImages get(@RequestParam(required=false) String id) {
		UploadImages entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = uploadImagesService.get(id);
		}
		if (entity == null){
			entity = new UploadImages();
		}
		return entity;
	}

	@RequestMapping(value = "list")
	public String formList(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/upload";
	}

	@RequestMapping(value = "form")
	public String formUp(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/uploadList";
	}

/*	@RequestMapping(value = "designUpload")
	public String designUpload(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/designUpload";
	}*/

	/**
	 * 图片管理列表页面
	 */
	@RequestMapping(value = {"images"})
	public String list(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/uploadList";
	}

/*	*//**
	 * 配网运行人员查看图片
	 *//*
	@RequestMapping(value = {"operatorUploadList"})
	public String operatorUploadList(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/operatorUploadList";
	}*/

	/**
	 * 经研所查看上传文件
	 */
	@RequestMapping(value = {"economicsDesignChange"})
	public String economicsDesignChange(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/economicsDesignChange";
	}

/*	*
	 * 配网项目经理查看图片
	@RequestMapping(value = {"managerUploadList"})
	public String managerUploadList(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/managerUploadList";
	}*/

	/**
	 * 施工单位判断变更后材料
	 */
	@RequestMapping(value = {"conAfterChange"})
	public String conAfterChange(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/conAfterChange";
	}



	/**
	 * 上传未签字版政策处理清单
	 */
	@RequestMapping(value = {"notSignUpload"})
	public String notSignUpload(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/notSignUpload";
	}

	/**
	 * 上传签字版政策处理清单
	 */
	@RequestMapping(value = {"signUploadList"})
	public String signUploadList(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/signUploadList";
	}


	/**
	 * 上传签字版合同
	 */
	@RequestMapping(value = {"ContractUploadList"})
	public String ContractUploadList(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/contractUploadList";
	}

	/**
	 * 上传借款单据
	 */
	@RequestMapping(value = {"loanUploadList"})
	public String loanUploadList(UploadImages uploadImages, Model model) {
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/loanUploadList";
	}

/*	*//**
	 * 交底:运行人员审核图纸
	 * *//*
	@RequestMapping(value = "auditDrawings")
	public String auditDrawings(UploadImages uploadImages, Model model) {
		//根据流程定义号获取所有的定义流程
		model.addAttribute("uploadImages", uploadImages);
		return "modules/sg/managementcenter/activiti/auditDrawings";
	}*/


	/**
	 * 查看，增加，编辑图片管理表单页面
	 */
	@RequestMapping(value = "imagesForm")
	public String form(UploadImages uploadImages, Model model) {
//		UploadImages uploadImages1 = uploadImagesService.get(uploadImages.getId());
//		model.addAttribute("uploadImages", uploadImages1);
		return "modules/sg/managementcenter/activiti/uploadListForm";
	}

	/**
	 * 文件管理列表数据
	 */
	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(UploadImages uploadImages, HttpServletRequest request, HttpServletResponse response, Model model,String procInsId) {
		if (procInsId!=null&&procInsId!="") {
			uploadImages.setProcInsId(procInsId);
		}
		//查询出所有的附件信息
		Page<UploadImages> page = uploadImagesService.findPage(new Page<UploadImages>(request, response), uploadImages);
		//当判定系统为linux的时候
		if (Global.SYS_TYPE.equals("2")) {
			List<UploadImages> list = page.getList();
			for (UploadImages upload:
				 list) {
				String a = "";
				String path = upload.getPath();
				String[] split = path.split("\\|");
				for (int i = 0; i < split.length; i++) {
					a += "http://" + request.getServerName() + split[i] + "|";
				}
				upload.setPath(a);
			}
		}
		List<UploadImages> list1 = page.getList();
		for (UploadImages images : list1) {
			String imgName = images.getImgName();
			boolean anInt = this.isInt(imgName);
			if (anInt) {
				String dictLabels = DictUtils.getDictLabel(imgName, "uploadFileType", "");
				images.setImgName(dictLabels);
			}
		}
		return getBootstrapData(page);
	}

	/**
	 * 文件管理列表数据
	 */
	@ResponseBody
	@RequestMapping(value = "materialUploadData")
	public Map<String, Object> materialUploadData(UploadImages uploadImages, HttpServletRequest request, HttpServletResponse response, Model model,String procInsId) {
		if (procInsId!=null&&procInsId!="") {
//			uploadImages.setProcInsId(procInsId);
		}
		Page<UploadImages> page = uploadImagesService.findMaterialImg(new Page<UploadImages>(request, response), uploadImages);
		//当判定系统为linux的时候
		if (Global.SYS_TYPE.equals("2")) {
			List<UploadImages> list = page.getList();
			for (UploadImages upload:
					list) {
				upload.setPath("http://" + request.getServerName()  +upload.getPath());
			}
		}
		List<UploadImages> list1 = page.getList();
		for (UploadImages images : list1) {
			String imgName = images.getImgName();
			boolean anInt = this.isInt(imgName);
			if (anInt) {
				String dictLabels = DictUtils.getDictLabel(imgName, "uploadFileType", "");
				images.setImgName(dictLabels);
			}
		}
		return getBootstrapData(page);
	}

	/**
	 * 保存图片管理
	 */
	@ResponseBody
	@RequestMapping(value = "save")
	public AjaxJson save(UploadImages uploadImages, Model model) throws Exception{
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(uploadImages);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		//新增或编辑表单保存
		uploadImagesService.save(uploadImages);//保存
		j.setSuccess(true);
		j.setMsg("保存图片管理成功");
		return j;
	}


	/**
	 * 删除图片管理
	 */
	@ResponseBody
	@RequestMapping(value = "delete")
	public AjaxJson delete(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			uploadImagesService.delete(uploadImagesService.get(id));
		}
		j.setMsg("删除图片管理成功");
		return j;
	}



	@ResponseBody
	@RequestMapping(value = "uploadSave")
	public AjaxJson uploadSave(UploadImages uploadImages, Model model,String pic) throws Exception{
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(uploadImages);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}

		/**
		 * 流程审批
		 */
		/*if (StringUtils.isBlank(uploadImages.getId())){
			uploadImages.setPath(pic);
			uploadImages.setProcInsId(uploadImages.getAct().getTaskId());
			uploadImages.setuId(UserUtils.getUser().getLoginName());
			uploadImagesService.save(uploadImages);
			//新增或编辑表单保存
			// 启动流程
			ProcessDefinition p = actProcessService.getProcessDefinition(uploadImages.getAct().getProcDefId());
			String title = uploadImages.getCurrentUser().getName()+"在"+ DateUtils.getDateTime()+"发起"+p.getName();
			actTaskService.startProcess(p.getKey(),  "xm_upolad_images", uploadImages.getId(), title);
			j.setMsg("提交审批成功!");
			j.getBody().put("targetUrl",  "/act/task/process/");
		}*/

		return j;
	}

	public boolean isInt(String s)
	{
		try
		{
			int i = Integer.parseInt(s);
			return true;
		}
		catch(Exception _ex)
		{
			return false;
		}
	}


}