package com.gxwebsoft.docs.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.docs.service.DocsUserService;
import com.gxwebsoft.docs.entity.DocsUser;
import com.gxwebsoft.docs.param.DocsUserParam;
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-user")
public class DocsUserController extends BaseController {
    @Resource
    private DocsUserService docsUserService;

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

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

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

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

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

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

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

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

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

}
