package com.zhongdao.jlr.business.action;

import com.util.RequestUtil;
import com.util.Vo;
import com.zhongdao.jlr.business.service.RoleService;
import com.zhongdao.jlr.business.vo.JLRMenu;
import com.zhongdao.jlr.pojo.BaseMenu;
import com.zhongdao.jlr.pojo.JLRRole;
import org.apache.http.util.TextUtils;
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.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 角色控制器
 */
@Controller
@RequestMapping("/role")
public class RoleAction {

    @Resource(name = "roleServiceImpl")
    RoleService service;

    /***
     * 跳转角色管理界面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/role.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String JumpTocaseInfo(HttpServletRequest request, HttpServletResponse response) {
        return "/page/role";
    }

    /***
     * 获取角色信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getRole.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void caseInfoList(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");// 案件编号
        //获取用户信息
        Vo vo = service.getRoleInfo(request, name);
        //将用户信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /***
     * 获取tree数据
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getTree.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getTreeInfo(HttpServletRequest request, HttpServletResponse response) {
        //获取用户信息
        List<JLRMenu> vo = service.getTreeInfo();
        //将用户信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ListtoJsonString(vo));
    }

    /**
     * 添加角色信息
     */
    @RequestMapping(value = "/insertRole.do", produces = "application/json;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map putMyEnterprise(HttpServletRequest request, HttpServletResponse response, JLRRole jlrRole, int[] ids) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (jlrRole != null) {
            //添加或更改用户信息
            boolean back = service.updateRoleInfo(jlrRole, ids);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        return result;
    }

    /**
     * 根据id查询角色信息
     */
    @RequestMapping(value = "/getRoleInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getById(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map集合,存储返回的结果信息
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        String id = request.getParameter("id");
        if (!TextUtils.isEmpty(id)) {
            //根据id获取角色信息
            JLRRole back = service.getRole(id);
            if (back != null) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
                result.put("data", back);
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将结果信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
    }

    /**
     * 根据id查询菜单信息
     */
    @RequestMapping(value = "/getMenuInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getMenu(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map集合,存储返回的结果信息
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        String id = request.getParameter("id");
        if (!TextUtils.isEmpty(id)) {
            //根据id获取角色信息
            List<BaseMenu> back = service.getMenu(id);
            if (back != null) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
                result.put("data", back);
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将结果信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
    }

    /**
     * 根据编号删除角色信息
     */
    @RequestMapping(value = "/deleteRole.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void deleteById(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map对象用于存放响应到客户端的数据
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(name)) {
            boolean back = service.deleteRole(id,name);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }
}