/**
* 文件：ParamController.java
* 版本：1.0.0
* 日期：
* Copyright &reg; 
* All right reserved.
*/
package com.mortals.xhx.base.system.param.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.code.PageDisplayType;
import com.mortals.framework.model.Context;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.model.ParamEntity;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DataSatusEnum;
import com.mortals.xhx.common.code.ModStatusEnum;

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

/**
 * <p>Title: 参数信息</p>
 * <p>Description: ParamController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author 
 * @version 1.0.0
 */
@RestController
@RequestMapping("param")
public class ParamController extends BaseCRUDJsonMappingController<ParamService,ParamForm,ParamEntity,Long> {

	public ParamController(){
		super.setFormClass(ParamForm.class);
		super.setModuleDesc("参数信息");
	}

	@Override
	protected void init(HttpServletRequest request, HttpServletResponse response, ParamForm form,
						Map<String, Object> model, Context context) {
		Map<String, Object> status = new HashMap<String, Object>();
		status.put("validStatus", DataSatusEnum.getEnumMap(DataSatusEnum.CLOSE.getValue(),
				DataSatusEnum.DELETE.getValue(), DataSatusEnum.OVERDUE.getValue(), DataSatusEnum.USEOUT.getValue()));
		status.put("modStatus", ModStatusEnum.getEnumMap());
		status.put("displayType", getPageDisplayType());
		model.put(KEY_RESULT_DICT, status);
		super.init(request, response, form, model, context);
	}

	/**
	 * 查询参数信息
	 */
	@Override
	@PostMapping(value = "list")
	public String list(HttpServletRequest request, HttpServletResponse response, ParamForm form) {
		try {
			return super.list(request, response, form);
		} catch (Exception e) {
			JSONObject ret = new JSONObject();
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, super.convertException(e));
			return ret.toJSONString();
		}
	}

	/**
	 * 添加参数信息
	 */
	@Override
	@PostMapping(value = "add")
	public String add(HttpServletRequest request, HttpServletResponse response, ParamForm form) {
		try {
			return super.add(request, response, form);
		} catch (Exception e) {
			JSONObject ret = new JSONObject();
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, super.convertException(e));
			return ret.toJSONString();
		}
	}

	/**
	 * 编辑参数信息
	 */
	@Override
	@PostMapping(value = "edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ParamForm form) {
		try {
			return super.edit(request, response, form);
		} catch (Exception e) {
			JSONObject ret = new JSONObject();
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, super.convertException(e));
			return ret.toJSONString();
		}
	}

	/**
	 * 保存参数信息
	 */
	@Override
	@PostMapping(value = "save")
	public String save(HttpServletRequest request, HttpServletResponse response, ParamForm form) {
		return super.save(request, response, form);
	}

	/**
	 * 删除参数信息
	 */
	@Override
	@PostMapping(value = "delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, ParamForm form) {
		return super.delete(request, response, form);
	}

	/**
	 * 查看参数信息
	 */
	@Override
	@PostMapping(value = "view")
	public String view(HttpServletRequest request, HttpServletResponse response, ParamForm form) {
		try {
			return super.view(request, response, form);
		} catch (Exception e) {
			JSONObject ret = new JSONObject();
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, super.convertException(e));
			return ret.toJSONString();
		}
	}


	private Map<String,Object> getPageDisplayType(){
		PageDisplayType[] pageDisplayTypes = PageDisplayType.values();
		Map<String,Object> result = new HashMap<>(pageDisplayTypes.length);
		for (PageDisplayType pageDisplayType : pageDisplayTypes) {
			result.put(pageDisplayType.getValue()+"",pageDisplayType.getDesc());
		}
		return result;
	}
	
}