<?php

defined('InShopNC') or exit('Access Invalid!');

class stat_dailyControl extends SystemControl
{
    public function __construct() {
        parent::__construct();
    }

    public function indexOp()
    {
        if (chksubmit()) {
        }

        $mod_stat = Model('stat');
        $stat_list = $mod_stat->statDailyList([], '*', 30);

        Tpl::output('page',$mod_stat->showpage());
        Tpl::output('dailies',$stat_list);
        Tpl::output('colomns',$this->daily_colomns());

        Tpl::showpage('stat.daily.list');

    }
    private function daily_colomns()
    {
        return ['日期','可用金额','今日发送金额','今日未领金额','今日发送个数','今日领取个数','红包支付金额','红包转发金额','红包转发次数','新增用户','总用户','日活','周活','月活','订单总数','订单总额','客单价'];
    }

    public function interactive_listOp()
    {
        $mod_stat = Model('stat');
        $stat_list = $mod_stat->statDailyList([], 'date_id,call_record', 30);

        Tpl::output('page',$mod_stat->showpage());
        Tpl::output('dailies',$stat_list);
        Tpl::output('colomns',$this->call_colomns());

        Tpl::showpage('stat.interactive.list');
    }

    private function call_colomns()
    {
        return ['日期','IOS','android','H5','首页','品牌页','专题','搜索','功效','商品详情','获取验证码','红包打开','发送','从红包发送','摇一摇','红包分享','邀请','收藏','我的F码','商城','我的订单','查看'];
    }

    public function interactive_goodsOp()
    {
        if(empty( $_GET['date_id'])) {
            $date_id = strtotime(date('Y-m-d',time()));
        } else {
            $date_id = $_GET['date_id'];
        }

        $mod_stat = Model('stat');
        $stat_list = $mod_stat->statDailyList(['date_id' => $date_id], 'date_id,call_record');
        if(empty($stat_list) || empty($stat_list[0]['call_record'])) {
            return;
        }
        $record = json_decode($stat_list[0]['call_record'],true);
        $goods = $this->all_goods($record);

        Tpl::output('dailies',$goods);
        Tpl::output('colomns',$this->goods_colomns());

        Tpl::showpage('stat.interactive.goods');
    }

    public function interactive_specialOp()
    {
        if(empty( $_GET['date_id'])) {
            $date_id = strtotime(date('Y-m-d',time()));
        } else {
            $date_id = $_GET['date_id'];
        }

        $mod_stat = Model('stat');
        $stat_list = $mod_stat->statDailyList(['date_id' => $date_id], 'date_id,call_record');
        if(empty($stat_list) || empty($stat_list[0]['call_record'])) {
            return;
        }
        $record = json_decode($stat_list[0]['call_record'],true);
        $specials = $this->all_special($record);

        Tpl::output('dailies',$specials);
        Tpl::output('colomns',$this->special_colomns());

        Tpl::showpage('stat.interactive.special');
    }

    public function interactive_searchOp()
    {
        if(empty( $_GET['date_id'])) {
            $date_id = strtotime(date('Y-m-d',time()));
        } else {
            $date_id = $_GET['date_id'];
        }

        $mod_stat = Model('stat');
        $stat_list = $mod_stat->statDailyList(['date_id' => $date_id], 'date_id,call_record');
        if(empty($stat_list) || empty($stat_list[0]['call_record'])) {
            return;
        }
        $record = json_decode($stat_list[0]['call_record'],true);
        $words = $this->all_keyword($record);

        Tpl::output('words',$words);
        Tpl::output('word_colomns',$this->word_colomns());

        Tpl::showpage('stat.interactive.search');
    }


    private function special_colomns()
    {
        return ['序号','专题编号','名称','点击量','子专题'];
    }

    private function goods_colomns()
    {
        return ['序号','商品编号','商品货架号','名称','点击量'];
    }

    private function brand_colomns()
    {
        return ['序号','品牌编号','名称','点击量'];
    }
    private function word_colomns()
    {
        return ['序号','关键词','搜索量'];
    }

    function compatible_decode($unicode)
    {
        $str = '';
        $arr = array_filter(explode('u', $unicode));
        foreach ($arr as $k => $v) {
            if (strlen($v) == 4) {
                $num = (int)hexdec($v);
                $ord_1 = decbin(0xe0 | ($num >> 12));
                $ord_2 = decbin(0x80 | (($num >> 6) & 0x3f));
                $ord_3 = decbin(0x80 | ($num & 0x3f));
                $str  .= chr(bindec($ord_1)) . chr(bindec($ord_2)) . chr(bindec($ord_3));
            }else{
                $str .= chr(hexdec($v));
            }
        }
        return $str;
    }

