package com.zcloud.controller.inspection;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.inspection.DictionaryService;
import com.zcloud.util.Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：数据字典处理类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/safetyenvironmental/dictionary")
public class DictionaryController extends BaseController {

	@Autowired
	private DictionaryService dictionaryService;


	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("dictionaries: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());
		String DICTIONARIES_ID = null == pd.get("DICTIONARIES_ID")?"":pd.get("DICTIONARIES_ID").toString();
		pd.put("DICTIONARIES_ID", DICTIONARIES_ID);						//上级ID
		pd.put("VISIBLE",1);
		page.setPd(pd);
		List<PageData>	varList = dictionaryService.list(page);		//列出Dictionaries列表
		if("".equals(DICTIONARIES_ID) || "0".equals(DICTIONARIES_ID)) {
			map.put("PARENT_ID", "0");									//上级ID
		}else {
			PageData parent = dictionaryService.findById(pd);
			map.put("PARENT_ID", parent.getString("PARENT_ID")); // 上级ID
			map.put("PARENT_NAME",parent.getString("NAME")); // 上级ID
		}
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**去新增页面
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goAdd")
	@ResponseBody
	public Object goAdd()throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String DICTIONARIES_ID = null == pd.get("DICTIONARIES_ID")?"":pd.get("DICTIONARIES_ID").toString();
		pd.put("DICTIONARIES_ID", DICTIONARIES_ID);						//上级ID
		map.put("pds",dictionaryService.findById(pd));				//传入上级所有信息
		map.put("result", errInfo);
		return map;
	}

	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	@ResponseBody
	public Object add() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("DICTIONARIES_ID", this.get32UUID());	//主键
		pd.put("ISDELETE", 0);	//主键
		dictionaryService.save(pd);
		map.put("result", errInfo);
		return map;
	}



	/**去修改页面
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	@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 = dictionaryService.findById(pd);							//根据ID读取
		map.put("pd", pd);												//放入视图容器
		pd.put("DICTIONARIES_ID",pd.get("PARENT_ID").toString());		//用作上级信息
		map.put("pds",dictionaryService.findById(pd));				//传入上级所有信息
		map.put("result", errInfo);										//返回结果
		return map;
	}


	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		dictionaryService.edit(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**
	 * 删除
	 * @param DICTIONARIES_ID
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestParam String DICTIONARIES_ID) throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		PageData pd = new PageData();
		pd.put("DICTIONARIES_ID", DICTIONARIES_ID);
		String errInfo = "success";
		if(dictionaryService.listSubDictByParentId(DICTIONARIES_ID).size() > 0){//判断是否有子级，是：不允许删除
			errInfo = "fail";
		}

		//逻辑删除，无需验证占用情况
//		else{
//			pd = dictionaryService.findById(pd);					//根据ID读取
//			if("yes".equals(pd.getString("YNDEL")))return null;		//当禁止删除字段值为yes, 则禁止删除，只能从手动从数据库删除
//			if(null != pd.get("TBSNAME") && !"".equals(pd.getString("TBSNAME"))){
//				String TBFIELD = pd.getString("TBFIELD");
//				if(Tools.isEmpty(TBFIELD))TBFIELD = "BIANMA"; 		//如果关联字段没有设置，就默认字段为 BIANMA
//				pd.put("TBFIELD", TBFIELD);
//				String[] table = pd.getString("TBSNAME").split(",");
//				for(int i=0;i<table.length;i++){
//					pd.put("thisTable", table[i]);
//					try {
//						if(Integer.parseInt(dictionaryService.findFromTbs(pd).get("zs").toString())>0){//判断是否被占用，是：不允许删除(去排查表检查字典表中的编码字段)
//							errInfo = "fail";
//							break;
//						}
//					} catch (Exception e) {
//						errInfo = "error2";
//						break;
//					}
//				}
//			}
//		}
		if("success".equals(errInfo)){
			dictionaryService.delete(pd);	//执行删除
		}
		map.put("result", errInfo);
		return map;
	}

}
