package cn.iocoder.yudao.module.system.controller.admin.permission;

import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuTenantRelatePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuTenantRelateRespVO;
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuTenantRelateSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuTenantRelateDO;
import cn.iocoder.yudao.module.system.service.permission.MenuTenantRelateService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 菜单租户关系")
@RestController
@RequestMapping("/menu/tenant-relate")
@Validated
public class MenuTenantRelateController {

    @Resource
    private MenuTenantRelateService tenantRelateService;

    @PostMapping("/create")
    @Operation(summary = "创建菜单租户关系")
    @PreAuthorize("@ss.hasPermission('menu:tenant-relate:create')")
    public CommonResult<Long> createTenantRelate(@Valid @RequestBody MenuTenantRelateSaveReqVO createReqVO) {
        return success(tenantRelateService.createTenantRelate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜单租户关系")
    @PreAuthorize("@ss.hasPermission('menu:tenant-relate:update')")
    public CommonResult<Boolean> updateTenantRelate(@Valid @RequestBody MenuTenantRelateSaveReqVO updateReqVO) {
        tenantRelateService.updateTenantRelate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜单租户关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('menu:tenant-relate:delete')")
    public CommonResult<Boolean> deleteTenantRelate(@RequestParam("id") Long id) {
        tenantRelateService.deleteTenantRelate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得菜单租户关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('menu:tenant-relate:query')")
    public CommonResult<MenuTenantRelateRespVO> getTenantRelate(@RequestParam("id") Long id) {
        MenuTenantRelateDO tenantRelate = tenantRelateService.getTenantRelate(id);
        return success(BeanUtils.toBean(tenantRelate, MenuTenantRelateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜单租户关系分页")
    @PreAuthorize("@ss.hasPermission('menu:tenant-relate:query')")
    public CommonResult<PageResult<MenuTenantRelateRespVO>> getTenantRelatePage(@Valid MenuTenantRelatePageReqVO pageReqVO) {
        PageResult<MenuTenantRelateDO> pageResult = tenantRelateService.getTenantRelatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MenuTenantRelateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出菜单租户关系 Excel")
    @PreAuthorize("@ss.hasPermission('menu:tenant-relate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantRelateExcel(@Valid MenuTenantRelatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MenuTenantRelateDO> list = tenantRelateService.getTenantRelatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "菜单租户关系.xls", "数据", MenuTenantRelateRespVO.class,
                        BeanUtils.toBean(list, MenuTenantRelateRespVO.class));
    }

}