package com.gxwebsoft.docs.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.docs.service.DocsService;
import com.gxwebsoft.docs.entity.Docs;
import com.gxwebsoft.docs.param.DocsParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 知识库控制器
 *
 * @author 科技小王子
 * @since 2025-03-26 09:29:15
 */
@Tag(name = "知识库管理")
@RestController
@RequestMapping("/api/docs/docs")
public class DocsController extends BaseController {
    @Resource
    private DocsService docsService;

    @PreAuthorize("hasAuthority('docs:docs:list')")
    @Operation(summary = "分页查询知识库")
    @GetMapping("/page")
    public ApiResult<PageResult<Docs>> page(DocsParam param) {
        // 使用关联查询
        return success(docsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('docs:docs:list')")
    @Operation(summary = "查询全部知识库")
    @GetMapping()
    public ApiResult<List<Docs>> list(DocsParam param) {
        // 使用关联查询
        return success(docsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('docs:docs:list')")
    @Operation(summary = "根据id查询知识库")
    @GetMapping("/{id}")
    public ApiResult<Docs> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(docsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('docs:docs:save')")
    @OperationLog
    @Operation(summary = "添加知识库")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Docs docs) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         docs.setUserId(loginUser.getUserId());
        }
        if (docsService.save(docs)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('docs:docs:update')")
    @OperationLog
    @Operation(summary = "修改知识库")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Docs docs) {
        if (docsService.updateById(docs)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('docs:docs:remove')")
    @OperationLog
    @Operation(summary = "删除知识库")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (docsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('docs:docs:save')")
    @OperationLog
    @Operation(summary = "批量添加知识库")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Docs> list) {
        if (docsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('docs:docs:update')")
    @OperationLog
    @Operation(summary = "批量修改知识库")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Docs> batchParam) {
        if (batchParam.update(docsService, "book_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('docs:docs:remove')")
    @OperationLog
    @Operation(summary = "批量删除知识库")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (docsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
