1
0

InterviewController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\workerapi\controller;
  3. use think\Request;
  4. use think\facade\Db;
  5. use app\workerapi\logic\MasterWorkerLogic;
  6. use app\workerapi\logic\MasterWorkerRegisterLogic;
  7. use app\common\model\master_worker\MasterWorkerQuestion;
  8. use app\common\model\master_worker\MasterWorkerInterview;
  9. use app\common\model\master_worker_register\MasterWorkerRegister;
  10. class InterviewController extends BaseApiController
  11. {
  12. public array $notNeedLogin = ['lists'];
  13. private array $list = [
  14. [
  15. 'id' => 1,
  16. 'title' => '请问您目前的工作状态是全职工作还是自由工作?',
  17. 'options' => [
  18. '全职工作',
  19. '自由工作'
  20. ],
  21. 'type' => 1,
  22. ],
  23. [
  24. 'id' => 2,
  25. 'title' => '请问您是否有独立上门给客户提供家电维修/清洗/安装的经验?',
  26. 'options' => [
  27. '有',
  28. '没有'
  29. ],
  30. 'type' => 1,
  31. ],
  32. [
  33. 'id' => 3,
  34. 'title' => '请问您有没有在其他同类型平台的工作经验?',
  35. 'options' => [
  36. '有',
  37. '没有'
  38. ],
  39. 'type' => 1,
  40. ],
  41. [
  42. 'id' => 4,
  43. 'title' => '请简要说明平台名称、工作时间年限、所服务的类目',
  44. 'type' => 3,
  45. ],
  46. [
  47. 'id' => 5,
  48. 'title' => '请选择您具备的经验?',
  49. 'options' => [
  50. '家电清洗',
  51. '家电维修',
  52. '不具备以上经验'
  53. ],
  54. 'type' => 1,
  55. ]
  56. ];
  57. private array $list2 = [
  58. [
  59. 'id' => 6,
  60. 'title' => '请选择您会的项目?最多选5个,最后会根据您所选的项目进行答题。',
  61. 'options' => [
  62. '天井机','风管机','家用中央空调','空调','油烟机','洗衣机','热水器','燃气灶','冰箱'
  63. ],
  64. 'type' => 2,
  65. ],
  66. [
  67. 'id' => 7,
  68. 'title' => '请选择您会的项目?最多选5个,最后会根据您所选的项目进行答题。',
  69. 'options' => [
  70. '空调加氟',
  71. '空调维修',
  72. '冰箱','制冷设备','油烟机','洗衣机','甩干机','燃气热水器','电热水器','燃气灶','干洗机',
  73. ],
  74. 'type' => 2,
  75. ]
  76. ];
  77. private array $list3 = [
  78. [
  79. 'id' => 9,
  80. 'title' => '请选择您持有的证书?',
  81. 'options' => [
  82. '低压电工作业',
  83. '高压电工作业',
  84. '高处作业',
  85. '制冷与空调作业',
  86. '焊接与热切割作业',
  87. '无',
  88. ],
  89. 'type' => 1,
  90. ],
  91. [
  92. 'id' => 10,
  93. 'title' => '请简单描述您具备的其他技能?',
  94. 'type' => 3,
  95. ]
  96. ];
  97. public function lists(Request $request)
  98. {
  99. // 获取单个 POST 参数
  100. $worker_register_id = $request->post('worker_register_id');
  101. $step = $request->post('step');
  102. // 获取所有 POST 参数
  103. $data = $request->post('data');
  104. if ( !in_array($step, [1,2,3,4,5])) {
  105. return $this->fail('参数错误');
  106. }
  107. if ($step > 1 && (empty($data) || !is_array($data))) {
  108. return $this->fail('答案不可为空');
  109. }
  110. $worker_register_id = MasterWorkerRegister::where('id',$worker_register_id)->value('id');
  111. if (!$worker_register_id) {
  112. return $this->fail('ID有误');
  113. }
  114. $info = MasterWorkerInterview::where("worker_register_id", $worker_register_id)->findOrEmpty();
  115. if ($info->isEmpty()) {
  116. $info = new MasterWorkerInterview();
  117. $info->worker_register_id = $worker_register_id;
  118. } else if ($info->status > 0) {
  119. return $this->fail('您已提交过面试题,请勿重复提交');
  120. }
  121. $msg = '操作成功';
  122. $has_next = 1;
  123. $is_success = 0;
  124. $result = [];
  125. $list = $this->list;
  126. $list2 = $this->list2;
  127. $list3 = $this->list3;
  128. $tmplist = array_column($list, null, 'id');
  129. if ($step == 1 ) {
  130. $result = $list;
  131. } elseif ($step == 2) {
  132. foreach($data as $item) {
  133. if (empty($item['id']) || ($item['id'] != 4 && empty($item['options']))) {
  134. return $this->fail('参数错误');
  135. }
  136. if ($item['id'] == 5) {
  137. $options = $item['options'];
  138. }
  139. }
  140. if (empty($options)) {
  141. return $this->fail('请选择您具备的经验');
  142. }
  143. $key = array_search($options, $tmplist[5]['options']);
  144. if ($key === false || !isset($list2[$key])) {
  145. $has_next = 0;
  146. $info->status = 2;
  147. $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  148. } else {
  149. //根据上一步的选项判断
  150. $result[] = $list2[$key];
  151. }
  152. switch($key) {
  153. case 0 :
  154. $info->type = 1;
  155. break;
  156. case 1 :
  157. $info->type = 2;
  158. break;
  159. default :
  160. $info->type = 0;
  161. }
  162. $info->content = json_encode([ 1 => $data]);
  163. $info->save();
  164. } elseif ($step == 3) {
  165. $data = current($data);
  166. $id = empty($data['id']) ? 0 : $data['id'];
  167. if ($id == 6 || $id == 7) {
  168. $question = MasterWorkerLogic::getQuestion(['type' => $info->type, 'category' => $data['options']]);
  169. foreach($question['list'] as $key => $item) {
  170. $temp = [
  171. "id" => $item['id'],
  172. "title" => $item['title'],
  173. "options" => explode(",",$item['options']),
  174. "type" => strpos($item['title'], '多选题') !== false ? 2 : 1,
  175. ];
  176. $result[] = $temp;
  177. }
  178. } else {
  179. return $this->fail('参数有误');
  180. }
  181. $content = json_decode($info->content, true);
  182. $content[2] = $data;
  183. $info->content = json_encode($content);
  184. $info->save();
  185. } elseif (($step == 4 && $info->type == 3) || $step == 5) {
  186. try{
  187. Db::startTrans();
  188. MasterWorkerRegisterLogic::createMasterWorker(['worker_register_id' => $worker_register_id,'is_rinse' => $info->type]);
  189. $content = json_decode($info->content, true);
  190. $content[$step - 1] = $data;
  191. $info->content = json_encode($content);
  192. $info->status = 1;
  193. $info->save();
  194. $has_next = 0;
  195. $is_success = 1;
  196. $msg = '恭喜您成功入驻众盾闪修平台!';
  197. Db::commit();
  198. } catch (\Exception $e) {
  199. Db::rollback();
  200. return $this->fail($e->getMessage());
  201. }
  202. } else {
  203. //校验
  204. $data = array_column($data, null, 'id');
  205. $ids = array_keys($data);
  206. $answers = MasterWorkerQuestion::where('type',$info->type)->whereIn('id',$ids)->column('answer','id');
  207. if (count($ids) != count($answers)) {
  208. return $this->fail('题目参数有误');
  209. }
  210. $rights = 0;
  211. foreach($data as &$item) {
  212. $answer = $answers[$item['id']];
  213. $item['options'] = preg_replace('/[^a-zA-Z]/', '', $item['options']);
  214. if ($answer == '对' || $answer == '错') {
  215. $item['options'] = $item['options'] == 'A' ? '对' : '错';
  216. }
  217. if ($item['options'] == $answer) {
  218. $rights ++;
  219. }
  220. }
  221. if ($rights / count($data) >= 0.8) {
  222. $result = $list3;
  223. } else {
  224. $has_next = 0;
  225. $info->status = 2;
  226. $msg = '很抱歉,您的测试未通过,感谢您的关注!';
  227. }
  228. $content = json_decode($info->content, true);
  229. $content[$step - 1] = $data;
  230. $info->content = json_encode($content);
  231. $info->save();
  232. }
  233. return $this->success($msg, ['has_next' => $has_next,'is_success' => $is_success, 'list' => $result], 1, 1);
  234. }
  235. }