    private function all_keyword($record)
    {
        $other = $record['other'];
        if(empty($other)) return false;

        $words = [];
        foreach ($other as $key => $value)
        {
            $params = preg_split('/_/', $key);
            if($params != false && count($params) == 2)
            {
                if($params[0] == 'keyword') {
                    $keyword = $this->compatible_decode($params[1]);
                    $count = $value['count'];
                    $words[] = array('keyword' => $keyword,'count' => $count);
                }
            }
        }
        if(!empty($words)) {
//            uasort($words,['special_item','count_desc']);
        }
        return $words;
    }

    private function all_special($record)
    {
        $other = $record['other'];
        if(empty($other)) return false;

        $specials = [];
        foreach ($other as $key => $value)
        {
            $params = preg_split('/_/', $key);
            if($params != false && count($params) == 2)
            {
                if($params[0] == 'special')
                {
                    $special_id = intval($params[1]);
                    $count = $value['count'];
                    $specials[] = new special_item($special_id,$count);
                }
            }
        }
        if(!empty($specials)) {
            uasort($specials,['special_item','count_desc']);

        }
        return $specials;
    }
    private function all_goods($record)
    {
        $other = $record['other'];
        if(empty($other)) return false;

        $gid_count = [];

        $gids = [];
        foreach ($other as $key => $value)
        {
            $params = preg_split('/_/', $key);
            if($params != false && count($params) == 2)
            {
                if($params[0] == 'goods')
                {
                    $goods_id = intval($params[1]);
                    $count = $value['count'];
                    $gid_count[$goods_id] = $count;
                    $gids[] = $goods_id;
                }
            }
        }
        if(!empty($gids)) {
            $mod_goods = Model('goods');
            $items = $mod_goods->getGoodsList(['goods_id' => ['in',$gids]],'goods_name,goods_id,goods_commonid');
        }

        $goods_items = [];
        if(!empty($items))
        {
            foreach ($items as $goods)
            {
                $gid = $goods['goods_id'];
                $cid = $goods['goods_commonid'];
                $name = $goods['goods_name'];
                if(array_key_exists($gid,$gid_count)) {
                    $count = $gid_count[$gid];
                    $goods_items[] = new goods_item($gid,$cid,$name,$count);
                }
            }
        }

        if(!empty($goods_items)) {
            uasort($goods_items,['goods_item','count_desc']);

        }
        return $goods_items;
    }
}
class goods_item
{
    private $mGoodsID;
    private $mCommonID;
    private $mGoodsName;
    private $mCount;

    public function __construct($gid,$cid,$name,$count)
    {
        $this->mGoodsID  = $gid;
        $this->mCommonID = $cid;
        $this->mGoodsName = $name;
        $this->mCount = $count;
    }
    public function count() {
        return $this->mCount;
    }
    public function fields()
    {
        $result = [];
        $result[] = $this->mGoodsID;
        $result[] = $this->mCommonID;
        $result[] = $this->mGoodsName;
        $result[] = $this->mCount;

        return $result;
    }

    static public function count_desc($left,$right)
    {
        $t_l = intval($left->count());
        $t_r = intval($right->count());

        if($t_l > $t_r) return -1;
        elseif($t_l < $t_r) return 1;
        else return 0;
    }
}

class special_item
{
    private $mSpId;
    private $mCount;
    public function __construct($special_id,$count)
    {
        $this->mSpId = $special_id;
        $this->mCount = $count;
    }
    public function count() {
        return $this->mCount;
    }
    public function fields()
    {
        $result = [];
        $result[] = $this->mSpId;
        $result[] = $this->special_name();
        $result[] = $this->mCount;

        return $result;
    }
    private function special_name()
    {
        $mod = Model('mb_special');
        $special = $mod->getMbSpecialList(['special_id' => $this->mSpId]);
        if(empty($special)) return false;
        return $special[0]['special_desc'];
    }
    private function sub_specials()
    {
    }

    static public function count_desc($left,$right)
    {
        $t_l = intval($left->count());
        $t_r = intval($right->count());

        if($t_l > $t_r) return -1;
        elseif($t_l < $t_r) return 1;
        else return 0;
    }
}

