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

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.aicourse.controller.admin.aiCourse.vo.AiCoursePageReqVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourse.vo.AiCourseRespVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourse.vo.AiCourseSaveReqVO;
import cn.iocoder.byzs.module.aicourse.dal.dataobject.aiCourse.AiCourseDO;
import cn.iocoder.byzs.module.aicourse.service.aiCourse.AiCourseService;
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")
@Validated
public class AiCourseController {

    @Resource
    private AiCourseService aiCourseService;

    @PostMapping("/create")
    @Operation(summary = "创建课程")
    @PreAuthorize("@ss.hasPermission('aiCourse:course:create')")
    public CommonResult<Long> createAiCourse(@Valid @RequestBody AiCourseSaveReqVO createReqVO) {
        return success(aiCourseService.createAiCourse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程")
    @PreAuthorize("@ss.hasPermission('aiCourse:course:update')")
    public CommonResult<Boolean> updateAiCourse(@Valid @RequestBody AiCourseSaveReqVO updateReqVO) {
        aiCourseService.updateAiCourse(updateReqVO);
        return success(true);
    }

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得课程分页")
    @PreAuthorize("@ss.hasPermission('aiCourse:course:query')")
    public CommonResult<PageResult<AiCourseRespVO>> getAiCoursePage(@Valid AiCoursePageReqVO pageReqVO) {
        PageResult<AiCourseRespVO> pageResult = aiCourseService.getAiCoursePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiCourseRespVO.class));
    }

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

}