Question.php
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?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() + $goods['anew_time'];
$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,'user_id'=>$user_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']]);
}
//判断商户是否有上级
$admin_user = Db::name('user')->where(['id'=>$admin['user_id']])->find();
if(!empty($admin_user['parent_id'])){
//商户上级信息
$admin_user_parent = Db::name('user')->where(['id'=>$admin_user['parent_id']])->find();
//平台承担上级的红包券
//获取上级获得奖券比例
$exp_ratio = Db::name('exp_ratio')->where(['id'=>3])->find();
//用户上级获取奖励
$arr3['user_id'] = $admin_user['parent_id'];
$arr3['before_exp'] = $admin_user_parent['exp'];
$arr3['exp'] = $goods['exp']*$exp_ratio['ratio']*0.01;
$arr3['after_exp'] = $arr3['exp']+$admin_user_parent['exp'];
$arr3['type'] = 6;
$arr3['goods_id'] = $goods_id;
$arr3['to_user_id'] = $admin['user_id'];
$arr3['createtime'] = time();
$result9 = Db::name('user_exp_log')->insert($arr3);
if(empty($result9)){
Db::rollback();
$this->error('sql执行失败');
}
$result10 = Db::name('user')->where(['id'=>$admin_user['parent_id']])->update(['exp'=>$arr3['exp']+$admin_user_parent['exp']]);
}
}
Db::commit();
$this->success('SUCCESS','',['question_error'=>$question_error,'exp'=>$goods['exp'],'resettime'=>$resettime]);
}
}