Browse Source

feat(knowledge): 新增知识库管理功能

- 新增知识库模型 KnowLedgeLibrary 及相关表结构
- 新增知识库控制器 KnowledgeLibraryController,包含保存、列表、删除接口
- 新增知识库数据访问对象 KnowledgeLibraryDao
- 新增知识库服务类 KnowledgeLibraryService,实现业务逻辑
- 新增知识库参数验证器 KnowledgeLibraryValidate
- 在路由中注册知识库相关接口路径
- 修复 DistillationQuestionsService 中变量命名错误问题
- 注释掉 AttachmentService 中冗余的 uuid 生成代码
master
zhangf@suq.cn 1 week ago
parent
commit
4036f77e69
  1. 36
      app/controller/KnowledgeLibraryController.php
  2. 15
      app/dao/KnowledgeLibraryDao.php
  3. 33
      app/model/KnowLedgeLibrary.php
  4. 11
      app/route/route.php
  5. 6
      app/service/DistillationQuestionsService.php
  6. 81
      app/service/KnowledgeLibraryService.php
  7. 19
      app/validate/KnowledgeLibraryValidate.php
  8. 2
      plugin/piadmin/app/service/AttachmentService.php

36
app/controller/KnowledgeLibraryController.php

@ -0,0 +1,36 @@
<?php
namespace app\controller;
use app\service\KnowledgeLibraryService;
use app\validate\KnowledgeLibraryValidate;
use support\Response;
class KnowledgeLibraryController
{
public function save(KnowledgeLibraryService $service): Response
{
$params = requestOnly([
'name' => '',
'url' => '',
]);
validate(KnowledgeLibraryValidate::class)->check($params, 'save');
return success($service->saveData($params));
}
public function index(KnowledgeLibraryService $service): Response
{
$params = requestOnly([
'name' => '',
'begin_time' => '',
'end_time' => ''
]);
return success($service->listData($params));
}
public function delete(KnowledgeLibraryService $service): Response
{
$id = input('id');
return success($service->deleteData($id));
}
}

15
app/dao/KnowledgeLibraryDao.php

@ -0,0 +1,15 @@
<?php
namespace app\dao;
use app\model\KnowLedgeLibrary;
use plugin\piadmin\app\base\BaseDao;
class KnowledgeLibraryDao extends BaseDao
{
protected function setModel(): string
{
return KnowLedgeLibrary::class;
}
}

33
app/model/KnowLedgeLibrary.php

@ -0,0 +1,33 @@
<?php
namespace app\model;
use plugin\piadmin\app\base\BaseModel;
/**
* 蒸馏词模型
*/
class KnowLedgeLibrary extends BaseModel
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'geo_knowledge_library';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
}

11
app/route/route.php

@ -1,6 +1,7 @@
<?php
use app\controller\DistillationQuestionsController;
use app\controller\KnowledgeLibraryController;
use app\controller\DistillationWordController;
use plugin\piadmin\app\middleware\AdminAuthorizationMiddleware;
use plugin\piadmin\app\middleware\PermissionsMiddleware;
@ -36,6 +37,16 @@ Route::group('/service/v1', function () {
//删除
Route::post('/delete', [DistillationQuestionsController::class, 'delete'])->setParams(['perm' => ['questionsDelete']]);
});
//智能体知识库
Route::group('/knowledge', function () {
//新增
Route::post('/save', [KnowledgeLibraryController::class, 'save'])->setParams(['perm' => ['questionsSave']]);
//列表
Route::get('/index', [KnowledgeLibraryController::class, 'index'])->setParams(['perm' => ['questionsIndex']]);
//删除
Route::post('/delete', [KnowledgeLibraryController::class, 'delete'])->setParams(['perm' => ['questionsDelete']]);
});
})->middleware([
AdminAuthorizationMiddleware::class,
PermissionsMiddleware::class

6
app/service/DistillationQuestionsService.php

@ -99,11 +99,11 @@ class DistillationQuestionsService extends BaseService
*/
public function readData(mixed $id): array
{
$user_group = $this->dao->get(['id' => $id], ['*'], ['distillation']);
if (empty($user_group)) {
$question = $this->dao->get(['id' => $id], ['*'], ['distillation']);
if (empty($question)) {
throw new ApiException('数据不存在');
}
return $user_group->toArray();
return $question->toArray();
}
/**

81
app/service/KnowledgeLibraryService.php

@ -0,0 +1,81 @@
<?php
namespace app\service;
use app\dao\KnowledgeLibraryDao;
use plugin\piadmin\app\base\BaseService;
use plugin\piadmin\app\exception\ApiException;
use support\think\Db;
class KnowledgeLibraryService extends BaseService
{
protected $dao;
public function __construct(KnowledgeLibraryDao $dao)
{
$this->dao = $dao;
}
/**
* 保存信息
* @param array $params
* @return array
*/
public function saveData(array $params): array
{
Db::startTrans();
try {
$data = $this->dao->save($params);
Db::commit();
} catch (\Exception $exception) {
Db::rollback();
throw new ApiException($exception->getMessage());
}
return $data->toArray();
}
/**
* 获取列表
* @param array $params
* @return array
*/
public function listData(array $params): array
{
$query = [
'delete_time' => 0
];
if (isNotBlank($params['name'])) {
$query[] = ['name', 'like', '%' . $params['name'] . '%'];
}
if (isNotBlank($params['begin_time'])) {
$query[] = ['create_time', '>=', strtotime($params['begin_time'])];
}
if (isNotBlank($params['end_time'])) {
$query[] = ['create_time', '<=', strtotime($params['end_time'] . ' 23:59:59')];
}
$list = $this->dao->getList($query);
$count = $this->dao->getCount($query);
return compact('list', 'count');
}
/**
* 删除信息
* @param mixed $id
* @return array
*/
public function deleteData(mixed $id): array
{
// 落库
Db::startTrans();
try {
$this->dao->update($id, ['delete_time' => time()]);
Db::commit();
} catch (\Exception $exception) {
Db::rollback();
throw new ApiException($exception->getMessage());
}
return ['id' => $id];
}
}

19
app/validate/KnowledgeLibraryValidate.php

@ -0,0 +1,19 @@
<?php
namespace app\validate;
use plugin\piadmin\app\base\BaseValidate;
class KnowledgeLibraryValidate extends BaseValidate
{
protected $group = [
'save' => [
'name' => 'require|max:90',
'url' => 'require',
],
];
protected $message = [
'name.require' => '知识库别名不能为空',
'url.require' => '文件地址不能为空',
];
}

2
plugin/piadmin/app/service/AttachmentService.php

@ -37,7 +37,7 @@ class AttachmentService extends BaseService
$path = parse_url($aliyunOssUrl, PHP_URL_PATH);
//封装数据
$data = [
'id' => IdUtils::uuid(),
// 'id' => IdUtils::uuid(),
'storage' => Attachment::getStorage(),
'type' => $file->getUploadMimeType(),
'name' => "$uniqueName.$fileExt",

Loading…
Cancel
Save