package com.yihu.hos.system.controller;

import com.yihu.hos.system.model.SystemRole;
import com.yihu.hos.system.service.RoleManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 角色管理
 */
@RequestMapping("/role")
@Controller
public class RoleController extends BaseController {
    @Resource(name = RoleManager.BEAN_ID)
    private RoleManager roleManager;

    /**
     * 角色管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String roleInitial(Model model) {
        model.addAttribute("contentPage", "system/role/role");
        return "partView";
    }

    /**
     * 角色列表
     *
     * @param request
     * @return
     */
    @RequestMapping("getRoleList")
    @ResponseBody
    public Result getRoleList(HttpServletRequest request) {
        try {
            Map<String, Object> params = new HashMap<>();
            String name = request.getParameter("name");
            params.put("name", name);
            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            return roleManager.getRoleList(params);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 角色编辑界面
     */
    @RequestMapping("editorRole")
    public String editorRole(Model model, String roleId) {
        try {
            //是否编辑
            if (roleId != null && roleId.length() > 0) {
                //获取菜单信息
                SystemRole role = roleManager.getRoleById(roleId);
                model.addAttribute("model", role);
            }
            model.addAttribute("contentPage", "system/role/editorRole");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "system/role/editorRole");
            return "pageView";
        }
    }

    /**
     * 新增角色
     */
    @RequestMapping("addRole")
    @ResponseBody
    public Result addRole(HttpServletRequest request) {
        try {
            SystemRole obj = new SystemRole();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setValid("true");

            return roleManager.addRole(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 新增角色
     */
    @RequestMapping("deleteRole")
    @ResponseBody
    public Result deleteRole(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return roleManager.deleteRole(id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 修改角色
     */
    @RequestMapping("updateRole")
    @ResponseBody
    public Result updateRole(HttpServletRequest request) {
        try {
            SystemRole obj = new SystemRole();
            BeanUtils.populate(obj, request.getParameterMap());

            return roleManager.updateRole(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    /**
     * 修改角色状态
     */
    @RequestMapping("activityRole")
    @ResponseBody
    public Result activityRole(String orgId, String activityFlag) {
        try {
            return roleManager.activityRole(orgId, activityFlag);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
}
