package com.fs.survey;

import java.util.List;

import com.fs.common.annotation.Log;
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.SurveyFieldConfig;
import com.fs.survey.service.ISurveyFieldConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 字段配置Controller
 *
 * 管理问卷字段的增删改查操作
 *
 */
@RestController
@RequestMapping("/survey/fieldConfig")
public class SurveyFieldConfigController extends BaseController {

    @Autowired
    private ISurveyFieldConfigService fieldConfigService;

    /**
     * 查询字段配置列表
     */
    @GetMapping("/list")
    public AjaxResult list(SurveyFieldConfig fieldConfig) {
        List<SurveyFieldConfig> list = fieldConfigService.selectSurveyFieldConfigList(fieldConfig);
        return AjaxResult.success(list);
    }

    /**
     * 根据ID获取字段配置详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(fieldConfigService.selectSurveyFieldConfigById(id));
    }

    /**
     * 根据问卷版本ID查询字段配置信息列表
     */
    @GetMapping("/byVersion/{versionId}")
    public AjaxResult getByVersionId(@PathVariable Long versionId) {
        List<SurveyFieldConfig> list = fieldConfigService.selectFieldConfigByVersionId(versionId);
        return AjaxResult.success(list);
    }

    /**
     * 新增字段配置
     */
    @Log(title = "字段配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurveyFieldConfig fieldConfig) {
        int row = fieldConfigService.insertSurveyFieldConfig(fieldConfig);
        return toAjax(row);
    }

    /**
     * 修改字段配置
     */
    @Log(title = "字段配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurveyFieldConfig fieldConfig) {
        int row = fieldConfigService.updateSurveyFieldConfig(fieldConfig);
        return toAjax(row);
    }

    /**
     * 删除字段配置，支持批量ID
     */
    @Log(title = "字段配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int row = fieldConfigService.deleteSurveyFieldConfigByIds(ids);
        return toAjax(row);
    }

    /**
     * 根据版本ID批量删除字段配置
     */
    @Log(title = "字段配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/byVersion/{versionId}")
    public AjaxResult removeByVersionId(@PathVariable Long versionId) {
        int row = fieldConfigService.deleteSurveyFieldConfigByVersionId(versionId);
        return toAjax(row);
    }
}
