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.core.text.Convert;
import com.nasc.common.enums.BusinessType;
import com.nasc.custom.domain.NascMatter;
import com.nasc.custom.service.INascMatterService;
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/matter")
public class NascMatterController extends BaseController {

    @Autowired
    private INascMatterService matterService;

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

    /**
     * @Author: Yujianq
     * @Date: 2024/6/6 16:52
     * @Description: 按条件查询所有（不分页）
     * @param matter
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:item:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(NascMatter matter) {
        List<NascMatter> list = matterService.selectMatterList(matter);
        return AjaxResult.success(list);
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 10:17
     * @Description: 新增审查事项
     * @param matter
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:item:add')")
    @Log(title = "审查事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody NascMatter matter) {
        if (matterService.checkMatterDuplicates(matter) > 0) {
            return error("添加审查事项'" + matter.getItemName() + "'失败，该审查事项已存在！");
        }
        matter.setCreateBy(Convert.toStr(getUserId()));
        return toAjax(matterService.insertMatter(matter));
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 14:22
     * @Description: 修改审查事项
     * @param matter
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:item:edit')")
    @Log(title = "审查事项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody NascMatter matter) {
        if (matterService.checkMatterDuplicates(matter) > 0) {
            return error("修改审查事项'" + matter.getItemName() + "'失败，该审查事项已存在！");
        }
        matter.setUpdateBy(Convert.toStr(getUserId()));
        return toAjax(matterService.updateMatter(matter));
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/5 17:25
     * @Description: 批量根据事项ID删除事项信息
     * @param ids
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:item:remove')")
    @Log(title = "审查事项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(matterService.deleteMatterByIds(ids));
    }

    /**
     * @Author: Yujianq
     * @Date: 2024/6/6 10:06
     * @Description: 根据审查事项ID查询详情
     * @param id
     * @return: com.nasc.common.core.domain.AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('matter:item:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id) {
        return AjaxResult.success(matterService.getMatterInfo(id));
    }

}
