package com.xhkjedu.suser.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.suser.model.system.TRole;
import com.xhkjedu.suser.model.system.TUserRole;
import com.xhkjedu.suser.service.system.RoleService;
import com.xhkjedu.suser.vo.system.RoleModuleVo;
import com.xhkjedu.suser.vo.system.RoleVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * Description TODO
 * Author WN
 * Date 2020/6/13 17:46
 **/
@RestController
@RequestMapping("role")
public class RoleController {
    @Resource
    private RoleService roleService;

    //列表
    @PostMapping("/list")
    public ResultVo list(@RequestBody TRole role){
        N_Utils.validation(new Object[]{
                role.getSchoolid(),"学校id",1,
                role.getPage(),"显示页数",1,
                role.getPageSize(),"显示条数",1
        });
        PageHelper.startPage(role.getPage(),role.getPageSize());
        List<TRole> roles = roleService.listAllRoles(role.getSchoolid());
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(roles));
        return new ResultVo(0,"获取角色分页列表成功",pageResult);
    }

    //保存
    @PostMapping("/save")
    @Action("角色管理-增加")
    public ResultVo save(@RequestBody TRole role){
        N_Utils.validation(new Object[]{
                role.getRolename(),"角色名称",2,
                role.getSchoolid(),"学校id",1
        });
        try {
            Integer result = roleService.save(role);
            if(result>0){
                return new ResultVo(0,"保存成功");
            }else{
                return new ResultVo(1,"保存失败");
            }
        } catch (Exception e) {
            return new ResultVo(1,"保存失败");
        }
    }

    //修改
    @PostMapping("/up")
    @Action("角色管理-修改")
    public ResultVo up(@RequestBody TRole role){
        N_Utils.validation(new Object[]{
                role.getRoleid(),"角色id",1,
                role.getRolename(),"角色名称",2
        });
        try {
            Integer result = roleService.update(role);
            if(result>0){
                return new ResultVo(0,"修改成功");
            }else{
                return new ResultVo(1,"修改失败");
            }
        } catch (Exception e) {
            return new ResultVo(1,"修改失败");
        }
    }

    //删除
    @PostMapping("/del")
    @Action("角色管理-删除")
    public ResultVo del(@RequestBody TRole role){
        N_Utils.validation(new Object[]{
                role.getRoleid(),"角色id",1
        });
        Integer result = roleService.del(role.getRoleid());
        if(result>0){
            return new ResultVo(0,"删除成功");
        }else{
            return new ResultVo(1,"删除失败");
        }
    }

    //角色详情
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TRole role){
        N_Utils.validation(new Object[]{
                role.getRoleid(),"角色id",1
        });
        RoleVo roleVo = roleService.getRoleDetail(role.getRoleid());
        return new ResultVo(0,"获取角色详情成功",roleVo);
    }

    //获取用户角色权限
    @PostMapping("/rmlst")
    public ResultVo roleModule(@RequestBody TUserRole userRole){
        N_Utils.validation(new Object[]{
                userRole.getUserid(),"用户id",1
        });
        if(N_Utils.isEmpty(userRole.getBelongcode())){
            userRole.setBelongcode(2);//兼容app未更改
        }
        List<RoleModuleVo> lst = roleService.listModulesByUserid(userRole.getUserid(),userRole.getBelongcode());
        return new ResultVo(0,"获取用户权限成功",lst);
    }

    /**
     *功能描述 获取所有角色列表
     * @author WN
     * @date 2020/6/17
     * @param  * @param role
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/listall")
    public ResultVo listAll(@RequestBody TRole role){
        N_Utils.validation(new Object[]{
                role.getSchoolid(),"学校id",1
        });
        List<Map> roles = roleService.listForSel(role.getSchoolid());
        return new ResultVo(0,"获取所有角色列表成功",roles);
    }

    /**
     * @Description 角色用户
     * @Date 2024/6/27 16:27
     * @Author YWX
     * @Param [role]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_ru")
    public ResultVo listRoleUser(@RequestBody TRole role) {
        Integer roleid = role.getRoleid();
        N_Utils.validation(new Object[]{roleid, "角色id", 1});
        PageHelper.startPage(role.getPage(), role.getPageSize());
        List<Map> list = roleService.listRoleUser(roleid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取角色用户列表成功", pageResult);
    }

    //角色用户-移除
    @PostMapping("/del_ru")
    @Action("角色管理-移除角色用户")
    public ResultVo delRoleUser(@RequestBody TUserRole role) {
        Integer urid = role.getUrid();
        N_Utils.validation(new Object[]{urid, "角色用户id", 1});
        roleService.delRoleUser(urid);
        return new ResultVo(0, "移除成功");
    }

    /**
     * @Description 应用权限
     * @Date 2024/6/27 16:41
     * @Author YWX
     * @Param [role]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_rm")
    public ResultVo listRoleModule(@RequestBody TRole role) {
        Integer roleid = role.getRoleid();
        N_Utils.validation(new Object[]{roleid, "角色id", 1});
        return new ResultVo(0, "获取应用权限列表成功", roleService.listRoleModule(roleid));
    }

}
