<?php
namespace User\Controller;

use Zhida\ExcelExporter;

class ReportController extends BaseUserController
{
    const APP_NAME = "dfzc-znts";

    public function index()
    {
        $this->mobile();
        return;
    }
    public function mobile()
    {
        $channel = $_GET['channel'];
        if (!$channel) {
            $channel = '__all__';
        }

        $url = "http://api2.izhida.cn/tongjiV2/report?app_stat_key=t6NkwipRePFcidTJ&app_stat_secret=yK48QGN2em3MjiZA";
        $content = file_get_contents($url);
        $data = json_decode($content, true);
        $data_list = $data['data'];

        $event_meta_list = $this->get_event_meta();
        $this->assign('event_meta_list', $event_meta_list);

        $this->assign('data_list', $data_list[$channel]);

        $this->display("Report_index");
    }
    public function export_pvuv()
    {
        $channel = $_GET['channel'];
        if (!$channel) {
            $channel = '__all__';
        }

        $url = "http://api2.izhida.cn/tongjiV2/report?app_stat_key=t6NkwipRePFcidTJ&app_stat_secret=yK48QGN2em3MjiZA";
        $content = file_get_contents($url);
        $data = json_decode($content, true);
        $data_list = $data['data'][$channel];
        $default_0 = function ($v) {
            if (isset($v)) {
                return $v;
            }
            return 0;
        };
        $columns = array(
            array('日期', 'date'),
            array('UV', 'index_uv', $default_0),
            array('PV', 'index_pv', $default_0),
            array('分享给朋友或微信群人数', 'share_uv_friend', $default_0),
            array('分享给朋友或微信群次数', 'share_pv_friend', $default_0),
            array('分享朋友圈人数', 'share_uv_timeline', $default_0),
            array('分享朋友圈次数', 'share_pv_timeline', $default_0),
            array('来自朋友圈人数', 'from_uv_timeline', $default_0),
            array('来自朋友圈次数', 'from_pv_timeline', $default_0),
            array('来自微信群人数', 'from_uv_groupmessage', $default_0),
            array('来自微信群次数', 'from_pv_groupmessage', $default_0),
            array('来自好友人数', 'from_uv_singlemessage', $default_0),
            array('来自好友次数', 'from_pv_singlemessage', $default_0),

        );
        $file_name = 'pvuv_' . date('Y-m-d_H:i:s');
        ExcelExporter::export($file_name, $columns, $data_list, 'pvuv');

    }
    public function export_photo()
    {
        $sql = "select a.id,a.nickname,a.headimgurl,a.region,b.take_time,b.channel from user a left join prize b on b.user_id = a.id where is_get=1 ";
        $list = M()->query($sql);
        $default_0 = function ($v) {
            if (isset($v)) {
                return $v;
            }
            return 0;
        };
        $columns = array(
            array('id', 'id', $default_0),
            array('名字', 'nickname', $default_0),
            array('地区', 'region', $default_0),
            array('头像', 'headimgurl', $default_0),
            array('渠道', 'channel', $default_0),
            array('时间', 'take_time', function($v){
                return date('Y年m月d日-H:i:s', $v);
            }),
        );
        $file_name = 'prize_' . date('Y-m-d_H:i:s');
        ExcelExporter::export($file_name, $columns, $list, 'prize');

    }
    public function open()
    {
        $type_sate = $c = M('Sate')->where(array(
            'id' => 1,
        ))->save(array(
            'sate' => 1,
        ));

    }
    public function stop()
    {
        $type_sate = $c = M('Sate')->where(array(
            'id' => 1,
        ))->save(array(
            'sate' => 0,
        ));

    }
    private function get_event_meta()
    {
        $event_meta_list = array(
            array("screen_name" => "UV", "item_name" => "index_uv", "type" => 'uv',),
            array("screen_name" => "PV", "item_name" => "index_pv", "type" => 'pv',),
            array("screen_name" => "分享给朋友或微信群人数", "item_name" => "share_uv_friend", "type" => 'uv',),
            array("screen_name" => "分享给朋友或微信群次数", "item_name" => "share_pv_friend", "type" => 'pv',),
            array("screen_name" => "分享朋友圈人数", "item_name" => "share_uv_timeline", "type" => 'uv',),
            array("screen_name" => "分享朋友圈次数", "item_name" => "share_pv_timeline", "type" => 'pv',),
            array("screen_name" => "来自朋友圈人数", "item_name" => "from_uv_timeline", "type" => 'uv',),
            array("screen_name" => "来自朋友圈次数", "item_name" => "from_pv_timeline", "type" => 'pv',),
            array("screen_name" => "来自微信群人数", "item_name" => "from_uv_groupmessage", "type" => 'uv',),
            array("screen_name" => "来自微信群次数", "item_name" => "from_pv_groupmessage", "type" => 'pv',),

            array("screen_name" => "来自好友人数", "item_name" => "from_uv_singlemessage", "type" => 'uv',),
            array("screen_name" => "来自好友次数", "item_name" => "from_pv_singlemessage", "type" => 'pv',),

        );


        foreach ($event_meta_list as &$item) {
            if (empty($item['item_name'])) {
                if ($item['condition']['category'] == "system") {
                    $item['item_name'] = $item['condition']["event_action"];
                } else {
                    $arr = array();
                    if (!empty($item['event_value'])) {
                        $arr[] = $item['category'];
                        $arr[] = $item['event_action'];
                        $arr[] = $item['event_name'];
                        $arr[] = $item['event_value'];
                    } elseif (!empty($item['event_name'])) {
                        $arr[] = $item['category'];
                        $arr[] = $item['event_action'];
                        $arr[] = $item['event_name'];
                    } elseif (!empty($item['event_action'])) {
                        $arr[] = $item['category'];
                        $arr[] = $item['event_action'];
                    } elseif (!empty($item['category'])) {
                        $arr[] = $item['category'];
                    } else {
                        continue;
                    }
                    $item_name = join('$', $arr);
                    if ($item['type'] == 'pv') {
                        $item_name .= '$__pv__';
                    } else {
                        $item_name .= '$__uv__';
                    }
                    $item['item_name'] = $item_name;
                }
            }
        }
        unset($item);
        return $event_meta_list;
    }


}