package com.fs.survey;

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.survey.domain.SurveyQuestionnaireVersion;
import com.fs.survey.service.ISurveyQuestionnaireVersionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问卷版本Controller
 *
 */
@RestController
@RequestMapping("/survey/version")
public class SurveyVersionController extends BaseController {
    @Autowired
    private ISurveyQuestionnaireVersionService versionService;

    /**
     * 查询问卷版本列表
     */
    @PreAuthorize("@ss.hasPermi('survey:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurveyQuestionnaireVersion version) {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        version.setCompanyId(currentCompanyId);
        List<SurveyQuestionnaireVersion> list = versionService.selectSurveyQuestionnaireVersionList(version);
        return getDataTable(list);
    }

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

    /**
     * 获取当前最新问卷版本
     * @param id 问卷ID
     * @return 最新问卷版本
     */
    @GetMapping(value = "/getLatestInfo")
    public AjaxResult getLatestInfo(@Param("id") Long id) {
        return AjaxResult.success(versionService.getLatestInfo(id));
    }

    /**
     * 新增/保存问卷版本
     */
    @PreAuthorize("@ss.hasPermi('survey:version:add')")
    @Log(title = "问卷版本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurveyQuestionnaireVersion version) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        version.setCompanyId(currentCompanyId);
        return AjaxResult.success(versionService.saveVersion(version));
    }

    /**
     * 修改问卷版本
     */
    @PreAuthorize("@ss.hasPermi('survey:version:edit')")
    @Log(title = "问卷版本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurveyQuestionnaireVersion version) {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        version.setCompanyId(currentCompanyId);
        return toAjax(versionService.updateSurveyQuestionnaireVersion(version));
    }

    /**
     * 发布版本
     */
    @PreAuthorize("@ss.hasPermi('survey:version:publish')")
    @Log(title = "发布版本", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable Long id) {
        return toAjax(versionService.companyPublishVersion(id));
    }

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