package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.Knowledge;
import com.ruoyi.web.work.service.IKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 知识库管理
 *
 * @author lsw
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/work/knowledge")
public class KnowledgeController extends BaseController {
    @Autowired
    private IKnowledgeService knowledgeService;

    @PreAuthorize("@ss.hasPermi('work:knowledge:list')")
    @GetMapping("/list")
    public TableDataInfo list(Knowledge knowledge) {
        startPage();
        List<Knowledge> list = knowledgeService.selectList(knowledge);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('work:knowledge:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(knowledgeService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:knowledge:add')")
    @Log(title = "知识库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Knowledge knowledge) {
        return toAjax(knowledgeService.save(knowledge));
    }

    @PreAuthorize("@ss.hasPermi('work:knowledge:edit')")
    @Log(title = "知识库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Knowledge knowledge) {
        return toAjax(knowledgeService.updateById(knowledge));
    }

    @PreAuthorize("@ss.hasPermi('work:knowledge:remove')")
    @Log(title = "知识库管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(knowledgeService.removeByIds(Arrays.asList(ids)));
    }
}
