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

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
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.module.blockly.controller.admin.blocklyType.vo.BlocklyTypeListReqVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyType.vo.BlocklyTypeRespVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyType.vo.BlocklyTypeSaveReqVO;
import cn.iocoder.byzs.module.blockly.dal.dataobject.blocklyType.BlocklyTypeDO;
import cn.iocoder.byzs.module.blockly.service.blocklyType.BlocklyTypeService;
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-type")
@Validated
public class BlocklyTypeController {

    @Resource
    private BlocklyTypeService blocklyTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建课程-类型")
    @PreAuthorize("@ss.hasPermission('blockly:blockly-type:create')")
    public CommonResult<Long> createBlocklyType(@Valid @RequestBody BlocklyTypeSaveReqVO createReqVO) {
        return success(blocklyTypeService.createBlocklyType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程-类型")
    @PreAuthorize("@ss.hasPermission('blockly:blockly-type:update')")
    public CommonResult<Boolean> updateBlocklyType(@Valid @RequestBody BlocklyTypeSaveReqVO updateReqVO) {
        blocklyTypeService.updateBlocklyType(updateReqVO);
        return success(true);
    }

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


    @GetMapping("/get")
    @Operation(summary = "获得课程-类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blockly:blockly-type:query')")
    public CommonResult<BlocklyTypeRespVO> getBlocklyType(@RequestParam("id") Long id) {
        BlocklyTypeDO blocklyType = blocklyTypeService.getBlocklyType(id);
        return success(BeanUtils.toBean(blocklyType, BlocklyTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程-类型列表")
    @PreAuthorize("@ss.hasPermission('blockly:blockly-type:query')")
    public CommonResult<List<BlocklyTypeRespVO>> getBlocklyTypeList(@Valid BlocklyTypeListReqVO listReqVO) {
        List<BlocklyTypeDO> list = blocklyTypeService.getBlocklyTypeList(listReqVO);
        return success(BeanUtils.toBean(list, BlocklyTypeRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得课程-类型列表")
    @PreAuthorize("@ss.hasPermission('blockly:blockly-type:query')")
    public CommonResult<List<BlocklyTypeRespVO>> getBlocklyTypeSimpleList(@Valid BlocklyTypeListReqVO listReqVO) {
        List<BlocklyTypeDO> list = blocklyTypeService.getBlocklyTypeSimpleList(listReqVO);
        return success(BeanUtils.toBean(list, BlocklyTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程-类型 Excel")
    @PreAuthorize("@ss.hasPermission('blockly:blockly-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBlocklyTypeExcel(@Valid BlocklyTypeListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<BlocklyTypeDO> list = blocklyTypeService.getBlocklyTypeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "课程-类型.xls", "数据", BlocklyTypeRespVO.class,
                        BeanUtils.toBean(list, BlocklyTypeRespVO.class));
    }

}