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

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.coursequestoption.vo.CourseQuestOptionPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestoption.vo.CourseQuestOptionRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestoption.vo.CourseQuestOptionSaveReqVO;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.coursequestoption.CourseQuestOptionDO;
import cn.iocoder.byzs.module.bjdx.service.coursequestoption.CourseQuestOptionService;
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-quest-option")
@Validated
public class CourseQuestOptionController {

    @Resource
    private CourseQuestOptionService courseQuestOptionService;

    @PostMapping("/create")
    @Operation(summary = "创建课程-试题-选项")
    @PreAuthorize("@ss.hasPermission('bjdx:course-quest-option:create')")
    public CommonResult<Long> createCourseQuestOption(@Valid @RequestBody CourseQuestOptionSaveReqVO createReqVO) {
        return success(courseQuestOptionService.createCourseQuestOption(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程-试题-选项")
    @PreAuthorize("@ss.hasPermission('bjdx:course-quest-option:update')")
    public CommonResult<Boolean> updateCourseQuestOption(@Valid @RequestBody CourseQuestOptionSaveReqVO updateReqVO) {
        courseQuestOptionService.updateCourseQuestOption(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得课程-试题-选项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:course-quest-option:query')")
    public CommonResult<CourseQuestOptionRespVO> getCourseQuestOption(@RequestParam("id") Long id) {
        CourseQuestOptionDO courseQuestOption = courseQuestOptionService.getCourseQuestOption(id);
        return success(BeanUtils.toBean(courseQuestOption, CourseQuestOptionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程-试题-选项分页")
    @PreAuthorize("@ss.hasPermission('bjdx:course-quest-option:query')")
    public CommonResult<PageResult<CourseQuestOptionRespVO>> getCourseQuestOptionPage(@Valid CourseQuestOptionPageReqVO pageReqVO) {
        PageResult<CourseQuestOptionDO> pageResult = courseQuestOptionService.getCourseQuestOptionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseQuestOptionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程-试题-选项 Excel")
    @PreAuthorize("@ss.hasPermission('bjdx:course-quest-option:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseQuestOptionExcel(@Valid CourseQuestOptionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseQuestOptionDO> list = courseQuestOptionService.getCourseQuestOptionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程-试题-选项.xls", "数据", CourseQuestOptionRespVO.class,
                        BeanUtils.toBean(list, CourseQuestOptionRespVO.class));
    }

}