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

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.constraints.*;
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 static cn.iocoder.byzs.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.byzs.module.bjdx.controller.admin.questionnaireresult.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.questionnaireresult.QuestionnaireResultDO;
import cn.iocoder.byzs.module.bjdx.service.questionnaireresult.QuestionnaireResultService;

@Tag(name = "管理后台 - 北京大学-问卷结果")
@RestController
@RequestMapping("/bjdxResult/questionnaire-result")
@Validated
public class QuestionnaireResultController {

    @Resource
    private QuestionnaireResultService questionnaireResultService;

    @PostMapping("/create")
    @Operation(summary = "创建北京大学-问卷结果")
    @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:create')")
    public CommonResult<Long> createQuestionnaireResult(@Valid @RequestBody QuestionnaireResultSaveReqVO createReqVO) {
        return success(questionnaireResultService.createQuestionnaireResult(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新北京大学-问卷结果")
    @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:update')")
    public CommonResult<Boolean> updateQuestionnaireResult(@Valid @RequestBody QuestionnaireResultSaveReqVO updateReqVO) {
        questionnaireResultService.updateQuestionnaireResult(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除北京大学-问卷结果")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:delete')")
    public CommonResult<Boolean> deleteQuestionnaireResult(@RequestParam("id") Long id) {
        questionnaireResultService.deleteQuestionnaireResult(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除北京大学-问卷结果")
                @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:delete')")
    public CommonResult<Boolean> deleteQuestionnaireResultList(@RequestParam("ids") List<Long> ids) {
        questionnaireResultService.deleteQuestionnaireResultListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得北京大学-问卷结果")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:query')")
    public CommonResult<QuestionnaireResultRespVO> getQuestionnaireResult(@RequestParam("id") Long id) {
        QuestionnaireResultDO questionnaireResult = questionnaireResultService.getQuestionnaireResult(id);
        return success(BeanUtils.toBean(questionnaireResult, QuestionnaireResultRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得北京大学-问卷结果分页")
    @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:query')")
    public CommonResult<PageResult<QuestionnaireResultRespVO>> getQuestionnaireResultPage(@Valid QuestionnaireResultPageReqVO pageReqVO) {
        PageResult<QuestionnaireResultDO> pageResult = questionnaireResultService.getQuestionnaireResultPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionnaireResultRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出北京大学-问卷结果 Excel")
    @PreAuthorize("@ss.hasPermission('bjdxResult:questionnaire-result:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuestionnaireResultExcel(@Valid QuestionnaireResultPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuestionnaireResultDO> list = questionnaireResultService.getQuestionnaireResultPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "北京大学-问卷结果.xls", "数据", QuestionnaireResultRespVO.class,
                        BeanUtils.toBean(list, QuestionnaireResultRespVO.class));
    }

}