StatisticalService.php 20.0 KB
Newer Older
D
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
5
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
D
devil_gong 已提交
6
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
7
// | Licensed ( https://opensource.org/licenses/mit-license.php )
D
devil_gong 已提交
8 9 10 11 12
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

D
Devil 已提交
13
use think\facade\Db;
D
devil_gong 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/**
 * 数据统计服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class StatisticalService
{
    // 近3天,近7天,近15天,近30天
    private static $nearly_three_days;
    private static $nearly_seven_days;
    private static $nearly_fifteen_days;
    private static $nearly_thirty_days;

30
    // 近30天
G
gongfuxiang 已提交
31 32 33
    private static $thirty_time_start;
    private static $thirty_time_end;

34
    // 近15天
D
devil_gong 已提交
35 36 37
    private static $fifteen_time_start;
    private static $fifteen_time_end;

38
    // 近7天
G
gongfuxiang 已提交
39 40 41
    private static $seven_time_start;
    private static $seven_time_end;

42 43 44 45 46 47 48 49 50
    // 上月
    private static $last_month_time_start;
    private static $last_month_time_end;

    // 当月
    private static $same_month_time_start;
    private static $same_month_time_end;

    // 昨天
D
devil_gong 已提交
51 52 53
    private static $yesterday_time_start;
    private static $yesterday_time_end;

54
    // 今天
D
devil_gong 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    private static $today_time_start;
    private static $today_time_end;

    /**
     * 初始化
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-02-22
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
    public static function Init($params = [])
    {
G
优化  
gongfuxiang 已提交
69 70 71 72 73
        static $object = null;
        if(!is_object($object))
        {
            // 初始化标记对象,避免重复初始化
            $object = (object) [];
G
gongfuxiang 已提交
74

G
gongfuxiang 已提交
75 76 77 78 79
            // 近30天日期
            self::$thirty_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-30 day')));
            self::$thirty_time_end = time();

            // 近15天日期
D
devil_gong 已提交
80 81 82
            self::$fifteen_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-15 day')));
            self::$fifteen_time_end = time();

G
优化  
gongfuxiang 已提交
83 84 85
            // 近7天日期
            self::$seven_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-7 day')));
            self::$seven_time_end = time();
D
devil_gong 已提交
86

87
            // 上月
D
Devil 已提交
88 89
            self::$last_month_time_start = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month', strtotime(date('Y-m', time())))));
            self::$last_month_time_end = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month', strtotime(date('Y-m', time())))));
90 91 92 93 94

            // 当月
            self::$same_month_time_start = strtotime(date('Y-m-01 00:00:00'));
            self::$same_month_time_end = time();

G
优化  
gongfuxiang 已提交
95 96 97
            // 昨天日期
            self::$yesterday_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-1 day')));
            self::$yesterday_time_end = strtotime(date('Y-m-d 23:59:59', strtotime('-1 day')));
D
devil_gong 已提交
98

G
优化  
gongfuxiang 已提交
99 100 101 102 103 104 105 106 107 108 109 110
            // 今天日期
            self::$today_time_start = strtotime(date('Y-m-d 00:00:00'));
            self::$today_time_end = time();

            // 近3天,近7天,近15天,近30天
            $nearly_all = [
                3   => 'nearly_three_days',
                7   => 'nearly_seven_days',
                15  => 'nearly_fifteen_days',
                30  => 'nearly_thirty_days',
            ];
            foreach($nearly_all as $day=>$name)
D
devil_gong 已提交
111
            {
G
优化  
gongfuxiang 已提交
112 113 114 115 116 117 118 119 120 121
                $date = [];
                $time = time();
                for($i=0; $i<$day; $i++)
                {
                    $date[] = [
                        'start_time'    => strtotime(date('Y-m-d 00:00:00', time()-$i*3600*24)),
                        'end_time'      => strtotime(date('Y-m-d 23:59:59', time()-$i*3600*24)),
                        'name'          => date('Y-m-d', time()-$i*3600*24),
                    ];
                }
D
devil_gong 已提交
122
                self::${$name} = array_reverse($date);
D
devil_gong 已提交
123 124 125
            }
        }
    }
G
gongfuxiang 已提交
126

D
devil_gong 已提交
127
    /**
128
     * 用户总数,今日,昨日,当月,上月总数
D
devil_gong 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function UserYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 总数
        $total_count = Db::name('User')->count();

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        // 上月
        $where = [
            ['status', '<=', 4],
            ['add_time', '>=', self::$last_month_time_start],
            ['add_time', '<=', self::$last_month_time_end],
        ];
        $last_month_count = Db::name('User')->where($where)->count();

        // 当月
        $where = [
            ['status', '<=', 4],
            ['add_time', '>=', self::$same_month_time_start],
            ['add_time', '<=', self::$same_month_time_end],
        ];
        $same_month_count = Db::name('User')->where($where)->count();

D
devil_gong 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        // 昨天
        $where = [
            ['add_time', '>=', self::$yesterday_time_start],
            ['add_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('User')->where($where)->count();

        // 今天
        $where = [
            ['add_time', '>=', self::$today_time_start],
            ['add_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('User')->where($where)->count();

        // 数据组装
G
gongfuxiang 已提交
174 175
        $result = [
            'total_count'       => $total_count,
176 177
            'last_month_count'  => $last_month_count,
            'same_month_count'  => $same_month_count,
G
gongfuxiang 已提交
178 179 180 181 182 183 184
            'yesterday_count'   => $yesterday_count,
            'today_count'       => $today_count,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
185
     * 订单总数,今日,昨日,当月,上月总数
G
gongfuxiang 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function OrderNumberYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 订单状态
        // (0待确认, 1已确认/待支付, 2已支付/待发货, 3已发货/待收货, 4已完成, 5已取消, 6已关闭)
199

G
gongfuxiang 已提交
200 201 202 203 204 205
        // 总数
        $where = [
            ['status', '<=', 4],
        ];
        $total_count = Db::name('Order')->where($where)->count();

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        // 上月
        $where = [
            ['status', '<=', 4],
            ['add_time', '>=', self::$last_month_time_start],
            ['add_time', '<=', self::$last_month_time_end],
        ];
        $last_month_count = Db::name('Order')->where($where)->count();

        // 当月
        $where = [
            ['status', '<=', 4],
            ['add_time', '>=', self::$same_month_time_start],
            ['add_time', '<=', self::$same_month_time_end],
        ];
        $same_month_count = Db::name('Order')->where($where)->count();

G
gongfuxiang 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        // 昨天
        $where = [
            ['status', '<=', 4],
            ['add_time', '>=', self::$yesterday_time_start],
            ['add_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('Order')->where($where)->count();

        // 今天
        $where = [
            ['status', '<=', 4],
            ['add_time', '>=', self::$today_time_start],
            ['add_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('Order')->where($where)->count();

        // 数据组装
        $result = [
            'total_count'       => $total_count,
241 242
            'last_month_count'  => $last_month_count,
            'same_month_count'  => $same_month_count,
G
gongfuxiang 已提交
243 244 245 246 247 248 249
            'yesterday_count'   => $yesterday_count,
            'today_count'       => $today_count,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
250
     * 订单成交总量,今日,昨日,当月,上月总数
G
gongfuxiang 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function OrderCompleteYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 订单状态
        // (0待确认, 1已确认/待支付, 2已支付/待发货, 3已发货/待收货, 4已完成, 5已取消, 6已关闭)
        
        // 总数
        $where = [
            ['status', '=', 4],
        ];
        $total_count = Db::name('Order')->where($where)->count();

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        // 上月
        $where = [
            ['status', '=', 4],
            ['add_time', '>=', self::$last_month_time_start],
            ['add_time', '<=', self::$last_month_time_end],
        ];
        $last_month_count = Db::name('Order')->where($where)->count();

        // 当月
        $where = [
            ['status', '=', 4],
            ['add_time', '>=', self::$same_month_time_start],
            ['add_time', '<=', self::$same_month_time_end],
        ];
        $same_month_count = Db::name('Order')->where($where)->count();

G
gongfuxiang 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        // 昨天
        $where = [
            ['status', '=', 4],
            ['add_time', '>=', self::$yesterday_time_start],
            ['add_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('Order')->where($where)->count();

        // 今天
        $where = [
            ['status', '=', 4],
            ['add_time', '>=', self::$today_time_start],
            ['add_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('Order')->where($where)->count();

        // 数据组装
        $result = [
D
devil_gong 已提交
305
            'total_count'       => $total_count,
306 307
            'last_month_count'  => $last_month_count,
            'same_month_count'  => $same_month_count,
D
devil_gong 已提交
308 309 310
            'yesterday_count'   => $yesterday_count,
            'today_count'       => $today_count,
        ];
G
gongfuxiang 已提交
311 312 313 314
        return DataReturn('处理成功', 0, $result);
    }

    /**
315
     * 订单收入总计,今日,昨日,当月,上月总数
G
gongfuxiang 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function OrderCompleteMoneyYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 订单状态
        // (0待确认, 1已确认/待支付, 2已支付/待发货, 3已发货/待收货, 4已完成, 5已取消, 6已关闭)
        
        // 总数
        $where = [
D
devil 已提交
332
            ['status', 'in', [2,3,4]],
G
gongfuxiang 已提交
333 334 335
        ];
        $total_count = Db::name('Order')->where($where)->sum('total_price');

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
        // 上月
        $where = [
            ['status', 'in', [2,3,4]],
            ['add_time', '>=', self::$last_month_time_start],
            ['add_time', '<=', self::$last_month_time_end],
        ];
        $last_month_count = Db::name('Order')->where($where)->sum('total_price');

        // 当月
        $where = [
            ['status', 'in', [2,3,4]],
            ['add_time', '>=', self::$same_month_time_start],
            ['add_time', '<=', self::$same_month_time_end],
        ];
        $same_month_count = Db::name('Order')->where($where)->sum('total_price');

G
gongfuxiang 已提交
352 353
        // 昨天
        $where = [
D
devil 已提交
354
            ['status', 'in', [2,3,4]],
G
gongfuxiang 已提交
355 356 357 358 359 360 361
            ['add_time', '>=', self::$yesterday_time_start],
            ['add_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('Order')->where($where)->sum('total_price');

        // 今天
        $where = [
D
devil 已提交
362
            ['status', 'in', [2,3,4]],
G
gongfuxiang 已提交
363 364 365 366 367 368 369 370
            ['add_time', '>=', self::$today_time_start],
            ['add_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('Order')->where($where)->sum('total_price');

        // 数据组装
        $result = [
            'total_count'       => PriceNumberFormat($total_count),
371 372
            'last_month_count'  => PriceNumberFormat($last_month_count),
            'same_month_count'  => PriceNumberFormat($same_month_count),
G
gongfuxiang 已提交
373 374 375 376 377 378
            'yesterday_count'   => PriceNumberFormat($yesterday_count),
            'today_count'       => PriceNumberFormat($today_count),
        ];
        return DataReturn('处理成功', 0, $result);
    }

D
devil 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392
    /**
     * 订单收益趋势, 30天数据
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function OrderProfitSevenTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 订单状态列表
D
Devil 已提交
393
        $order_status_list = MyConst('common_order_user_status');
D
devil 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406
        $status_arr = array_column($order_status_list, 'id');

        // 循环获取统计数据
        $data = [];
        $value_arr = [];
        $name_arr = [];
        if(!empty($status_arr))
        {
            foreach(self::$nearly_thirty_days as $day)
            {
                // 当前日期名称
                $name_arr[] = $day['name'];

D
dev  
Devil 已提交
407
                // 根据状态获取数量
D
devil 已提交
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
                foreach($status_arr as $status)
                {
                    // 获取订单
                    $where = [
                        ['status', '=', $status],
                        ['add_time', '>=', $day['start_time']],
                        ['add_time', '<=', $day['end_time']],
                    ];
                    $value_arr[$status][] = Db::name('Order')->where($where)->sum('pay_price');
                }
            }
        }

        // 数据格式组装
        foreach($status_arr as $status)
        {
            $data[] = [
                'name'      => $order_status_list[$status]['name'],
                'type'      => ($status == 4) ? 'line' : 'bar',
                'tiled'     => '总量',
                'data'      => empty($value_arr[$status]) ? [] : $value_arr[$status],
            ];
        }

        // 数据组装
        $result = [
            'title_arr' => array_column($order_status_list, 'name'),
            'name_arr'  => $name_arr,
            'data'      => $data,
        ];
        return DataReturn('处理成功', 0, $result);
    }

G
gongfuxiang 已提交
441
    /**
G
gongfuxiang 已提交
442
     * 订单交易趋势, 30天数据
G
gongfuxiang 已提交
443 444 445 446 447 448 449 450 451 452 453 454
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function OrderTradingTrendSevenTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 订单状态列表
D
Devil 已提交
455
        $order_status_list = MyConst('common_order_user_status');
G
gongfuxiang 已提交
456 457 458 459
        $status_arr = array_column($order_status_list, 'id');

        // 循环获取统计数据
        $data = [];
D
devil 已提交
460
        $value_arr = [];
G
gongfuxiang 已提交
461 462 463
        $name_arr = [];
        if(!empty($status_arr))
        {
G
gongfuxiang 已提交
464
            foreach(self::$nearly_thirty_days as $day)
G
gongfuxiang 已提交
465 466 467 468
            {
                // 当前日期名称
                $name_arr[] = $day['name'];

D
dev  
Devil 已提交
469
                // 根据状态获取数量
G
gongfuxiang 已提交
470 471 472 473 474 475 476 477
                foreach($status_arr as $status)
                {
                    // 获取订单
                    $where = [
                        ['status', '=', $status],
                        ['add_time', '>=', $day['start_time']],
                        ['add_time', '<=', $day['end_time']],
                    ];
D
devil 已提交
478
                    $value_arr[$status][] = Db::name('Order')->where($where)->count();
G
gongfuxiang 已提交
479 480 481 482 483 484 485 486 487
                }
            }
        }

        // 数据格式组装
        foreach($status_arr as $status)
        {
            $data[] = [
                'name'      => $order_status_list[$status]['name'],
D
devil 已提交
488
                'type'      => ($status == 4) ? 'bar' : 'line',
G
gongfuxiang 已提交
489
                'tiled'     => '总量',
D
devil 已提交
490
                'data'      => empty($value_arr[$status]) ? [] : $value_arr[$status],
G
gongfuxiang 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503
            ];
        }

        // 数据组装
        $result = [
            'title_arr' => array_column($order_status_list, 'name'),
            'name_arr'  => $name_arr,
            'data'      => $data,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
D
cj  
devil 已提交
504
     * 支付方式, 30天数据
G
gongfuxiang 已提交
505 506 507 508 509 510
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
D
cj  
devil 已提交
511
    public static function PayTypeSevenTodayTotal($params = [])
G
gongfuxiang 已提交
512 513 514 515 516 517
    {
        // 初始化
        self::Init($params);

        // 获取支付方式名称
        $where = [
D
cj  
devil 已提交
518 519
            ['business_type', '<>', ''],
            ['status', '=', 1],
G
gongfuxiang 已提交
520 521 522 523 524 525
        ];
        $pay_name_arr = Db::name('PayLog')->where($where)->group('payment_name')->column('payment_name');


        // 循环获取统计数据
        $data = [];
D
devil 已提交
526
        $value_arr = [];
G
gongfuxiang 已提交
527 528 529
        $name_arr = [];
        if(!empty($pay_name_arr))
        {
G
gongfuxiang 已提交
530
            foreach(self::$nearly_thirty_days as $day)
G
gongfuxiang 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543
            {
                // 当前日期名称
                $name_arr[] = date('m-d', strtotime($day['name']));

                // 根据支付名称获取数量
                foreach($pay_name_arr as $payment)
                {
                    // 获取订单
                    $where = [
                        ['payment_name', '=', $payment],
                        ['add_time', '>=', $day['start_time']],
                        ['add_time', '<=', $day['end_time']],
                    ];
D
devil 已提交
544
                    $value_arr[$payment][] = Db::name('PayLog')->where($where)->count();
G
gongfuxiang 已提交
545 546 547 548 549 550 551 552 553 554 555 556
                }
            }
        }

        // 数据格式组装
        foreach($pay_name_arr as $payment)
        {
            $data[] = [
                'name'      => $payment,
                'type'      => 'line',
                'stack'     => '总量',
                'areaStyle' => (object) [],
D
devil 已提交
557
                'data'      => empty($value_arr[$payment]) ? [] : $value_arr[$payment],
G
gongfuxiang 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570
            ];
        }

        // 数据组装
        $result = [
            'title_arr' => $pay_name_arr,
            'name_arr'  => $name_arr,
            'data'      => $data,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
G
gongfuxiang 已提交
571
     * 热销商品, 30天数据
G
gongfuxiang 已提交
572 573 574 575 576 577 578 579 580 581 582
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function GoodsHotSaleSevenTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);
    
D
devil_gong 已提交
583
        // 获取订单id
G
gongfuxiang 已提交
584
        $where = [
D
devil_gong 已提交
585
            ['status', '<=', 4],
G
gongfuxiang 已提交
586 587
            ['add_time', '>=', self::$thirty_time_start],
            ['add_time', '<=', self::$thirty_time_end],
G
gongfuxiang 已提交
588
        ];
D
devil_gong 已提交
589 590 591 592 593 594 595
        $order_ids = Db::name('Order')->where($where)->column('id');

        // 获取订单详情热销商品
        if(empty($order_ids))
        {
            $data = [];
        } else {
D
Devil 已提交
596
            $data = Db::name('OrderDetail')->field('goods_id, sum(buy_number) AS value')->where('order_id', 'IN', $order_ids)->group('goods_id')->order('value desc')->limit(10)->select()->toArray();
D
devil_gong 已提交
597 598
        }

G
gongfuxiang 已提交
599 600 601 602
        if(!empty($data))
        {
            foreach($data as &$v)
            {
D
devil 已提交
603 604
                // 获取商品名称(这里不一次性读取、为了兼容 mysql 5.7+版本)
                $v['name'] = Db::name('OrderDetail')->where('goods_id', $v['goods_id'])->value('title');
G
gongfuxiang 已提交
605
                if(mb_strlen($v['name'], 'utf-8') > 12)
G
gongfuxiang 已提交
606
                {
G
gongfuxiang 已提交
607
                    $v['name'] = mb_substr($v['name'], 0, 12, 'utf-8').'...';
G
gongfuxiang 已提交
608
                }
D
devil 已提交
609
                unset($v['goods_id']);
G
gongfuxiang 已提交
610 611 612 613 614 615 616 617 618
            }
        }

        // 数据组装
        $result = [
            'name_arr'  => array_column($data, 'name'),
            'data'      => $data,
        ];
        return DataReturn('处理成功', 0, $result);
D
devil_gong 已提交
619 620 621
    }
}
?>