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


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;

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.menu.model.MenuEntity;
import com.mortals.xhx.base.system.menu.service.MenuService;
import com.mortals.xhx.common.code.DataSatus;
import com.mortals.xhx.common.code.MenuAuthType;
import com.mortals.xhx.common.code.MenuComm;
import com.mortals.xhx.common.code.MenuLinkType;
import com.mortals.xhx.common.code.MenuType;

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


/**
 * <p>Title: 菜单信息</p>
 * <p>Description: MenuController  </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author 
 * @version 1.0.0
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseCRUDJsonMappingController<MenuService, MenuForm,MenuEntity,Long> {

	public MenuController(){
		super.setFormClass(MenuForm.class);
		super.setModuleDesc("菜单信息");
	}


	@Override
	protected void doListBefore(HttpServletRequest request, HttpServletResponse response, MenuForm form, Map<String, Object> model, Context context) throws AppException {
		List<OrderCol> orderColList = new ArrayList<OrderCol>();
		orderColList.add(new OrderCol("parentId"));
		orderColList.add(new OrderCol("orderId"));
		form.getQuery().setOrderColList(orderColList);
	}

	@Override
	protected void init(HttpServletRequest request, HttpServletResponse response, MenuForm form, Map<String, Object> model,
						Context context) {
		Map<String, Object> status = new HashMap<String, Object>();
		status.put("status", DataSatus.getEnumMap(DataSatus.CLOSE.getValue(), DataSatus.DELETE.getValue(), DataSatus.OVERDUE.getValue(), DataSatus.USEOUT.getValue()));
		status.put("linkType", MenuLinkType.getEnumMap());
		status.put("commMenu", MenuComm.getEnumMap());
		status.put("menuType", MenuType.getEnumMap());
		status.put("authType", MenuAuthType.getEnumMap());
		model.put(KEY_RESULT_DICT, status);
		super.init(request, response, form, model, context);
	}

	/**
	 * 改变状态
	 */
	@RequestMapping(value = "change/status")
	public String changeStatus(HttpServletRequest request, HttpServletResponse response, MenuForm form) {
		JSONObject ret = new JSONObject();
		Context context = getContext();
		try {
			MenuEntity entity = this.service.get(form.getEntity().getId(), context);//.doSubmitAudit(form.getEntity(), context);
			if (null == entity) {
				throw new AppException("菜单不存在!");
			}
			if (null == form.getEntity().getStatus()) {
				throw new AppException("菜单状态不能为空!");
			}
			if (form.getEntity().getStatus() != DataSatus.ENABLE.getValue() && form.getEntity().getStatus() != DataSatus.DISENABLE.getValue()) {
				throw new AppException("非法菜单状态!");
			}
			String busiDesc = DataSatus.getByValue(form.getEntity().getStatus()).getDesc();
			entity.setStatus(form.getEntity().getStatus());
			this.service.update(entity, context);
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
			ret.put(KEY_RESULT_MSG, busiDesc + "成功");
			recordSysLog(request, busiDesc + " 【成功】 [id:" + entity.getId() + "]");

		} catch (Exception e) {
			ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
			ret.put(KEY_RESULT_MSG, super.convertException(e));
		}
		return ret.toJSONString();
	}
}