StatisticalService.php 30.4 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

/**
 * 数据统计服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class StatisticalService
{
    // 近3天,近7天,近15天,近30天
25 26 27 28 29 30 31 32 33 34 35 36
    public static $nearly_three_days;
    public static $nearly_seven_days;
    public static $nearly_fifteen_days;
    public static $nearly_thirty_days;

    // 昨天
    public static $yesterday_time_start;
    public static $yesterday_time_end;

    // 今天
    public static $today_time_start;
    public static $today_time_end;
D
devil_gong 已提交
37

38 39 40 41 42 43 44 45
    // 近365天
    public static $year_time_start;
    public static $year_time_end;

    // 近180天
    public static $half_year_time_start;
    public static $half_year_time_end;

46
    // 近30天
47 48
    public static $thirty_time_start;
    public static $thirty_time_end;
G
gongfuxiang 已提交
49

50
    // 近15天
51 52
    public static $fifteen_time_start;
    public static $fifteen_time_end;
D
devil_gong 已提交
53

54
    // 近7天
55 56 57 58 59 60
    public static $seven_time_start;
    public static $seven_time_end;

    // 近3天
    public static $three_time_start;
    public static $three_time_end;
G
gongfuxiang 已提交
61

62
    // 上月
63 64
    public static $last_month_time_start;
    public static $last_month_time_end;
65 66

    // 当月
67 68
    public static $this_month_time_start;
    public static $this_month_time_end;
69

70 71 72
    // 去年
    public static $this_year_time_start;
    public static $this_year_time_end;
D
devil_gong 已提交
73

74 75 76
    // 今年
    public static $last_year_time_start;
    public static $last_year_time_end;
D
devil_gong 已提交
77 78 79 80 81 82 83 84 85 86 87 88

    /**
     * 初始化
     * @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 已提交
89 90 91 92 93
        static $object = null;
        if(!is_object($object))
        {
            // 初始化标记对象,避免重复初始化
            $object = (object) [];
G
gongfuxiang 已提交
94

95 96 97 98 99 100 101 102
            // 昨天日期
            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')));

            // 今天日期
            self::$today_time_start = strtotime(date('Y-m-d 00:00:00'));
            self::$today_time_end = time();

103 104 105 106 107 108 109 110
            // 近365天日期
            self::$year_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-365 day')));
            self::$year_time_end = time();

            // 近180天日期
            self::$half_year_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-180 day')));
            self::$half_year_time_end = time();

G
gongfuxiang 已提交
111
            // 近30天日期
112
            self::$thirty_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-29 day')));
G
gongfuxiang 已提交
113 114 115
            self::$thirty_time_end = time();

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

G
优化  
gongfuxiang 已提交
119
            // 近7天日期
120
            self::$seven_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-6 day')));
G
优化  
gongfuxiang 已提交
121
            self::$seven_time_end = time();
D
devil_gong 已提交
122

123 124 125 126
            // 近3天日期
            self::$three_time_start = strtotime(date('Y-m-d 00:00:00', strtotime('-2 day')));
            self::$three_time_end = time();

127
            // 上月
D
Devil 已提交
128 129
            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())))));
130 131

            // 当月
132 133
            self::$this_month_time_start = strtotime(date('Y-m-01 00:00:00'));
            self::$this_month_time_end = time();
134

135 136 137 138 139 140 141
            // 去年
            self::$last_year_time_start = strtotime(date('Y-01-01 00:00:00', strtotime('-1 year', strtotime(date('Y-m', time())))));
            self::$last_year_time_end = strtotime(date('Y-12-31 23:59:59', strtotime('-1 year', strtotime(date('Y-m', time())))));

            // 今年
            self::$this_year_time_start = strtotime(date('Y-01-01 00:00:00'));
            self::$this_year_time_end = time();
D
devil_gong 已提交
142

G
优化  
gongfuxiang 已提交
143 144 145 146 147 148 149 150 151

            // 近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 已提交
152
            {
G
优化  
gongfuxiang 已提交
153 154 155 156 157 158 159 160 161 162
                $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 已提交
163
                self::${$name} = array_reverse($date);
D
devil_gong 已提交
164 165 166
            }
        }
    }
G
gongfuxiang 已提交
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
    /**
     * 获取时间列表
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-08-31
     * @desc    description
     * @param   [array]           $params [数据参数]
     */
    public static function DateTimeList($params = [])
    {
        // 初始化
        self::Init($params);

        // 统计时间配置列表
        return [
            '3-day' => [
                'name'  => '近3天',
                'start' => date('Y-m-d H:i:s', StatisticalService::$three_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$three_time_end),
            ],
            '7-day' => [
                'name'  => '近7天',
                'start' => date('Y-m-d H:i:s', StatisticalService::$seven_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$seven_time_end),
            ],
            '15-day'    => [
                'name'  => '近15天',
                'start' => date('Y-m-d H:i:s', StatisticalService::$fifteen_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$fifteen_time_end),
            ],
            '30-day'    => [
                'name'  => '近30天',
                'start' => date('Y-m-d H:i:s', StatisticalService::$thirty_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$thirty_time_end),
            ],
204 205 206 207 208 209 210 211 212 213
            '180-day'    => [
                'name'  => '近半年',
                'start' => date('Y-m-d H:i:s', StatisticalService::$half_year_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$half_year_time_end),
            ],
            '365-day'    => [
                'name'  => '近1年',
                'start' => date('Y-m-d H:i:s', StatisticalService::$year_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$year_time_end),
            ],
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            'this-month'    => [
                'name'  => '当月',
                'start' => date('Y-m-d H:i:s', StatisticalService::$this_month_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$this_month_time_end),
            ],
            'last-month'    => [
                'name'  => '上月',
                'start' => date('Y-m-d H:i:s', StatisticalService::$last_month_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$last_month_time_end),
            ],
            'this-year' => [
                'name'  => '今年',
                'start' => date('Y-m-d H:i:s', StatisticalService::$this_year_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$this_year_time_end),
            ],
            'last-year' => [
                'name'  => '去年',
                'start' => date('Y-m-d H:i:s', StatisticalService::$last_year_time_start),
                'end'   => date('Y-m-d H:i:s', StatisticalService::$last_year_time_end),
            ],
        ];
    }

