package com.sunxung.factoring.web.sys;

import com.sunxung.factoring.entity.sys.vo.SysGradationVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunxung.factoring.entity.sys.Role;
import com.sunxung.factoring.entity.sys.vo.SearchRoleanization;
import com.sunxung.factoring.service.sys.RoleService;

/**
 * @className:RoleController
 * @description:角色controller
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年1月19日 下午2:07:29
 */
@Controller
public class RoleController {

    private static Logger log = LogManager.getLogger(RoleController.class);
    private static String PREFIX = "sys/role/";
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/role/do_list", method = RequestMethod.GET)
    @ResponseBody
    public Object do_list(SearchRoleanization search) {
        return roleService.findBySearch(search);
    }

    @RequestMapping("/role/list")
    public String list() {
        return PREFIX + "roleList";
    }

    @RequestMapping("/role/select")
    public String select() {
        return PREFIX + "roleSelect";
    }

    @RequestMapping("/role/add")
    public String add(String roleOrgId, Model model) {
        model.addAttribute("roleOrgId", roleOrgId);
        return PREFIX + "roleAdd";
    }

    @RequestMapping(value = "/role/do_add", method = RequestMethod.POST)
    @ResponseBody
    public Object do_add(Role role, Model model) {
        return roleService.add(role);
    }

    @RequestMapping("/role/update")
    public String update(Long id, Model model) {
        Role role1 = roleService.get(id);
        model.addAttribute("role", role1);
        return PREFIX + "roleUpdate";
    }

    @RequestMapping(value = "/role/do_update", method = RequestMethod.POST)
    @ResponseBody
    public Object do_update(Role role) {
        return roleService.update(role);
    }

    @RequestMapping(value = "/role/do_delete", method = RequestMethod.POST)
    @ResponseBody
    public Object do_delete(Long id) {
        return roleService.delete(id);
    }

    @RequestMapping(value = "/role/do_deletes", method = RequestMethod.POST)
    @ResponseBody
    public Object do_deletes(String[] ids) {
        return roleService.deletes(ids);
    }

    @RequestMapping(value = "/role/do_get", method = RequestMethod.GET)
    @ResponseBody
    public Object do_get(Long id) {
        return roleService.getDetail(id);
    }

    @RequestMapping(value = "/role/findGroupRole", method = RequestMethod.GET)
    @ResponseBody
    public Object do_findGroupRole() {
        return roleService.findGroupRole();
    }

    @RequestMapping(value = "/role/do_addUserRole", method = RequestMethod.POST)
    @ResponseBody
    public Object do_addUserRole(Long roleId, String[] userIds) {
        return roleService.addUserRole(roleId, userIds);
    }

    @RequestMapping(value = "/role/do_removeUserRole", method = RequestMethod.POST)
    @ResponseBody
    public Object do_removeUserRole(Long roleId, String[] userIds) {
        return roleService.removeUserRole(roleId, userIds);
    }

    /** 添加验证 */
    @RequestMapping(value = "/role/validateAdd", method = RequestMethod.GET)
    @ResponseBody
    public boolean do_validateAdd(String roleName) {
        return roleService.validateAdd(roleName);
    }

    /** 修改验证 */
    @RequestMapping("/role/validateUpdate")
    @ResponseBody
    public boolean validateUpdate(String roleName, Long roleId) {
        return roleService.validateUpdate(roleName, roleId);
    }

    /**角色内人员排序*/
    @RequestMapping(value = "/role/do_adjustOrder", method = RequestMethod.POST)
    @ResponseBody
    public Object do_adjustOrder(SysGradationVo sysVo) {
        return roleService.adjustOrder(sysVo);
    }

    /**根据当前用户ID查询角色列表 */
    @RequestMapping(value = "/role/do_getRoleListByUseId",method = RequestMethod.GET)
    @ResponseBody
    public Object do_getRoleListByUseId(){
        return roleService.getRoleListByUseId();
    }
}
