package jnpf.authority.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.authority.service.FtbPermissionFunctionMenuService;
import jnpf.authority.utils.PermissionsEnums;
import jnpf.authority.utils.PermissionsUtils;
import jnpf.base.ActionResult;
import jnpf.base.model.module.MenuListVO;
import jnpf.base.vo.PageListVO;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.model.authority.dto.menu.FtbPermissionFunctionMenuDTO;
import jnpf.model.authority.dto.menu.FtbPermissionMenuDirectoryDTO;
import jnpf.model.authority.dto.menu.FunctionMenuRemoteDTO;
import jnpf.model.authority.dto.menu.FuntionMenuDTO;
import jnpf.model.authority.po.FtbPermissionFunctionMenu;
import jnpf.model.authority.vo.menu.FtbPermissionFunctionMenuVO;
import jnpf.model.authority.vo.menu.FtbPermissionMenuConfigTreeVO;
import jnpf.model.authority.vo.menu.FtbPermissionMenuDirectoryVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.util.NoDataSourceBind;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * web权限管理功能菜单配置模块
 *
 * @author peng.hao
 */
@RestController
@RequestMapping("/web/permission-function-menu")
public class FtbPermissionFunctionMenuController {
    /**
     * 服务对象
     */
    @Resource
    private FtbPermissionFunctionMenuService ftbPermissionFunctionMenuService;


    /**
     * 获取菜单列表
     * @param category  web/app/PC
     * @param keyword    关键字
     */
    @RequestMapping("/getAListOfMenus")
    public ActionResult<List<MenuListVO>> getAListOfMenus(String category, String keyword) {
     return  ActionResult.success(ftbPermissionFunctionMenuService.getAListOfMenus(category,keyword));
    }
    /**
     * 角色管理获取菜单目录列表
     * @param roleId  编辑是需要携带roleId查询回显
     */
    @GetMapping("/getAListOfMenuDirectory")
    public ActionResult<List<FtbPermissionMenuDirectoryVO>> getAListOfMenuDirectory(String roleId) {
        return ActionResult.success(ftbPermissionFunctionMenuService.getAListOfMenuDirectory(roleId));
    }
    /**
     * 获取功能菜单和按钮配置
     */
    @GetMapping("/getAListOfFunctionMenuConfigurations")
    public ActionResult<List<FtbPermissionMenuConfigTreeVO>> getAListOfFunctionMenuConfigurations(FtbPermissionMenuDirectoryDTO dto) {
        return ActionResult.success(ftbPermissionFunctionMenuService.getAListOfFunctionMenuConfigurations(dto));
    }
    /**
     * 查询子页面列表
     * @param menuId 菜单id
     *
     */
    @GetMapping("/getAListOfSubPages")
    public ActionResult<PageListVO<FtbPermissionFunctionMenuVO>> getAListOfSubPages(String menuId, CultivatePage page) {
        return ActionResult.success(ftbPermissionFunctionMenuService.getAListOfSubPages(menuId,page));
    }
    /**
     * 查询子页面按钮
     * @param subPageId 子页面id
     */
    @GetMapping("/querySubpageButton")
    public ActionResult<List<FtbPermissionFunctionMenuVO>> querySubpageButton(@RequestParam("subPageId") String subPageId) {
        return ActionResult.success(ftbPermissionFunctionMenuService.querySubpageButton(subPageId));
    }
    /**
     * 添加子页面/按钮
     */
    @PostMapping("/addAListOfSubPages")
    public ActionResult<Void> addAListOfSubPages(@Validated @RequestBody FtbPermissionFunctionMenuDTO dto) {
        ftbPermissionFunctionMenuService.addAListOfSubPages(dto);
        return ActionResult.success("保存成功");
    }

    /**
     * 编辑子页面/按钮
     */
    @PutMapping("/updateAListOfSubPages")
    public ActionResult<Void> updateAListOfSubPages(@Validated @RequestBody FtbPermissionFunctionMenuDTO dto) {
        ftbPermissionFunctionMenuService.updateAListOfSubPages(dto);
        return ActionResult.success("保存成功");
    }
    /**
     * 删除子页面/按钮
     */
    @DeleteMapping("/deleteAListOfSubPages/{id}")
    public ActionResult<Void> deleteAListOfSubPages(@PathVariable String id) {
        ftbPermissionFunctionMenuService.removeById(id);
        LambdaQueryWrapper<FtbPermissionFunctionMenu> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPermissionFunctionMenu::getParentId, id);
        ftbPermissionFunctionMenuService.remove(queryWrapper);
        return ActionResult.success("删除成功");
    }

    /**
     * 远程client
     * 包含新增修改
     */
    @SneakyThrows
    @PostMapping("/remoteClient")
    @NoDataSourceBind
    public void remoteClient(FunctionMenuRemoteDTO dto) {
        TenantDataSourceUtil.switchTenant(dto.getTenantId());
        ftbPermissionFunctionMenuService.saveOrUpdateBatch(dto.getFunctionMenuList());
    }
}
