/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.entity.BaseSys;
import com.tofly.base.entity.BaseSysmenu;
import com.tofly.base.entity.Role;
import com.tofly.base.service.BaseSysService;
import com.tofly.base.service.BaseSysmenuService;
import com.tofly.base.service.RoleService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.update;

/**
 * 角色信息表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(tags="角色管理")
public class RoleController {

    private final  RoleService roleService;

    private final BaseSysmenuService baseSysmenuService;

    private final BaseSysService baseSysService;
    /**
    * 分页查询
    * @param page 分页对象
    * @param role 角色信息表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getRolePage(Page page, @ModelAttribute Role role) {
      return ResultRespone.success(roleService.page(page, Wrappers.query(role)));
    }


    /**
     * 分页查询
     * @param page 分页对象
     * @param role 角色信息表
     * @return
     */
    @GetMapping("/pageTree" )
    @ApiOperation(value = "分页查询(树状)")
    public ResultRespone getRolePageTree(Page page, @ModelAttribute Role role) {
        IPage<Map<String,Object>> page1 = roleService.pageMaps(page, Wrappers.query(role));
        page1.getRecords().stream().forEach(r->{
            String pathids=(String)r.get("treeValue");
            String sysid = (String)r.get("sysId");
            r.put("treeList",baseSysmenuService.getTreeSys(sysid,pathids));
        });
        return ResultRespone.success(page1);
    }

    /**
     * 分页查询
     * @param role 角色信息表
     * @return
     */
    @GetMapping("/list" )
    @ApiOperation(tags = {"用户管理","角色管理"},value = "角色列表")
    public ResultRespone getRoleList(@ModelAttribute Role role) {
        return ResultRespone.success(roleService.list(Wrappers.query(role)));
    }

    /**
     * 获取子系统列表
     * @param  sysId 子系统ID
     * @return
     */
    @GetMapping("/getSysid" )
    @ApiOperation(value = "查询子系统")
    public ResultRespone getRoleList(String sysId) {

        return ResultRespone.success(baseSysmenuService.getTreeSys(sysId));
    }


    /**
     * 列表查询
     * @return
     */
    @GetMapping("/sysList" )
    @ApiOperation(value = "子系统列表")
    public ResultRespone getBaseSysmenuPage() {
        List list = baseSysService.listMaps(Wrappers.<BaseSys>lambdaQuery()
                .select(BaseSys::getSysid, BaseSys::getSysname));
        return ResultRespone.success(list);
    }


    /**
     * 通过id查询角色信息表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(roleService.getById(id));
    }


    /**
     * 通过id查询角色信息表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/getTreeById/{id}" )
    @ApiOperation(value = "通过ID查询(树状)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getByIdTree(@PathVariable("id" ) Long id) {
        Map<String, Object> map = roleService.getMap(Wrappers.<Role>lambdaQuery()
                .eq(Role::getId, id));
        String pathids=(String)map.get("treeValue");
        String sysid = (String)map.get("sysId");
        map.put("treeList",baseSysmenuService.getTreeSys(sysid,pathids));
        return ResultRespone.success(map);
    }

    /**
     * 新增角色信息表
     * @param role 角色信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增角色信息表" )
    @ApiOperation(value = "新增角色信息表")
    @PostMapping
    public ResultRespone save(@RequestBody Role role) {
      return ResultRespone.success(roleService.save(role));
    }

    /**
     * 修改角色信息表
     * @param role 角色信息表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改角色信息表" )
    @ApiOperation(value = "修改角色信息表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Role role) {
      return ResultRespone.success(roleService.updateById(role));
    }

    /**
     * 通过id删除角色信息表
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除角色信息表" )
    @ApiOperation(value = "通过ID删除角色信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
        //非传统意义删除，修改禁用标志
        Role role=new Role();
        role.setId(id);
        role.setStatusFlag("0");

        return ResultRespone.success(update(role));

        //return ResultRespone.success(roleService.removeById(id));
    }


    /**
     * 通过id查询菜单列表
     * @param id id
     * @return ResultRespone
     */
//    @GetMapping("/menuList/{id}" )
//    @ApiOperation(value = "通过角色ID查询菜单列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
//    })
//    public ResultRespone getMenuList(@PathVariable("id" ) Long id) {
//        return ResultRespone.success(roleService.getMenuListByRole(id));
//    }

    /**
     * 通过id查询菜单列表
     * @param id id
     * @return ResultRespone
     */
//    @GetMapping("/menuListById" )
//    @ApiOperation(value = "通过角色ID查询菜单列表,包含所有菜单")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long")
//    })
//    public ResultRespone getMenuListById(Long id) {
//        return ResultRespone.success(roleService.listMenuByRoleId(id));
//    }
//

    /**
     * 通过id查询角色信息表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/userList/{id}" )
    @ApiOperation(value = "通过角色ID查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone userList(@PathVariable("id" ) Long id) {
        return ResultRespone.success(roleService.getExistUserListByRole(id));
    }

    /**
     * 通过id查询不存在的用户列表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/noUserList/{id}" )
    @ApiOperation(value = "通过角色id查询不存在的用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone noUserList(@PathVariable("id" ) Long id) {
        return ResultRespone.success(roleService.getNoExistUserListByRole(id));
    }

    /**
     * 通过id删除角色
     * @param ids id数组
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除角色" )
    @ApiOperation(value = "通过ID删除角色")
    @DeleteMapping("/removeByIds" )
    public ResultRespone removeByIds(@ApiParam(value = "通过ID拼接的字符串,以逗号隔开.如,'1,2,3,4,5,6,7",name = "ids",required = true) String ids) {
        String msg=roleService.deleteRolesByIds(ids);
        return StringUtil.isEmpty(msg)?ResultRespone.success(true):ResultRespone.failed(msg);
    }
}

