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

import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;

import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.courseconfig.CourseConfigDO;
import cn.iocoder.byzs.module.bjdx.service.courseconfig.CourseConfigService;
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-config")
@Validated
public class CourseConfigController {

    @Resource
    private CourseConfigService courseConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建课程配置")
    @PreAuthorize("@ss.hasPermission('bjdx:course-config:create')")
    public CommonResult<Long>createCourseConfig(@Valid @RequestBody CourseConfigSaveReqVO createReqVO) {
        return success(courseConfigService.createCourseConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程配置")
    @PreAuthorize("@ss.hasPermission('bjdx:course-config:update')")
    public CommonResult<Boolean> updateCourseConfig(@Valid @RequestBody CourseConfigSaveReqVO updateReqVO) {
        courseConfigService.updateCourseConfig(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得课程配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:course-config:query')")
    public CommonResult<CourseConfigRespVO> getCourseConfig(@RequestParam("id") Long id) {
        CourseConfigDO courseConfig = courseConfigService.getCourseConfig(id);
        return success(BeanUtils.toBean(courseConfig, CourseConfigRespVO.class));
    }

    @GetMapping("/getConfigQuest")
    @Operation(summary = "获得课程配置试题列表")
    @Parameter(name = "ccCourseId", description = "课程id", required = true, example = "1025")
    @PreAuthorize("@ss.hasPermission('bjdx:course-config:query')")
    public CommonResult<List<CourseConfigRespVO>> getCourseConfigQuestion(@RequestParam("ccCourseId") Long ccCourseId) {
        List<CourseConfigRespVO> courseConfigList = courseConfigService.getCourseConfigQuestion(ccCourseId);
        return success(courseConfigList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程配置分页")
    @PreAuthorize("@ss.hasPermission('bjdx:course-config:query')")
    public CommonResult<PageResult<CourseConfigPageReqVO>> getCourseConfigPage(@Valid CourseConfigPageReqVO pageReqVO) {
        PageResult<CourseConfigPageReqVO> pageResult = courseConfigService.getCourseConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseConfigPageReqVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程配置 Excel")
    @PreAuthorize("@ss.hasPermission('bjdx:course-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseConfigExcel(@Valid CourseConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseConfigPageReqVO> list = courseConfigService.getCourseConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程配置.xls", "数据", CourseConfigPageReqVO.class,
                        BeanUtils.toBean(list, CourseConfigPageReqVO.class));
    }

}