package com.ruoyi.web.controller.liandao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.query.LambdaQueryWrapperX;
import com.ruoyi.liandao.domain.QuestionnaireResult;
import com.ruoyi.liandao.domain.QuestionnaireSubmit;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireDto;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireResultPageReqDto;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireSubmitReqDto;
import com.ruoyi.liandao.service.QuestionnaireConfigService;
import com.ruoyi.liandao.service.QuestionnaireResultService;
import com.ruoyi.liandao.service.QuestionnaireSubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问卷结果接口
 *
 * @Author : chenzhengyu
 * @create 2024/3/19 9:02
 */
@RestController
@RequestMapping("/liandao/questionnaire/result")
public class QuestionnaireResultController extends BaseController {

    @Autowired
    private QuestionnaireConfigService configService;
    @Autowired
    private QuestionnaireSubmitService submitService;
    @Autowired
    private QuestionnaireResultService resultService;

    /**
     * 获取问卷
     *
     * @return
     */
    @GetMapping("/getQuestionnaire")
    public AjaxResult getQuestionnaire() {
        List<QuestionnaireDto> result = configService.getQuestionnaire();
        return success(result);
    }

    /**
     * 提交问卷
     *
     * @param dto
     * @return
     */
    @PostMapping("/submitQuestionnaire")
    public AjaxResult submitQuestionnaire(@RequestBody QuestionnaireSubmitReqDto dto) {
        submitService.submitQuestionnaire(dto.getQuestionnaireList());
        return success();
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public TableDataInfo page(QuestionnaireResultPageReqDto dto) {
        IPage<QuestionnaireSubmit> iPage = submitService.page((IPage<QuestionnaireSubmit>) dto.getPage(),
                new LambdaQueryWrapperX<QuestionnaireSubmit>()
                        .betweenIfPresent(QuestionnaireSubmit::getSubmitTime, dto.getStartTime(), dto.getEndTime())
                        .orderByDesc(QuestionnaireSubmit::getUpdateTime));
        return getDataTable(iPage);
    }

    /**
     * 获取问卷提交结果
     * @param id
     * @return
     */
    @GetMapping("/getResult")
    public AjaxResult getResult(@RequestParam Long id){
        List<QuestionnaireResult> result = resultService.list(new LambdaQueryWrapperX<QuestionnaireResult>()
                .eq(QuestionnaireResult::getSubmitId, id));
        return success(result);
    }

}
