package cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseType;

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.aicourse.controller.admin.aiCourseType.vo.AiCourseTypeListReqVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseType.vo.AiCourseTypeRespVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseType.vo.AiCourseTypeSaveReqVO;
import cn.iocoder.byzs.module.aicourse.dal.dataobject.aiCourseType.AiCourseTypeDO;
import cn.iocoder.byzs.module.aicourse.service.aiCourseType.AiCourseTypeService;
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("/aiCourse/course-type")
@Validated
public class AiCourseTypeController {

    @Resource
    private AiCourseTypeService courseTypeService;

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

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

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


    @GetMapping("/get")
    @Operation(summary = "获得课程-类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-type:query')")
    public CommonResult<AiCourseTypeRespVO> getAiCourseType(@RequestParam("id") Long id) {
        AiCourseTypeDO aiCourseType = courseTypeService.getAiCourseType(id);
        return success(BeanUtils.toBean(aiCourseType, AiCourseTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程-类型列表")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-type:query')")
    public CommonResult<List<AiCourseTypeRespVO>> getAiCourseTypeList(@Valid AiCourseTypeListReqVO listReqVO) {
        List<AiCourseTypeDO> list = courseTypeService.getAiCourseTypeList(listReqVO);
        return success(BeanUtils.toBean(list, AiCourseTypeRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得课程-类型列表")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-type:query')")
    public CommonResult<List<AiCourseTypeRespVO>> getAiCourseTypeSimpleList(@Valid AiCourseTypeListReqVO listReqVO) {
        List<AiCourseTypeDO> list = courseTypeService.getAiCourseTypeSimpleList(listReqVO);
        return success(BeanUtils.toBean(list, AiCourseTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程-类型 Excel")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAiCourseTypeExcel(@Valid AiCourseTypeListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<AiCourseTypeDO> list = courseTypeService.getAiCourseTypeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "课程-类型.xls", "数据", AiCourseTypeRespVO.class,
                        BeanUtils.toBean(list, AiCourseTypeRespVO.class));
    }

}