package com.nasc.web.controller.custom;

import com.nasc.common.annotation.Log;
import com.nasc.common.core.controller.BaseController;
import com.nasc.common.core.domain.AjaxResult;
import com.nasc.common.core.page.TableDataInfo;
import com.nasc.common.enums.BusinessType;
import com.nasc.custom.domain.NascPreQuestion;
import com.nasc.custom.service.INascQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Jianq Yu
 * @Date: 2024/6/4 16:23
 * @Description: 审查事项前置问题
 */
@RestController
@RequestMapping("/nasc/question")
public class NascQuestionController extends BaseController {

    @Autowired
    private INascQuestionService questionService;

    /**
     * @Author: Yujianq
     * @Date: 2024/6/6 14:16
     * @Description: 根据条件分页查询审查事项问题列表
     * @param question
     * @return: com.nasc.common.core.page.TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('matter:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(NascPreQuestion question) {
        startPage();
        List<NascPreQuestion> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/6 14:26
     * @Description: 新增审查事项前置问题信息
     * @param question
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:question:add')")
    @Log(title = "前置问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody NascPreQuestion question) {
        if (questionService.checkQuestionDuplicates(question) > 0) {
            return error("添加前置问题'" + question.getDesc() + "'失败，该前置问题已存在！");
        }
        return toAjax(questionService.insertPreQuestion(question));
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/6 14:38
     * @Description: 修改审查事项前置问题信息
     * @param question
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:question:edit')")
    @Log(title = "前置问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody NascPreQuestion question) {
        if (questionService.checkQuestionDuplicates(question) > 0) {
            return error("修改前置问题'" + question.getDesc() + "'失败，该前置问题已存在！");
        }
        return toAjax(questionService.updatePreQuestion(question));
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 17:25
     * @Description: 批量根据问题ID删除问题信息
     * @param id
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:question:remove')")
    @Log(title = "前置问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(questionService.deleteQuestionById(id));
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/6 14:53
     * @Description: 根据问题ID查询问题详细信息
     * @param id
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:question:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getQuestionInfo(@PathVariable(value = "id", required = false) Long id) {
        return AjaxResult.success(questionService.getQuestionInfo(id));
    }

}
