<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2016/11/2
 * Time: 下午2:57
 */

require_once (BASE_ROOT_PATH . '/helper/stat_helper.php');

class statistics_helper
{
    private static $stInstance;

    private $mItems;
    private $mOther;

    private $mIOSCount;
    private $mAndroidCount;
    private $mWapCount;

    private $mRecordTime;

    const interval_time = 600;

    public static function instance()
    {
        if(self::$stInstance == null) {
            self::$stInstance = new statistics_helper();
        }
        return self::$stInstance;
    }

    private function __construct()
    {
        $this->reinit();
    }

    public function add_logs($params)
    {
        $record_time = $params['star_time'];
        if($this->read_data($record_time))
        {
            $this->mIOSCount     += $params['ios_count'];
            $this->mAndroidCount += $params['android_count'];
            $this->mWapCount     += $params['wap_count'];
            $this->add_items($params);
            $this->add_others($params);

            $data = ['function' => $this->mItems,'other' => $this->mOther,
                'ios_count' => $this->mIOSCount,'android_count' => $this->mAndroidCount,'wap_count' => $this->mWapCount];
            $stat_call = new \statistics\statcall($record_time);
            $stat_call->update($data);
        }
    }

    private function read_data($time)
    {
        if($time <= 0) return false;

        $stat_call = new \statistics\statcall($time);
        $record = $stat_call->read();
        if($record == false) {
            $this->reinit();
        } else {
            $this->mIOSCount     = $record['ios_count'];
            $this->mAndroidCount = $record['android_count'];
            $this->mWapCount = $record['wap_count'];
            $this->mItems = $record['function'];
            $this->mOther = $record['other'];
        }
        return true;
    }

    private function add_others($params)
    {
        $others = $params['other'];
        foreach ($others as $key => $val) {
            $count = $val['count'];
            $this->add_other($key,$count);
        }
    }

    private function add_items($params)
    {
        $acts = $params['function'];
        foreach ($acts as $act => $ops)
        {
            if($this->act($act) == false) continue;

            foreach ($ops as $opx => $data)
            {
                if($this->op($act,$opx) == false) continue;
                $oper = &$this->mItems[$act][$opx];

                foreach ($data as $key => $value)
                {
                    if($key == 'count')
                    {
                        if(empty($oper['count'])) {
                            $oper['count'] = 0;
                        }
                        $oper['count'] += intval($value);
                    }
                }
            }
        }
    }

    private function same_day($cur,$record)
    {
        $left = strtotime(date('Y-m-d',$cur));
        $right = strtotime(date('Y-m-d',$record));

        return ($left == $right);
    }

    public function add_call($param)
    {
        $cur = time();
        if($cur >= $this->mRecordTime + self::interval_time || $this->same_day($cur,$this->mRecordTime) == false)
        {
            $data = ['star_time'=> $this->mRecordTime,'end_time' => time(),
                'function' => $this->mItems,'other' => $this->mOther,
                'ios_count' => $this->mIOSCount,'android_count' => $this->mAndroidCount,'wap_count' => $this->mWapCount];
            QueueClient::push("savelog",$data);
            $this->reinit();
        }

        $act = $param['act'];
        $op  = $param['op'];

        if($this->act($act) == false) return;
        if($this->op($act,$op) == false) return;

        $client_type = $param['client_type'];
        if($client_type == 'ios') {
            $this->mIOSCount += 1;
        }
        elseif($client_type == 'android') {
            $this->mAndroidCount += 1;
        }
        else {
            $this->mWapCount += 1;
        }

        $this->add_data($act,$op,$param);
    }

    private function add_data($act,$op,$param)
    {
        $oper = &$this->mItems[$act][$op];

        if(empty($oper['count'])) {
            $oper['count'] = 1;
        } else {
            $oper['count'] += 1;
        }

        if($act == 'goods_common') {
            return $this->add_goods($op,$param);
        }
        elseif($act == 'special') {
            return $this->add_special($op,$param);
        }
        elseif($act == 'member_bonus') {
            return $this->add_bonus($op,$param);
        }
        elseif($act == 'search') {
            return $this->add_search($op,$param);
        }
    }

    private function add_other($key,$count)
    {
        if(array_key_exists($key,$this->mOther) == false) {
            $this->mOther[$key] = [];
            $this->mOther[$key]['count'] = $count;
        } else {
            $this->mOther[$key]['count'] += $count;
        }
        return true;
    }

    private function add_goods($op,$param)
    {
        if($op != 'index') return false;

        $goods_id  = intval($param['goods_id']);
        if($goods_id > 0) {
            $key = 'goods_' . $goods_id;
            return $this->add_other($key,1);
        } else {
            return false;
        }
    }
    private function add_special($op,$param) {
        if($op != 'index') return false;

        $special_id = intval($param['special_id']);
        if($special_id >= 0) {
            $key = 'special_' . $special_id;
            return $this->add_other($key,1);
        } else {
            return false;
        }
    }
    private function add_bonus($op,$param)
    {
        if($op == 'make') {
            $key = 'make_bonus';
            $count = intval($param['total_num']);
            return $this->add_other($key,$count);
        }
        else {
            return false;
        }
    }
    private function add_search($op,$param)
    {
        if($op == "index")
        {
            $brand_id = intval($param['brand_id']);
            $hot_id   = intval($param['hot_id']);
            $keyword  = trim(urldecode($param['keyword']));

            if($brand_id > 0) {
                $key = "brand_{$brand_id}";
                return $this->add_other($key,1);
            }
            if($hot_id > 0) {
                $key = "hot_{$hot_id}";
                return $this->add_other($key,1);
            }
            if(!empty($keyword)) {
                $key = "keyword_{$keyword}";
                return $this->add_other($key,1);
            }
        }
        elseif($op == "history")
        {
            $keyword  = trim(urldecode($param['keyword']));
            if(!empty($keyword)) {
                $key = "keyword_{$keyword}";
                return $this->add_other($key,1);
            }
        }
        else {
            return false;
        }
    }

    private function act($act)
    {
        if(empty($act)) return false;

        if(isset($this->mItems[$act]) == false) {
            $this->mItems[$act] = [];
        }
        return true;
    }
    private function op($act,$op) {
        if(empty($op)) return false;

        if(isset($this->mItems[$act][$op]) == false) {
            $this->mItems[$act][$op] = [];
        }
        return true;
    }

    private function reinit()
    {
        $this->mItems = [];
        $this->mOther = [];
        $this->mWapCount = 0;
        $this->mIOSCount = 0;
        $this->mAndroidCount = 0;
        $this->mRecordTime = time();
    }
}