class search_item
{

}

class call_item
{
    private $mDateID;
    private $mRecord;
    public function __construct($item)
    {
        $this->mDateID = $item['date_id'];
        $srecord = $item['call_record'];
        if(empty($srecord)) {
            $this->mRecord = [];
        }
        else{
            $this->mRecord = json_decode($srecord,true);
        }
    }
    public function date_id() {
        return $this->mDateID;
    }
    public function date()
    {
        return strftime('%Y%m%d',intval($this->mDateID));
    }
    public function fields()
    {
        $result = [];
        $result[] = $this->date();
        if(empty($this->mRecord)) return $result;

        $result[] = $this->mRecord['ios_count'];
        $result[] = $this->mRecord['android_count'];
        $result[] = $this->mRecord['wap_count'];

        $funs = $this->mRecord['function'];
        $result[] = $this->accumulate($funs['index']);
        $result[] = $this->accumulate($funs['brand']);
        $result[] = $this->accumulate($funs['special']);
        $result[] = $this->accumulate($funs['search']);
        $result[] = $this->accumulate($funs['category']);
        $result[] = $this->accumulate($funs['goods_common']);
        $result[] = $this->accumulate_op($funs['login'],'getcodex');

        $result[] = $this->accumulate_op($funs['bonusex'],'open');
        $result[] = $this->accumulate_op($funs['member_bonus'],'make');
        $result[] = $this->accumulate_op($funs['member_bonus'],'makeby_bonus');
        $result[] = $this->accumulate_op($funs['member_bonus'],'shake');
        $result[] = $this->accumulate_op($funs['member_bonus'],'share');
        $result[] = $this->accumulate_op($funs['member_bonus'],'invite');
        $result[] = $this->accumulate($funs['member_favorites']);
        $result[] = $this->accumulate($funs['member_fcode']);
        $result[] = $this->accumulate($funs['mshop']);
        $result[] = $this->accumulate($funs['member_order']);

        return $result;
    }
    private function accumulate($act)
    {
        if(empty($act)) return 0;

        $count = 0;
        foreach ($act as $op => $val) {
            $count += $val['count'];
        }
        return $count;
    }
    private function accumulate_op($act,$op)
    {
        if(empty($act)) return 0;
        foreach ($act as $key => $val)
        {
            if($key == $op) {
                return $val['count'];
            }
        }
        return 0;
    }
}

class daily_item
{
    private $mItem;
    public function __construct($item)
    {
        $this->mItem = $item;
    }
    public function date()
    {
        return strftime('%Y-%m-%d',intval($this->mItem['date_id']));
    }
    public function fields()
    {
        $result = [];
        $result[] = $this->date();
        $result[] = $this->accumulate($this->mItem['available_total_amount']);
        $result[] = $this->accumulate($this->mItem['all_total_amount']);
        $result[] = $this->accumulate($this->mItem['all_remain_amount']);
        $result[] = $this->mItem['all_bonus_count'];
        $result[] = $this->mItem['all_bind_count'];
        $result[] = $this->accumulatex($this->mItem['bonus_pay_amount']);
        $result[] = $this->accumulatex($this->mItem['person_send_amount']);
        $result[] = $this->mItem['all_bonus_count'] - $this->mItem['admin_bonus_count'];
        $result[] = $this->mItem['today_user'];
        $result[] = $this->mItem['member_total_num'];
        $result[] = $this->mItem['day_active_member'];
        $result[] = $this->mItem['week_active_member'];
        $result[] = $this->mItem['month_active_member'];
        $result[] = $this->mItem['order_total_num'];
        $result[] = $this->mItem['order_amount'];
        $result[] = $this->mItem['order_price_avg'];

        return $result;
    }
    private function accumulate($input)
    {
        $total = 0;
        if(empty($input)) return $total;

        $moneys = unserialize($input);
        if($moneys == false) return $total;

        foreach ($moneys as $val)
        {
            if(empty($val)) continue;
            foreach ($val as $rate => $money) {
                $total += $money;
            }
        }
        return $total;
    }
    private function accumulatex($input)
    {
        $total = 0;
        if(empty($input)) return $total;

        $moneys = unserialize($input);
        if($moneys == false) return $total;

        foreach ($moneys as $rate => $money)
        {
            $total += $money;
        }
        return $total;
    }
}