package jnpf.authority.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.authority.service.FtbPermissionRoleAuthorizePostService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.authority.dto.authorize.FtbPermissionAuthorizeVO;
import jnpf.model.authority.dto.person.FtbPermissionRoleBatchDeleteDTO;
import jnpf.model.authority.dto.post.FtbPermissionRolePostAddDTO;
import jnpf.model.authority.vo.post.FtbAuthorizedPostVO;
import jnpf.model.cultivate.CultivatePage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * web权限管理角色岗位授权模块
*
* @author xxxxx
*/
@RestController
@RequestMapping("/web/permission-role-authorize-post")
public class FtbPermissionRoleAuthorizePostController {

    @Resource
    private FtbPermissionRoleAuthorizePostService ftbPermissionRoleAuthorizePostService;

    /**
     * 添加岗位授权
     */
    @PostMapping("/add-post")
    public ActionResult<Void> addPost(@RequestBody @Validated FtbPermissionRolePostAddDTO ftbPermissionRolePostAddDTO) {
        ftbPermissionRoleAuthorizePostService.addPost(ftbPermissionRolePostAddDTO);
        return ActionResult.success("操作成功");
    }

    /**
     * 批量删除岗位授权
     */
    @DeleteMapping("batch-delete")
    public ActionResult<Void> batchDelete(@RequestBody @Validated FtbPermissionRoleBatchDeleteDTO rolePersonDeleteDTO) {
        ftbPermissionRoleAuthorizePostService.batchDelete(rolePersonDeleteDTO);
        return ActionResult.success("操作成功");
    }

    /**
     * 授权岗位列表
     *
     * @param cultivatePage 培养页面
     * @param roleId        角色主键id(必传)
     * @return {@link ActionResult }<{@link PageListVO }<{@link FtbAuthorizedPostVO }>>
     */
    @GetMapping("/authorized-post")
    public ActionResult<PageListVO<FtbAuthorizedPostVO>> authorizePost(CultivatePage cultivatePage, @RequestParam(value = "roleId") String roleId) {
        Page<FtbAuthorizedPostVO> page = cultivatePage.coverCultivatePage();
        PageListVO<FtbAuthorizedPostVO> result  = ftbPermissionRoleAuthorizePostService.authorizePost(page, roleId);
        return ActionResult.success(result);
    }

    /**
     * 根据 postId 返回角色未绑定 postId 集合
     *
     */
    @PostMapping("/getRoleIdsByPostIds")
    public ActionResult<List<String>> getRoleIdsByPostIds(@RequestBody FtbPermissionAuthorizeVO vo) {
        return ActionResult.success(ftbPermissionRoleAuthorizePostService.getRoleIdsByPostIds(vo));
    }

    /**
     * 更具岗位id清除所有绑定的角色
     */
    @PostMapping("/clearRoleByPostId")
    public void clearRoleByPostId(@RequestBody List<String> postIds) {
        ftbPermissionRoleAuthorizePostService.clearRoleByPostId(postIds);
    }
}
