package com.fs.his.controller;

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.common.utils.DateUtils;
import com.fs.his.domain.FsNewcomerQuestionnaire;
import com.fs.his.service.NewcomerQuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 新人问卷调查（动态表单 JSON：formSchema）
 */
@RestController
@RequestMapping("/his/newcomerQuestionnaire")
public class FsNewcomerQuestionnaireController extends BaseController {

    @Autowired
    private NewcomerQuestionnaireService newcomerQuestionnaireService;

    @PreAuthorize("@ss.hasPermi('his:newcomerQuestionnaire:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsNewcomerQuestionnaire query) {
        startPage();
        List<FsNewcomerQuestionnaire> list = newcomerQuestionnaireService.selectList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('his:newcomerQuestionnaire:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(newcomerQuestionnaireService.selectById(id));
    }

    @PreAuthorize("@ss.hasPermi('his:newcomerQuestionnaire:add')")
    @Log(title = "新人问卷调查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsNewcomerQuestionnaire row) {
        row.setCreateTime(DateUtils.getNowDate());
        row.setUpdateTime(DateUtils.getNowDate());
        if (row.getSortOrder() == null) {
            row.setSortOrder(0);
        }
        if (row.getStatus() == null) {
            row.setStatus(1);
        }
        return toAjax(newcomerQuestionnaireService.insert(row));
    }

    @PreAuthorize("@ss.hasPermi('his:newcomerQuestionnaire:edit')")
    @Log(title = "新人问卷调查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsNewcomerQuestionnaire row) {
        row.setUpdateTime(DateUtils.getNowDate());
        return toAjax(newcomerQuestionnaireService.update(row));
    }

    @PreAuthorize("@ss.hasPermi('his:newcomerQuestionnaire:remove')")
    @Log(title = "新人问卷调查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(newcomerQuestionnaireService.deleteByIds(ids));
    }

    /**
     * 当前 App 将使用的问卷（与系统参数 newcomer.welfare.questionnaire_id 及启用状态解析逻辑一致）
     */
    @PreAuthorize("@ss.hasPermi('his:newcomerQuestionnaire:query')")
    @GetMapping("/appActive")
    public AjaxResult appActive() {
        return AjaxResult.success(newcomerQuestionnaireService.resolveForApp());
    }
}
