审查视图

application/home/controller/Question.php 8.7 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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
{
    /**
王晓刚 authored
19
     * 获取题(废弃)
王晓刚 authored
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
     * @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
     */
王晓刚 authored
88
    public function push_answer(){
王晓刚 authored
89 90
        $question_keys = $this->request->param('question_keys/a');
        $answers = $this->request->param('answers/a');
王晓刚 authored
91 92
        $goods_id = $this->request->param('goods_id',0,'intval');
        $user_id = $this->request->param('user_id',0,'intval');
王晓刚 authored
93 94 95 96
//        print_r($question_keys);
//        print_r($answers);
//        exit();
        if(empty($question_keys) || empty($answers) || empty($goods_id) || empty($user_id)){
王晓刚 authored
97 98 99 100
            $this->error('404');
        }
        $user = Db::name('user')->where(['id'=>$user_id])->find();//用户信息
        $goods = Db::name('goods')->where(['id'=>$goods_id])->find();//广告信息
王晓刚 authored
101
        $admin = Db::name('admin')->where(['id'=>$goods['admin_id']])->find();//商户信息
王晓刚 authored
102 103 104 105 106 107
        //判断广告信息是否过期
        if($goods['end_time'] < time()){
            $this->error('广告已过截止时间');
        }
        Db::startTrans();
        //清空上次答题记录
王晓刚 authored
108 109 110
        $question_error = [];
        $resettime = 0;
        $result1 = Db::name('user_question_answer')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->delete();
王晓刚 authored
111
        foreach($question_keys as $key => $question_key){
王晓刚 authored
112
            if(!isset($question_key)){
王晓刚 authored
113
                $this->error("第".($key+1)."个question_key不能为空");
王晓刚 authored
114 115 116 117 118
            }
            if(empty($answers[$key])){
                $this->error("第".($key+1)."个answer不能为空");
            }
            //判断是否正确
王晓刚 authored
119
            $question = json_decode($goods['question'],true);
王晓刚 authored
120
            if($question[$question_key]['question_answer'] == $answers[$key]){
王晓刚 authored
121 122
                $is_correct = 1;
            }else{
王晓刚 authored
123
                $question_error[] = $question_key;
王晓刚 authored
124 125
                $is_correct = 0;
                $arr['resettime'] = time() + 300;
王晓刚 authored
126
                $resettime = $arr['resettime'] - time();
王晓刚 authored
127 128 129
            }
            $arr['user_id'] = $user_id;
            $arr['goods_id'] = $goods_id;
王晓刚 authored
130
            $arr['question_key'] = $question_key;
王晓刚 authored
131
            $arr['answer'] = $answers[$key];
王晓刚 authored
132
            $arr['type'] = $question[$question_key]['question_type'];
王晓刚 authored
133 134 135 136 137 138 139 140 141
            $arr['is_correct'] = $is_correct;
            $arr['createtime'] = time();
            $result2 = Db::name('user_question_answer')->insert($arr);
            if(empty($result2)){
                Db::rollback();
                $this->error('sql执行失败');
            }
        }
        //判断本次答题是否全部正确
王晓刚 authored
142
        $exp = 0;
王晓刚 authored
143 144 145 146 147
        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)){
王晓刚 authored
148
                Db::rollback();
王晓刚 authored
149 150 151
                $this->error('您已获得过该广告的奖励');
            }
            //判断商家余额是否够用
王晓刚 authored
152
            if($admin['money'] < $goods['exp']){
王晓刚 authored
153
                Db::rollback();
王晓刚 authored
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                $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']]);
            //扣除商家余额
王晓刚 authored
171
            $arr2['admin_id'] = $admin['id'];
王晓刚 authored
172
            $arr2['before_money'] = $admin['money'];
王晓刚 authored
173
            $arr2['money'] = $goods['exp'];
王晓刚 authored
174
            $arr2['after_money'] = $admin['money']-$goods['exp'];
王晓刚 authored
175 176 177 178 179 180 181 182
            $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执行失败');
            }
王晓刚 authored
183
            $result6 = Db::name('admin')->where(['id'=>$admin['id']])->update(['money'=>$admin['money']-$goods['exp']]);
王晓刚 authored
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            //判断是否有上级
            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();
王晓刚 authored
209
        $this->success('SUCCESS','',['question_error'=>$question_error,'exp'=>$goods['exp'],'resettime'=>$resettime]);
王晓刚 authored
210 211
    }
}