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

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyConfig.vo.BlocklyConfigPageReqVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyConfig.vo.BlocklyConfigRespVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyConfig.vo.BlocklyConfigSaveReqVO;
import cn.iocoder.byzs.module.blockly.dal.dataobject.blocklyConfig.BlocklyConfigDO;
import cn.iocoder.byzs.module.blockly.service.blocklyConfig.BlocklyConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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-config")
@Validated
public class BlocklyConfigController {

    @Resource
    private BlocklyConfigService blocklyConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建课程配置")
    @PreAuthorize("@ss.hasPermission('blockly:course-config:create')")
    public CommonResult<Long>createBlocklyConfig(@Valid @RequestBody BlocklyConfigSaveReqVO createReqVO) {
        return success(blocklyConfigService.createBlocklyConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程配置")
    @PreAuthorize("@ss.hasPermission('blockly:course-config:update')")
    public CommonResult<Boolean> updateBlocklyConfig(@Valid @RequestBody BlocklyConfigSaveReqVO updateReqVO) {
        blocklyConfigService.updateBlocklyConfig(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得课程配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blockly:course-config:query')")
    public CommonResult<BlocklyConfigRespVO> getBlocklyConfig(@RequestParam("id") Long id) {
        BlocklyConfigDO blocklyConfig = blocklyConfigService.getBlocklyConfig(id);
        return success(BeanUtils.toBean(blocklyConfig, BlocklyConfigRespVO.class));
    }

    @GetMapping("/getConfigQuest")
    @Operation(summary = "获得课程配置试题列表")
    @Parameter(name = "ccCourseId", description = "课程id", required = true, example = "1025")
    @PreAuthorize("@ss.hasPermission('blockly:course-config:query')")
    public CommonResult<List<BlocklyConfigRespVO>> getBlocklyConfigQuestion(@RequestParam("ccCourseId") Long ccCourseId) {
        List<BlocklyConfigRespVO> blocklyConfigList = blocklyConfigService.getBlocklyConfigQuestion(ccCourseId);
        return success(blocklyConfigList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程配置分页")
    @PreAuthorize("@ss.hasPermission('blockly:course-config:query')")
    public CommonResult<PageResult<BlocklyConfigPageReqVO>> getBlocklyConfigPage(@Valid BlocklyConfigPageReqVO pageReqVO) {
        PageResult<BlocklyConfigPageReqVO> pageResult = blocklyConfigService.getBlocklyConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BlocklyConfigPageReqVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程配置 Excel")
    @PreAuthorize("@ss.hasPermission('blockly:course-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBlocklyConfigExcel(@Valid BlocklyConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BlocklyConfigPageReqVO> list = blocklyConfigService.getBlocklyConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程配置.xls", "数据", BlocklyConfigPageReqVO.class,
                        BeanUtils.toBean(list, BlocklyConfigPageReqVO.class));
    }

}