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

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.questionnaire.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.questionnaire.QuestionnaireDO;
import cn.iocoder.byzs.module.bjdx.service.questionnaire.QuestionnaireService;

@Tag(name = "管理后台 - 问卷")
@RestController
@RequestMapping("/bjdx/questionnaire")
@Validated
public class QuestionnaireController {

    @Resource
    private QuestionnaireService questionnaireService;

    @PostMapping("/create")
    @Operation(summary = "创建问卷")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:create')")
    public CommonResult<Long> createQuestionnaire(@Valid @RequestBody QuestionnaireSaveReqVO createReqVO) {
        return success(questionnaireService.createQuestionnaire(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新问卷")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:update')")
    public CommonResult<Boolean> updateQuestionnaire(@Valid @RequestBody QuestionnaireSaveReqVO updateReqVO) {
        questionnaireService.updateQuestionnaire(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除问卷")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:delete')")
    public CommonResult<Boolean> deleteQuestionnaire(@RequestParam("id") Long id) {
        questionnaireService.deleteQuestionnaire(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除问卷")
                @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:delete')")
    public CommonResult<Boolean> deleteQuestionnaireList(@RequestParam("ids") List<Long> ids) {
        questionnaireService.deleteQuestionnaireListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得问卷")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:query')")
    public CommonResult<QuestionnaireRespVO> getQuestionnaire(@RequestParam("id") Long id) {
        QuestionnaireDO questionnaire = questionnaireService.getQuestionnaire(id);
        return success(BeanUtils.toBean(questionnaire, QuestionnaireRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得问卷分页")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:query')")
    public CommonResult<PageResult<QuestionnaireRespVO>> getQuestionnairePage(@Valid QuestionnairePageReqVO pageReqVO) {
        PageResult<QuestionnaireDO> pageResult = questionnaireService.getQuestionnairePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionnaireRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出问卷 Excel")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuestionnaireExcel(@Valid QuestionnairePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuestionnaireDO> list = questionnaireService.getQuestionnairePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "问卷.xls", "数据", QuestionnaireRespVO.class,
                        BeanUtils.toBean(list, QuestionnaireRespVO.class));
    }





    @GetMapping("/getConfigQuest")
    @Operation(summary = "获得问卷配置的试题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:query')")
    public CommonResult<PageResult<ConfigQuestionRespVO>> getConfigQuest(@Valid ConfigQuestionRespVO configQuestionRespVO) {
        PageResult<ConfigQuestionRespVO> configQuestPage = questionnaireService.getConfigQuestPage(configQuestionRespVO);
        return success(configQuestPage);
    }

    @GetMapping("/getConfigQuestNo")
    @Operation(summary = "获得问卷配置的未配置试题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:query')")
    public CommonResult<PageResult<ConfigQuestionRespVO>> getConfigQuestNo(@Valid ConfigQuestionRespVO configQuestionRespVO) {
        PageResult<ConfigQuestionRespVO> configQuestPage = questionnaireService.getConfigQuestNoPage(configQuestionRespVO);
        return success(configQuestPage);
    }

    @GetMapping("/addConfigQuest")
    @Operation(summary = "获得问卷配置的试题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:query')")
    public CommonResult<Boolean> addConfigQuest(@Valid ConfigQuestionRespVO configQuestionRespVO) {
        questionnaireService.addConfigQuest(configQuestionRespVO);
        return success(true);
    }

    @GetMapping("/delConfigQuest")
    @Operation(summary = "获得问卷配置的未配置试题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bjdx:questionnaire:query')")
    public CommonResult<Boolean> delConfigQuest(@Valid ConfigQuestionRespVO configQuestionRespVO) {
        questionnaireService.delConfigQuest(configQuestionRespVO);
        return success(true);
    }

}