package com.xinelu.manage.controller.termbank;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.termbank.TermBank;
import com.xinelu.manage.service.termbank.ITermBankService;
import com.xinelu.manage.vo.termbank.TermBankTreeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 常用术语(知识库)Controller
 *
 * @author xinelu
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/system/bank")
public class TermBankController extends BaseController {
    @Resource
    private ITermBankService termBankService;

    /**
     * 查询常用术语(知识库)列表
     */
    @PreAuthorize("@ss.hasPermi('system:bank:list')")
    @GetMapping("/list")
    public TableDataInfo list(TermBank termBank) {
        startPage();
        List<TermBank> list = termBankService.list(termBank);
        return getDataTable(list);
    }

    @GetMapping("/selectTermBankList")
    public AjaxResult selectTermBankList(TermBank termBank) {
        return AjaxResult.success(termBankService.selectTermBankList(termBank));
    }

    /**
     * 导出常用术语(知识库)列表
     */
    @PreAuthorize("@ss.hasPermi('system:bank:export')")
    @Log(title = "常用术语(知识库)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TermBank termBank) {
        List<TermBank> list = termBankService.selectTermBankList(termBank);
        ExcelUtil<TermBank> util = new ExcelUtil<>(TermBank.class);
        util.exportExcel(response, list, "常用术语(知识库)数据");
    }

    /**
     * 获取常用术语(知识库)详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bank:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(termBankService.selectTermBankById(id));
    }

    /**
     * 新增常用术语(知识库)
     */
    @PreAuthorize("@ss.hasPermi('system:bank:add')")
    @Log(title = "常用术语(知识库)", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TermBank termBank) {
        if (StringUtils.isBlank(termBank.getTermContent())) {
            return AjaxResult.success();
        }
        return termBankService.insertTermBank(termBank);
    }

    /**
     * 修改常用术语(知识库)
     */
    @PreAuthorize("@ss.hasPermi('system:bank:edit')")
    @Log(title = "常用术语(知识库)", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TermBank termBank) {
        return termBankService.updateTermBank(termBank);
    }

    /**
     * 删除常用术语(知识库)
     */
    @PreAuthorize("@ss.hasPermi('system:bank:remove')")
    @Log(title = "常用术语(知识库)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(termBankService.deleteTermBankByIds(ids));
    }

    /**
     * 常用术语层级树图
     */
    @GetMapping("/bankLevel")
    public List<TermBankTreeVO> bankLevel(Long level) {
        return termBankService.bankLevel(level);
    }
}
