-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbstat.php
543 lines (491 loc) · 15.8 KB
/
bstat.php
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
<?php
/**
* Created by PhpStorm.
* User: houchao
* Date: 2016-03-18
* Time: 12:19
*/
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require(__DIR__ . '/core/init.php');
$request = new Request();
$response = new Response();
SimPHP::I()->boot();
define('ONE_DAY_TIME', 86400);
define('DAY_SEP', ' ');
define('DAY_BEGIN', DAY_SEP.'00:00:00');
define('DAY_END', DAY_SEP.'23:59:59');
define('CSV_SEP', ',');
define('CSV_LN', "\n");
@header("Content-Type: text/html;charset=UTF-8");
$startDate = @$_REQUEST['startDate'];
$endDate = @$_REQUEST['endDate'];
$page = @$_REQUEST['page'] ? $_REQUEST['page'] : 1;
if (!isdate($startDate) || !isdate($endDate)
&& !empty($endDate) && !empty($startDate)
) {
echo "<span style='color: red;'>*格式必须为xxxx-xx-xx!</span>";
}
if ($startDate > $endDate) {
echo "开始时间必须大于结束时间!";
}
$startTime = strtotime($startDate);
$endTime = strtotime($endDate.DAY_END);
$html = "";
if ($startTime && $endTime) {
$html = getHtmlInfo($startTime, $endTime, $page);
// var_dump(D()->getSqlFinal());
}
function isdate($str, $format = "Y-m-d")
{
$strArr = explode("-", $str);
if (empty($strArr)) {
return false;
}
foreach ($strArr as $val) {
if (strlen($val) < 2) {
$val = "0" . $val;
}
$newArr[] = $val;
}
$str = implode("-", $newArr);
$unixTime = strtotime($str);
$checkDate = date($format, $unixTime);
if ($checkDate == $str) {
return true;
} else {
return false;
}
}
/**
* 获取日周月的浏览量
* @param $getLevelNum
*/
//$data = GetInfo::getDayNumber($time);
function getResult($data)
{
if (!is_array($data)) {
return ['day' => 0, 'week' => 0, 'month' => 0];
};
$list = [];
// var_dump($data);exit;
for ($i = 0; $i < count($data); $i++) {
$result = $data[$i]['totalVie'] - $data[$i]['userCount'];
switch ($i) {
case 0:
$list['day'] = $result;
break;
case 1:
$list['week'] = $result;
break;
case 2:
$list['month'] = $result;
break;
}
}
return $list;
}
/**
*得到第一,二页数据的HTML
* @param $starTime
* @param $endTime
*/
function getHtmlInfo($starTime, $endTime, $page = 1)
{
$html = "";
if ($page == 1) {
for ($star_t = $starTime; $star_t <= $endTime; $star_t += ONE_DAY_TIME) {
$end_t = $star_t + ONE_DAY_TIME - 1;
$ectb_order = Order::table();
$order = GetInfo::getTotalOrderNumber($ectb_order, $star_t, $end_t);//订单成交量
$perNum = GetInfo::susPayPerNum($star_t, $end_t);
$quitOrder = GetInfo::quitPayMoney($star_t, $end_t);
$MaxOrderNum = GetInfo::getMaxOrderNum($star_t, $end_t);
$html .= '<tr>';
$html .= '<td>' . date('Y-m-d', $star_t) . '</td>';
$html .= '<td>' . $order['total_order'] . '</td>';
$html .= '<td>' . $order['order_amount'] . '</td>';
$html .= '<td>' . $perNum['user_num'] . '</td>';
$html .= '<td> ' . $perNum['order_amount'] . '</td>';
$html .= '<td> ' . $perNum['order_num'] . ' </td>';
$html .= '<td>' . $quitOrder['totalMoney'] . '</td>';
$html .= '<td>' . $MaxOrderNum['goodsNumber'] . '</td>';
$html .= '<td>' . $MaxOrderNum['money_paid'] . '</td>';
$html .= '</tr>';
}
} elseif ($page == 2) {
for ($star_t = $starTime; $star_t <= $endTime; $star_t += ONE_DAY_TIME) {
$end_t = $star_t + ONE_DAY_TIME - 1;
$getLevelNum = GetInfo::getLevelNum($star_t, $end_t);//chnum 最大一级的人数
$maxLevelNum = GetInfo::getMaxLevelNum($star_t, $end_t);//todo 最大一二三级人数
$maxCommision = GetInfo::getMaxCommision($star_t, $end_t);//commision 最大佣金
$maxOrderNum = GetInfo::getPlantMoney($star_t, $end_t);//平台收益 int
$userNumber = GetInfo::getUserNumber($star_t, $end_t);//userNum平台用户数
$vieNumber = GetInfo::getVieNumber($star_t, $end_t);//pv uv ip数目
$dayNumber = GetInfo::getDayNumber($star_t);//获取日 周 月活跃数 vieCount - userCount
//var_dump($dayNumber);exit;
$result = getResult($dayNumber); //day ,week,month
$html .= '<tr>';
$html .= '<td>' . date('Y-m-d', $star_t) . '</td>';
$html .= '<td>' . $getLevelNum['chnum'] . '</td>';
$html .= '<td>' . $maxLevelNum. '</td>';
$html .= '<td>' . $maxCommision['commision'] . '</td>';
$html .= '<td> ' . $maxOrderNum . '</td>';
$html .= '<td> ' . $userNumber['userNum'] . ' </td>';
$html .= '<td> ' . "--" . ' </td>';
$html .= '<td>' . $vieNumber['pv'] . '</td>';
$html .= '<td>' . $vieNumber['uv'] . '</td>';
$html .= '<td>' . $vieNumber['ip'] . '</td>';
$html .= '<td>' . $result['day'] . '</td>';
$html .= '<td>' . $result['week'] . '</td>';
$html .= '<td>' . $result['month'] . '</td>';
$html .= '</tr>';
}
}
return $html;
}
class GetInfo
{
/**求订单的总数,以及订单的总金额
* @param $ectb_order
* @param $startTime
* @param $endTime
* @return array
*/
static function getTotalOrderNumber($ectb_order, $startTime, $endTime)
{
$sql = "SELECT count(*) as total_order ,sum(order_amount) as order_amount FROM
{$ectb_order} WHERE is_separate = 0 AND add_time BETWEEN
$startTime AND $endTime";
$result = D()->query($sql)->get_one();
$result['total_order'] = empty($result['total_order']) ? 0 : $result['total_order'];
$result['order_amount'] = empty($result['order_amount']) ? 0 : $result['order_amount'];
return $result;
}
/**获取成交的数量
* @param $starTime
* @param $endTime
* @return int
*/
static function susPayPerNum($starTime, $endTime)
{
$sql = "select count(DISTINCT user_id) as user_num,count('order_id') as order_num ,SUM(order_amount) as order_amount from shp_order_info
where pay_status = 2 and pay_time BETWEEN $starTime and $endTime and is_separate = 0";
$result = D()->query($sql)->get_one();
$result['user_num'] = empty($result['user_num']) ? 0 : $result['user_num'];
$result['order_num'] = empty($result['order_num']) ? 0 : $result['order_num'];
$result['order_amount'] = empty($result['order_amount']) ? 0 : $result['order_amount'];
return $result;
}
/**
* 已经退款的金额
* @param $starTime
* @param $endTime
*/
static function quitPayMoney($starTime, $endTime)
{
$sql = "select sum(money_paid) as totalMoney from shp_order_info where pay_status in (3,4) AND pay_time BETWEEN $starTime and $endTime;";
$result = D()->query($sql)->get_one();
if (empty($result['totalMoney'])) {
return ['totalMoney' => 0];
}
return $result;
}
/**
* 最大单品成交订单数,最大成交额
* @param $starTime
* @param $endTime
*/
static function getMaxOrderNum($starTime, $endTime)
{
$sql = "select sum(goods.goods_number) as goodsNumber,goods.goods_id as
goods_id ,info.order_id,sum(info.money_paid) as money_paid from shp_order_info info LEFT JOIN shp_order_goods
goods on info.order_id=goods.order_id where info.pay_time BETWEEN $starTime and $endTime and info.pay_status=2 GROUP BY
goods.goods_id
ORDER BY goods_id desc LIMIT 1";
$result = D()->query($sql)->get_one();
if (empty($result)) {
return ['goodsNumber' => 0, 'money_paid' => 0];
}
return $result;
}
/**
* 最大一级用户人数@page2
* @param $starTime
* @param $endTime
*/
static function getLevelNum($starTime, $endTime)
{
$sql = "select max(childnum_1) as chnum from shp_users where reg_time BETWEEN $starTime and $endTime";
$result = D()->query($sql)->get_one();
if (empty($result['chnum'])) {
return ['chnum' => 0];
}
return $result;
}
/**
* 最大一,二,三级用户人数@page2
* @param $starTime
* @param $endTime
*/
static function getMaxLevelNum($starTime, $endTime)
{
$sql = "select max(childnum_1) as chnum_1,max(childnum_2) as chnum_2,max(childnum_3) as chnum_3
from shp_users where reg_time BETWEEN $starTime and $endTime";
$result = D()->query($sql)->get_one();
$result['chnum_1'] = empty($result['chnum_1']) ? 0 : $result['chnum_1'];
$result['chnum_2'] = empty($result['chnum_2']) ? 0 : $result['chnum_2'];
$result['chnum_2'] = empty($result['chnum_3']) ? 0 : $result['chnum_3'];
$num=$result['chnum_1']+$result['chnum_2']+$result['chnum_2'];
return $num;
}
//commision
/**
* 最大佣金
* @param $starTime
* @param $endTime
*/
static function getMaxCommision($starTime, $endTime)
{
$sql = "select max(commision) as commision from shp_order_info where pay_status = 2
and shipping_time BETWEEN $starTime AND $endTime";
$commision = D()->query($sql)->get_one();
$commision['commision'] = empty($commision['commision']) ? 0 : $commision['commision'];
return $commision;
}
/**
* 平台收益额 = 商品卖出的总价钱-供货价-给米客的佣金
* @param $starTime
* @param $endTime
*/
static function getPlantMoney($starTime, $endTime)
{
$it = self::goods_sell_list($starTime, $endTime);
$totalMoney = 0;
foreach ($it as $val) {
$totalMoney += $val['order_goods_num'] * $val['commision'] * PLATFORM_COMMISION;
}
return $totalMoney;
}
/**
* 平台用户数目
* @param $starTime
* @param $endTime
*/
static function getUserNumber($starTime, $endTime)
{
$sql = "select count(user_id) as userNum from shp_users where reg_time BETWEEN $starTime AND $endTime";
$userNum = D()->query($sql)->get_one();
$userNum['userNum'] = empty($userNum['userNum']) ? 0 : $userNum['userNum'];
return $userNum;
}
/**获取pv uv ip数目
* @param $starTime
* @param $endTime
*/
static function getVieNumber($starTime, $endTime)
{
$sql = "select count(1) as pv , count(DISTINCT ip) as ip ,count(DISTINCT uv) as uv from tb_visiting
where created BETWEEN $starTime AND $endTime";
$vieNumber = D()->query($sql)->get_one();
$vieNumber['pv'] = empty($vieNumber['pv']) ? 0 : $vieNumber['pv'];
$vieNumber['ip'] = empty($vieNumber['ip']) ? 0 : $vieNumber['ip'];
$vieNumber['uv'] = empty($vieNumber['uv']) ? 0 : $vieNumber['uv'];
return $vieNumber;
}
static function money_yuan($val)
{
$val = number_format($val, 2, '.', '');
if (preg_match('/\.0+$/', strval($val))) {
$val = intval($val);
}
return $val;
}
/**
* 商品销售列表
*/
static function goods_sell_list($startTime, $endTime)
{
$sql = "SELECT g.goods_id,g.goods_name,g.income_price,g.shop_price,g.paid_order_count,g.commision,m.facename,SUM(og.goods_number) AS order_goods_num
FROM `shp_goods` g INNER JOIN `shp_merchant` m ON g.merchant_uid=m.admin_uid
INNER JOIN `shp_order_goods` og ON g.goods_id=og.goods_id
WHERE m.created BETWEEN $startTime and $endTime
GROUP BY g.goods_id
ORDER BY paid_order_count DESC";
$list = D()->query($sql)->fetch_array_all();
return $list;
}
/**获取日 周 月活跃数 =浏览的数目 - 注册的数目
* @param $starTime
* @param $endTime
*/
static function getDayNumber($starTime)
{
//取出当天
$lastDay = $starTime + ONE_DAY_TIME - 1;
$weekTime = self::getDateStam($starTime);
$weekStart = $weekTime['start_day'];
$weekEnd = $weekTime['end_day'];
$monthTime = self::getMonthStam($starTime);
$monthStart = $monthTime['start_day'];
$monthEnd = $monthTime['end_day'];
$sql = "select count(users.user_id) as userCount from shp_users users where users.reg_time BETWEEN {$starTime} and {$lastDay}
UNION ALL
select count(users.user_id) as userCount from shp_users users where users.reg_time BETWEEN {$weekStart} and {$weekEnd}
UNION ALL
select count(users.user_id) as userCount from shp_users users where users.reg_time BETWEEN {$monthStart} and {$monthEnd}";
$result = D()->query($sql)->fetch_array_all();
$sql = "select count(distinct uid) as vieNum from tb_visiting where created BETWEEN $starTime and $lastDay UNION ALL
select count(distinct uid) as vieNum from tb_visiting where created BETWEEN $weekStart and $weekEnd UNION ALL
select count(distinct uid) as vieNum from tb_visiting where created BETWEEN $monthStart and $monthEnd ";
$data = D()->query($sql)->fetch_array_all();
//var_dump(D()->getSqlFinal());exit;
foreach ($data as $val1) {
foreach ($result as &$val2) {
$val2['totalVie'] = $val1['vieNum'];
}
}
return $result;
}
/**
* 获取本周的第一天和最后一天
*/
static function getDateStam($stratTime)
{
$date = new DateTime();
$date->setTimestamp($stratTime);
$date->modify("this week");
$first_day_of_week = $date->format('Y-m-d');
$date->modify('this week +6 days');
$end_day_of_week = $date->format('Y-m-d');
$result['start_day'] = strtotime($first_day_of_week);
$result['end_day'] = strtotime($end_day_of_week);
return $result;
}
/**
* 获取本月的第一天和最后一天
*/
static function getMonthStam($time)
{
$beginThismonth = mktime(0, 0, 0, date('m', $time), 1, date('Y', $time));
$endThismonth = mktime(23, 59, 59, date('m', $time), date('t', $time), date('Y', $time));
$result['start_day'] = $beginThismonth;
$result['end_day'] = $endThismonth;
return $result;
// echo date("Y-m-d",$beginThismonth)."-----".date("Y-m-d",$endThismonth);
}
}
?>
<style>
* {
margin: 0;
padding: 0;
}
.midle {
/*border: 1px solid red;*/
margin: 0 auto;
width: 800px;
height: auto;
position: relative;
}
.midle button {
background: blueviolet;
color: white;
width: 100px;
}
.midle input {
width: 100px;
}
table {
width: 100%;
border-spacing: 0;
}
th, td {
border-style: solid;
border-color: #ddd;
border-width: 0 0 1px;
padding: 5px 2px;
}
td {
text-align: center;
}
th {
font-weight: bold;
}
h1 {
text-align: center;
margin-bottom: 15px;
font-size: 18px;
border-bottom: 2px solid #ccc;
padding: 10px 0;
}
.page {
position: fixed;
background: blueviolet;
color: #FFFFFF;
font-weight: 100;
font-size: 14px;
border-radius: 5px;
text-align: center;
height: 20px;
line-height: 20px;
width: 90px;
left: 95%;
bottom: 5%;
}
.page a {
display: inline-block;
font-size: 14px;
font-weight: 800;
color: white;
}
</style>
<h1>统计</h1>
<form action="sdtat.php?page=<?= $page ?>" method="post">
<div class="midle">
请输入开始日期:<input type="text" placeholder="<?php echo empty($startDate) ? '请输入开始日期' : $startDate ?>"
name="startDate"/>
请输入结束日期:<input type="text" placeholder="<?php echo empty($endDate) ? '请输入结束日期' : $endDate ?>" name="endDate"/>
<button type="submit">提交</button>
</div>
</form>
<table>
<?php if ($page == 1): ?>
<tr>
<th>日期时间</th>
<th>拍下订单数</th>
<th>拍下总金额(GMV)</th>
<th>成交用户数</th>
<th>成交金额</th>
<th>成交订单数</th>
<th>退款金额</th>
<th>最大成品成交订单数</th>
<th>最大单品成交金额</th>
</tr>
<!-- --><?= $html ?>
<div class="page"><a
href="sdtat.php?page=<?= $page + 1 ?>&startDate=<?= $startDate ?>&endDate=<?= $endDate ?>"><b>下一页</b></a>
</div>
<?php endif; ?>
<?php if ($page == 2): ?>
<tr>
<th>日期时间</th>
<th>最大一级用户数</th>
<th>最大一,二,三级用户数</th>
<th>最大佣金收益金额</th>
<th>平台收益额</th>
<th>平台用户数</th>
<th>平台公众号数目</th>
<th>PV数</th>
<th>UV数</th>
<th>IP数</th>
<th>日活跃数</th>
<th>周活跃数</th>
<th>月活跃数</th>
</tr>
<?= $html ?>
<div class="page"><a
href="sdtat.php?page=<?= $page - 1 ?>&startDate=<?= $startDate ?>&endDate=<?= $endDate ?>"><b>上一页</b></a>
</div>
<?php endif; ?>
</table>