diff --git a/app/controller/DistillationQuestionsController.php b/app/controller/DistillationQuestionsController.php new file mode 100644 index 0000000..6c559e4 --- /dev/null +++ b/app/controller/DistillationQuestionsController.php @@ -0,0 +1,54 @@ + '', + '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)); + } + +} \ No newline at end of file diff --git a/app/controller/DistillationWordController.php b/app/controller/DistillationWordController.php index 1253eaa..3108815 100644 --- a/app/controller/DistillationWordController.php +++ b/app/controller/DistillationWordController.php @@ -36,6 +36,10 @@ class DistillationWordController { $params = requestOnly([ 'name' => '', + 'drill_status' => '', + 'algorithm' => '', + 'begin_time' => '', + 'end_time' => '' ]); return success($service->listData($params)); } diff --git a/app/model/DistillationQuestions.php b/app/model/DistillationQuestions.php index c48c716..89fe6cf 100644 --- a/app/model/DistillationQuestions.php +++ b/app/model/DistillationQuestions.php @@ -30,4 +30,9 @@ class DistillationQuestions extends BaseModel */ public $timestamps = true; + public function distillation() + { + return $this->belongsTo(DistillationWord::class, 'distillation_id', 'id'); + } + } \ No newline at end of file diff --git a/app/model/DistillationWord.php b/app/model/DistillationWord.php index 27d4ba6..7c17c50 100644 --- a/app/model/DistillationWord.php +++ b/app/model/DistillationWord.php @@ -42,7 +42,7 @@ class DistillationWord extends BaseModel public function questions() { - return $this->hasMany(DistillationQuestions::class, 'distillation_id', 'id'); + return $this->hasMany(DistillationQuestions::class, 'distillation_id', 'id')->where(['delete_time' => 0]); } } \ No newline at end of file diff --git a/app/route/route.php b/app/route/route.php index 807ffb4..2fe1399 100644 --- a/app/route/route.php +++ b/app/route/route.php @@ -1,5 +1,6 @@ setParams(['perm' => 'distillationPureIndex']); }); + + //拓展问题 + Route::group('/questions', function () { + //新增 + Route::post('/save', [DistillationQuestionsController::class, 'save'])->setParams(['perm' => ['questionsSave']]); + //修改 + Route::post('/update', [DistillationQuestionsController::class, 'update'])->setParams(['perm' => ['questionsUpdate']]); + //列表 + Route::get('/index', [DistillationQuestionsController::class, 'index'])->setParams(['perm' => ['questionsIndex']]); + //详情 + Route::get('/read', [DistillationQuestionsController::class, 'read'])->setParams(['perm' => ['questionsRead']]); + //删除 + Route::post('/delete', [DistillationQuestionsController::class, 'delete'])->setParams(['perm' => ['questionsDelete']]); + }); })->middleware([ AdminAuthorizationMiddleware::class, PermissionsMiddleware::class diff --git a/app/service/DistillationQuestionsService.php b/app/service/DistillationQuestionsService.php new file mode 100644 index 0000000..0c6611f --- /dev/null +++ b/app/service/DistillationQuestionsService.php @@ -0,0 +1,132 @@ +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]; + } + +} \ No newline at end of file diff --git a/app/service/DistillationWordService.php b/app/service/DistillationWordService.php index d344291..405ac68 100644 --- a/app/service/DistillationWordService.php +++ b/app/service/DistillationWordService.php @@ -114,7 +114,19 @@ class DistillationWordService extends BaseService if (isNotBlank($params['name'])) { $query[] = ['name', 'like', '%' . $params['name'] . '%']; } - $list = $this->dao->getList($query, '*', 0, 0, 'id DESC', ['questions.count']); + if (isNotBlank($params['drill_status'])) { + $query[] = ['drill_status', '=', $params['drill_status']]; + } + if (isNotBlank($params['algorithm'])) { + $query[] = ['algorithm', '=', $params['algorithm']]; + } + 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', [], ['questions']); $count = $this->dao->getCount($query); return compact('list', 'count'); } diff --git a/app/validate/DistillationQuestionsValidate.php b/app/validate/DistillationQuestionsValidate.php new file mode 100644 index 0000000..b20c51b --- /dev/null +++ b/app/validate/DistillationQuestionsValidate.php @@ -0,0 +1,25 @@ + [ + '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' => '拓展问题不能为空', + ]; +} \ No newline at end of file diff --git a/plugin/piadmin/app/base/BaseDao.php b/plugin/piadmin/app/base/BaseDao.php index 26cf34e..1a73bf7 100644 --- a/plugin/piadmin/app/base/BaseDao.php +++ b/plugin/piadmin/app/base/BaseDao.php @@ -691,9 +691,9 @@ abstract class BaseDao } } - public function getList($where, $field='*', $page=0, $limit=0, $order='id desc',$with=[]): array + public function getList($where, $field='*', $page=0, $limit=0, $order='id desc',$with=[],$withCOunt=[]): array { - $query = $this->search($where)->with($with)->field($field)->order($order); + $query = $this->search($where)->with($with)->withCount($withCOunt)->field($field)->order($order); if($page !== 0 && $limit !== 0){ $query = $query->page($page,$limit); }