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


import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
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.moduleacquisition.entity.ModuleBlock;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.entity.ModuleListing;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.service.ModuleBlockService;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.service.ModuleListingService;
import com.jeeplus.modules.sg.managementcenter.moduleacquisition.util.ModuleUtil;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/managementcenter/moduleacquisition/moduleListing")
public class ModuleListingWeb extends BaseController {

	@Autowired
	private ModuleListingService moduleListingService;

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

	/**
	 * 项目列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(ModuleListing moduleListing, Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.setAttribute("blockId",moduleListing.getBlockId());
		model.addAttribute("ModuleListing", moduleListing);
		return "modules/sg/managementcenter/moduleacquisition/moduleListing";
	}

	@RequestMapping(value = {"list1", ""})
	public String list1(ModuleListing moduleListing, Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.setAttribute("blockId",moduleListing.getBlockId());
		model.addAttribute("ModuleListing", moduleListing);
		return "modules/sg/managementcenter/moduleacquisition/moduleNewListing";
	}

		/**
	 * 列表数据
	 */
	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(ModuleListing moduleListing, HttpServletRequest request, HttpServletResponse response, Model model) {
		HttpSession session = request.getSession();
		String blockId = (String) session.getAttribute("blockId");
		if (null!=blockId&&!blockId.equals("")){
			moduleListing.setBlockId(blockId);
		}else {
			moduleListing.setBlockId("0");
		}
		Page<ModuleListing> page = moduleListingService.findPage(new Page<ModuleListing>(request, response), moduleListing);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑表单页面
	 */
	@RequestMapping(value = "form")
	public String form(ModuleListing moduleListing, Model model) {
		model.addAttribute("moduleListing", moduleListing);
		return "modules/sg/managementcenter/moduleacquisition/moduleListingNewForm";
	}


	/**
	 * 删除项目
	 */
	@ResponseBody
	@RequestMapping(value = "delete")
	public AjaxJson delete(ModuleListing moduleListing) {
		AjaxJson j = new AjaxJson();
		moduleListingService.delete(moduleListing);
		j.setMsg("删除项目成功");
		return j;
	}
	
	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			moduleListingService.delete(moduleListingService.get(id));
		}
		j.setMsg("删除项目成功");
		return j;
	}
	@ResponseBody
//	@RequiresPermissions(value={"managementcenter:moduleacquisition:add","managementcenter:moduleacquisition:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(ModuleListing moduleListing, Model model,HttpServletRequest request) throws Exception{
		AjaxJson j = new AjaxJson();
		HttpSession session = request.getSession();
		String blockId = (String) session.getAttribute("blockId");
		moduleListing.setBlockId(blockId);
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(moduleListing);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		//新增或编辑表单保存
		moduleListingService.save(moduleListing);//保存
		j.setSuccess(true);
		j.setMsg("保存模块成功");
		return j;
	}
}