D
devil_gong 已提交
237
    /**
238
     * 用户总数,今日,昨日,当月,上月总数
D
devil_gong 已提交
239 240 241 242 243 244 245 246 247 248 249
     * @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);

250 251 252 253 254 255 256 257 258 259 260
        // 上月
        $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],
261 262
            ['add_time', '>=', self::$this_month_time_start],
            ['add_time', '<=', self::$this_month_time_end],
263 264 265
        ];
        $same_month_count = Db::name('User')->where($where)->count();

D
devil_gong 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        // 昨天
        $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 已提交
281
        $result = [
282 283
            'last_month_count'  => $last_month_count,
            'same_month_count'  => $same_month_count,
G
gongfuxiang 已提交
284 285 286 287 288 289 290
            'yesterday_count'   => $yesterday_count,
            'today_count'       => $today_count,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
291
     * 订单总数,今日,昨日,当月,上月总数
G
gongfuxiang 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304
     * @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已关闭)
305 306 307 308 309 310 311 312 313 314 315 316

        // 上月
        $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],
317 318
            ['add_time', '>=', self::$this_month_time_start],
            ['add_time', '<=', self::$this_month_time_end],
319 320 321
        ];
        $same_month_count = Db::name('Order')->where($where)->count();

G
gongfuxiang 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        // 昨天
        $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 = [
340 341
            'last_month_count'  => $last_month_count,
            'same_month_count'  => $same_month_count,
G
gongfuxiang 已提交
342 343 344 345 346 347 348
            'yesterday_count'   => $yesterday_count,
            'today_count'       => $today_count,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
349
     * 订单成交总量,今日,昨日,当月,上月总数
G
gongfuxiang 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363
     * @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已关闭)

364 365 366 367 368 369 370 371 372 373 374
        // 上月
        $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],
375 376
            ['add_time', '>=', self::$this_month_time_start],
            ['add_time', '<=', self::$this_month_time_end],
377 378 379
        ];
        $same_month_count = Db::name('Order')->where($where)->count();

G
gongfuxiang 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        // 昨天
        $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 = [
398 399
            'last_month_count'  => $last_month_count,
            'same_month_count'  => $same_month_count,
D
devil_gong 已提交
400 401 402
            'yesterday_count'   => $yesterday_count,
            'today_count'       => $today_count,
        ];
G
gongfuxiang 已提交
403 404 405 406
        return DataReturn('处理成功', 0, $result);
    }

    /**
407
     * 订单收入总计,今日,昨日,当月,上月总数
G
gongfuxiang 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421
     * @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已关闭)

D
Devil 已提交
422 423 424 425 426 427 428 429 430 431
        // 是否有收入统计权限
        if(AdminIsPower('index', 'income'))
        {
            // 上月
            $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');
432

D
Devil 已提交
433 434 435 436 437 438 439
            // 当月
            $where = [
                ['status', 'in', [2,3,4]],
                ['add_time', '>=', self::$this_month_time_start],
                ['add_time', '<=', self::$this_month_time_end],
            ];
            $same_month_count = Db::name('Order')->where($where)->sum('total_price');
440

D
Devil 已提交
441 442 443 444 445 446 447
            // 昨天
            $where = [
                ['status', 'in', [2,3,4]],
                ['add_time', '>=', self::$yesterday_time_start],
                ['add_time', '<=', self::$yesterday_time_end],
            ];
            $yesterday_count = Db::name('Order')->where($where)->sum('total_price');
G
gongfuxiang 已提交
448

D
Devil 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461
            // 今天
            $where = [
                ['status', 'in', [2,3,4]],
                ['add_time', '>=', self::$today_time_start],
                ['add_time', '<=', self::$today_time_end],
            ];
            $today_count = Db::name('Order')->where($where)->sum('total_price');
        } else {
            $last_month_count = 0.00;
            $same_month_count = 0.00;
            $yesterday_count = 0.00;
            $today_count = 0.00;
        }
G
gongfuxiang 已提交
462 463 464

        // 数据组装
        $result = [
465 466
            'last_month_count'  => PriceNumberFormat($last_month_count),
            'same_month_count'  => PriceNumberFormat($same_month_count),
G
gongfuxiang 已提交
467 468 469 470 471 472
            'yesterday_count'   => PriceNumberFormat($yesterday_count),
            'today_count'       => PriceNumberFormat($today_count),
        ];
        return DataReturn('处理成功', 0, $result);
    }

D
devil 已提交
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
     * 基础数据总计
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-08-31
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public static function BaseTotalCount($params = [])
    {
        // 日期条件处理
        $where = [];
        if(!empty($params['start']))
        {
            $where[] = ['add_time', '>=', $params['start']];
        }
        if(!empty($params['end']))
        {
            $where[] = ['add_time', '<=', $params['end']];
        }

        // 用户总数
        $user_count = Db::name('User')->where($where)->count();

        // 订单总数
        $order_count = Db::name('Order')->where(array_merge($where, [['status', '<=', 4]]))->count();

        // 订单成交总量
        $order_sale_count = Db::name('Order')->where(array_merge($where, [['status', '=', 4]]))->count();

D
Devil 已提交
504 505 506 507 508 509 510 511
        // 订单收入总计、是否有收入统计权限
        if(AdminIsPower('index', 'income'))
        {
            $order_complete_total = Db::name('Order')->where(array_merge($where, [['status', 'in', [2,3,4]]]))->sum('total_price');
        } else {
            $order_complete_total = 0.00;
        }
        
512 513 514 515 516

        $result = [
            'user_count'            => $user_count,
            'order_count'           => $order_count,
            'order_sale_count'      => $order_sale_count,
D
Devil 已提交
517
            'order_complete_total'  => PriceNumberFormat($order_complete_total),
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
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
     * 区间时间创建
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-08-30
     * @desc    description
     * @param   [int]          $start [起始时间]
     * @param   [int]          $end   [结束时间]
     */
    public static function DayCreate($start, $end)
    {
        $data = [];
        while(true)
        {
            // 计算时间条件
            $temp_end = strtotime('+1 day', $start);

            // 最大时间减1秒,条件使用 start >= ? && end <= ?
            // start 2021-01-01 00:00:00 , end 2021-01-01 23:59:58
            $data[] = [
                'start' => $start,
                'end'   => $temp_end-1,
                'date'  => date('Y-m-d H:i:s', $start).' - '.date('Y-m-d H:i:s', $temp_end-1),
            ];

            // 结束跳出循环
            if($temp_end >= $end)
            {
                // 结束使用最大时间替代计算的最后一个最大时间
                $count = count($data)-1;
                $data[$count]['end'] = $end;
                $data[$count]['date'] = date('Y-m-d H:i:s', $data[$count]['start']).' - '.date('Y-m-d H:i:s', $end);
                break;
            }
            $start = $temp_end;
        }
        return $data;
    }

    /**
     * 订单收益趋势
D
devil 已提交
564 565 566 567 568 569
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
570
    public static function OrderProfitTotal($params = [])
D
devil 已提交
571 572
    {
        // 订单状态列表
D
Devil 已提交
573
        $order_status_list = MyConst('common_order_status');
D
devil 已提交
574 575 576 577 578 579 580 581
        $status_arr = array_column($order_status_list, 'id');

        // 循环获取统计数据
        $data = [];
        $value_arr = [];
        $name_arr = [];
        if(!empty($status_arr))
        {
582 583
            $date = self::DayCreate($params['start'], $params['end']);
            foreach($date as $day)
D
devil 已提交
584 585
            {
                // 当前日期名称
586
                $name_arr[] = date('Y-m-d', $day['start']);
D
devil 已提交
587

D
dev  
Devil 已提交
588
                // 根据状态获取数量
D
devil 已提交
589 590 591 592 593
                foreach($status_arr as $status)
                {
                    // 获取订单
                    $where = [
                        ['status', '=', $status],
594 595
                        ['add_time', '>=', $day['start']],
                        ['add_time', '<=', $day['end']],
D
devil 已提交
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
                    ];
                    $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 已提交
622
    /**
623
     * 订单交易趋势
G
gongfuxiang 已提交
624 625 626 627 628 629
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
630
    public static function OrderTradingTotal($params = [])
G
gongfuxiang 已提交
631 632
    {
        // 订单状态列表
D
Devil 已提交
633
        $order_status_list = MyConst('common_order_status');
G
gongfuxiang 已提交
634 635 636 637
        $status_arr = array_column($order_status_list, 'id');

        // 循环获取统计数据
        $data = [];
D
devil 已提交
638
        $value_arr = [];
G
gongfuxiang 已提交
639 640 641
        $name_arr = [];
        if(!empty($status_arr))
        {
642 643
            $date = self::DayCreate($params['start'], $params['end']);
            foreach($date as $day)
G
gongfuxiang 已提交
644 645
            {
                // 当前日期名称
646
                $name_arr[] = date('Y-m-d', $day['start']);
G
gongfuxiang 已提交
647

D
dev  
Devil 已提交
648
                // 根据状态获取数量
G
gongfuxiang 已提交
649 650 651 652 653
                foreach($status_arr as $status)
                {
                    // 获取订单
                    $where = [
                        ['status', '=', $status],
654 655
                        ['add_time', '>=', $day['start']],
                        ['add_time', '<=', $day['end']],
G
gongfuxiang 已提交
656
                    ];
D
devil 已提交
657
                    $value_arr[$status][] = Db::name('Order')->where($where)->count();
G
gongfuxiang 已提交
658 659 660 661 662 663 664 665 666
                }
            }
        }

        // 数据格式组装
        foreach($status_arr as $status)
        {
            $data[] = [
                'name'      => $order_status_list[$status]['name'],
D
devil 已提交
667
                'type'      => ($status == 4) ? 'bar' : 'line',
G
gongfuxiang 已提交
668
                'tiled'     => '总量',
D
devil 已提交
669
                'data'      => empty($value_arr[$status]) ? [] : $value_arr[$status],
G
gongfuxiang 已提交
670 671 672 673 674 675 676 677 678 679 680 681
            ];
        }

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

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
    /**
     * 热销商品
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
    public static function GoodsHotTotal($params = [])
    {    
        // 获取订单id
        $where = [
            ['status', '<=', 4],
        ];
        if(!empty($params['start']))
        {
            $where[] = ['add_time', '>=', $params['start']];
        }
        if(!empty($params['end']))
        {
            $where[] = ['add_time', '<=', $params['end']];
        }
        $order_ids = Db::name('Order')->where($where)->column('id');

        // 获取订单详情热销商品
        if(empty($order_ids))
        {
            $data = [];
        } else {
            $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(30)->select()->toArray();
        }

        if(!empty($data))
        {
            foreach($data as &$v)
            {
                // 获取商品名称(这里不一次性读取、为了兼容 mysql 5.7+版本)
                $v['name'] = Db::name('OrderDetail')->where('goods_id', $v['goods_id'])->value('title');
                if(mb_strlen($v['name'], 'utf-8') > 12)
                {
                    $v['name'] = mb_substr($v['name'], 0, 12, 'utf-8').'...';
                }
                unset($v['goods_id']);
            }
        }

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

G
gongfuxiang 已提交
736
    /**
737
     * 支付方式
G
gongfuxiang 已提交
738 739 740 741 742 743
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
744
    public static function PayTypeTotal($params = [])
G
gongfuxiang 已提交
745 746 747
    {
        // 获取支付方式名称
        $where = [
D
cj  
devil 已提交
748 749
            ['business_type', '<>', ''],
            ['status', '=', 1],
G
gongfuxiang 已提交
750 751 752 753 754 755
        ];
        $pay_name_arr = Db::name('PayLog')->where($where)->group('payment_name')->column('payment_name');


        // 循环获取统计数据
        $data = [];
D
devil 已提交
756
        $value_arr = [];
G
gongfuxiang 已提交
757 758 759
        $name_arr = [];
        if(!empty($pay_name_arr))
        {
760 761
            $date = self::DayCreate($params['start'], $params['end']);
            foreach($date as $day)
G
gongfuxiang 已提交
762 763
            {
                // 当前日期名称
764
                $name_arr[] = date('m-d', $day['start']);
G
gongfuxiang 已提交
765 766 767 768 769 770 771

                // 根据支付名称获取数量
                foreach($pay_name_arr as $payment)
                {
                    // 获取订单
                    $where = [
                        ['payment_name', '=', $payment],
772 773
                        ['add_time', '>=', $day['start']],
                        ['add_time', '<=', $day['end']],
G
gongfuxiang 已提交
774
                    ];
D
devil 已提交
775
                    $value_arr[$payment][] = Db::name('PayLog')->where($where)->count();
G
gongfuxiang 已提交
776 777 778 779 780 781 782 783 784 785 786 787
                }
            }
        }

        // 数据格式组装
        foreach($pay_name_arr as $payment)
        {
            $data[] = [
                'name'      => $payment,
                'type'      => 'line',
                'stack'     => '总量',
                'areaStyle' => (object) [],
D
devil 已提交
788
                'data'      => empty($value_arr[$payment]) ? [] : $value_arr[$payment],
G
gongfuxiang 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801
            ];
        }

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

    /**
802
     * 订单地域分布
G
gongfuxiang 已提交
803 804 805 806 807 808
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
809 810
    public static function OrderWholeCountryTotal($params = [])
    {
D
devil_gong 已提交
811
        // 获取订单id
G
gongfuxiang 已提交
812
        $where = [
D
devil_gong 已提交
813
            ['status', '<=', 4],
814
            ['order_model', 'in', [0,2]],
G
gongfuxiang 已提交
815
        ];
D
Devil 已提交
816 817 818 819 820 821 822 823
        if(!empty($params['start']))
        {
            $where[] = ['add_time', '>=', $params['start']];
        }
        if(!empty($params['end']))
        {
            $where[] = ['add_time', '<=', $params['end']];
        }
D
devil_gong 已提交
824 825 826 827 828 829 830
        $order_ids = Db::name('Order')->where($where)->column('id');

        // 获取订单详情热销商品
        if(empty($order_ids))
        {
            $data = [];
        } else {
831
            $data = Db::name('OrderAddress')->field('province_name as name, count(*) AS value')->where('order_id', 'IN', $order_ids)->group('province_name')->order('value asc')->select()->toArray();
G
gongfuxiang 已提交
832 833 834 835 836
        }

        // 数据组装
        $result = [
            'name_arr'  => array_column($data, 'name'),
837
            'data'      => array_column($data, 'value'),
G
gongfuxiang 已提交
838 839
        ];
        return DataReturn('处理成功', 0, $result);
D
devil_gong 已提交
840
    }
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

    /**
     * 统计数据
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-08-30
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public static function StatsData($params = [])
    {
        // 请求类型
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'type',
                'error_msg'         => '类型为空',
            ],
D
Devil 已提交
860 861 862 863
        ];
        if(isset($params['type']) && in_array($params['type'], ['order-profit', 'order-trading', 'pay-type']))
        {
            $p[] = [
864 865
                'checked_type'      => 'empty',
                'key_name'          => 'start',
D
Devil 已提交
866 867 868
                'error_msg'         => '开始时间不能为空',
            ];
            $p[] = [
869 870
                'checked_type'      => 'empty',
                'key_name'          => 'end',
D
Devil 已提交
871 872 873
                'error_msg'         => '结束时间不能为空',
            ];
        }
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
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 时间处理
        $params['start'] = strtotime($params['start']);
        $params['end'] = strtotime($params['end']);
        if($params['end'] < $params['start'])
        {
            return DataReturn('开始时间不能小于结束时间', -1);
        }

        // 根据类型处理数据
        switch($params['type'])
        {
            // 基础配置
            case 'base-count' :
                $ret = self::BaseTotalCount($params);
                break;

            // 订单成交金额走势
            case 'order-profit' :
                $ret = self::OrderProfitTotal($params);
                break;

            // 订单交易走势
            case 'order-trading' :
                $ret = self::OrderTradingTotal($params);
                break;

            // 热销商品
            case 'goods-hot' :
                $ret = self::GoodsHotTotal($params);
                break;

            // 支付方式
            case 'pay-type' :
                $ret = self::PayTypeTotal($params);
                break;

916 917 918 919 920
            // 订单地域分布
            case 'order-whole-country' :
                $ret = self:: OrderWholeCountryTotal($params);
                break;

921 922 923 924 925
            default :
                $ret = DataReturn('类型有误', -1);
        }
        return $ret;
    }
D
devil_gong 已提交
926 927
}
?>