1
0

TrainingQuestionsLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\TrainingQuestions;
  16. use app\common\logic\BaseLogic;
  17. use think\facade\Db;
  18. /**
  19. * TrainingQuestions逻辑
  20. * Class TrainingQuestionsLogic
  21. * @package app\adminapi\logic
  22. */
  23. class TrainingQuestionsLogic extends BaseLogic
  24. {
  25. public static $select_value = [];
  26. /**
  27. * @notes 添加
  28. * @param array $params
  29. * @return bool
  30. * @author likeadmin
  31. * @date 2025/02/14 13:49
  32. */
  33. public static function add(array $params): bool
  34. {
  35. if($params['subclass']) $params['subclass'] = end($params['subclass']);
  36. Db::startTrans();
  37. try {
  38. TrainingQuestions::create([
  39. 'training_course_id' => $params['training_course_id'],
  40. 'title' => $params['title'],
  41. 'question_type' => $params['question_type'],
  42. //'question_selects' => $params['question_selects'],
  43. 'question_selects' => self::configureReservedField($params['question_selects']??[]),
  44. 'question_answer' => self::$select_value?implode(',',self::$select_value):'',
  45. 'question_analysis' => $params['question_analysis'],
  46. 'subclass' => $params['subclass']??0,
  47. ]);
  48. Db::commit();
  49. return true;
  50. } catch (\Exception $e) {
  51. Db::rollback();
  52. self::setError($e->getMessage());
  53. return false;
  54. }
  55. }
  56. /**
  57. * @notes 编辑
  58. * @param array $params
  59. * @return bool
  60. * @author likeadmin
  61. * @date 2025/02/14 13:49
  62. */
  63. public static function edit(array $params): bool
  64. {
  65. Db::startTrans();
  66. try {
  67. TrainingQuestions::where('id', $params['id'])->update([
  68. 'training_course_id' => $params['training_course_id'],
  69. 'title' => $params['title'],
  70. 'question_type' => $params['question_type'],
  71. 'question_selects' => json_encode(self::configureReservedField($params['question_selects']??[])),
  72. 'question_answer' => self::$select_value?implode(',',self::$select_value):'',
  73. 'question_analysis' => $params['question_analysis'],
  74. ]);
  75. Db::commit();
  76. return true;
  77. } catch (\Exception $e) {
  78. Db::rollback();
  79. self::setError($e->getMessage());
  80. return false;
  81. }
  82. }
  83. /**
  84. * @notes 删除
  85. * @param array $params
  86. * @return bool
  87. * @author likeadmin
  88. * @date 2025/02/14 13:49
  89. */
  90. public static function delete(array $params): bool
  91. {
  92. return TrainingQuestions::destroy($params['id']);
  93. }
  94. /**
  95. * @notes 获取详情
  96. * @param $params
  97. * @return array
  98. * @author likeadmin
  99. * @date 2025/02/14 13:49
  100. */
  101. public static function detail($params): array
  102. {
  103. return TrainingQuestions::findOrEmpty($params['id'])->toArray();
  104. }
  105. public static function configureReservedField($data): array
  106. {
  107. $select_value = [];
  108. foreach ($data as $key => &$item) {
  109. $data[$key]['block_key'] = $key+1;
  110. if($item['select_value']){
  111. $select_value[] = $data[$key]['block_key'];
  112. $item['select_value'] = \true;
  113. }else{
  114. $item['select_value'] = \false;
  115. }
  116. }
  117. self::$select_value = $select_value;
  118. return $data??[];
  119. }
  120. public static function questionImport($questions)
  121. {
  122. $newQuestionArray = [];
  123. $time = time();
  124. foreach ($questions as $key => $question) {
  125. $newQuestionArray[$key]['title'] = $question['title'];
  126. $newQuestionArray[$key]['question_analysis'] = '暂无';
  127. $newQuestionArray[$key]['subclass'] = self::getSubclass($question['subclass']??'');
  128. $newQuestionArray[$key]['create_time'] = $time;
  129. $newQuestionArray[$key]['update_time'] = $time;
  130. $typeAnswer = self::getQuestionTypeAnswer($question['question_answer']);
  131. $newQuestionArray[$key]['question_type'] = $typeAnswer['question_type']??0;
  132. $newQuestionArray[$key]['question_answer'] = $typeAnswer['question_answer']??'';
  133. $newQuestionArray[$key]['question_selects'] = self::getQuestionSelects($question,$typeAnswer['question_answer'])??'';
  134. }
  135. Db::startTrans();
  136. try {
  137. TrainingQuestions::insertAll($newQuestionArray);
  138. Db::commit();
  139. return true;
  140. } catch (\Exception $e) {
  141. Db::rollback();
  142. self::setError($e->getMessage());
  143. return false;
  144. }
  145. }
  146. public static function getSubclass($subclass)
  147. {
  148. if(empty($subclass)) return 0;
  149. if(is_numeric($subclass)) return $subclass;
  150. return \app\common\model\goods_category\GoodsCategory::where('name', $subclass)->value('id')??0;
  151. }
  152. public static function getQuestionTypeAnswer($answer)
  153. {
  154. // 统一转大写
  155. $answer = strtoupper($answer);
  156. $optionMap = [
  157. 'A' => 1,
  158. 'B' => 2,
  159. 'C' => 3,
  160. 'D' => 4,
  161. 'E' => 5,
  162. 'F' => 6
  163. ];
  164. $cleanedAnswer = preg_replace('/[^A-Z]/', '', $answer);
  165. if (strlen($cleanedAnswer) === 0) {
  166. $question_type = 0;
  167. } elseif (strlen($cleanedAnswer) === 1) {
  168. $question_type = 1;
  169. } else {
  170. $question_type = 2;
  171. }
  172. $newArray = [];
  173. for ($i = 0; $i < strlen($cleanedAnswer); $i++) {
  174. $letter = $cleanedAnswer[$i];
  175. if (isset($optionMap[$letter])) {
  176. $newArray[] = $optionMap[$letter];
  177. }
  178. }
  179. $question_answer = implode(',', $newArray);
  180. return ['question_type'=>$question_type,'question_answer'=>$question_answer];
  181. }
  182. public static function getQuestionSelects($options_data,$question_answer)
  183. {
  184. $keyArr = [
  185. "options_a" => "0",
  186. "options_b" => "1",
  187. "options_c" => "2",
  188. "options_d" => "3",
  189. "options_e" => "4",
  190. "options_f" => "5"
  191. ];
  192. $res = [];
  193. foreach ($options_data as $key => $item) {
  194. isset($keyArr[$key]) && $res[$keyArr[$key]] = $item;
  195. }
  196. $arr = array_values($res);
  197. $answerArr = explode(',', $question_answer);
  198. $data = [];
  199. foreach ($arr as $key => $name) {
  200. $data[$key]['block_key'] = $key+1;
  201. $data[$key]['name'] = trim($name);
  202. if(in_array($data[$key]['block_key'],$answerArr)){
  203. $data[$key]['select_value'] = \true;
  204. }else{
  205. $data[$key]['select_value'] = \false;
  206. }
  207. }
  208. return json_encode($data);
  209. }
  210. }