package com.th.demo.controller.system;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.SysMenu;
import com.th.demo.model.system.TreeNode;
import com.th.demo.model.system.Type;
import com.th.demo.service.impl.system.SysMenuServiceImpl;
import com.th.demo.service.system.SysMenuService;
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.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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by litai on 2017/5/4.
 */

@RestController
@RequestMapping(value = "/SysParent")
public class SysParentController {


    @Autowired
    public SysMenuService sysMenuService;

    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

    String result = Type.FAIL;

    @RequestMapping(value = "/queryTree.do")
    public String queryTree(String userId,
                           HttpServletResponse response) throws IOException {

        List<SysMenu> list  = sysMenuService.queryByUserId(userId);
        List<TreeNode> tree = Tools.getTree(list,"0");
        result = gson.toJson(tree);
        return  result;
    }

    @RequestMapping(value = "/queryMenu.do")
    public String queryWeb(String userId,
                           HttpServletResponse response) throws IOException {

        List<SysMenu> list  = sysMenuService.queryByUserId(userId);
        List<TreeNode> tree = Tools.getTree(list,"0");
        result = gson.toJson(tree);
        return  result;
    }

    @RequestMapping(value = "/queryRoute.do")
    public String queryRoute(String userId,
                         HttpServletResponse response) throws IOException {

        List<SysMenu> list  = sysMenuService.queryByUserId(userId);
        List<TreeNode> tree = Tools.getRoute(list,"0");
        result = gson.toJson(tree);
        return  result;
    }


    @RequestMapping(value = "/queryMenuForRoleMenu.do")
    public String queryMenuForRoleMenu(HttpServletRequest request) throws IOException {
        String userId = (String) request.getAttribute("userId");
        String belongId = (String) request.getAttribute("belongId");
        result =  sysMenuService.queryByUserIdForRoleMenu(userId);

        return  result;
    }


}

