TrainingCourseLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\training;
  15. use app\common\model\training\TrainingCourse;
  16. use app\common\logic\BaseLogic;
  17. use think\facade\Db;
  18. /**
  19. * TrainingCourse逻辑
  20. * Class TrainingCourseLogic
  21. * @package app\adminapi\logic
  22. */
  23. class TrainingCourseLogic extends BaseLogic
  24. {
  25. /**
  26. * @notes 添加
  27. * @param array $params
  28. * @return bool
  29. * @author likeadmin
  30. * @date 2025/02/14 17:37
  31. */
  32. public static function add(array $params): bool
  33. {
  34. Db::startTrans();
  35. try {
  36. TrainingCourse::create([
  37. 'course_name' => $params['course_name'],
  38. 'course_picture' => $params['course_picture'],
  39. 'course_url' => $params['course_url'],
  40. 'course_lock' => $params['course_lock'],
  41. 'course_length' => $params['course_length'],
  42. 'course_question_setting' => self::configureReservedField($params['course_question_setting']??[]),
  43. 'course_question_score' => $params['course_question_score'],
  44. ]);
  45. Db::commit();
  46. return true;
  47. } catch (\Exception $e) {
  48. Db::rollback();
  49. self::setError($e->getMessage());
  50. return false;
  51. }
  52. }
  53. /**
  54. * @notes 编辑
  55. * @param array $params
  56. * @return bool
  57. * @author likeadmin
  58. * @date 2025/02/14 17:37
  59. */
  60. public static function edit(array $params): bool
  61. {
  62. Db::startTrans();
  63. try {
  64. TrainingCourse::where('id', $params['id'])->update([
  65. 'course_name' => $params['course_name'],
  66. 'course_picture' => $params['course_picture'],
  67. 'course_url' => $params['course_url'],
  68. 'course_lock' => $params['course_lock'],
  69. 'course_length' => $params['course_length'],
  70. 'course_question_setting' => json_encode(self::configureReservedField($params['course_question_setting']??[])),
  71. 'course_question_score' => $params['course_question_score'],
  72. ]);
  73. Db::commit();
  74. return true;
  75. } catch (\Exception $e) {
  76. Db::rollback();
  77. self::setError($e->getMessage());
  78. return false;
  79. }
  80. }
  81. /**
  82. * @notes 删除
  83. * @param array $params
  84. * @return bool
  85. * @author likeadmin
  86. * @date 2025/02/14 17:37
  87. */
  88. public static function delete(array $params): bool
  89. {
  90. return TrainingCourse::destroy($params['id']);
  91. }
  92. /**
  93. * @notes 获取详情
  94. * @param $params
  95. * @return array
  96. * @author likeadmin
  97. * @date 2025/02/14 17:37
  98. */
  99. public static function detail($params): array
  100. {
  101. return TrainingCourse::findOrEmpty($params['id'])->toArray();
  102. }
  103. public static function configureReservedField($data): array
  104. {
  105. foreach ($data as $key => &$item) {
  106. $data[$key]['block_key'] = $key+1;
  107. $item['select_value'] = (int)$item['select_value'];
  108. }
  109. return $data??[];
  110. }
  111. }