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

import cn.iocoder.byzs.module.bjdx.controller.admin.courselabel.vo.CourseLabelPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courselabel.vo.CourseLabelRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courselabel.vo.CourseLabelSaveReqVO;
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.courselabel.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.courselabel.CourseLabelDO;
import cn.iocoder.byzs.module.bjdx.service.courselabel.CourseLabelService;
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-label")
@Validated
public class CourseLabelController {

    @Resource
    private CourseLabelService courseLabelService;

    @PostMapping("/create")
    @Operation(summary = "创建课程-标签")
    @PreAuthorize("@ss.hasPermission('bjdx:course-label:create')")
    public CommonResult<Long> createCourseLabel(@Valid @RequestBody CourseLabelSaveReqVO createReqVO) {
        return success(courseLabelService.createCourseLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程-标签")
    @PreAuthorize("@ss.hasPermission('bjdx:course-label:update')")
    public CommonResult<Boolean> updateCourseLabel(@Valid @RequestBody CourseLabelSaveReqVO updateReqVO) {
        courseLabelService.updateCourseLabel(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得课程-标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:course-label:query')")
    public CommonResult<CourseLabelRespVO> getCourseLabel(@RequestParam("id") Long id) {
        CourseLabelDO courseLabel = courseLabelService.getCourseLabel(id);
        return success(BeanUtils.toBean(courseLabel, CourseLabelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程-标签分页")
    @PreAuthorize("@ss.hasPermission('bjdx:course-label:query')")
    public CommonResult<PageResult<CourseLabelRespVO>> getCourseLabelPage(@Valid CourseLabelPageReqVO pageReqVO) {
        PageResult<CourseLabelDO> pageResult = courseLabelService.getCourseLabelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseLabelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程-标签 Excel")
    @PreAuthorize("@ss.hasPermission('bjdx:course-label:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseLabelExcel(@Valid CourseLabelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseLabelDO> list = courseLabelService.getCourseLabelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程-标签.xls", "数据", CourseLabelRespVO.class,
                        BeanUtils.toBean(list, CourseLabelRespVO.class));
    }

}