Browse Source
feat(distillation): 新增拓展问题管理功能
feat(distillation): 新增拓展问题管理功能
- 在BaseDao中增加withCount参数支持关联统计 - 新增DistillationQuestions模型并建立与DistillationWord的关联关系 - 创建DistillationQuestions控制器和服务类实现增删改查逻辑 - 添加验证规则确保数据完整性 - 在路由中注册拓展问题相关接口地址 - 优化DistillationWord模型中的questions关联查询条件 - 完善DistillationWord列表查询时的筛选字段和统计功能master
9 changed files with 251 additions and 4 deletions
-
54app/controller/DistillationQuestionsController.php
-
4app/controller/DistillationWordController.php
-
5app/model/DistillationQuestions.php
-
2app/model/DistillationWord.php
-
15app/route/route.php
-
132app/service/DistillationQuestionsService.php
-
14app/service/DistillationWordService.php
-
25app/validate/DistillationQuestionsValidate.php
-
4plugin/piadmin/app/base/BaseDao.php
@ -0,0 +1,54 @@ |
|||
<?php |
|||
namespace app\controller; |
|||
|
|||
use app\service\DistillationQuestionsService; |
|||
use app\validate\DistillationQuestionsValidate; |
|||
use plugin\piadmin\app\utils\ArrayUtils; |
|||
use support\Response; |
|||
|
|||
class DistillationQuestionsController |
|||
{ |
|||
public function save(DistillationQuestionsService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'distillation_id' => '', |
|||
'names' => '', |
|||
]); |
|||
validate(DistillationQuestionsValidate::class)->check($params, 'save'); |
|||
return success($service->saveData($params)); |
|||
} |
|||
|
|||
public function update(DistillationQuestionsService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'id' => '', |
|||
'name' => '', |
|||
]); |
|||
validate(DistillationQuestionsValidate::class)->check($params, 'update'); |
|||
return success($service->updateData(ArrayUtils::filterNotEmpty($params))); |
|||
} |
|||
|
|||
public function index(DistillationQuestionsService $service): Response |
|||
{ |
|||
$params = requestOnly([ |
|||
'word_name' => '', |
|||
'name' => '', |
|||
'begin_time' => '', |
|||
'end_time' => '' |
|||
]); |
|||
return success($service->listData($params)); |
|||
} |
|||
|
|||
public function read(DistillationQuestionsService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->readData($id)); |
|||
} |
|||
|
|||
public function delete(DistillationQuestionsService $service): Response |
|||
{ |
|||
$id = input('id'); |
|||
return success($service->deleteData($id)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,132 @@ |
|||
<?php |
|||
|
|||
namespace app\service; |
|||
|
|||
use app\dao\DistillationQuestionsDao; |
|||
use app\dao\DistillationWordDao; |
|||
use plugin\piadmin\app\base\BaseService; |
|||
use plugin\piadmin\app\exception\ApiException; |
|||
use support\think\Db; |
|||
|
|||
class DistillationQuestionsService extends BaseService |
|||
{ |
|||
|
|||
protected $dao; |
|||
protected $wordDao;//主表Dao
|
|||
|
|||
public function __construct(DistillationQuestionsDao $dao) |
|||
{ |
|||
$this->dao = $dao; |
|||
$this->wordDao = app()->make(DistillationWordDao::class); |
|||
} |
|||
|
|||
/** |
|||
* 保存信息 |
|||
* @param array $params |
|||
* @return array |
|||
*/ |
|||
public function saveData(array $params): array |
|||
{ |
|||
Db::startTrans(); |
|||
try { |
|||
$questions = []; |
|||
foreach ($params['names'] as $item) { |
|||
$questions[] = [ |
|||
'distillation_id' => $params['distillation_id'], |
|||
'name' => $item |
|||
]; |
|||
} |
|||
$this->dao->insertAll($questions); |
|||
Db::commit(); |
|||
} catch (\Exception $exception) { |
|||
Db::rollback(); |
|||
throw new ApiException($exception->getMessage()); |
|||
} |
|||
return $questions; |
|||
} |
|||
|
|||
/** |
|||
* 修改信息 |
|||
* @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['word_name'])) { |
|||
$word_ids = $this->wordDao->getColumn([['name', 'like', '%' . $params['word_name'] . '%']], 'id'); |
|||
$query[] = ['distillation_id', 'in', $word_ids]; |
|||
} |
|||
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, '*', 0, 0, 'id DESC', ['distillation']); |
|||
$count = $this->dao->getCount($query); |
|||
return compact('list', 'count'); |
|||
} |
|||
|
|||
/** |
|||
* 获取信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function readData(mixed $id): array |
|||
{ |
|||
$user_group = $this->dao->get(['id' => $id], ['*'], ['distillation']); |
|||
if (empty($user_group)) { |
|||
throw new ApiException('数据不存在'); |
|||
} |
|||
return $user_group->toArray(); |
|||
} |
|||
|
|||
/** |
|||
* 删除信息 |
|||
* @param mixed $id |
|||
* @return array |
|||
*/ |
|||
public function deleteData(mixed $id): array |
|||
{ |
|||
$data = $this->dao->get(['id' => $id]); |
|||
if (empty($data)) { |
|||
return []; |
|||
} |
|||
// 落库
|
|||
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,25 @@ |
|||
<?php |
|||
|
|||
namespace app\validate; |
|||
|
|||
use plugin\piadmin\app\base\BaseValidate; |
|||
|
|||
class DistillationQuestionsValidate extends BaseValidate |
|||
{ |
|||
protected $group = [ |
|||
'save' => [ |
|||
'distillation_id' => 'require', |
|||
'names' => 'require|array', |
|||
], |
|||
'update' => [ |
|||
'id' => 'require', |
|||
'name' => 'require|max:90' |
|||
] |
|||
]; |
|||
protected $message = [ |
|||
'id.require' => 'ID不能为空', |
|||
'distillation_id.require' => '蒸馏主词不能为空', |
|||
'names.require' => '拓展问题不能为空', |
|||
'name.require' => '拓展问题不能为空', |
|||
]; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue