| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- // +----------------------------------------------------------------------
- // | likeadmin快速开发前后端分离管理后台(PHP版)
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
- // | github下载:https://github.com/likeshop-github/likeadmin
- // | 访问官网:https://www.likeadmin.cn
- // | likeadmin团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeadminTeam
- // +----------------------------------------------------------------------
- namespace app\adminapi\logic\training;
- use app\common\model\training\TrainingCourse;
- use app\common\logic\BaseLogic;
- use think\facade\Db;
- /**
- * TrainingCourse逻辑
- * Class TrainingCourseLogic
- * @package app\adminapi\logic
- */
- class TrainingCourseLogic extends BaseLogic
- {
- /**
- * @notes 添加
- * @param array $params
- * @return bool
- * @author likeadmin
- * @date 2025/02/14 17:37
- */
- public static function add(array $params): bool
- {
- Db::startTrans();
- try {
- TrainingCourse::create([
- 'course_name' => $params['course_name'],
- 'course_picture' => $params['course_picture'],
- 'course_url' => $params['course_url'],
- 'course_lock' => $params['course_lock'],
- 'course_length' => $params['course_length'],
- 'course_question_setting' => self::configureReservedField($params['course_question_setting']??[]),
- 'course_question_score' => $params['course_question_score'],
- ]);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 编辑
- * @param array $params
- * @return bool
- * @author likeadmin
- * @date 2025/02/14 17:37
- */
- public static function edit(array $params): bool
- {
- Db::startTrans();
- try {
- TrainingCourse::where('id', $params['id'])->update([
- 'course_name' => $params['course_name'],
- 'course_picture' => $params['course_picture'],
- 'course_url' => $params['course_url'],
- 'course_lock' => $params['course_lock'],
- 'course_length' => $params['course_length'],
- 'course_question_setting' => json_encode(self::configureReservedField($params['course_question_setting']??[])),
- 'course_question_score' => $params['course_question_score'],
- ]);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 删除
- * @param array $params
- * @return bool
- * @author likeadmin
- * @date 2025/02/14 17:37
- */
- public static function delete(array $params): bool
- {
- return TrainingCourse::destroy($params['id']);
- }
- /**
- * @notes 获取详情
- * @param $params
- * @return array
- * @author likeadmin
- * @date 2025/02/14 17:37
- */
- public static function detail($params): array
- {
- return TrainingCourse::findOrEmpty($params['id'])->toArray();
- }
- public static function configureReservedField($data): array
- {
- foreach ($data as $key => &$item) {
- $data[$key]['block_key'] = $key+1;
- $item['select_value'] = (int)$item['select_value'];
- }
- return $data??[];
- }
- }
|