Browse Source
feat(task): 新增AI创作任务管理功能
feat(task): 新增AI创作任务管理功能
- 创建任务模型、控制器、服务及验证器 - 实现任务的增删改查接口 - 添加任务列表筛选与分页逻辑 - 配置路由并注册权限控制中间件 - 支持知识库ID数组转字符串存储及回显转换master
6 changed files with 303 additions and 2 deletions
-
70app/controller/CreationTaskController.php
-
15app/dao/CreationTaskDao.php
-
38app/model/CreationTask.php
-
19app/route/route.php
-
130app/service/CreationTaskService.php
-
33app/validate/CreationTaskValidate.php
@ -0,0 +1,70 @@ |
|||
<?php |
|||
namespace app\controller; |
|||
|
|||
use app\service\CreationTaskService; |
|||
use app\validate\CreationTaskValidate; |
|||
use plugin\piadmin\app\utils\ArrayUtils; |
|||
use support\Response; |
|||
|
|||
class CreationTaskController |
|||
{ |
|||
public function save(CreationTaskService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'name' => '', |
|||
'distillation_id' => '', |
|||
'portrait_category_id' => '', |
|||
'image_count' => '', |
|||
'knowledge_ids' => '', |
|||
'content_ai_command_id' => '', |
|||
'title_ai_command_id' => '', |
|||
'creation_count' => '' |
|||
]); |
|||
validate(CreationTaskValidate::class)->check($params, 'save'); |
|||
return success($service->saveData($params)); |
|||
} |
|||
|
|||
public function update(CreationTaskService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'id' => '', |
|||
'name' => '', |
|||
'distillation_id' => '', |
|||
'portrait_category_id' => '', |
|||
'image_count' => '', |
|||
'knowledge_id' => '', |
|||
'content_ai_command_id' => '', |
|||
'title_ai_command_id' => '', |
|||
'creation_count' => '', |
|||
'status' => '' |
|||
]); |
|||
validate(CreationTaskValidate::class)->check($params, 'update'); |
|||
return success($service->updateData(ArrayUtils::filterNotEmpty($params))); |
|||
} |
|||
|
|||
public function index(CreationTaskService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'name' => '', |
|||
'distillation_word' => '', |
|||
'status' => '', |
|||
'begin_time' => '', |
|||
'end_time' => '' |
|||
]); |
|||
return success($service->listData($params)); |
|||
} |
|||
|
|||
public function read(CreationTaskService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->readData($id)); |
|||
} |
|||
|
|||
public function delete(CreationTaskService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->deleteData($id)); |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
<?php |
|||
|
|||
namespace app\dao; |
|||
|
|||
use app\model\CreationTask; |
|||
use plugin\piadmin\app\base\BaseDao; |
|||
|
|||
class CreationTaskDao extends BaseDao |
|||
{ |
|||
protected function setModel(): string |
|||
{ |
|||
return CreationTask::class; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
<?php |
|||
|
|||
namespace app\model; |
|||
|
|||
use plugin\piadmin\app\base\BaseModel; |
|||
|
|||
/** |
|||
* AI创作指令模型 |
|||
*/ |
|||
class CreationTask extends BaseModel |
|||
{ |
|||
/** |
|||
* The table associated with the model. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $table = 'geo_creation_task'; |
|||
|
|||
/** |
|||
* The primary key associated with the table. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $primaryKey = 'id'; |
|||
|
|||
/** |
|||
* Indicates if the model should be timestamped. |
|||
* |
|||
* @var bool |
|||
*/ |
|||
public $timestamps = true; |
|||
|
|||
public function distillaction() |
|||
{ |
|||
return $this->belongsTo(DistillationWord::class, 'distillation_id', 'id'); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,130 @@ |
|||
<?php |
|||
|
|||
namespace app\service; |
|||
|
|||
use app\dao\CreationTaskDao; |
|||
use app\dao\DistillationWordDao; |
|||
use plugin\piadmin\app\base\BaseService; |
|||
use plugin\piadmin\app\exception\ApiException; |
|||
use support\think\Db; |
|||
|
|||
class CreationTaskService extends BaseService |
|||
{ |
|||
|
|||
protected $dao; |
|||
protected $distillationDao; |
|||
|
|||
public function __construct(CreationTaskDao $dao) |
|||
{ |
|||
$this->dao = $dao; |
|||
$this->distillationDao = app()->make(DistillationWordDao::class); |
|||
} |
|||
|
|||
/** |
|||
* 保存信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function saveData(array $params): array |
|||
{ |
|||
Db::startTrans(); |
|||
try { |
|||
//同步创建文章分类,若存在则不创建
|
|||
|
|||
if (isNotBlank($params['knowledge_ids'])) { |
|||
$params['knowledge_ids'] = implode(',', $params['knowledge_ids']); |
|||
} |
|||
$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 updateData(array $params): array |
|||
{ |
|||
// 落库
|
|||
Db::startTrans(); |
|||
try { |
|||
$this->dao->update(['id' => $params['id']], $params); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $params; |
|||
} |
|||
|
|||
/** |
|||
* 获取列表 |
|||
* @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['distillation_word'])) { |
|||
$ids = $this->distillationDao->getColumn([['name', 'like', '%' . $params['distillation_word'] . '%']], 'id'); |
|||
$query[] = ['distillation_id', 'in', $ids]; |
|||
} |
|||
if (isNotBlank($params['status'])) { |
|||
$query[] = ['status', '=', $params['status']]; |
|||
} |
|||
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, '*', 0, 0, 'id DESC', ['distillaction']); |
|||
$count = $this->dao->getCount($query); |
|||
return compact('list', 'count'); |
|||
} |
|||
|
|||
/** |
|||
* 获取信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function readData(mixed $id): array |
|||
{ |
|||
$aiCommand = $this->dao->get(['id' => $id]); |
|||
if (empty($aiCommand)) { |
|||
throw new ApiException('数据不存在'); |
|||
} |
|||
$aiCommand['knowledge_ids'] = array_map('intval', explode(',', $aiCommand['knowledge_ids'])); |
|||
return $aiCommand->toArray(); |
|||
} |
|||
|
|||
/** |
|||
* 删除信息 |
|||
* @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]; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<?php |
|||
|
|||
namespace app\validate; |
|||
|
|||
use plugin\piadmin\app\base\BaseValidate; |
|||
|
|||
class CreationTaskValidate extends BaseValidate |
|||
{ |
|||
protected $group = [ |
|||
'save' => [ |
|||
'name' => 'require', |
|||
'distillation_id' => 'require|number', |
|||
'portrait_category_id' => 'require|number', |
|||
'creation_count' => 'require|number' |
|||
], |
|||
'update' => [ |
|||
'id' => 'require', |
|||
'name' => 'require', |
|||
'distillation_id' => 'require|number', |
|||
'portrait_category_id' => 'require|number', |
|||
'creation_count' => 'require|number', |
|||
'status' => 'require|number', |
|||
], |
|||
]; |
|||
protected $message = [ |
|||
'id.require' => 'ID不能为空', |
|||
'name.require' => '任务名称不能为空', |
|||
'distillation_id.require' => '蒸馏训练词不能为空', |
|||
'portrait_category_id.require' => '画像图库不能为空', |
|||
'creation_count.require' => 'AI创作数量不能为空', |
|||
'status' => '状态不能为空', |
|||
]; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue