package com.rongwei.training.controller;

import com.rongwei.rwcommon.base.R;
import com.rongwei.training.vo.PreviewTrainDemandVo;
import com.rongwei.training.vo.TrainingDemandVo;
import com.rongwei.trainingcommon.sys.service.impl.ExportTrainingDemandServiceImpl;
import com.rongwei.trainingcommon.sys.service.impl.TrainingDemandServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * TrainingDemandController class
 *
 * @author XH
 * @date 2023/11/23
 */
@RestController
@RequestMapping("/train/demand")
public class TrainingDemandController {
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private TrainingDemandServiceImpl trainingDemandService;
    @Autowired
    private ExportTrainingDemandServiceImpl exportTrainingDemandService;

    /**
     * 发布需求调查
     *
     * @param parameter
     * @return
     */
    @PostMapping("/release")
    public R releaseTrainDemand(@RequestBody Map<String, Object> parameter) {
        log.info("开发发布需求调查问卷");
        return trainingDemandService.releaseTrainDemand(parameter);
    }

    /**
     * 培训需求预览
     *
     * @param previewTrainDemandVo
     * @return
     */
    @PostMapping("/preview")
    public R previewTrainDemand(@RequestBody PreviewTrainDemandVo previewTrainDemandVo) {
        log.info("开发发布需求调查问卷");
        return trainingDemandService.previewTrainDemand(previewTrainDemandVo);
    }

    /**
     * 培训需求预览
     *
     * @param previewTrainDemandVo
     * @return
     */
    @GetMapping("/released/preview")
    public R releasedPreviewTrainDemand(@RequestParam(name = "surveyId") String surveyId) {
        log.info("开发发布需求调查问卷");
        return trainingDemandService.releasedPreviewTrainDemand(surveyId);
    }

    /**
     * 获取需要用户参加的问卷调查
     *
     * @return
     */
    @GetMapping("/list")
    public R getInfoByUser(@RequestParam(name = "userId", required = false) String userId) {
        log.info("开发发布需求调查问卷");
        return trainingDemandService.getInfoByUser(userId);
    }

    /**
     * 展示问卷调查
     *
     * @param surveyId 问卷调查ID
     * @return
     */
    @GetMapping("/show")
    public R showQuestionnaire(@RequestParam(name = "surveyId") String surveyId,
                               @RequestParam(name = "showAnswer") boolean showAnswer) {
        log.info("开发发布需求调查问卷");
        return trainingDemandService.showQuestionnaire(surveyId, showAnswer);
    }

    @PostMapping("/submit")
    public R submit(@RequestBody List<TrainingDemandVo> trainingDemandVoList) {
        log.info("开发发布需求调查问卷");
        return trainingDemandService.submit(trainingDemandVoList);
    }

    @PostMapping("/paper")
    public void generatePaper(@RequestParam(name = "surveyId") String surveyId,
                              HttpServletResponse response) {
        log.info("开始生成培训问卷");
        exportTrainingDemandService.generatePaper(surveyId, response);
    }

    @PostMapping("/user/paper")
    public void generateUserPaper(@RequestParam(name = "surveyId") String surveyId,
                                  @RequestParam(name = "userId") String userId,
                                  HttpServletResponse response) {
        log.info("开始生成用户培训问卷");
        exportTrainingDemandService.generateUserPaper(surveyId, userId, response);
    }

    @PostMapping("/records")
    public void generateRecord(@RequestParam(name = "surveyId") String surveyId,
                               HttpServletResponse response) {
        log.info("开始生成培训问卷");
        exportTrainingDemandService.generateRecord(surveyId, response);
    }
}
