Index.php
55.7 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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
<?php
/**
* tpshop
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Author: 当燃
* Date: 2015-09-09
*/
namespace app\timer\controller;
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
use think\Cookie;
class Index extends Controller {
//定时器任务,统计一个月注册成为分销商会员的数量,0点任务
public function timeset(){
$date=date('Y-m-d H:i:s');
mlog('时间是:'.$date,'timeset');
//$year = date("Y"); $month = date("m"); $day = date("d");
//当时间是1号的时候,即月初
/*---if($day==1) {}---*/
$stoid=I('stoid');
if(empty($stoid)) {
$uu = M("store_module_endtime")->where('type', 2)
->where('end_time<' . time())->select();
if ($uu) {
$stoidarr = get_arr_column($uu, 'store_id');
M('store_distribut')->where('store_id', 'in', $stoidarr)->save(['distribut_num' => 0]);
mlog('过期商家:' . json_encode($stoidarr),'timeset');
}
}else{
$uu = M("store_module_endtime")->where('type', 2)->where('store_id',$stoid)
->where('end_time<' . time())->find();
if ($uu) {
M('store_distribut')->where('store_id', $stoid)->save(['distribut_num' => 0]);
mlog('过期商家:' . $stoid,'timeset');
}
}
$this->clear_order($stoid);
$time=strtotime('-1 day');
M("storage_recharge")->where('billdate<'.$time)
->where('transaction_id','')
->where('recharge_state',0)->delete();
}
//十分钟的工作任务,针对天天拼单的方法
public function time_minutes(){
$date=date('Y-m-d H:i:s');
$now_time=time();
mlog('时间是:'.$date."-".$now_time,'time_minutes');
//对于一些阶梯团,判断这些阶梯团是否成团,
//同时计算尾款的金额
if(empty($stoid)) {
$olist1 =M('teamgroup')->where('team_type',3)->where('state',2)->select();
foreach ($olist1 as $k=>$v){
//当这个团已经到时间了
if($v['kt_end_time']<=$now_time) {
//查找成团人数
$ordarr = M('order')->where('is_zsorder', 4)->where('pt_listno', $v['listno'])
->where('pt_prom_id', $v['team_id'])->where('pt_status', 1)
->where('store_id',$v['store_id'])
->where('order_status=0 or order_status=1')
->field('order_id,order_sn,order_amount,user_id,store_id,pt_prom_id,user_money,shipping_price,pt_status')
->select();
$count=count($ordarr);
$jsarr=json_decode($v['jt_json'],true);
//如果阶梯团大于这个人数,就是成团了,
if($count>=$jsarr[0]['rynum']){
$endtime=strtotime('+24 hour');
M('teamgroup')->where('id',$v['id'])
->save(['state'=>4,'wk_end_time'=>$endtime,'jt_ct_num'=>$count,'buyenddate'=>$now_time]);
//要计算尾款的价钱
$price=0;
foreach ($jsarr as $k=>$v){ if($count>=$v['rynum']) $price=$v['price'];}
///$oid=get_arr_column($ordarr,'order_id');
foreach ($ordarr as $kp=>$vp){
//更新订单商品表的单价
$dd['goods_price']=$price;
$dd['member_goods_price']=$price;
M('order_goods')->where('order_id',$vp['order_id'])->save($dd);
//更新尾款,要加上物流费用的计算
$num= M('order_goods')->where('order_id',$vp['order_id'])->sum('goods_num');
M('order')->where('order_id',$vp['order_id'])
->save(['pt_status'=>2,
'pt_tail_money'=>$price*$num+$vp['shipping_price']-$vp['order_amount']-$vp['user_money']
]);
M('order_goods')->where('order_id',$vp['order_id'])
->save(['goods_price'=>$price,'member_goods_price'=>$price,'account'=>$price]);
}
//如果是满团,就要进行推送
$timer = new \app\timer\controller\Index();
$olist=$ordarr;
if($olist){
foreach ($olist as $k=>$v){
$stoid=$v['store_id'];
$title='拼团满团,等待支付尾款';
$remark='感谢您的参与~';
$usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find();
$erpid=tpCache('shop_info.ERPId',$stoid);
$gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find();
$teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find();
$backurl=curHostURL(). "/Mobile/User/order_detail/id/".$v['order_id']."/stoid/".$stoid;
$timer->post_msg($stoid,$erpid,$usr['user_id'],$title,
$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl);
}
}
}else{
//未成团处理
//M('teamgroup')->where('id',$v['id'])->save(['buyenddate'=>time(),'state'=>1]);
$this->out_tuan($v);
}
}
}
}else{
$this->deal_jt_pt($stoid);
}
//以下是拼单退款处理
include_once "plugins/payment/weixin/weixin.class.php";
$wx=new \weixin();
//对于拼单商品要进行退款。
//俩种,1中是未成团 2是尾款没有支付完成
if(empty($stoid)) {
//-------处理30分钟都还没有成团的商家团-------
$tt=strtotime("-30 minute");
$tgr_ten_mini_list=M('teamgroup')
->where('addtime<'.$tt)
->where('state',2)
->where('team_type',1)->order('store_id asc,team_id asc')->select();
//对所有的未成团的进行中的团,要进行拆解
if($tgr_ten_mini_list){
$tg_id=null;
foreach ($tgr_ten_mini_list as $kk=>$vv){
$tg_id[$vv['team_id']][]=['tgr_id'=>$vv['id'],'listno'=>$vv['listno'],'stoid'=>$vv['store_id']];
}
foreach($tg_id as $kh=>$vh){
//如果只有一个就重新循环
if(count($vh)==1) continue;
//把订单ID收集起来
$ord_id=[];
foreach($vh as $kb=>$vb){
$ord=M("order")->where('pt_listno',$vb['listno'])->where('pt_status',1)
->where('store_id',$vb['stoid'])->field('order_id')->select();
if($ord){
foreach ($ord as $kj=>$vj){
$ord_id[]=$vj['order_id'];
}
}
//删除团
M("teamgroup")->where('id',$vb['tgr_id'])->delete();
}
//订单的listno都清空
M('order')->where("order_id","in",$ord_id)->save(['pt_listno'=>'']);
//按ct_num分组
$tlist=M('teamlist')->where('id',$kh)->field('ct_num,store_id,kt_time')->find();
//分组成团
$this->fen_zu_treamgroup($ord_id,
$tlist['ct_num'],$kh,$tlist['store_id'],$tlist['kt_time']);
}
}
//处理未成团,并且已经过期的团,商家团
$tgrlist=M('teamgroup')
->where('kt_end_time<'.$now_time)
->where('state',2)
->where('team_type',1)->select();
if($tgrlist) {
foreach ($tgrlist as $kl => $vl) {
$tlist = M('teamlist')->where('store_id', $vl['store_id'])
->where('id', $vl['team_id'])
->field('goods_num,ct_num,buy_num')->find();
$olist = M('order')->where('store_id', $vl['store_id'])
->where('pt_prom_id', $vl['team_id'])
->where('pt_listno', $vl['listno'])
->where('order_status', 1)
->where('pt_status', 1)
//->field('order_id,order_sn,order_amount,user_id,store_id,pt_listno,pt_prom_id,user_money,pt_status')
->select();
mlog("商家团:".$vl['listno']."-".json_encode($olist),"time_minutes/".$vl['store_id']);
if ($olist) {
$count = count($olist);
$oidarr = get_arr_column($olist, 'order_id');
//最后一个人买完,且没有库存,虽然达不到成团人数,那么这个团让它成团
/*
if ($tlist['goods_num'] - $tlist['buy_num']<=0) {
M('teamgroup')->where('id', $vl['id'])->save(['state' => 3, 'buyenddate' => time()]);
M('order')->where('store_id', $vl['store_id'])
->where('order_id', 'in', $oidarr)
->save(['pt_status' => 2, 'pay_status' => 1, 'order_status' => 1]);
//如果是满团,就要进行推送
$timer = new \app\timer\controller\Index();
if ($olist) {
foreach ($olist as $k => $v) {
$stoid = $v['store_id'];
$title = '拼团满团成功';
$remark = '感谢您的参与~';
$usr = M('users')->where('user_id', $v['user_id'])->field('user_id')->find();
$erpid = tpCache('shop_info.ERPId', $stoid);
$gd = M('order_goods')->where('order_id', $v['order_id'])->field('goods_name')->find();
$teamlist = M('teamlist')->where('id', $v['pt_prom_id'])->field('ct_num')->find();
$backurl= curHostURL() ."/index.php/Mobile/team/team_success/orderno/".$v['order_sn']."/stoid/".$stoid;
$timer->post_msg($stoid, $erpid, $usr['user_id'], $title, $gd['goods_name'],
$teamlist['ct_num'], $remark,$backurl);
}
}
} else {
*/
foreach ($olist as $k => $v) {
if($v['pt_status']==1)
$this->back_order_money($v,$wx);
}
//更新团的结束时间,对失效的团要进行处理
M('teamgroup')->where('id', $vl['id'])->save(['buyenddate' => $now_time, 'state' => 1]);
//}
}else{
//删除一些无效的商家团
M('teamgroup')->where('id',$vl['id'])->delete();
}
}
}
//对于订单要进行退款,处理会员团
$tgrlist1=M('teamgroup')
->where('state',2)
->where('kt_end_time<'.$now_time)
->where('team_type=2')
->select();
if($tgrlist1) {
$idarr = get_arr_column($tgrlist1, 'id');
//M('teamgroup')->where('id', 'in', $idarr)->save(['state' => 1, 'buyenddate' => time()]);
foreach ($tgrlist1 as $kt=>$vt) {
//对于订单要进行退款,处理会员团,阶梯团
$olist = M('order')
->where('store_id', $vt['store_id'])
->where('pt_prom_id', $vt['team_id'])
->where('pt_listno', $vt['listno'])
->where('order_status=0 or order_status=1')
->select();
mlog("会员团:".$vt['listno']."-".json_encode($olist),"time_minutes/".$vt['store_id']);
if ($olist) {
foreach ($olist as $k => $v) {
//有支付的订单
if($v['pt_status']==1) {
$this->back_order_money($v,$wx);
}
//未支付的订单
else if($v['pt_status']==0){
$this->cancel_order($v);
}
}
}
M('teamgroup')->where('id', $vt['id'])->save(['state' => 1, 'buyenddate' => $now_time]);
}
}
//处理阶梯团,但是尾款未支付的订单
$olist1=M('order')->alias('a')->join('teamgroup b','b.listno=a.pt_listno and a.pt_prom_id=b.team_id')
->where('a.is_zsorder', 4)
->where('b.wk_end_time<'.$now_time)
->where('a.pt_status',2)
->where('b.state',4)
->where('a.pay_status=0')->field('a.*,b.id as tgid')->select();
if($olist1){
$idarr=get_arr_column($olist1,'tgid');
$idarr=array_unique($idarr);
//更新订单状态
foreach ($olist1 as $k=>$v) {
if($v['pt_status']<>4 && $v['pt_status']<>6 ){
//未支付尾款的订单,取消订单
$rw= M('order')->where('order_id', $v['order_id'])
->where('order_status<>3')
->save(['order_status' => 3, 'pt_status' => 6]);
if($rw) {
//M("teamgroup")->where('listno', $v['pt_listno'])
//->where('team_id', $v['pt_prom_id'])
//->save(['state' => 5]);
mlog('pingtuan尾款过期的订单:' . json_encode($olist1), 'timeset');
$this->back_store_redis($v, '拼团未支付尾款失败');
}
}
M('teamgroup')->where('id',$v['tgid'])->save(['state'=>5]);
}
}
}
else{
$this->claer_pt_out($stoid);
}
//处理一些已经支付了的订单,余额支付的
$this->update_set_order($stoid);
//十分钟处理赠送礼品的订单
$this->deal_lipin_order();
//十分钟处理推送
wechat_store_wxsend();
wechat_user_wxsend();
}
//在进入拼单详情页时候要进行处理
public function claer_pt_out($stoid){
//以下是拼单退款处理
include_once "plugins/payment/weixin/weixin.class.php";
$wx=new \weixin();
//处理未成团,并且已经过期的团,商家团
$tgrlist=M('teamgroup')
->where('store_id',$stoid)
->where('kt_end_time<'.time())
->where('state',2)
->where('team_type',1)->select();
if($tgrlist) {
foreach ($tgrlist as $kl => $vl) {
$tlist = M('teamlist')->where('store_id', $vl['store_id'])
->where('id', $vl['team_id'])
->field('goods_num,ct_num,buy_num')->find();
$olist = M('order')->where('store_id', $vl['store_id'])
->where('pt_prom_id', $vl['team_id'])
->where('pt_listno', $vl['listno'])
->where('order_status', 1)
->where('pt_status', 1)
//->field('order_id,order_sn,order_amount,user_id,store_id,pt_listno,pt_prom_id,user_money')
->select();
if ($olist) {
$count = count($olist);
$oidarr = get_arr_column($olist, 'order_id');
//最后一个人买完,且没有库存,虽然达不到成团人数,那么这个团让它成团
if ($tlist['goods_num'] - $tlist['buy_num']<=0) {
M('teamgroup')->where('id', $vl['id'])->save(['state' => 3, 'buyenddate' => time()]);
M('order')->where('store_id', $vl['store_id'])
->where('order_id', 'in', $oidarr)
->save(['pt_status' => 2, 'pay_status' => 1, 'order_status' => 1]);
//如果是满团,就要进行推送
$timer = new \app\timer\controller\Index();
if ($olist) {
foreach ($olist as $k => $v) {
$stoid = $v['store_id'];
$title = '拼团满团成功';
$remark = '感谢您的参与~';
$usr = M('users')->where('user_id', $v['user_id'])->field('user_id')->find();
$erpid = tpCache('shop_info.ERPId', $stoid);
$gd = M('order_goods')->where('order_id', $v['order_id'])->field('goods_name')->find();
$teamlist = M('teamlist')->where('id', $v['pt_prom_id'])->field('ct_num')->find();
$backurl= curHostURL() ."/index.php/Mobile/team/team_success/orderno/".$v['order_sn']."/stoid/".$stoid;
$timer->post_msg($stoid, $erpid, $usr['user_id'],
$title, $gd['goods_name'], $teamlist['ct_num'], $remark,$backurl);
}
}
} else {
//更新团的结束时间,对失效的团要进行处理
M('teamgroup')->where('id', $vl['id'])->save(['buyenddate' => time(), 'state' => 1]);
foreach ($olist as $k => $v) {
$this->back_order_money($v,$wx);
}
}
}
}
}
//对于订单要进行退款,处理会员团
$tgrlist1=M('teamgroup')
->where('store_id',$stoid)
->where('state',2)
->where('kt_end_time<'.time())
->where('team_type=2')
->select();
if($tgrlist1) {
$idarr = get_arr_column($tgrlist1, 'id');
M('teamgroup')->where('id', 'in', $idarr)->save(['state' => 1, 'buyenddate' => time()]);
foreach ($tgrlist1 as $kt=>$vt) {
//对于订单要进行退款,处理会员团,阶梯团
$olist = M('order')
->where('store_id', $vt['store_id'])
->where('pt_prom_id', $vt['team_id'])
->where('pt_listno', $vt['listno'])
->where('order_status=0 or order_status=1')
->select();
if ($olist) {
foreach ($olist as $k => $v) {
//有支付的订单
if($v['pt_status']==1) {
$this->back_order_money($v,$wx);
}
//未支付的订单
else if($v['pt_status']==0){
$this->cancel_order($v);
}
}
}
}
}
//处理阶梯团,但是尾款未支付的订单
$olist1=M('order')->alias('a')->join('teamgroup b','b.listno=a.pt_listno and a.pt_prom_id=b.team_id')
->where('a.is_zsorder', 4)->where('a.store_id',$stoid)
->where('b.wk_end_time<'.time())
->where('a.pt_status',2)
->where('b.state',4)
->where('a.pay_status=0')->field('a.*,b.id as tgid')->select();
if($olist1){
$idarr=get_arr_column($olist1,'tgid');
$idarr=array_unique($idarr);
M('teamgroup')->where('id','in',$idarr)->save(['state'=>5]);
//更新订单状态
foreach ($olist1 as $k=>$v) {
if($v['pt_status']<>4){
//未支付尾款的订单,取消订单
$rw= M('order')->where('order_id', $v['order_id'])
->where('order_status<>3')
->save(['order_status' => 3, 'pt_status' => 6]);
//M("teamgroup")->where('listno', $v['pt_listno'])
//->where('team_id', $v['pt_prom_id'])
//->save(['state' => 5]);
if($rw){
mlog('pingtuan尾款过期的订单:' . json_encode($olist1), 'timeset');
$this->back_store_redis($v, '拼团未支付尾款失败');
}
}
}
}
}
//对于一些阶梯团,判断这些阶梯团是否成团
public function deal_jt_pt($stoid){
$olist1 =M('teamgroup')->where('team_type',3)->where('state',2)->where('store_id',$stoid)->select();
foreach ($olist1 as $k=>$v){
//当这个团已经到时间了
if($v['kt_end_time']<=time()) {
//查找成团人数
$ordarr = M('order')->where('is_zsorder', 4)->where('pt_listno', $v['listno'])
->where('pt_prom_id', $v['team_id'])->where('pt_status', 1)->where('store_id',$v['store_id'])
->where('order_status=0 or order_status=1')
->field('order_id,order_sn,order_amount,user_id,store_id,pt_prom_id,user_money,shipping_price')
->select();
$count=count($ordarr);
$jsarr=json_decode($v['jt_json'],true);
//如果阶梯团大于这个人数,就是成团了,
if($count>=$jsarr[0]['rynum']){
$endtime=strtotime('+24 hour');
M('teamgroup')->where('id',$v['id'])
->save(['state'=>4,'wk_end_time'=>$endtime,'jt_ct_num'=>$count,'buyenddate'=>time()]);
//要计算尾款的价钱
$price=0;
foreach ($jsarr as $k=>$v){ if($count>=$v['rynum']) $price=$v['price'];}
//$oid=get_arr_column($ordarr,'order_id');
foreach ($ordarr as $kp=>$vp){
//更新订单商品表的单价
$dd['goods_price']=$price;
$dd['member_goods_price']=$price;
M('order_goods')->where('order_id',$vp['order_id'])->save($dd);
//更新尾款,要计算物流费用
$num= M('order_goods')->where('order_id',$vp['order_id'])->sum('goods_num');
M('order')->where('order_id',$vp['order_id'])
->save(['pt_status'=>2,
'pt_tail_money'=>$price*$num+$vp['shipping_price']-$vp['order_amount']-$vp['user_money']
]);
}
//如果是满团,就要进行推送
$timer = new \app\timer\controller\Index();
$olist= $ordarr;
if($olist){
foreach ($olist as $k=>$v){
$stoid=$v['store_id'];
$title='拼团满团,等待支付尾款';
$remark='感谢您的参与~';
$usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find();
$erpid=tpCache('shop_info.ERPId',$stoid);
$gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find();
$teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find();
$backurl=curHostURL(). "/Mobile/User/order_detail/id/".$v['order_id']."/stoid/".$stoid;
$timer->post_msg($stoid,$erpid,$usr['user_id'],
$title,$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl);
}
}
}
else{
//未成团处理
M('teamgroup')->where('id',$v['id'])->save(['buyenddate'=>time(),'state'=>1]);
$this->out_tuan($v);
}
}
}
}
/*----公用函数---*/
public function back_store_redis($v,$title="拼团未满团到期失败"){
$stoid=$v['store_id'];
$usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find();
$erpid=tpCache('shop_info.ERPId',$stoid);
$gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find();
$teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find();
$remark='感谢您的参与~';
$backurl= curHostURL() ."/index.php/Mobile/team/team_success/orderno/".$v['order_sn']."/stoid/".$stoid;
$this->post_msg($stoid,$erpid,$usr['user_id'],$title,$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl);
//退回库存
$ord_good=M('order_goods')->where('order_sn',$v['order_sn'])->find();
// 减少商品销售量
M('goods')->where('goods_id', $ord_good['goods_id'])->setDec('sales_sum', $ord_good['goods_num']);
// 增加库存
M('goods')->where('goods_id', $ord_good['goods_id'])->setInc('store_count', $ord_good['goods_num']);
//补回活动数量
M('teamlist')->where('id',$v['pt_prom_id'])->setDec('buy_num',$ord_good['goods_num']);
M('teamlist')->where('id',$v['pt_prom_id'])->setDec('order_num');
//$redis=new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
//补回redis
//$name='pind'.$v['pt_prom_id'].'-'.$ord_good['store_id'];
$name=get_redis_name($v['pt_prom_id'],6,$ord_good['store_id']);
if(!doublefind($redis,$name,$ord_good['rec_id'])) {
for ($i = 0; $i < $ord_good['goods_num']; $i++) {
$redis->lPush($name, $ord_good['rec_id']. '_' . $i);
$jsda['num'] =$ord_good['goods_num'];
$jsda['order_sn'] = $ord_good['order_sn'];
$jsda['type'] = 6; //10分退款补回
mlog(json_encode($jsda), 'pt_log/' . $stoid . '/' .$ord_good['prom_id']."_3");
}
}
if($v['is_zsorder']==3){
//$name2 = 'pind'.$v['pt_prom_id'].'-'.$v['pt_listno'].$ord_good['store_id'];
$name2 = get_redis_name($v['pt_prom_id'],6,$ord_good['store_id']) .':'.$v['pt_listno'];
//$redis->delete($name2);
del_redis($redis,$name2);
}
}
//推送消息
public function post_msg($stoid,$erpid,$user_id,$title,$goods_name,$num,$remark,$backurl=""){
$colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#FF0000|#FF0000|#ca003a");
//$url = curHostURL() . "/home/api/poswxcode?typeid=1023&Acc=" . $erpid . "&ErpVipid=" . $erpvipid . "&title=" . $title . "&key1=" . $goods_name . "&key2=" . $num . "人团&remark=" . $remark.'&colorlist='. $colorlist."&backurl=" . $backurl;
//mlog($url,'poswxcode/'.$erpid);
$data['typeid']='1023';//微信消息类型 1001至1010
$data['title']=$title;
$data['remark']=$remark;
$data['key1']=$goods_name;
$data['key2']=$num.'人团';
$data['colorlist']=$colorlist;
$data['backurl']=$backurl;
$data['Acc']=$erpid;
$data['user_id']=$user_id;
$api = new \app\home\controller\Api();
//$resp=httpRequest($url, "GET");
$resp=$api->poswxcode2($data);
return $resp;
}
//处理过期的阶梯团里的订单
public function out_tuan($vt){
//以下是拼单退款处理
include_once "plugins/payment/weixin/weixin.class.php";
$wx=new \weixin();
//对于订单要进行退款,处理会员团,阶梯团
$olist = M('order')
->where('store_id', $vt['store_id'])
->where('pt_prom_id', $vt['team_id'])
->where('pt_listno', $vt['listno'])
->where('order_status=0 or order_status=1')
->select();
if ($olist) {
foreach ($olist as $k => $v) {
if($v['pt_status']==1) {
$this->back_order_money($v,$wx);
}else if($v['pt_status']==0){
$this->cancel_order($v);
}
}
M('teamgroup')->where('id',$vt['id'])->save(['buyenddate'=>time(),'state'=>1]);
}
}
//清除不是当天未付款的订单
public function clear_order($stoid=0){
$where="1=1";
if(!empty($stoid)){
$where.=" and store_id=".$stoid ;
}
$time=strtotime('-1 day');
//清除不是当天未付款的订单
$delTempOrder1 = M('order')
->where('pay_status=0 and order_status in(0,1) and is_zsorder=0')
->where("add_time<".$time)
->where($where)
->field("user_id,user_money,coupon_no,store_id,order_id,is_zsorder,pt_prom_id,pt_listno,is_pt_tz,gift_receive_id")
->select();
$delTempOrder2 = M('order')
->where($where)
->where('pt_status=0 and order_status in(0,1) and is_zsorder>1')
->where("add_time<".$time)
->field("user_id,user_money,coupon_no,store_id,order_id,is_zsorder,pt_prom_id,pt_listno,is_pt_tz,gift_receive_id")
->select();
$delTempOrder=array_merge($delTempOrder1,$delTempOrder2);
foreach ($delTempOrder as $k => $v) {
$ur= M('order')->where(array('order_id' => $v['order_id']))
->where('order_status<>3')
->save(['order_status' => 3]);
if($ur) {
$user_id = $v['user_id'];
$stoid= $v['store_id'];
// mlog($v['order_id'],"temporder");
// M('ordr_goods')->where(' order_id=')
if ($v['user_money'] > 0) {
//解冻余额
$rs = M('Users')->where("user_id", $v['user_id'])
->where('frozen_money>=' . $v['user_money'])->find();
if ($rs)
M('Users')->where("user_id", $v['user_id'])->setDec('frozen_money', $v['user_money']);
}
/*---优惠券冻结---*/
$quanno = $v['coupon_no'];
if (!empty($quanno)) {
$dada = ['CashRepNo' => $quanno, 'user_id' => $user_id];
//冻结优惠券
M('frozen_quan')->where($dada)->delete();
}
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
$hh = M('order_goods')->where('order_id', $v['order_id'])
->where('prom_type',1)->where('store_id',$stoid)
->field('order_id,goods_num,prom_id,order_sn,rec_id')->select();
if($hh) {
$odarr0 = get_arr_column($hh, 'order_id');
//增加队列
foreach ($hh as $k => $v) {
//$name0 ='ms'.$v['prom_id'] . '-' . $stoid;
$name0 =get_redis_name($v['prom_id'],1,$stoid);
//不重复退回redis
if(!doublefind($redis,$name0,$v['rec_id'])) {
for ($i = 0; $i < $v['goods_num']; $i++) {
$redis->lPush($name0, $v['rec_id'] . '_' . $i);
}
$jsda['num'] = $v['goods_num'];
$jsda['order_sn'] = $v['order_sn'];
$jsda['type'] = 1; //1是5分钟自动补回
mlog(json_encode($jsda), 'flash_log/' . $stoid . '/' . $v['prom_id']);
}
}
}
$hh2 = M('order_goods')->where('order_id', $v['order_id'])
->where('prom_type',2)->where('store_id',$stoid)
->field('order_id,goods_num,prom_id,order_sn,rec_id')->select();
if($hh2){
$odarr0 = get_arr_column($hh, 'order_id');
//增加队列
foreach ($hh2 as $k => $v) {
//$name_g ='grb'.$v['prom_id'] . '-' . $stoid;
$name_g =get_redis_name($v['prom_id'],2,$stoid);
//不重复退回redis
if(!doublefind($redis,$name_g,"2".$v['rec_id'])) {
for ($i = 0; $i < $v['goods_num']; $i++) {
$redis->lPush($name_g, "2".$v['rec_id'] . '_' . $i);
}
$jsda['num'] = $v['goods_num'];
$jsda['order_sn'] = $v['order_sn'];
$jsda['type'] = 1; //1是5分钟自动补回
mlog(json_encode($jsda), 'group_buy_log/' . $stoid . '/' . $v['prom_id']);
}
}
}
if ($v['is_zsorder'] == 3) {
//$name2 = 'pind' . $v['pt_prom_id'] . '-' . $v['pt_listno'] . $stoid;
$name2 = get_redis_name($v['pt_prom_id'],6,$stoid) . ':' . $v['pt_listno'];
if (!doublefind($redis, $name2, $v['order_id'])) {
$redis->lPush($name2, $v['order_id'] . '_0');
}
//$len = $redis->lLen($name2);
//如果这个团,全部补回,那这个单就没有意思了,要delete掉
//$teamnum = M('teamlist')->field('ct_num')
// ->where('id', $v['pt_prom_id'])->find();
//if ($len == $teamnum['ct_num']) {
if ($v['is_pt_tz']==1) {
//$redis->delete($name2);
del_redis($redis,$name2);
//要取消这个团
$da['state'] = 1;
$map['team_id'] = $v['pt_prom_id'];
$map['listno'] = $v['pt_listno'];
$map['store_id'] = $stoid;
M('teamgroup')->where($map)->save($da);
}
}
//补回购买的数量
$hh = M('order_goods')->where('order_id', $v['order_id'])
->where('prom_type',6)->where('store_id',$stoid)
->field('order_id,goods_num,prom_id,rec_id,order_sn')->select();
if($hh) {
//增加队列
foreach ($hh as $k => $v) {
//$name0 ='pind'.$v['prom_id'] . '-' . $stoid;
$name0 =get_redis_name($v['prom_id'],6,$stoid);
if(!doublefind($redis,$name0,$v['rec_id'])) {
for ($i = 0; $i < $v['goods_num']; $i++) {
$redis->lPush($name0, $v['rec_id'] . "_" . $i);
$jsdac['num'] = $v['goods_num'];
$jsdac['order_sn'] = $v['order_sn'];
$jsdac['type'] = 1; //1是5分钟自动补回
mlog(json_encode($jsdac), 'pt_log/' . $stoid . '/' . $v['prom_id']."_c");
}
}
}
}
if (!empty($v['gift_receive_id'])) {
//M('gift_receive')->where(['id' => ['in', $order['gift_receive_id']]])->delete();//去掉赠品领用记录
$org=M("order_goods")->where('order_id',$v['order_id'])
->where('is_gift',1)->where('store_id',$stoid)
->field('rec_id,gift_id,goods_num,prom_id')->select();
if($org){
foreach($org as $ko=>$vo){
//从表上删除记录,主表做标记,赠品被取消,同时补回活动的库存
//M('order_goods')->where('rec_id',$vo['rec_id'])->delete();
M("order")->where('order_id',$v['order_id'])->where('store_id',$stoid)
->save(['is_del_gift'=>1]);
M("gift")->where('store_id',$stoid)
->where('id',$vo['gift_id'])
->setInc('goods_num',$vo['goods_num']);
M('gift_receive')->where(["orderGoods_id"=>$vo['rec_id'],'user_id'=>$v['user_id']])->delete();
}
}
}
}
}
}
//退款
public function back_order_money($v,$wx){
$stoid = $v['store_id'];
$row= M('order')->where('store_id', $stoid)
->where('order_id', $v['order_id'])
->where('order_status<>3')
->save(['order_status' => 3, 'pt_status' => 3]);
if($row) {
$dd=null;
$refund_type = tpCache('basic.refund_type', $stoid);
//退到余额
if ($refund_type == 0) {
$user_money = $v['order_amount'] + $v['user_money'];
$sql = "update wxd_users set user_money=user_money+" . $user_money . " where user_id=" . $v['user_id'];
$rs1 = Db::execute($sql);
if ($rs1) {
if ($user_money > 0) {
$odata['user_id'] = $v['user_id'];
$odata['create_time'] = time();
$odata['money'] = $user_money;
$odata['remark'] = "退款";
$odata['store_id'] = $v['store_id'];
$odata['order_sn'] = $v['order_sn'];
$odata['status'] = 1;
$odata['type'] = 1;
M('withdrawals')->save($odata);
}
} else {
mlog($v['order_sn']."退款到余额失败,", 'refund/' . $stoid);
$dd['user_money']=$user_money;
}
} //原路返回
else if ($refund_type == 1) {
if ($v['order_amount'] > 0) {
$ordno = $v['order_sn'];
$total_fee = $v['order_amount'];
$refund_fee = $total_fee;
//$erpid = tpCache('shop_info.ERPId', $stoid);
$path = BASE_PATH . 'public/cert/' . $stoid . '/';
try {
//$wx->setCong($v['store_id']);
$rs = $wx->refund2($ordno, $total_fee, $refund_fee, $path,$stoid,null,$v['source_type']);
if ($rs['return_code'] == 'FAIL') {
mlog($v['order_sn']."退款失败," . $rs['return_msg'], 'refund/' . $stoid);
$dd['weixin_err']=$rs['return_msg'];
$dd['weixin']=$v['order_amount'];
}
} catch (\Exception $e) {
mlog($v['order_sn']."退款签名失败," . $e->getMessage(), 'refund/' . $stoid);
$dd['weixin_err']=$e->getMessage();
$dd['weixin']=$v['order_amount'];
}
}
if ($v['user_money'] > 0) {
$user_money = $v['user_money'];
$sql = "update wxd_users set user_money=user_money+" . $user_money . " where user_id=" . $v['user_id'];
$rs1 = Db::execute($sql);
if ($rs1) {
if ($user_money > 0) {
$odata['user_id'] = $v['user_id'];
$odata['create_time'] = time();
$odata['money'] = $user_money;
$odata['remark'] = "退款";
$odata['store_id'] = $v['store_id'];
$odata['order_sn'] = $v['order_sn'];
$odata['status'] = 1;
$odata['type'] = 1;
M('withdrawals')->save($odata);
}
} else {
mlog($v['order_sn']."退款到余额失败,", 'refund/' . $stoid);
$dd['user_money']=$v['user_money'];
}
}
}
//如果退款失败要进行记录
if($dd) M("order")->where('order_id',$v['order_id'])->save(['back_err_json'=>json_encode($dd)]);
//M("teamgroup")->where('listno', $v['pt_listno'])
// ->where('team_id', $v['pt_prom_id'])
// ->save(['state' => 1]);
mlog('pingtuan过期订单:' . json_encode($v), 'timeset');
$this->back_store_redis($v, '拼团未满团到期失败');
}
}
/*** 取消订单*/
public function cancel_order($v){
$order = $v;
$order_id=$v['order_id'];
$user_id=$v['user_id'];
$row = M('order')->where(array('order_id' => $order_id, 'user_id' => $user_id))
->where('order_status<>3')
->save(array('order_status' => 3));
if($row) {
//获取记录表信息
//$log = M('account_log')->where(array('order_id'=>$order_id))->find();
//有余额支付的情况
if ($order['user_money'] > 0) {
//解冻冻余额
$rs= M('Users')->where("user_id", $user_id)
->where('frozen_money>='.$order['user_money'])->find();
if($rs) {
M('Users')->where("user_id", $user_id)->setDec('frozen_money', $order['user_money']);
}
}
/*---优惠券冻结---*/
$quanno= $order['coupon_no'];
if(!empty($quanno)) {
$dada=['CashRepNo'=>$quanno,'user_id'=>$user_id];
//冻结优惠券
M('frozen_quan')->where($dada)->delete();
}
//查一下拼团
$stoid = $order['store_id'];
if ($order['is_zsorder'] >= 2) {
//$redis = new \Redis();
//$redis->connect(redisip, 6379);
$redis=get_redis_handle();
//查找会员团,删除团redis
$v = $order;
if ($v['is_zsorder'] == 3) {
//$name2 = 'pind' . $v['pt_prom_id'] . '-' . $v['pt_listno'] . $stoid;
$name2 = get_redis_name($v['pt_prom_id'],6,$stoid) . ':' . $v['pt_listno'];
//$redis->delete($name2);
del_redis($redis,$name2);
}
//补回购买的数量
$hh = M('order_goods')->where('order_id', $order_id)
->where('prom_type', 6)->where('store_id', $stoid)
->field('order_id,goods_num,prom_id,rec_id,order_sn')->select();
if ($hh) {
//增加队列
foreach ($hh as $k => $v) {
//$name0 = 'pind' . $v['prom_id'] . '-' . $stoid;
$name0 = get_redis_name($v['prom_id'],6,$stoid);
if (!doublefind($redis, $name2, $v['rec_id'])) {
for ($i = 0; $i < $v['goods_num']; $i++) {
$redis->lPush($name0, $v['rec_id'] . "_" . $i);
$jsdad['num'] =$v['goods_num'];
$jsdad['order_sn'] = $v['order_sn'];
$jsdad['type'] = 7; //10分取消订单退回
mlog(json_encode($jsdad), 'pt_log/' . $stoid . '/' . $v['prom_id']."_4");
}
}
}
}
}
}
}
//更新一些余额支付的订单
public function update_set_order($stoid=0){
$where="1=1";
if($stoid){$where.=" and store_id=".$stoid;}
//循环更新订单状态,以及库存
$odr_arr=M('order')
->where($where)
->where('user_id',$this->user_id)
->where('order_amount',0)
->where('(pay_status=0 and pt_prom_id=0) or (pt_status=0 and pt_prom_id>0)')
->where('order_status=0 or order_status=1')
->field('order_sn')->select();
if($odr_arr) {
foreach ($odr_arr as $k => $v) {
update_pay_status2($v['order_sn'], 1);
}
}
}
//处理十分都未支付的礼品订单
public function deal_lipin_order()
{
//查找有促销活动的商家
$now0 = time();
$where0 = "start_time<=" . $now0 . " and " . $now0 . "<=end_time and is_end=0";
$rrs = M('prom_goods')->where($where0)
->field('store_id')->select();
mlog("11".json_encode($rrs),"deal_lipin_order");
if(!$rrs) return;
$newarr = $this->clear_chongfu($rrs);
$tt=strtotime("-10 minute");
if(!$newarr) return;
//开始清理10分钟订单,未支付的订单
foreach ($newarr as $kv => $val) {
$stoid=$val['store_id'];
$order=M("order")
->where('order_status between 0 and 1')
->where("add_time<".$tt)
->where('pay_status',0)
->where('pay_time',0)
->where("store_id",$stoid)
->field("order_id,user_id")->select();
if(!$order) continue;
mlog(json_encode($order),"deal_lipin_order/".$stoid);
foreach($order as $k=>$v){
$org=M("order_goods")->where('order_id',$v['order_id'])
->where('is_gift',1)->where('store_id',$stoid)
->field('rec_id,gift_id,goods_num,prom_id')->select();
if(!$org) continue;
foreach($org as $ko=>$vo){
//从表上删除记录,主表做标记,赠品被取消,同时补回活动的库存
M('order_goods')->where('rec_id',$vo['rec_id'])->delete();
M("order")->where('order_id',$v['order_id'])->where('store_id',$stoid)
->save(['is_del_gift'=>1]);
M("gift")->where('store_id',$stoid)
->where('id',$vo['gift_id'])
->setInc('goods_num',$vo['goods_num']);
M('gift_receive')->where(["orderGoods_id"=>$vo['rec_id'],'user_id'=>$v['user_id']])->delete();
}
}
}
}
//--去掉重复的数组--
function clear_chongfu($data){
$u=null;
foreach ($data as $k=>$v){
if($u){
if(in_array($v,$u)) continue;
$u[]=$v;
}else{
$u[]=$v;
}
}
return $u;
}
//time_minutes
public function five_minutes(){
//找到所有有活动的商家
$now0=time();
$where0 = "(s_time<=" . $now0 . " or s_time<=" . $now0 . ") and " . $now0 . "<=e_time and is_end=0";
$where1="prom_type=1 or prom_type=2 or prom_type=6";
$rrs = M('activitylist')->where($where0)
->where($where1)
->field('store_id')->select();
if(!$rrs) return;
$newarr= $this->clear_chongfu($rrs);
if(!$newarr) return;
//开始清理5分钟订单
foreach ($newarr as $kv=>$val){
clear_flash_Order($val['store_id']);
clear_pind_Order($val['store_id']);
}
return "1";
}
/***
* -30分钟自动成团-
***/
public function fen_zu_treamgroup($odls,$ct_num,$team_id,$stoid,$kt_time)
{
$num = count($odls);
$i = $num / $ct_num;
if (($num % $ct_num) > 0) {
$i++;
}
for ($ii = 0; $ii < $i; $ii++) {
$fir = $ii * $ct_num;
$end = ($ii + 1) * $ct_num;
if ($end > $num) $end = $num;
$ud = null;
for ($j = $fir; $j < $end; $j++) {
$ud[] = $odls[$j];
}
//$ud如果是空,就要跳出
if (empty($ud)) break;
//生成期号
$qh = $team_id . "_" . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
//如果数量是达到成团数量,就要重新分期
if (count($ud) == $ct_num) {
$udata['pt_status'] = 2;
$udata['pt_listno'] = $qh;
$udata['pay_status'] = 1;
//修改订单的状态
M('order')->where('order_id', 'in', $ud)
->save($udata);
$t = time();
$grda['store_id'] = $stoid;
$grda['team_id'] = $team_id;
$grda['listno'] = $qh;
$grda['buystartdate'] = $t;
$grda['buyenddate'] = $t;
$grda['state'] = 3;
$grda['addtime'] = $t;
$grda['kt_end_time'] = $t;
M("teamgroup")->save($grda);
//如果是满团,就要进行推送
$timer = new \app\timer\controller\Index();
$olist = M('order')->where('order_id', 'in', $ud)->where('store_id', $stoid)
->field('store_id,user_id,order_id,pt_prom_id,order_sn')
->select();
if ($olist) {
foreach ($olist as $k => $v) {
$stoid = $v['store_id'];
$title = '拼团满团成功';
$remark = '感谢您的参与~';
$usr = M('users')->where('user_id', $v['user_id'])->field('erpvipid')->find();
$erpid = tpCache('shop_info.ERPId', $stoid);
$gd = M('order_goods')->where('order_id', $v['order_id'])->field('goods_name')->find();
$teamlist = M('teamlist')->where('id', $v['pt_prom_id'])->field('ct_num')->find();
$backurl = curHostURL() . "/index.php/Mobile/team/team_success/orderno/" . $v['order_sn'] . "/stoid/" . $stoid;
$timer->post_msg($stoid, $erpid, $usr['erpvipid'],
$title, $gd['goods_name'], $teamlist['ct_num'], $remark, $backurl);
}
}
} else {
$udata['pt_listno'] = $qh;
M('order')->where('order_id', 'in', $ud)->where('store_id', $stoid)
->save($udata);
//这个地方要用主数据库查询
$o_c = M('order')->where('pt_listno', $qh)->count();
if ($o_c > 0) {
$t = time();
$grda['store_id'] = $stoid;
$grda['team_id'] = $team_id;
$grda['listno'] = $qh;
$grda['buystartdate'] = $t;
$grda['state'] = 2;
$grda['addtime'] = $t;
$grda['kt_end_time'] = strtotime('+' . $kt_time . ' hour', $t);
M("teamgroup")->save($grda);
}
break;
}
}
}
/*--给明海调用的接口,阶梯团--*/
public function back_ord(){
$store_id=I('store_id');
$teamgroup_id=I('teamgroup_id');
if (empty($store_id) || empty($teamgroup_id))
{
return json(['code'=>-1,'msg'=>"参数不全"]);
}
$tgr=M("teamgroup")->where(array('store_id'=>$store_id,'id'=>$teamgroup_id,'team_type'=>3))->find();
if($tgr){
$this->out_tuan($tgr);
return json(['code'=>0,'msg'=>"成功"]);
}
return json(['code'=>-1,'msg'=>"失败"]);
}
//判断团的人数是否到了
public function check_the_team($v){
//查找成团人数
$ordarr = M('order')->where('is_zsorder', 4)->where('pt_listno', $v['listno'])
->where('pt_prom_id', $v['team_id'])->where('pt_status', 1)->where('store_id',$v['store_id'])
->where('order_status=0 or order_status=1')
->field('order_id,order_sn,order_amount,user_id,store_id,pt_prom_id,user_money,shipping_price')
->select();
$count=count($ordarr);
$jsarr=json_decode($v['jt_json'],true);
//如果阶梯团大于这个人数,就是成团了,
if($count>=$jsarr[0]['rynum']) {
$endtime=strtotime('+24 hour');
M('teamgroup')->where('id',$v['id'])
->save(['state'=>4,'wk_end_time'=>$endtime,'jt_ct_num'=>$count,'buyenddate'=>time()]);
//要计算尾款的价钱
$price=0;
foreach ($jsarr as $k=>$v){ if($count>=$v['rynum']) $price=$v['price'];}
//$oid=get_arr_column($ordarr,'order_id');
foreach ($ordarr as $kp=>$vp){
//更新订单商品表的单价
$dd['goods_price']=$price;
$dd['member_goods_price']=$price;
M('order_goods')->where('order_id',$vp['order_id'])->save($dd);
//更新尾款,要计算物流费用
$num= M('order_goods')->where('order_id',$vp['order_id'])->sum('goods_num');
M('order')->where('order_id',$vp['order_id'])
->save(['pt_status'=>2,
'pt_tail_money'=>$price*$num+$vp['shipping_price']-$vp['order_amount']-$vp['user_money']
]);
}
//如果是满团,就要进行推送
//$timer = new \app\timer\controller\Index();
$olist= $ordarr;
if($olist){
foreach ($olist as $k=>$v){
$stoid=$v['store_id'];
$title='拼团满团,等待支付尾款';
$remark='感谢您的参与~';
$usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find();
$erpid=tpCache('shop_info.ERPId',$stoid);
$gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find();
$teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find();
$backurl=curHostURL(). "/Mobile/User/order_detail/id/".$v['order_id']."/stoid/".$stoid;
$this->post_msg($stoid,$erpid,$usr['user_id'],
$title,$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl);
}
}
return true;
}
return false;
}
}