123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- <?php
- /**
- * Created by PhpStorm.
- * User: hanl
- * Date: 2018/6/8
- * Time: 10:13
- */
-
- namespace App\Services;
-
- use DB;
- use Excel;
- use App\Common\Api;
- use App\Models\Questions;
- use App\Models\Categorys;
- use App\Models\Configs;
- use Illuminate\Support\Facades\Redis;
-
- class QuestionService
- {
- public function __construct($configs = [])
- {
-
- }
-
- const QUESTION_UPLOAD_FAIL = 'bsb_question_upload_fail:';
-
- /**
- * 获取题库详情
- * @param $activity_id
- * @param $corp_id
- * @param $category_id
- * @return array|mixed
- */
- public static function getCategoryInfo($activity_id, $category_id){
- $res = Categorys::where('activity_id', $activity_id)->where('category_id', $category_id)->first();
-
- if($res){
- $res = collect($res)->toArray();
- $res = Api::dateFormat($res);
- }
-
- return $res;
- }
-
- /**
- * 添加题库
- * @param $params
- * @return array
- */
- public static function saveCategory($params){
- $act_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']);
- if(!$act_info){
- return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
- }
-
- $query = [];
- $query['category_name'] = $params['category_name'];
- $query['update_time'] = time();
- if(!empty($params['category_id']) && isset($params['category_id'])){
- $res = Categorys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('category_id', $params['category_id'])->update($query);
- $category_id = $params['category_id'];
- } else {
- $query['corp_id'] = $params['corp_id'];
- $query['activity_id'] = $params['activity_id'];
- $query['create_time'] = time();
- $res = Categorys::insertGetId($query);
- $category_id = $res;
- }
-
- if($res){
- $data['category_id'] = $category_id;
- return Api::arr(config('code.success'), trans('msg.success'), $data);
- } else {
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
- }
-
- /**
- * 获取题库列表
- * @param $params
- * @return array
- */
- public static function getCategoryList($params){
- $act_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']);
- if(!$act_info){
- return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
- }
-
- $categorys = Categorys::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->get()->toArray();
-
- foreach ($categorys as $k=>$v) {
- $question_num = Questions::where('category_id', $v['category_id'])
- ->where('corp_id', $params['corp_id'])
- ->where('activity_id', $params['activity_id'])
- ->where('status', 1) //题目状态 0 无效 1有效
- ->count();
- $v['questionNum'] = $question_num?$question_num:0;
-
- //时间格式化
- $categorys[$k] = Api::dateFormat($v);
- }
-
- return Api::arr(config('code.success'), trans('msg.success'), $categorys);
-
- }
-
- /**
- * 计算多个题库内的题目总数
- */
- public static function getQuestionsCount($categorys, $corp_id, $activity_id){
- $category = explode(',', $categorys);
-
- $count = 0;
- foreach($category as $k=>$v){
- $category_count = Questions::where('corp_id', $corp_id)->where('activity_id', $activity_id)->where('category_id', $v)->count();
- $count += $category_count;
- }
-
- return $count;
- }
-
- /**
- * 答题设置
- * @param $params
- * @return array
- */
- public static function questionConfigDetail($params){
- //闯关、必答、PK 题库不能为空 必选项
- if(empty($params['must_category_ids']) || empty($params['blockade_category_ids']) || empty($params['pk_category_ids'])){
- return Api::arr(config('code.params_error'), trans('msg.category_ids_null'));
- }
-
- $must_category_ids = $params['must_category_ids'];
- $blockade_category_ids = $params['blockade_category_ids'];
- $pk_category_ids = $params['pk_category_ids'];
-
- //计算选中的题库内题目总数
- $must_count = self::getQuestionsCount($must_category_ids, $params['corp_id'], $params['activity_id']);
- $blockade_count = self::getQuestionsCount($blockade_category_ids, $params['corp_id'], $params['activity_id']);
- $pk_count = self::getQuestionsCount($pk_category_ids, $params['corp_id'], $params['activity_id']);
-
- //题目数不能大于选中的题库内题目的总数
- if ($must_count < $params['must_question_num'] || $blockade_count < $params['blockade_question_num'] || $pk_count < $params['pk_question_num']) {
- return Api::arr(config('code.params_error'), trans('msg.num_greater_category'));
- }
-
- //允许答错的题目数不能大于总题目数
- if ($params['must_answer_wrong_num'] > $params['must_question_num'] || $params['blockade_answer_wrong_num'] > $params['blockade_question_num']) {
- return Api::arr(config('code.params_error'), trans('msg.wrong_greater_num'));
- }
-
- //获取配置项信息
- $config_info = Configs::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->first();
- if(!$config_info){
- return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
- }
-
- //配置项入库
- $config_info = collect($config_info)->toArray();
- $extend_configs = json_decode($config_info['extend_configs'], true);
- $extend_configs['must_question_num'] = $params['must_question_num'];
- $extend_configs['must_answer_wrong_num'] = $params['must_answer_wrong_num'];
- $extend_configs['must_answer_success_km'] = $params['must_answer_success_km'];
- $extend_configs['blockade_question_num'] = $params['blockade_question_num'];
- $extend_configs['blockade_answer_wrong_num'] = $params['blockade_answer_wrong_num'];
- $extend_configs['blockade_answer_success_km'] = $params['blockade_answer_success_km'];
- $extend_configs['pk_question_num'] = $params['pk_question_num'];
- $extend_configs['pk_last_double'] = $params['pk_last_double'];
- $extend_configs['pk_kms'] = $params['pk_kms'];
- $extend_configs['must_category_ids'] = $must_category_ids;
- $extend_configs['blockade_category_ids'] = $blockade_category_ids;
- $extend_configs['pk_category_ids'] = $pk_category_ids;
- $extend_configs['top_prize_open'] = $params['top_prize_open'];
- $extend_configs['top_prize_text'] = $params['top_prize_text'];
- $extend_configs['top_prize_num'] = $params['top_prize_num'];
- $extend_configs['must_answer_count_down'] = isset($params['must_answer_count_down']) && $params['must_answer_count_down']>=5?$params['must_answer_count_down']:10;
- $extend_configs['blockade_answer_count_down'] = isset($params['blockade_answer_count_down']) && $params['blockade_answer_count_down']>=5?$params['blockade_answer_count_down']:10;
-
- $extend_configs = json_encode($extend_configs);
- $res_config = Configs::where('corp_id', $params['corp_id'])->where('activity_id', $params['activity_id'])->update(['extend_configs' => $extend_configs, 'update_time' => time()]);
-
- return Api::arr(config('code.success'), trans('msg.success'));
- }
-
- /**
- * 修改/新增题目
- * @param $params
- * @return array
- */
- public static function questionModify($params){
- $query = [];
- $question_options= [];
- $answers = [];
- if(!empty($params['option_a'])){
- $question_options['A'] = Api::trimAll($params['option_a']);
- $answers[] = 'A';
- }
- if(!empty($params['option_b'])){
- $question_options['B'] = Api::trimAll($params['option_b']);
- $answers[] = 'B';
- }
- if(!empty($params['option_c'])){
- $question_options['C'] = Api::trimAll($params['option_c']);
- $answers[] = 'C';
- }
- if(!empty($params['option_d'])){
- $question_options['D'] = Api::trimAll($params['option_d']);
- $answers[] = 'D';
- }
-
- //题目和答案不可为空
- if(empty(Api::trimAll($params['question_title'])) || empty($params['correct_answer'])){
- return Api::arr(config('code.params_error'), trans('msg.title_answer_null'));
- }
-
- //选项最少需要两个 答案必须在已有选项当中
- if(count($question_options)<2 || !in_array(strtoupper($params['correct_answer']), $answers)){
- return Api::arr(config('code.params_error'), trans('msg.option_error'));
- }
-
- //参数组装
- $query['question_title'] = Api::trimAll($params['question_title']);
- $query['question_options'] = serialize($question_options);
- $query['correct_answer'] = strtoupper($params['correct_answer']);
- $query['category_id'] = intval($params['category_id']);
- $query['update_time'] = time();
-
- //题目ID存在则修改题目,不存在则新增题目
- if(isset($params['question_id']) && $params['question_id'] != 0){
- $question = Questions::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('question_id', $params['question_id'])->first();
- if(!$question){
- return Api::arr(config('code.question_not_exist'), trans('msg.question_not_exist'));
- }
-
- $res = Questions::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('question_id', $params['question_id'])->update($query);
- if(!$res){
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
-
- } else {
- $query['corp_id'] = $params['corp_id'];
- $query['activity_id'] = $params['activity_id'];
- $query['create_time'] = time();
-
- $res = Questions::insert($query);
- if(!$res){
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
- }
-
- return Api::arr(config('code.success'), trans('msg.success'));
- }
-
- /**
- * 获取题库内题目数量
- * @param $corp_id
- * @param $activity_id
- * @return array
- */
- public static function getCategoryCount($corp_id, $activity_id){
- $categorys = Categorys::where('activity_id', $activity_id)->where('corp_id', $corp_id)->get()->toArray();
-
- $data = [];
- if($categorys) {
- foreach ($categorys as $category) {
- $data[$category['category_name']] = Questions::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('category_id', $category['category_id'])->count();
- }
- }
-
- return $data;
- }
-
- /**
- * 新增题库及题目上传
- * @param $data
- * @param $params
- * @return array|string
- */
- public static function questionUpload($data, $params){
- if(empty($data)){
- return Api::arr(config('code.fail'), trans('msg.question_not_exist'));
- }
-
- //添加题库
- $categoryRes = self::saveCategory($params);
-
- if($categoryRes['code'] != config('code.success')){
- return Api::arr(config('code.fail'), trans('msg.category_add_fail'));
- }
-
- $category_id = $categoryRes['data']['category_id'];
-
- DB::beginTransaction();
- $errors = [];
- $error_num = 0;
- foreach ($data as $k => $v) {
- $question_options = array();
- $question_title = $v[0];
- $answers = array();
- if(!empty($v[1]) || $v[1] == '0'){
- $question_options['A'] = $v[1];
- $answers[] = 'A';
- }
- if(!empty($v[2]) || $v[2] == '0'){
- $question_options['B'] = $v[2];
- $answers[] = 'B';
- }
- if(!empty($v[3]) || $v[3] == '0'){
- $question_options['C'] = $v[3];
- $answers[] = 'C';
- }
- if(!empty($v[4]) || $v[4] == '0'){
- $question_options['D'] = $v[4];
- $answers[] = 'D';
- }
-
- $correct_answer = $v[5];
-
- $query = array();
- $query['corp_id'] = $params['corp_id'];
- $query['activity_id'] = $params['activity_id'];
- $query['category_id'] = $category_id;
- $query['question_title'] = $question_title;
- $query['correct_answer'] = strtoupper($correct_answer);
-
- //答案单选 答案不能为空 题目不能为空 选项最少两个 答案不在选项中 都视为错误题目
- if(strlen($query['correct_answer']) > 1){
- $v[6] = '答案多选';
- $errors[] = $v;
- $error_num += 1;
- continue;
- }
- if(empty($query['correct_answer'])){
- $v[6] = '答案为空';
- $errors[] = $v;
- $error_num += 1;
- continue;
- }
- if(empty($query['question_title'])){
- $v[6] = '题目为空';
- $errors[] = $v;
- $error_num += 1;
- continue;
- }
- if(count($answers) < 2){
- $v[6] = '选项少于2个';
- $errors[] = $v;
- $error_num += 1;
- continue;
- }
- if(!in_array($query['correct_answer'], $answers)){
- $v[6] = '答案不在选项中';
- $errors[] = $v;
- $error_num += 1;
- continue;
- }
-
-
- $query['question_options'] = serialize($question_options); //序列化
- $query['create_time'] = time();
- $query['update_time'] = time();
-
- $res = Questions::insert($query);
- if (!$res) {
- DB::rollBack();
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
- }
-
- DB::commit();
-
- //错误记录存入redis
- Redis::set(self::QUESTION_UPLOAD_FAIL.$params['activity_id'], json_encode($errors),'EX',3600);
-
- $res = [];
- $res['total'] = count($data);
- $res['error_num'] = $error_num;
- $res['success_num'] = $res['total'] - $error_num;
- $res['category_id'] = $category_id;
- return Api::arr(config('code.success'), trans('msg.success'), $res);
- }
-
- /**
- * 删除题目
- * 2018-06-22 10:32:00
- * @param $params
- * @return array
- */
- public static function delQuestion($params){
- //题目信息
- $questionInfo = Questions::where('activity_id', $params['activity_id'])
- ->where('corp_id', $params['corp_id'])
- ->where('category_id', $params['category_id'])
- ->where('question_id', $params['question_id'])
- ->where('status', 1)
- ->first();
-
- if(!$questionInfo){
- return Api::arr(config('code.question_not_exist'), trans('msg.question_not_exist'));
- }
-
- //删除题目
- $res = Questions::where('activity_id', $params['activity_id'])
- ->where('corp_id', $params['corp_id'])
- ->where('category_id', $params['category_id'])
- ->where('question_id', $params['question_id'])
- ->where('status', 1)
- ->delete();
-
- if($res){
- return Api::arr(config('code.success'), trans('msg.success'));
- } else {
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
- }
-
- /**
- * 下载上传失败的题目数据
- * @param $data
- */
- public static function downloadQuestionExcel($data){
- $cellData = [
- ['题目标题', 'A选项', 'B选项', 'C选项', 'D选项', '答案', '错误原因'],
- ['填写规则'],
- ];
-
- foreach($cellData as $k=>$v){
- array_unshift($data, $v);
- }
-
- Excel::create('题目导入错误数据'.date('mdHi'),function($excel) use ($data){
- $excel->sheet('题目信息', function($sheet) use ($data){
-
- $tot = count($data) ;
- $sheet->setWidth(array(
- 'A' => 50,
- 'B' => 15,
- 'C' => 15,
- 'D' => 15,
- 'E' => 15,
- 'F' => 15,
- 'G' => 15,
- ))->rows($data)->setFontSize(12);
-
- //设置第一行的表格格式
- $sheet->setStyle([
- 'font' => [
- 'size' => 12,
- 'bold' => false,
- ]
- ])
- ->mergeCells('A1:G1') //合并单元格
- ->setHeight(1, 100) //设置行高
- ->setHeight(2, 30)
- ->cells('A1:G1', function($cells){ //设置单元格格式
- $cells->setValignment('center'); //文本对齐方式
- $cells->setFontWeight('bold'); //粗体
- })
- ->cells('A2:G2', function($cells){
- $cells->setValignment('center');
- $cells->setBackground('#92D050');
- $cells->setFontWeight('bold');
- $cells->setFontSize(10);
- $cells->setAlignment('center');
- })
- ->cells('B2:G'.$tot, function($cells) {
- $cells->setAlignment('center');
- })
- ->cells('G3:G'.$tot, function($cells) {
- $cells->setFontColor('#ff0000');
- $cells->setFontWeight('bold');
- });
- });
- })->export('xls');
- }
-
- /**
- * 添加默认题库
- * @param $activity_id
- * @return array
- */
- public static function addDefault($activity_id){
- $activity_info = ActivityService::getActivityInfo($activity_id);
- if(!$activity_info){
- return Api::arr(config('code.fail'), trans('msg.activity_not_exist'));
- }
-
- $default_questions = Questions::where('corp_id', 0)->where('activity_id', 0)->get()->toArray();
- $query_category = [];
- $query_category['corp_id'] = $activity_info['corp_id'];
- $query_category['activity_id'] = $activity_id;
- $query_category['category_name'] = '样例题库';
- $query_category['create_time'] = time();
- $query_category['update_time'] = time();
-
- $res = Categorys::insertGetId($query_category);
- if(!$res){
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
-
- foreach($default_questions as $k=>$v){
- $query_question = [];
- $query_question['corp_id'] = $activity_info['corp_id'];
- $query_question['activity_id'] = $activity_id;
- $query_question['question_title'] = $v['question_title'];
- $query_question['category_id'] = $res;
- $query_question['question_options'] = $v['question_options'];
- $query_question['correct_answer'] = $v['correct_answer'];
- $query_question['create_time'] = time();
- $query_question['update_time'] = time();
- Questions::insert($query_question);
- }
-
- $data['category_id'] = $res;
- return Api::arr(config('code.success'), trans('msg.success'), $data);
- }
-
- /**
- * 删除题库(活动委发布状态才可删除)
- * @param $activity_id
- * @param $category_id
- * @return array
- */
- public static function delCategory($activity_id, $category_id){
- $activityInfo = ActivityService::getActivityInfo($activity_id);
-
- if(!$activityInfo){
- return Api::arr(config('code.fail'), trans('msg.activity_not_exist'));
- }
-
- if($activityInfo['is_complete_set'] == 1){
- return Api::arr(config('code.fail'), '活动已发布,无法删除题库');
- }
-
- $categoryInfo = Categorys::where('category_id', $category_id)->first();
- if(!$categoryInfo){
- return Api::arr(config('code.fail'), trans('msg.category_not_exist'));
- }
-
- Categorys::where('category_id', $category_id)->delete();
-
- $questions = Questions::where('category_id', $category_id)->get();
- foreach($questions as $k=>$v){
- Questions::where('question_id', $v['question_id'])->delete();
- }
-
- return Api::arr(config('code.success'), trans('msg.success'));
- }
- }
- ?>
|