package cn.iocoder.byzs.module.blockly.controller.admin.blockly;

import cn.iocoder.byzs.module.blockly.controller.admin.blockly.vo.BlocklyPageReqVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blockly.vo.BlocklyRespVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blockly.vo.BlocklySaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;

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

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

import cn.iocoder.byzs.module.blockly.controller.admin.blockly.vo.*;
import cn.iocoder.byzs.module.blockly.dal.dataobject.blockly.BlocklyDO;
import cn.iocoder.byzs.module.blockly.service.blockly.BlocklyService;
import static cn.iocoder.byzs.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 课程")
@RestController
@RequestMapping("/blockly/blockly")
@Validated
public class BlocklyController {

    @Resource
    private BlocklyService blocklyService;

    @PostMapping("/create")
    @Operation(summary = "创建课程")
    @PreAuthorize("@ss.hasPermission('blockly:blockly:create')")
    public CommonResult<Long> createBlockly(@Valid @RequestBody BlocklySaveReqVO createReqVO) {
        return success(blocklyService.createBlockly(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程")
    @PreAuthorize("@ss.hasPermission('blockly:blockly:update')")
    public CommonResult<Boolean> updateBlockly(@Valid @RequestBody BlocklySaveReqVO updateReqVO) {
        blocklyService.updateBlockly(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blockly:blockly:delete')")
    public CommonResult<Boolean> deleteBlockly(@RequestParam("id") Long id) {
        blocklyService.deleteBlockly(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程")
    @PreAuthorize("@ss.hasPermission('blockly:blockly:delete')")
    public CommonResult<Boolean> deleteBlocklyList(@RequestParam("ids") List<Long> ids) {
        blocklyService.deleteBlocklyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blockly:blockly:query')")
    public CommonResult<BlocklyRespVO> getBlockly(@RequestParam("id") Long id) {
        BlocklyDO blockly = blocklyService.getBlockly(id);
        return success(BeanUtils.toBean(blockly, BlocklyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程分页")
    @PreAuthorize("@ss.hasPermission('blockly:blockly:query')")
    public CommonResult<PageResult<BlocklyRespVO>> getBlocklyPage(@Valid BlocklyPageReqVO pageReqVO) {
        PageResult<BlocklyRespVO> pageResult = blocklyService.getBlocklyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BlocklyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程 Excel")
    @PreAuthorize("@ss.hasPermission('blockly:blockly:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBlocklyExcel(@Valid BlocklyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BlocklyRespVO> list = blocklyService.getBlocklyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程.xls", "数据", BlocklyRespVO.class,list);
    }

}