package com.fs.app.controller;

import com.fs.common.annotation.Log;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.survey.domain.SurveyQuestionnaire;
import com.fs.survey.service.ISurveyQuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 问卷Controller
 *
 */
@RestController
@RequestMapping("/survey/questionnaire")
public class SurveyQuestionnaireController extends BaseController {
    @Autowired
    private ISurveyQuestionnaireService surveyQuestionnaireService;

    /**
     * 查询问卷列表
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurveyQuestionnaire surveyQuestionnaire) {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        surveyQuestionnaire.setCompanyId(currentCompanyId);
        List<SurveyQuestionnaire> list = surveyQuestionnaireService.selectSurveyQuestionnaireList(surveyQuestionnaire);
        return getDataTable(list);
    }

    /**
     * 导出问卷列表
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:export')")
    @Log(title = "问卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurveyQuestionnaire surveyQuestionnaire) throws IOException {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        surveyQuestionnaire.setCompanyId(currentCompanyId);
        List<SurveyQuestionnaire> list = surveyQuestionnaireService.selectSurveyQuestionnaireList(surveyQuestionnaire);
        ExcelUtil<SurveyQuestionnaire> util = new ExcelUtil<SurveyQuestionnaire>(SurveyQuestionnaire.class);
        util.exportExcel(response, list, "问卷数据");
    }

    /**
     * 获取问卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(surveyQuestionnaireService.selectSurveyQuestionnaireById(id));
    }

    /**
     * 新增问卷
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:add')")
    @Log(title = "问卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurveyQuestionnaire surveyQuestionnaire) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        surveyQuestionnaire.setCompanyId(currentCompanyId);
        return toAjax(surveyQuestionnaireService.insertSurveyQuestionnaire(surveyQuestionnaire));
    }

    /**
     * 修改问卷
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:edit')")
    @Log(title = "问卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurveyQuestionnaire surveyQuestionnaire) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        surveyQuestionnaire.setCompanyId(currentCompanyId);
        return toAjax(surveyQuestionnaireService.updateSurveyQuestionnaire(surveyQuestionnaire));
    }

    /**
     * 删除问卷
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:remove')")
    @Log(title = "问卷", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(surveyQuestionnaireService.deleteSurveyQuestionnaireByIds(ids));
    }

    /**
     * 发布问卷
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:publish')")
    @Log(title = "发布问卷", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{id}/{versionId}")
    public AjaxResult publish(@PathVariable Long id, @PathVariable Long versionId) {
        return toAjax(surveyQuestionnaireService.publishQuestionnaire(id, versionId));
    }

    /**
     * 停用问卷
     */
    @PreAuthorize("@ss.hasPermi('survey:questionnaire:disable')")
    @Log(title = "停用问卷", businessType = BusinessType.UPDATE)
    @PutMapping("/disable/{id}")
    public AjaxResult disable(@PathVariable Long id) {
        return toAjax(surveyQuestionnaireService.disableQuestionnaire(id));
    }

    /**
     * 获取公司定级申请问卷（不需要参数）
     */
    @GetMapping("/getCompanyDoctorLevelApplyQuestionnaire")
    public AjaxResult getCompanyDoctorLevelApplyQuestionnaire(Long companyId ) {
        return AjaxResult.success(surveyQuestionnaireService.getCompanyDoctorLevelApplyQuestionnaire(companyId));

    }



}
