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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.orderspacecapsule.vo.OrderSpaceCapsulePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.orderspacecapsule.vo.OrderSpaceCapsuleRespVO;
import cn.iocoder.yudao.module.member.controller.admin.orderspacecapsule.vo.OrderSpaceCapsuleSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.orderspacecapsule.OrderSpaceCapsuleDO;
import cn.iocoder.yudao.module.member.service.orderspacecapsule.OrderSpaceCapsuleService;
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("/order-space-capsule")
@Validated
public class OrderSpaceCapsuleController {

    @Resource
    private OrderSpaceCapsuleService orderSpaceCapsuleService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得太空舱订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:order-space-capsule:query')")
    public CommonResult<OrderSpaceCapsuleRespVO> getOrderSpaceCapsule(@RequestParam("id") Long id) {
        OrderSpaceCapsuleDO orderSpaceCapsule = orderSpaceCapsuleService.getOrderSpaceCapsule(id);
        return success(BeanUtils.toBean(orderSpaceCapsule, OrderSpaceCapsuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得太空舱订单分页")
    @PreAuthorize("@ss.hasPermission('t:order-space-capsule:query')")
    public CommonResult<PageResult<OrderSpaceCapsuleRespVO>> getOrderSpaceCapsulePage(@Valid OrderSpaceCapsulePageReqVO pageReqVO) {
        PageResult<OrderSpaceCapsuleDO> pageResult = orderSpaceCapsuleService.getOrderSpaceCapsulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderSpaceCapsuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出太空舱订单 Excel")
    @PreAuthorize("@ss.hasPermission('t:order-space-capsule:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderSpaceCapsuleExcel(@Valid OrderSpaceCapsulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderSpaceCapsuleDO> list = orderSpaceCapsuleService.getOrderSpaceCapsulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "太空舱订单.xls", "数据", OrderSpaceCapsuleRespVO.class,
                        BeanUtils.toBean(list, OrderSpaceCapsuleRespVO.class));
    }

}