<?php

namespace Stat\Lib;

use Dever;

class Source
{
    const NUM = 3;

    public function data_api()
    {

    }
    /**
     * 获取数据-可以放到cron里
     *
     * @return mixed
     */
    public function data()
    {
        # 获取最近self::NUM天的数据
        $num = Dever::input('num', self::NUM);

        $day = Dever::input('day');
        if ($day) {
            $day = Dever::maketime($day);
        } else {
            $day = time();
        }
        $start = date('Y-m-d 00:00:00', $day);
        $option = array
        (
            'state' => 1,
            'start' => array('yes-cdate', '>'),
            'end' => array('yes-cdate', '<'),
        );
        $where['option'] = $option;

        $source = Dever::db('source/info')->state(array('type' => 2));
        $journal = Dever::db('journal/info')->state(array('buy' => 1));

        if ($source && $journal) {
        	foreach ($source as $sk => $sv) {
        		for ($i = 0; $i < $num; $i++) {
        			$where['start'] = strtotime("$start -$i day");
	                $where['end'] = $where['start'] + 86399;
	                //$log = Dever::load('source/lib/core')->get(date('Y_m_d', $where['start']), 'list', $sv['id'], $where['start'], $where['end']);
                    $log['pv'] = $log['uv'] = $log['user_num'] = $log['user_yes_num'] = 0;
        			foreach ($journal as $k => $v) {
	                    $this->total($sv, $v, $where, $log['pv'], $log['uv'], $log['user_num'], $log['user_yes_num']);
		            }
                }
            }
        }
        return true;
    }

    private function total($source, $info, $where, $pv, $uv, $user_num, $user_yes_num)
    {
        $day = date('Y-m-d', $where['start']);
        $order_num = $order_yes_num = $order_no_num = $buy_num = $buy_cash = 0;
        /*
        $sql = 'select * from wonderful_journal_order where type in(1,3) and source = '.$source['id'].' and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
        $order = Dever::db('journal/order')->fetchAll($sql);

        

        if ($order) {
            foreach ($order as $k => $v) {
                $order_num++;
                if ($v['status'] == 1) {
                    # 待支付
                    $order_no_num++;
                } elseif ($v['status'] == 2) {
                    # 已支付
                    $order_yes_num++;

                    if ($v['buy_id']) {
                        $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
                        if ($buy['num']) {
                            $buy_num += $buy['num'];
                        }
                    }

                    if ($v['cash']) {
                        $buy_cash += $v['cash'];
                    }
                }
            }
        }
        */

        $sql = 'select * from wonderful_act_order where type = 3 and source = '.$source['id'].' and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
        $order = Dever::db('act/order')->fetchAll($sql);

        if ($order) {
            foreach ($order as $k => $v) {
                $order_num++;
                if ($v['status'] == 1) {
                    # 待支付
                    $order_no_num++;
                } elseif ($v['status'] == 2) {
                    # 已支付
                    $order_yes_num++;

                    if ($v['buy_id']) {
                        $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
                        if ($buy['num']) {
                            $buy_num += $buy['num'];
                        }
                    }

                    if ($v['cash']) {
                        $buy_cash += $v['cash'];
                    }
                }
            }
        }

        $this->update($source['id'], $info['cate_id'], $info['id'], $day, $pv, $uv, $user_num, $user_yes_num, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash);
    }

    public function update($source_id, $cate_id, $info_id, $day, $pv, $uv, $user_num, $user_yes_num, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash)
    {
        $update = array();
        $update['day_string'] = $day;
        $update['day_int'] = Dever::maketime($day);
        $update['pv'] = $pv;
        $update['uv'] = $uv;
        $update['user_num'] = $user_num;
        $update['user_yes_num'] = $user_yes_num;
        $update['order_num'] = $order_num;
        $update['order_yes_num'] = $order_yes_num;
        $update['order_no_num'] = $order_no_num;
        $update['buy_num'] = $buy_num;
        $update['buy_cash'] = $buy_cash;
        $update['source_id'] = $source_id;
        $update['cate_id'] = $cate_id;
        $update['journal_id'] = $info_id;
        $update['cdate'] = time();

        $where = array();
        $where['day_int'] = $update['day_int'];
        $where['source_id'] = $update['source_id'];
        $where['journal_id'] = $update['journal_id'];

        $info = Dever::db('stat/source')->one($where);
        if (!$info) {
            $id = Dever::db('stat/source')->insert($update);
        } else {
            $id = $info['id'];
            $update['where_id'] = $id;
            Dever::db('stat/source')->update($update);
        }
    }
}
