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.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.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.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@RestController
@RequestMapping(value = "/SysMenu")
public class SysMenuController {

    @Autowired
    public SysMenuService sysMenuService;

    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();
    String result = Type.FAIL;


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

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

    @RequestMapping(value = "/query.do", method = RequestMethod.POST)
    public String query(int page,
                        int rows,
                        String pid,
                        String userId) throws IOException {
        result = sysMenuService.query(page, rows, pid, userId);
        return result;
    }


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

        result = sysMenuService.queryAppMenu(userId);


        Tools.out(response, result);

    }


    @RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
    public String save(String id,
                       String menuPid,
                       String menuCode,
                       String menuName,
                       String menuOpenBs,
                       String menuBsIcon,
                       String menuShow,
                       String userId) throws IOException {

        result = sysMenuService.save(id, menuPid, menuCode, menuName, menuOpenBs, menuBsIcon, menuShow, userId);
        return result;
    }

    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public String remove(String id,
                         String userId) throws IOException {

        result = sysMenuService.remove(id, userId);
        return result;
    }
    @RequestMapping(value = "/queryByMenuCode.do")
    public String queryByMenuCode(String code) throws IOException {
        result = sysMenuService.queryByMenuCode(code);
        return result;
    }

}
