<?php
namespace app\adminapi\logic\data_analysis;

use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * DataAnalysis逻辑
 * Class DataAnalysisLogic
 * @package app\adminapi\logic
 */
class DataAnalysisLogic extends BaseLogic
{

    public static function serviceOrdersNumber(array $params = [])
    {
        $condition = self::paramsToSelectCondition($params);
        return Db::name('service_work')->alias('a')->field($condition['field'])
            ->where($condition['where'])
            ->group($condition['group'])
            ->order($condition['order'])
            ->select()->column('y', 'x');
    }
    public static function paramsToSelectCondition(array $params = [])
    {
        $where = [];
        $field = [];
        $group = [];
        $order = [];
        if($params['type'] == 'hour'){
            $params = $params['hour'];
            $field[] = Db::raw("HOUR(FROM_UNIXTIME(a.create_time)) AS x");
            if($params['date_range']){
                list($start_time,$end_time) = $params['date_range'];
                $where[] = ['a.create_time','between',[strtotime($start_time),strtotime($end_time)+86400-1]];
            }
            $group[] = Db::raw("HOUR(FROM_UNIXTIME(a.create_time))");
            $order[] = 'x';
        }else{
            $params = $params['category_type'];
            switch ($params['segmented']){
                case 1:
                    $field[] = Db::raw("FROM_UNIXTIME(a.create_time,'%Y-%m-%d') AS x");
                    if($params['date_range']){
                        list($start_time,$end_time) = $params['date_range'];
                        $where[] = ['a.create_time','between',[strtotime($start_time),strtotime($end_time)+86400-1]];
                    }
                    $group[] = Db::raw("x");
                    $order[] = 'x';
                    break;
                case 2:
                    $field[] = Db::raw("FLOOR((a.create_time - UNIX_TIMESTAMP('".$params['monthdate']."-01')) / (7 * 24 * 60 * 60)) AS x");
                    if($params['monthdate']){
                        $dateTime = new \DateTime($params['monthdate']);
                        $dateTime->modify('last day of this month');
                        $where[] = ['a.create_time','between',[strtotime($params['monthdate'].'-01'),strtotime($dateTime->format('Y-m-d'))+86400-1]];
                    }
                    $group[] = Db::raw("x");
                    $order[] = 'x';
                    break;
                case 3:
                    $field[] = Db::raw("FROM_UNIXTIME(a.create_time,'%m') AS x");
                    if($params['yeardate']){
                        $where[] = ['a.create_time','between',[strtotime($params['yeardate'].'-01-01'),strtotime($params['yeardate'].'-12-31')+86400-1]];
                    }
                    $group[] = Db::raw("x");
                    $order[] = 'x';
                    break;
            }
        }
        $field[] = Db::raw("COUNT(a.id) AS y");
        return [
            'where' => $where,
            'field' => $field,
            'group' => $group,
            'order' => $order
        ];
    }

}