package com.th.demo.controller.system;

import com.th.demo.service.system.SysRoleService;
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 java.io.IOException;

@RestController
@RequestMapping(value = "/SysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping(value = "/query.do")
    public String query(int page,
                        int rows,
                        String query,
                        HttpServletRequest request) {
        String result = "";
        String userId = (String) request.getAttribute("userId");
        String belongId = (String) request.getAttribute("belongId");
        try {
            result = sysRoleService.query(page, rows,query,userId,belongId);
        } 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");
        String belongId = (String) request.getAttribute("belongId");
        try {
            result = sysRoleService.save(json, userId, belongId);
        } 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 = sysRoleService.remove(id, userId);
        } catch (Exception ex) {
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
            ex.printStackTrace();
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/querySystemRole.do")
    public String querySystemRole( String selectUserId,
                                   HttpServletRequest request) throws IOException {
        String result = "";
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = sysRoleService.querySystemRole(selectUserId,userId,belongId);
        } catch (Exception ex) {
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
            ex.printStackTrace();
        } finally {
            return result;
        }
    }


    @RequestMapping(value = "/queryUserRole.do")
    public String queryUserRole(String selectUserId,
                                HttpServletRequest request){
        String result = "";
        try {
            String userId = (String) request.getAttribute("userId");
            String belongId = (String) request.getAttribute("belongId");
            result = sysRoleService.queryUserRole(selectUserId,userId,belongId);
        } catch (Exception ex) {
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_EX, ex.getMessage(), null));
            ex.printStackTrace();
        } finally {
            return result;
        }
    }



}
