package com.xintong.visualinspection.controller;

import com.alibaba.fastjson.JSON;
import com.xintong.visualinspection.bean.Msg;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件名：TestController
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@Controller
public class TestController {

    @Autowired
    private UserService userService;


    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request){
        Msg msg =  new Msg("测试标题","测试内容","额外信息，只对管理员显示");
        model.addAttribute("msg", msg);
        request.isUserInRole("ROLE_ADMIN");

        return "home";
    }
    @RequestMapping("/admin")
    @ResponseBody
    public String hello(){
        return "hello admin";
    }



    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/user1", method = RequestMethod.GET)
    @ResponseBody
    public String getList(){
        return "hello getList";
    }


    @RequestMapping(value = "/user1", method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        return "hello save";
    }


    @RequestMapping(value = "/user1", method = RequestMethod.PUT)
    @ResponseBody
    public String update(){
        return "hello update";
    }

    @RequestMapping(value = "/test1")
    @ResponseBody
    public String index(){

        User u = userService.getOne(1);


        return JSON.toJSON(u).toString();
    }

    @RequestMapping(value = "/test")
    public String test(){

        User user = new User();
        user.setUsername("ss");
        user.setAge(22);
        userService.insert(user);
        return "test";
    }

}
