package com.yiqi.web.controller.system;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.UrlConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.system.domain.SysDocument;
import com.yiqi.system.service.ISysDocumentService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 系统文档Controller
 *
 * @author xiaohei
 * @date 2023-09-01
 */
@Api(tags = "管理后台 - 系统文档")
@RestController
@RequestMapping(UrlConstants.managerApi + "/system/document")
public class SysDocumentController extends BaseController {
    @Autowired
    private ISysDocumentService sysDocumentService;

    /**
     * 查询系统文档列表
     */
    @PreAuthorize("@ss.hasPermi('system:document:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询系统文档列表")
    public TableDataInfo list(SysDocument sysDocument) {
        startPage();
        List<SysDocument> list = sysDocumentService.selectSysDocumentList(sysDocument);
        return getDataTable(list);
    }

    /**
     * 导出系统文档列表
     */
    @PreAuthorize("@ss.hasPermi('system:document:export')")
    @Log(title = "系统文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出系统文档列表")
    public void export(HttpServletResponse response, SysDocument sysDocument) {
        List<SysDocument> list = sysDocumentService.selectSysDocumentList(sysDocument);
        ExcelUtil<SysDocument> util = new ExcelUtil<SysDocument>(SysDocument. class);
        util.exportExcel(response, list, "系统文档数据");
    }

    /**
     * 获取系统文档详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:document:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取系统文档详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysDocumentService.selectSysDocumentById(id));
    }

    /**
     * 获取系统文档详细信息
     */
    @GetMapping(value = "/getInfoByCode")
    @ApiOperation(value = "获取系统文档详细信息")
    public AjaxResult getInfoByCode(String code) {
        return success(sysDocumentService.getOne(new QueryWrapper<SysDocument>().lambda().eq(SysDocument::getCode, code), false));
    }

    /**
     * 新增系统文档
     */
    @PreAuthorize("@ss.hasPermi('system:document:add')")
    @Log(title = "系统文档", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增系统文档")
    public AjaxResult add(@RequestBody SysDocument sysDocument) {
        sysDocument.setCreateById(getUserId());
        sysDocument.setCreateBy(getUsername());
        return toAjax(sysDocumentService.insertSysDocument(sysDocument));
    }

    /**
     * 修改系统文档
     */
    @PreAuthorize("@ss.hasPermi('system:document:edit')")
    @Log(title = "系统文档", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改系统文档")
    public AjaxResult edit(@RequestBody SysDocument sysDocument) {
        sysDocument.setUpdateById(getUserId());
        sysDocument.setUpdateBy(getUsername());
        return toAjax(sysDocumentService.updateSysDocument(sysDocument));
    }

    /**
     * 删除系统文档
     */
    @PreAuthorize("@ss.hasPermi('system:document:remove')")
    @Log(title = "系统文档", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除系统文档")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysDocumentService.deleteSysDocumentByIds(ids));
    }

}
