package com.sptg.permissions.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.permissions.api.dto.RoleDto;
import com.sptg.permissions.api.entity.Menu;
import com.sptg.permissions.api.entity.Role;
import com.sptg.permissions.api.entity.RoleMenu;
import com.sptg.permissions.api.feigns.RoleFeign;
import com.sptg.permissions.biz.service.MenuServiceInterface;
import com.sptg.permissions.biz.service.RoleMenuServiceInterface;
import com.sptg.permissions.biz.service.RoleServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * title: RoleController
 * package: com.sptg.permissions.biz.controller
 * description: 角色-控制器
 *
 *                  
 *                 
 */
@RestController
@RequiredArgsConstructor
public class RoleController implements RoleFeign {

    /* -------------------------------------------------- 接口注入 -------------------------------------------------- */

    private final RoleServiceInterface roleServiceInterface;
    private final RoleMenuServiceInterface roleMenuServiceInterface;
    private final MenuServiceInterface menuServiceInterface;

    /* --------------------------------------------------- 控制器 --------------------------------------------------- */

    @Override
    @PostMapping(value = "/role/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody PageBean<Role,RoleDto> pageBean) {
        return this.roleServiceInterface.pagingQuery(pageBean);
    }

    @Override
    @PostMapping(value = "/role/add")
    public ResultBean<Boolean> addEntity(@RequestBody RoleDto dto) {
        return this.roleServiceInterface.addEntity(dto);
    }

    @Override
    @GetMapping(value = "/role/get/{id}")
    public Role getById(@PathVariable(value = "id") Long id) {
        //  获取角色信息
        Role role = this.roleServiceInterface.selectById(id);
        if (null != role){
            //  获取角色可操作的权限列表
            List<RoleMenu> roleMenuList = this.roleMenuServiceInterface.selectListByRoleId(role.getId());
            List<Menu> menuList = new ArrayList<>(roleMenuList.size());
            roleMenuList.forEach(roleMenu -> {
                Menu menu = this.menuServiceInterface.selectById(roleMenu.getMenuId());
                if (null != menu){
                    menuList.add(menu);
                }
            });
            role.setMenuList(menuList);
        }
        return role;
    }

    @Override
    @PostMapping(value = "/role/update")
    public ResultBean<Boolean> updateEntity(@RequestBody RoleDto dto) {
        return this.roleServiceInterface.updateEntity(dto);
    }

    @Override
    @PostMapping(value = "/role/delete")
    public ResultBean<Boolean> deleteEntity(@RequestBody RoleDto dto) {
        return this.roleServiceInterface.deleteEntity(dto);
    }

    @Override
    @GetMapping(value = "/role/getListByOrgId/{orgId}")
    public List<Role> getListByOrgId(@PathVariable(value = "orgId") Long orgId) {
        return this.roleServiceInterface.getListByOrgId(orgId);
    }

    @Override
    @GetMapping(value = "/role/getListByAccountId/{accountId}")
    public List<Role> getListByAccountId(@PathVariable(value = "accountId") Long accountId) {
        return this.roleServiceInterface.getListByAccountId(accountId);
    }
}
