package com.th.demo.controller.system;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.ResultJSON;
import com.th.demo.model.system.SysRoleMenu;
import com.th.demo.model.system.Type;
import com.th.demo.service.impl.system.SysRoleMenuServiceImpl;
import com.th.demo.service.system.SysRoleMenuService;
import com.th.demo.tools.Tools;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@RestController
@RequestMapping(value = "/SysRoleMenu")
public class SysRoleMenuController {

	@Autowired
	public SysRoleMenuService sysRoleMenuService;

	String result = Type.FAIL;

	@RequestMapping(value = "/queryByRoleId.do")
	public String queryByRoleId(String roleId,
								HttpServletRequest request) throws IOException {
		String result = "";
		try {
			String userId = (String) request.getAttribute("userId");
			String belongId = (String) request.getAttribute("belongId");
			result  = sysRoleMenuService.queryByRoleId(roleId,userId,belongId);
		} catch (Exception ex) {
			result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
			ex.printStackTrace();
		} finally {
			return result;
		}


	}
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
	public String save(String roleId,
					   String checkedMenuIdLeaf,
					   String checkedMenuId,
					   String halfCheckMenuId,
					   String userId,
					   HttpServletResponse response) throws IOException {
		result = sysRoleMenuService.save(roleId,checkedMenuIdLeaf, checkedMenuId, halfCheckMenuId, userId);
		return result;
	}



	@RequestMapping(value = "/queryByRoleIdOnlyLeaf.do")
	public String queryByRoleIdOnlyLeaf(String roleId,
										HttpServletResponse response) throws IOException {
		result = sysRoleMenuService.queryByRoleIdOnlyLeaf(roleId);
		return result;
	}

}
