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

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.bjdx.controller.admin.coursetype.vo.CourseTypeListReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursetype.vo.CourseTypeRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursetype.vo.CourseTypeSaveReqVO;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.coursetype.CourseTypeDO;
import cn.iocoder.byzs.module.bjdx.service.coursetype.CourseTypeService;
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-type")
@Validated
public class CourseTypeController {

    @Resource
    private CourseTypeService courseTypeService;

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

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

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


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

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

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

}