TurnPrize.php
20.6 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
namespace app\serviceapi\controller;
use app\serviceapi\model\TurnPrizePrize as PrizePrizeModel;
use app\serviceapi\model\TurnPrizeWin as PrizeWinModel;
use app\serviceapi\model\TurnPrizeMember as PrizeMemberModel;
use app\serviceapi\model\TurnPrizeRule as PrizeRuleModel;
use app\serviceapi\model\PrizeToken as PrizeTokenModel;
use think\Request;
use util\MCurl;
use think\Cache;
use think\Log;
header('Access-Control-Allow-Origin:*');
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
/**
* 抽奖
* Created by PhpStorm.
* User: niuyongqing
* Date: 2020/9/20
* Time: 11:37
*/
class TurnPrize extends PingAn
{
/**
* 函数功能描述 获取微信token
* Date: 2020/9/25
* Time: 10:00
* @author nyq
*/
public function getAccessToken()
{
$prize_model = new PrizeTokenModel();
$prize = $prize_model->getToken();
return $prize;
}
/**
* 函数功能描述 更新微信token
* Date: 2020/9/25
* Time: 10:00
* @author nyq
*/
public function updateAccessToken($data)
{
//$data = $this->request->post();
if (isset($data['id']) && !empty($data['id'])) {
$where['id'] = $data['id'];
}
$prize_model = new PrizeTokenModel();
unset($data['id']);
$prize = $prize_model->updataToken($where,$data);
return $prize;
}
/**
* 函数功能描述 添加微信token
* Date: 2020/9/25
* Time: 10:00
* @author nyq
*/
public function addAccessToken($data)
{
//$data = $this->request->post();
$prize_model = new PrizeTokenModel();
$prize = $prize_model->addToken($data);
return $prize;
}
/**
* 函数功能描述 奖品列表
* Date: 2020/9/26
* Time: 10:00
* @author nyq
*/
public function prizeList($data)
{
$where=[];
if (isset($data['id']) && !empty($data['id'])) {
$where['id'] = $data['id'];
}
//姓名
if (isset($data['name']) && !empty($data['name'])) {
$where['name']=[ 'like', "%".$data['name']."%"];
//$where['name']=['like','% .$data['name']];
// $where['name'] = $data['name'];
}
$prize_model = new PrizePrizeModel();
$prize = $prize_model->getPrize($where, $data['offset'], $data['rows']);
return $prize;
}
/**
* 函数功能描述 添加奖品
* Date: 2020/9/25
* Time: 15:00
* @author nyq
*/
public function prizeAdd($data)
{
//$data = $this->request->post();
$prize_model = new PrizePrizeModel();
if(!empty($data['prize_id'])){
$where['id']=$data['prize_id'];
$prize_p = $prize_model->getPrizePrize($where);
if($prize_p['img'] != $data['img']){
$filename = ROOT_PATH . 'public' . $prize_p['img'];
if(file_exists($filename)){
unlink($filename);
}
}
unset($data['prize_id']);
$prize = $prize_model->prizeUp($where,$data);
}else{
unset($data['prize_id']);
$prize = $prize_model->prizeAdd($data);
}
return $prize;
}
/**
* 函数功能描述 中奖用户列表
* Date: 2020/9/26
* Time: 10:00
* @author nyq
*/
public function prizeUserList($data)
{
//$data = $this->request->post();
$where['is_del']=['<>',1];
//区部
if (isset($data['source']) && !empty($data['source']) && $data['source'] !=" ") {
$where['source']=$data['source'];
}
if (isset($data['id']) && !empty($data['id'])) {
$where['member_id']=$data['id'];
}
//时间
//时间搜索
if (isset($data['start']) && !empty($data['start']) && isset($data['end']) && !empty($data['end'])) {
$start_time = strtotime($data['start'].' 00:00:01');
$end_time = strtotime($data['end'].' 23:59:59');
$where['addTime'] = ['between', [$start_time, $end_time]];
} else if (isset($data['start']) && !empty($data['start']) && isset($data['end']) == false) {
$start_time = strtotime($data['start'].' 00:00:01');
$where['addTime'] = ['egt', $start_time];
} else if (isset($data['start']) == false && isset($data['end']) && !empty($data['end'])) {
$end_time = strtotime($data['end'].' 23:59:59');
$where['addTime'] = ['elt', $end_time];
}
//电话
if (isset($data['phone']) && !empty($data['phone'])) {
$wheres['phone']=$data['phone'];
$prize_member_model = new PrizeMemberModel();
$prize_member_phone_list = $prize_member_model->getPrizeMember($wheres);
$prize_member_phone_list=json_decode($prize_member_phone_list,true);
$array_type=array_column($prize_member_phone_list,'id');
$str = implode(",", $array_type);
$where['member_id']=['in',$str];
}
//姓名
if (isset($data['name']) && !empty($data['name'])) {
$wheres['uname']=['like',$data['name']];
$prize_member_model = new PrizeMemberModel();
$prize_member_phone_list = $prize_member_model->getPrizeMember($wheres);
$prize_member_phone_list=json_decode($prize_member_phone_list,true);
//return $prize_member_phone_list;
$array_type=array_column($prize_member_phone_list,'id');
$str = implode(",", $array_type);
$where['member_id']=['in',$str];
}
$where['receive']=1;
//获取中奖列表
$prize_win_model = new PrizeWinModel();
if (isset($data['to_excel']) && !empty($data['to_excel']) || isset($data['id']) && !empty($data['id'])) {
$prize_user = $prize_win_model->getPrizeWinToExcel($where);
}else{
$prize_user = $prize_win_model->getPrizeWin($where, $data['offset'], $data['rows']);
}
$prize_win=json_decode($prize_user,true);
$member_id = array_column($prize_win,'member_id');
//$prize_id = array_column($prize_win,'prize_id');
//中奖的用户列表
$map['id'] = ['in', $member_id];
$prize_member_model = new PrizeMemberModel();
$prize_member = $prize_member_model->getPrizeMember($map);
$prize_member=json_decode($prize_member,true);
//var_dump($prize_member);die;
//中奖的奖品列表
// $arr['id'] = ['in', $prize_id];
// $prize_prize_model = new PrizePrizeModel();
// $prize_prize = $prize_prize_model->getPrizeList($arr);
// $prize_prize=json_decode($prize_prize,true);
//合并数组
//用户
$name = array_column($prize_member, 'uname', 'id');
$phone = array_column($prize_member, 'phone', 'id');
//奖品
//$prizeName = array_column($prize_prize, 'name', 'id');
foreach($prize_win as $key => $item) {
$exist = array_key_exists($item['member_id'], $name);
$prize_win[$key]['uname'] = $exist ? $name[$item['member_id']] : '';
$exist = array_key_exists($item['member_id'], $phone);
$prize_win[$key]['phone'] = $exist ? $phone[$item['member_id']] : '';
// $exist = array_key_exists($item['prize_id'], $prizeName);
// $prize_win[$key]['prizeName'] = $exist ? $prizeName[$item['prize_id']] : '';
}
//TODO 如果是执行数据导出 那么就只返回 搜索的数据
if (isset($data['to_excel']) && !empty($data['to_excel'])) {
$result=$prize_win;
}else{
$result["total"] =$this->prizeUserCount($where);
$result['rows']=$prize_win;
}
return $result;
}
/**
* 函数功能描述 统计中奖用户数两
* Date: 2020/8/25
* Time: 15:00
* @author nyq
*/
public function prizeUserCount($where)
{
$prize_win_model = new PrizeWinModel();
$prize_user = $prize_win_model->getPrizeWinCount($where);
return $prize_user;
}
/**
* 函数功能描述 重置
* Date: 2020/9/25
* Time: 15:00
* @author nyq
*/
public function prizeReset()
{
//查询当前规则
$prize_rule_model = new PrizeRuleModel();
$prize_rule = $prize_rule_model->getPrizeRule();
$prize_rule=json_decode($prize_rule,true);
$arr['frequency']=$prize_rule['number'];
$wheres['frequency'] = ['neq', $prize_rule['number']];
$prize_member_model = new PrizeMemberModel();
$prize_member = $prize_member_model->prizeReset($wheres,$arr);
return $prize_member;
}
/**
* 函数功能描述 抽奖规则
* Date: 2020/9/25
* Time: 15:00
* @author nyq
*/
public function prizeRule()
{
//查询当前规则
$prize_rule_model = new PrizeRuleModel();
$prize_rule = $prize_rule_model->getPrizeRule();
return $prize_rule;
}
/**
* 函数功能描述 删除奖项
* Date: 2020/9/25
* Time: 15:00
* @author nyq
*/
public function delUser($data)
{
//$data = $this->request->post();
$where['id']=$data['id'];
$arr['is_del']=1;
$prize_win_model = new PrizeWinModel();
// $prize_user = $prize_win_model->delUser($where);
$prize_user = $prize_win_model->winReset($where,$arr);
return $prize_user;
}
/**
* 函数功能描述 编辑抽奖规则
* Date: 2020/9/27
* Time: 15:00
* @author nyq
*/
public function prizeRuleEdit($data)
{
//$data = $this->request->post();
$prize_rule_model = new PrizeRuleModel();
if(!empty($data['rule_id'])){
$where['id']=$data['rule_id'];
unset($data['rule_id']);
$urle = $prize_rule_model->prizeRullEdit($where,$data);
}else{
unset($data['rule_id']);
$urle = $prize_rule_model->prizeRullAdd($data);
}
return $urle;
}
/**
* 函数功能描述 合适身份信息
* Date: 2020/9/26
* Time: 10:00
* @author nyq
*/
public function selPrizeMember($data)
{
//$data = $this->request->post();
$where['openid']=$data['openid'];
//传递 openID prize_id number设置的次数
//获取奖品列表
$prize_model = new PrizePrizeModel();
$prize = $prize_model->getPrize('1=1', 0, 25);
$prize = json_decode($prize,true);
//把数组分割成一个是必中的12个$newPrize[0].一个是不中的10个$newPrize[1]
$newPrize=array_chunk($prize,12);
//设置12中不中奖的规则
$tempArr[0] = [$newPrize[1][0],$newPrize[1][5],$newPrize[1][8],$newPrize[1][7],$newPrize[1][9]];
$tempArr[1] = [$newPrize[1][1],$newPrize[1][3],$newPrize[1][2],$newPrize[1][6],$newPrize[1][8]];
$tempArr[2] = [$newPrize[1][3],$newPrize[1][5],$newPrize[1][4],$newPrize[1][9],$newPrize[1][2]];
$tempArr[3] = [$newPrize[1][5],$newPrize[1][10],$newPrize[1][1],$newPrize[1][9],$newPrize[1][0]];
$tempArr[4] = [$newPrize[1][8],$newPrize[1][6],$newPrize[1][7],$newPrize[1][3],$newPrize[1][1]];
$tempArr[5] = [$newPrize[1][9],$newPrize[1][5],$newPrize[1][4],$newPrize[1][6],$newPrize[1][3]];
$tempArr[6] = [$newPrize[1][6],$newPrize[1][9],$newPrize[1][10],$newPrize[1][3],$newPrize[1][1]];
$tempArr[7] = [$newPrize[1][3],$newPrize[1][8],$newPrize[1][0],$newPrize[1][2],$newPrize[1][4]];
$tempArr[8] = [$newPrize[1][4],$newPrize[1][6],$newPrize[1][5],$newPrize[1][1],$newPrize[1][2]];
$tempArr[9] = [$newPrize[1][11],$newPrize[1][5],$newPrize[1][1],$newPrize[1][8],$newPrize[1][0]];
$tempArr[10] = [$newPrize[1][8],$newPrize[1][11],$newPrize[1][4],$newPrize[1][3],$newPrize[1][5]];
$tempArr[11] = [$newPrize[1][10],$newPrize[1][6],$newPrize[1][8],$newPrize[1][9],$newPrize[1][0]];
// //TODO 随机生成五个不中的奖项
// $no = array();
// $tempArr = array_rand($newPrize[1],5);//随机取出二维数组的键
// foreach ( $tempArr as $value ){
// $no[] = $newPrize[1][$value];
// }
//判断当前用户是否登陆过
$prize_member_model = new PrizeMemberModel();
$prize_member = $prize_member_model->getPrizeMember($where);
$prize_member = json_decode($prize_member,true);
//return $prize_member;
if(count($prize_member)==0){
//判断如果以openID为基准的用户不存在,就新增当前用户
$rest['openid']=$data['openid'];
$rest['frequency']=$data['number'];
$rest['addTime']=time();
$prize_member_model->getPrizeMemberAdd($rest);
//TODO 设置奖项和抽奖机会
//必中的奖项
$yes[]=$newPrize[0][0];
$no=$tempArr[0];
$prizeList=array_merge($yes,$no);
$request=['hade'=>$rest,'data'=>$prizeList];
}else{
//TODO 1,先判断有无抽奖机会
//获取到抽奖记录
$map['member_id']=$prize_member[0]['id'];
$prize_win_model = new PrizeWinModel();
$prize_user = $prize_win_model->getPrizeWinlist($map);
if(count($prize_user)>0){
$prizetime=date('n',$prize_user[0]['addTime']);//记录中最新抽奖月份
}else{
$prizetime=date('n',time());//记录中最新抽奖月份
}
$newtime=date('n',time()); //当前月份
//用于抽奖的用户信息
$rest['openid']=$data['openid'];
$rest['addTime']=time();
//判断,如果抽奖记录月份比当前月份小或者是抽奖机会是1 就证明本次登录有抽奖机会 并且修改数据库中的抽奖次数
if($prizetime == $newtime && $prize_member[0]['frequency']==0){
$rest['frequency']=0;
}else{
$rest['frequency']=1;
$wheres['id']=$prize_member[0]['id'];
//更新当前用户的抽奖信息
$prize_member_model = new PrizeMemberModel();
$prize_member_model->prizeReset($wheres,$rest);
}
//todo 2,判断当前应当抽中的奖品
$num=count($prize_user);
//TODO 设置必中奖项
if ($num == 11) {
$yes_num = 1;
} elseif ($num > 11) {
$yes_num = $num - 11-1;
} else {
$yes_num = $num;
}
$yes[]=$newPrize[0][$yes_num];
//todo 设置不中奖项
$no=$tempArr[$yes_num];
$prizeList=array_merge($yes,$no);
$request=['hade'=>$rest,'data'=>$prizeList];
}
return $request;
}
/**
* 函数功能描述 抽奖接口
* Date: 2020/9/26
* Time: 10:00
* @author nyq
*/
public function luckDraw($data)
{
//$data = $this->request->post();
//传递 openID prize_id
$map['openid']=$data['openid'];
$prize_member_model = new PrizeMemberModel();
$prize_member = $prize_member_model->getPrizeMember($map);
$prize_member=json_decode($prize_member,true);
if(($prize_member[0]['frequency'] - 1) < 0){
$res['openid']=$prize_member[0]['openid'];
$res['number']=-1;
return $res;//抽奖次数不足
}else{
$where['id']=$prize_member[0]['id'];
$are['frequency']=$prize_member[0]['frequency']-1;
$prize_me = $prize_member_model->prizeReset($where,$are);
//生成中奖用户信息
$arr['member_id']=$prize_member[0]['id'];
$arr['prize_id']=$data['prize_id'];
$arr['prize_name']=$data['prize_name'];
$arr['receive']=0;
$arr['source']=$data['source'];
$arr['addTime']=time();
$prize_win_model = new PrizeWinModel();
$prize_user = $prize_win_model->prizeAdd($arr);
$prize_win=json_decode($prize_user,true);
//减库存
$prize['id']=$data['prize_id'];
//查询当前奖品的数量
$prize_prize_model = new PrizePrizeModel();
$prize_p = $prize_prize_model->getPrizePrize($prize);
$prize_p=json_decode($prize_p,true);
$aary['number']=$prize_p['number'] - 1;
if($aary['number'] == 0){
$aary['probability']=0;
}
$prize_set = $prize_prize_model->prizeUp($prize,$aary);
}
$res['openid']=$prize_member[0]['openid'];
$res['number']=$prize_member[0]['frequency']-1; //剩余
$res['prize_number']=$aary['number'];
return $res;
}
/**
* 函数功能描述 中奖用户列表最新 20条
* Date: 2020/9/26
* Time: 10:00
* @author nyq
*/
public function prizeWinList($data)
{
//$data = $this->request->post();
$where[]=[];
//姓名
$prize_member_model = new PrizeMemberModel();
if (isset($data['openid']) && !empty($data['openid'])) {
$where['openid'] = $data['openid'];
$prize_member = $prize_member_model->getPrizeMember($where);
unset($where['openid']);
$where['member_id']=$prize_member[0]['id'];
}
//获取中奖列表
$prize_win_model = new PrizeWinModel();
$prize_user = $prize_win_model->getPrizeWinlist($where);
$prize_win=json_decode($prize_user,true);
$member_id = array_column($prize_win,'member_id');
//$prize_id = array_column($prize_win,'prize_id');
//中奖的用户列表
$map['id'] = ['in', $member_id];
$prize_member = $prize_member_model->getPrizeMember($map);
$prize_member=json_decode($prize_member,true);
//var_dump($prize_member);die;
//中奖的奖品列表
// $arr['id'] = ['in', $prize_id];
// $prize_prize_model = new PrizePrizeModel();
// $prize_prize = $prize_prize_model->getPrizeList($arr);
// $prize_prize=json_decode($prize_prize,true);
//合并数组
//用户
$name = array_column($prize_member, 'uname', 'id');
$phone = array_column($prize_member, 'phone', 'id');
//奖品
// $prizeName = array_column($prize_prize, 'name', 'id');
foreach($prize_win as $key => $item) {
$exist = array_key_exists($item['member_id'], $name);
$prize_win[$key]['uname'] = $exist ? $name[$item['member_id']] : '';
$exist = array_key_exists($item['member_id'], $phone);
$prize_win[$key]['phone'] = $exist ? $phone[$item['member_id']] : '';
// $exist = array_key_exists($item['prize_id'], $prizeName);
// $prize_win[$key]['prizeName'] = $exist ? $prizeName[$item['prize_id']] : '';
}
//新增随机抽奖记录中,没有用户名不显示
if (!isset($data['openid']) && empty($data['openid'])) {
$arr =array();
foreach($prize_win as $key =>$val){
if($val['uname'] !="") {
$arr[$key]['uname']=$val['uname'];
$arr[$key]['prizeName']=$val['prizeName'];
$arr[$key]['addTime']=$val['addTime'];
}
}
return $arr;
}else{
return $prize_win;
}
}
/**
* 函数功能描述 兑奖接口
* Date: 2020/9/26
* Time: 10:00
* @author nyq
*/
public function cashPrize($data)
{
//$data = $this->request->post();
//传递 openID prize_id
$map['openid']=$data['openid'];
$map['uname']=$data['name'];
$map['phone']=$data['phone'];
$where['openid'] = $data['openid'];
$prize_member_model = new PrizeMemberModel();
$prize_member = $prize_member_model->getMember($where);
//$prize_member=json_decode($prize_member,true);
unset($where['openid']);
$aa=[];
$aa['id'] = $prize_member['id'];
//补全用户信息
$prize_mem = $prize_member_model->prizeReset($aa,$map);
$bb=[];
$bb['member_id'] = $prize_member['id'];
$prize_win_model = new PrizeWinModel();
$arr['receive'] = 1;
$prize_user = $prize_win_model->winReset($bb,$arr);
//$prize_win=json_decode($prize_user,true);
return $prize_user;
}
}