<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/10/23 * Time: 13:59 */ namespace app\home\controller; use app\common\controller\WechatBase; use app\home\model\UserQuestionAnswer; use think\Db; class Question extends WechatBase { /** * 获取题(废弃) * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function get_question(){ $user_id = $this->request->param('user_id',0,'intval'); $goods_id = $this->request->param('goods_id',0,'intval'); if(empty($user_id) || empty($goods_id)){ $this->error('404'); } $userModel = new \app\home\model\User(); $user = $userModel->findData(['id'=>$user_id]); $user['age'] = getAge($user['birthday']);//年龄 $goodsModel = new \app\home\model\Goods(); $goods = $goodsModel->findData(['id'=>$goods_id]); //判断是否符合答题条件 if($goods['sex'] != 0){ if(empty($user['gender'])){ $this->error('请去完善信息'); } if($user['gender'] == $goods['sex']){ $this->error('性别不符合要求'); } } if($goods['min_age'] != 0 || $goods['max_age'] != 0){ if(empty($user['birthday'])){ $this->error('请去完善信息'); } if($goods['min_age'] <= $user['age'] && $user['age'] <= $goods['max_age']){ $this->error('年龄不符合要求'); } } $distance = distance($goods,$user);//根据两地经纬度获取距离 if($goods['distance'] != 0){ if($distance > $goods['distance']){ $this->error('距离不符合要求'); } } //题 $questionModel = new \app\home\model\Question(); $question = $questionModel ->alias('q') ->field('q.*,q_o.id as q_o_id,q_o.option_name') ->join('fa_question_option q_o','q_o.question_id = q.id','left') ->where(['q.goods_id',$goods_id]) ->order('q.list_order desc,id desc') ->select(); //用户上次答题答案 $userQuestionAnswerModel = new UserQuestionAnswer(); foreach($question as $key => $q){ $user_question_answer = $userQuestionAnswerModel->where(['user_id'=>$user_id,'goods_id'=>$goods_id,'question_id'=>$q['id']])->find(); if(!empty($user_question_answer)){ //重新答题倒计时 $user_question_answer['backwards'] = $user_question_answer['resettime'] - time(); } $question[$key]['user_question_answer'] = $user_question_answer; } $this->success('SUCCESS'); } /** * 提交答案 * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function push_answer(){ $question_keys = $this->request->param('question_keys/a'); $answers = $this->request->param('answers/a'); $goods_id = $this->request->param('goods_id',0,'intval'); $user_id = $this->request->param('user_id',0,'intval'); // print_r($question_keys); // print_r($answers); // exit(); if(empty($question_keys) || empty($answers) || empty($goods_id) || empty($user_id)){ $this->error('404'); } $user = Db::name('user')->where(['id'=>$user_id])->find();//用户信息 $goods = Db::name('goods')->where(['id'=>$goods_id])->find();//广告信息 $admin = Db::name('admin')->where(['id'=>$goods['admin_id']])->find();//商户信息 //判断广告信息是否过期 if($goods['end_time'] < time()){ $this->error('广告已过截止时间'); } Db::startTrans(); //清空上次答题记录 $question_error = []; $resettime = 0; $result1 = Db::name('user_question_answer')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->delete(); foreach($question_keys as $key => $question_key){ if(!isset($question_key)){ $this->error("第".($key+1)."个question_key不能为空"); } if(empty($answers[$key])){ $this->error("第".($key+1)."个answer不能为空"); } //判断是否正确 $question = json_decode($goods['question'],true); if($question[$question_key]['question_answer'] == $answers[$key]){ $is_correct = 1; }else{ $question_error[] = $question_key; $is_correct = 0; $arr['resettime'] = time() + 300; $resettime = $arr['resettime'] - time(); } $arr['user_id'] = $user_id; $arr['goods_id'] = $goods_id; $arr['question_key'] = $question_key; $arr['answer'] = $answers[$key]; $arr['type'] = $question[$question_key]['question_type']; $arr['is_correct'] = $is_correct; $arr['createtime'] = time(); $result2 = Db::name('user_question_answer')->insert($arr); if(empty($result2)){ Db::rollback(); $this->error('sql执行失败'); } } //判断本次答题是否全部正确 $exp = 0; if(empty($question_error)){ //全部正确获取红包券 //判断是否获取过该广告的红包 $user_exp_log = Db::name('user_exp_log')->where(['type'=>4,'goods_id'=>$goods_id])->find(); if(!empty($user_exp_log)){ Db::rollback(); $this->error('您已获得过该广告的奖励'); } //判断商家余额是否够用 if($admin['money'] < $goods['exp']){ Db::rollback(); $this->error('商家余额不足'); } //用户获得奖励 $arr1['user_id'] = $user_id; $arr1['before_exp'] = $user['exp']; $arr1['exp'] = $goods['exp']; $arr1['after_exp'] = $user['exp']+$goods['exp']; $arr1['type'] = 4; $arr1['goods_id'] = $goods_id; $arr1['createtime'] = time(); $result3 = Db::name('user_exp_log')->insert($arr1); if(empty($result3)){ Db::rollback(); $this->error('sql执行失败'); } $result4 = Db::name('user')->where(['id'=>$user_id])->update(['exp'=>$user['exp']+$goods['exp']]); //扣除商家余额 $arr2['admin_id'] = $admin['id']; $arr2['before_money'] = $admin['money']; $arr2['money'] = $goods['exp']; $arr2['after_money'] = $admin['money']-$goods['exp']; $arr2['type'] = 5; $arr2['goods_id'] = $goods_id; $arr2['createtime'] = time(); $result5 = Db::name('user_money_log')->insert($arr2); if(empty($result5)){ Db::rollback(); $this->error('sql执行失败'); } $result6 = Db::name('admin')->where(['id'=>$admin['id']])->update(['money'=>$admin['money']-$goods['exp']]); //判断是否有上级 if(!empty($user['parent_id'])){ //用户上级信息 $parent = Db::name('user')->where(['id'=>$user['parent_id']])->find(); //平台承担上级的红包券 //获取上级获得奖券比例 $exp_ratio = Db::name('exp_ratio')->where(['id'=>2])->find(); //用户上级获取奖励 $arr3['user_id'] = $user['parent_id']; $arr3['before_exp'] = $parent['exp']; $arr3['exp'] = $goods['exp']*$exp_ratio['ratio']*0.01; $arr3['after_exp'] = $arr3['exp']+$parent['exp']; $arr3['type'] = 5; $arr3['goods_id'] = $goods_id; $arr3['to_user_id'] = $user_id; $arr3['createtime'] = time(); $result7 = Db::name('user_exp_log')->insert($arr3); if(empty($result7)){ Db::rollback(); $this->error('sql执行失败'); } $result8 = Db::name('user')->where(['id'=>$user['parent_id']])->update(['exp'=>$arr3['exp']+$parent['exp']]); } } Db::commit(); $this->success('SUCCESS','',['question_error'=>$question_error,'exp'=>$goods['exp'],'resettime'=>$resettime]); } }