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

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sysnumberclass.entity.SysNumberClass;
import com.jeeplus.modules.sysnumberclass.service.SysNumberClassService;

/**
 * 编码管理Controller
 * @author 丁旭
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/sysnumberclass/sysNumberClass")
public class SysNumberClassController extends BaseController {

	@Autowired
	private SysNumberClassService sysNumberClassService;
	
	@ModelAttribute
	public SysNumberClass get(@RequestParam(required=false) String id) {
		SysNumberClass entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysNumberClassService.get(id);
		}
		if (entity == null){
			entity = new SysNumberClass();
		}
		return entity;
	}
	
	/**
	 * 编码管理列表页面
	 */
	@RequiresPermissions("sysnumberclass:sysNumberClass:list")
	@RequestMapping(value = {"list", ""})
	public String list(SysNumberClass sysNumberClass, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<SysNumberClass> list = sysNumberClassService.findList(sysNumberClass); 
		model.addAttribute("list", list);
		return "modules/sysnumberclass/sysNumberClassList";
	}

	/**
	 * 查看，增加，编辑编码管理表单页面
	 */
	@RequiresPermissions(value={"sysnumberclass:sysNumberClass:view","sysnumberclass:sysNumberClass:add","sysnumberclass:sysNumberClass:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(SysNumberClass sysNumberClass, Model model) {
		if (sysNumberClass.getParent()!=null && StringUtils.isNotBlank(sysNumberClass.getParent().getId())){
			sysNumberClass.setParent(sysNumberClassService.get(sysNumberClass.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(sysNumberClass.getId())){
				SysNumberClass sysNumberClassChild = new SysNumberClass();
				sysNumberClassChild.setParent(new SysNumberClass(sysNumberClass.getParent().getId()));
				List<SysNumberClass> list = sysNumberClassService.findList(sysNumberClass); 
				if (list.size() > 0){
					sysNumberClass.setSort(list.get(list.size()-1).getSort());
					if (sysNumberClass.getSort() != null){
						sysNumberClass.setSort(sysNumberClass.getSort() + 30);
					}
				}
			}
		}
		if (sysNumberClass.getSort() == null){
			sysNumberClass.setSort(30);
		}
		model.addAttribute("sysNumberClass", sysNumberClass);
		return "modules/sysnumberclass/sysNumberClassForm";
	}

	/**
	 * 保存编码管理
	 */
	@RequiresPermissions(value={"sysnumberclass:sysNumberClass:add","sysnumberclass:sysNumberClass:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(SysNumberClass sysNumberClass, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, sysNumberClass)){
			return form(sysNumberClass, model);
		}
		if(!sysNumberClass.getIsNewRecord()){//编辑表单保存
			SysNumberClass t = sysNumberClassService.get(sysNumberClass.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(sysNumberClass, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			sysNumberClassService.save(t);//保存
		}else{//新增表单保存
			sysNumberClassService.save(sysNumberClass);//保存
		}
		addMessage(redirectAttributes, "保存编码管理成功");
		return "redirect:"+Global.getAdminPath()+"/sysnumberclass/sysNumberClass/?repage";
	}
	
	/**
	 * 删除编码管理
	 */
	@RequiresPermissions("sysnumberclass:sysNumberClass:del")
	@RequestMapping(value = "delete")
	public String delete(SysNumberClass sysNumberClass, RedirectAttributes redirectAttributes) {
		sysNumberClassService.delete(sysNumberClass);
		addMessage(redirectAttributes, "删除编码管理成功");
		return "redirect:"+Global.getAdminPath()+"/sysnumberclass/sysNumberClass/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<SysNumberClass> list = sysNumberClassService.findList(new SysNumberClass());
		for (int i=0; i<list.size(); i++){
			SysNumberClass e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}