package com.th.demo.controller.system;


import com.alibaba.fastjson.JSON;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.ResultJSON;
import com.th.demo.model.system.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import com.th.demo.service.system.SysDicService;

@RestController
@RequestMapping(value = "/SysDic")
public class SysDicController {
    @Autowired
    public SysDicService sysDicService;

    String result = Type.FAIL;


    @RequestMapping(value = "/query.do")
    public String query(int page,
                        int rows,
                        String dicType,
                        HttpServletRequest request) {

        String result = "";
        String userId = (String) request.getAttribute("userId");
        try {
            result = sysDicService.query(page, rows, dicType);
        } catch (Exception ex) {
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
            ex.printStackTrace();
        } finally {
            return result;
        }

    }
    @RequestMapping(value = "/queryByType.do")
    public String queryByType(int page,
                        int rows,
                        String dicType,
                        HttpServletRequest request) {

        String result = "";
        String userId = (String) request.getAttribute("userId");
        try {
            result = sysDicService.queryByType(page, rows, dicType);
        } 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)
    public String save(String json,
                       HttpServletRequest request) {
        String result = "";
        String userId = (String) request.getAttribute("userId");
        try {
            result = sysDicService.save(json, userId);
        } catch (Exception ex) {
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
            ex.printStackTrace();
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "/remove.do", method = RequestMethod.POST)
    public String remove(String id,
                         HttpServletRequest request) {
        String result = "";
        String userId = (String) request.getAttribute("userId");
        try {
            result = sysDicService.remove(id, userId);
        } catch (Exception ex) {
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
            ex.printStackTrace();
        } finally {
            return result;
        }

    }


}