package cn.iocoder.yudao.module.member.controller.admin.spacecapsule;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.spacecapsule.vo.SpaceCapsulePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.spacecapsule.vo.SpaceCapsuleRespVO;
import cn.iocoder.yudao.module.member.controller.admin.spacecapsule.vo.SpaceCapsuleSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.spacecapsule.SpaceCapsuleDO;
import cn.iocoder.yudao.module.member.service.spacecapsule.SpaceCapsuleService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
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 javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 太空舱")
@RestController
@RequestMapping("/space-capsule")
@Validated
public class SpaceCapsuleController {

    @Resource
    private SpaceCapsuleService spaceCapsuleService;

    @PostMapping("/create")
    @Operation(summary = "创建太空舱")
    @PreAuthorize("@ss.hasPermission('t:space-capsule:create')")
    public CommonResult<Long> createSpaceCapsule(@Valid @RequestBody SpaceCapsuleSaveReqVO createReqVO) {
        return success(spaceCapsuleService.createSpaceCapsule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新太空舱")
    @PreAuthorize("@ss.hasPermission('t:space-capsule:update')")
    public CommonResult<Boolean> updateSpaceCapsule(@Valid @RequestBody SpaceCapsuleSaveReqVO updateReqVO) {
        spaceCapsuleService.updateSpaceCapsule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除太空舱")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:space-capsule:delete')")
    public CommonResult<Boolean> deleteSpaceCapsule(@RequestParam("id") Long id) {
        spaceCapsuleService.deleteSpaceCapsule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得太空舱")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:space-capsule:query')")
    public CommonResult<SpaceCapsuleRespVO> getSpaceCapsule(@RequestParam("id") Long id) {
        SpaceCapsuleDO spaceCapsule = spaceCapsuleService.getSpaceCapsule(id);
        return success(BeanUtils.toBean(spaceCapsule, SpaceCapsuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得太空舱分页")
    @PreAuthorize("@ss.hasPermission('t:space-capsule:query')")
    public CommonResult<PageResult<SpaceCapsuleRespVO>> getSpaceCapsulePage(@Valid SpaceCapsulePageReqVO pageReqVO) {
        PageResult<SpaceCapsuleDO> pageResult = spaceCapsuleService.getSpaceCapsulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpaceCapsuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出太空舱 Excel")
    @PreAuthorize("@ss.hasPermission('t:space-capsule:export')")
    @OperateLog(type = EXPORT)
    public void exportSpaceCapsuleExcel(@Valid SpaceCapsulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpaceCapsuleDO> list = spaceCapsuleService.getSpaceCapsulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "太空舱.xls", "数据", SpaceCapsuleRespVO.class,
                        BeanUtils.toBean(list, SpaceCapsuleRespVO.class));
    }

}