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

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialStandard;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialVersion;
import com.jeeplus.modules.sg.managementcenter.materialinfo.service.MaterialVersionService;
import com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/managementcenter/materialversion")
public class MaterialVersionController extends BaseController {

	@Autowired
	private MaterialVersionService materialVersionService;
	
	
	@ModelAttribute
	public MaterialVersion get(@RequestParam(required=false) String id) {
		MaterialVersion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = materialVersionService.get(id);
		}
		if (entity == null){
			entity = new MaterialVersion();
		}
		return entity;
	}
	
	/**
	 * 项目列表页面
	 */
	@RequiresPermissions("managementcenter:materialversion:list")
	@RequestMapping(value = {"list", ""})
	public String list(MaterialVersion materialVersion, Model model) {
		model.addAttribute("materialVersion", materialVersion);
		return "modules/sg/managementcenter/materialversion/materialVersionList";
	}
	
		/**
	 * 列表数据
	 */
	@ResponseBody
	@RequiresPermissions("managementcenter:materialversion:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(MaterialVersion materialVersion, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MaterialVersion> page = materialVersionService.findPage(new Page<MaterialVersion>(request, response), materialVersion);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑表单页面
	 */
	@RequiresPermissions(value={"managementcenter:materialversion:view","managementcenter:materialversion:add","managementcenter:materialversion:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(MaterialVersion materialVersion, Model model) {
		model.addAttribute("materialVersion", materialVersion);
		return "modules/sg/managementcenter/materialversion/materialVersionForm";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequiresPermissions(value={"managementcenter:materialversion:add","managementcenter:materialversion:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(MaterialVersion materialVersion, Model model) throws Exception{
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(materialVersion);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		//新增或编辑表单保存
		materialVersionService.save(materialVersion);//保存
		j.setSuccess(true);
		j.setMsg("保存项目成功");
		return j;
	}

	/**
	 * 导入页面跳转
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "importExcel")
	public String importExcel(Model model) {
		return "modules/sg/managementcenter/materialversion/importexcel";
	}

	/**
	 *导入
	 */
	@ResponseBody
	@RequiresPermissions("managementcenter:materialversion:import")
	@RequestMapping(value = "import")
	public AjaxJson importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request){
		String batch = request.getParameter("batch");
		AjaxJson j = new AjaxJson();
		try {
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<MaterialStandard> list = ei.getDataList(MaterialStandard.class);
			for(MaterialStandard materialStandard : list){
				materialStandard.setBatch(batch);
			}
			materialVersionService.saveList(list,batch);
			j.setMsg( "导入成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入失败！失败信息："+e.getMessage());
		}
		return j;
	}

	/**
	 * 导入验证，如果批次号存在，则返回，不存在，则直接读入
	 */
	@ResponseBody
	@RequestMapping(value = "importValidation")
	public AjaxJson importValidation(@RequestParam("file")MultipartFile file,String batch) {
		AjaxJson j = new AjaxJson();
		try {
			MaterialVersion materialVersion = new MaterialVersion();
			materialVersion.setBatch(batch);
			List<MaterialVersion> list = materialVersionService.findList(materialVersion);
			if(list != null && list.size()>0){
				j.setSuccess(true);
			}else {
				ImportExcel ei = new ImportExcel(file, 1, 0);
				List<MaterialStandard> list1 = ei.getDataList(MaterialStandard.class);
				for(MaterialStandard materialStandard1 : list1){
					materialStandard1.setBatch(batch);
				}
				materialVersionService.saveList(list1,batch);
				j.setSuccess(true);
				j.setErrorCode("0");
				j.setMsg("导入成功");
			}
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入失败！失败信息："+e.getMessage());
		}
		return j;
	}
	
	/**
	 * 删除项目
	 */
	@ResponseBody
	@RequiresPermissions("managementcenter:materialversion:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(MaterialVersion materialVersion) {
		AjaxJson j = new AjaxJson();
		materialVersionService.delete(materialVersion);
		j.setMsg("删除项目成功");
		return j;
	}
	
	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequiresPermissions("managementcenter:materialversion:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			materialVersionService.delete(materialVersionService.get(id));
		}
		j.setMsg("删除项目成功");
		return j;
	}






}