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.SysPost;
import com.th.demo.model.system.Type;
import com.th.demo.service.impl.system.SysPostServiceImpl;
import com.th.demo.service.system.SysPostService;
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;
import java.util.List;

/**
 * Created by dell on 2017-07-07.
 */
@RestController
@RequestMapping(value = "/SysPost")
public class SysPostController {

    @Autowired
    public SysPostServiceImpl sysPostServiceImpl;

    @Autowired
    public SysPostService sysPostService;

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


    @RequestMapping(value = "/show.do")
    public ModelAndView show() throws IOException {
        return new ModelAndView("system/syspost");
    }

    @RequestMapping(value = "/queryComboBox.do" )
    public void queryComboBox(   HttpServletResponse response) throws IOException {

        List<SysPost> list = sysPostServiceImpl.queryAll();
        result =  gson.toJson(list) ;



        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-type", "text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/query.do")//查询方法 前两个参数是分页参数
    public String query(int page,
                             int rows,
                             String postName,
                        HttpServletRequest request) throws IOException {
        try {
            String userId = (String) request.getAttribute("userId");
            result =sysPostService.query(page, rows, postName);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
    public String saveVue(String id,
                        String postName,
                        String postDesc,
                          HttpServletRequest request) throws IOException {
        try {
            String userId = (String) request.getAttribute("userId");
            result = sysPostService.save(id,postName,postDesc,userId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }

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


        try {
            String userId = (String) request.getAttribute("userId");
            result = sysPostService.remove(id,userId);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
        } finally {
            return result;
        }
    }
}
