package cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion;

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.bjdx.controller.admin.coursequestion.vo.CourseQuestionPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionSaveReqVO;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.coursequestion.CourseQuestionDO;
import cn.iocoder.byzs.module.bjdx.service.coursequestion.CourseQuestionService;
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("/bjdx/course-question")
@Validated
public class CourseQuestionController {

    @Resource
    private CourseQuestionService courseQuestionService;

    @PostMapping("/create")
    @Operation(summary = "创建课程-试题")
    @PreAuthorize("@ss.hasPermission('bjdx:course-question:create')")
    public CommonResult<Long> createCourseQuestion(@Valid @RequestBody CourseQuestionSaveReqVO createReqVO) {
        return success(courseQuestionService.createCourseQuestion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程-试题")
    @PreAuthorize("@ss.hasPermission('bjdx:course-question:update')")
    public CommonResult<Boolean> updateCourseQuestion(@Valid @RequestBody CourseQuestionSaveReqVO updateReqVO) {
        courseQuestionService.updateCourseQuestion(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得课程-试题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:course-question:query')")
    public CommonResult<CourseQuestionRespVO> getCourseQuestion(@RequestParam("id") Long id) {
        CourseQuestionDO courseQuestion = courseQuestionService.getCourseQuestion(id);
        return success(BeanUtils.toBean(courseQuestion, CourseQuestionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程-试题分页")
    @PreAuthorize("@ss.hasPermission('bjdx:course-question:query')")
    public CommonResult<PageResult<CourseQuestionRespVO>> getCourseQuestionPage(@Valid CourseQuestionPageReqVO pageReqVO) {
        PageResult<CourseQuestionDO> pageResult = courseQuestionService.getCourseQuestionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseQuestionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程-试题 Excel")
    @PreAuthorize("@ss.hasPermission('bjdx:course-question:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseQuestionExcel(@Valid CourseQuestionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseQuestionDO> list = courseQuestionService.getCourseQuestionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程-试题.xls", "数据", CourseQuestionRespVO.class,
                        BeanUtils.toBean(list, CourseQuestionRespVO.class));
    }

}