<?php

namespace Stat\Lib;

use Dever;

class Journal
{
    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;

        $info_where['buy'] = 1;
        $hot = Dever::input('hot');
        if ($hot) {
            $info_where['hot'] = $hot;
        }
        
        $journal = Dever::db('journal/info')->state($info_where);

        if ($journal) {
            for ($i = 0; $i < $num; $i++) {
                $where['start'] = strtotime("$start -$i day");
                $where['end'] = $where['start'] + 86399;
                foreach ($journal as $k => $v) {
                    $this->total($v, $where);
                }
            }
        }
        return true;
    }

    private function total($info, $where)
    {
        # 这里就是为了做下sql的实现方式，其实用dever自带的功能完全可以实现。
        $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,4) 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'] && $v['buy_id'] > 0) {
                        $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
                        if ($buy['num']) {
                            $buy_num += $buy['num'];
                        }
                    } elseif ($v['buy_num'] > 0) {
                        $buy_num += $v['buy_num'];
                    }

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

        $sql = 'select * from wonderful_act_order where type = 3 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'] && $v['buy_id'] > 0) {
                        $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
                        if ($buy['num']) {
                            $buy_num += $buy['num'];
                        }
                    } elseif ($v['buy_num'] > 0) {
                        $buy_num += $v['buy_num'];
                    }

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

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

    public function update($cate_id, $info_id, $day, $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['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['cate_id'] = $cate_id;
        $update['journal_id'] = $info_id;
        $update['cdate'] = time();

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

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

        $stat = Dever::db('stat/journal')->getNumByJournal(array('journal_id' => $where['journal_id']));
        if ($stat && $stat['buy_num']) {
            $set['num_ding'] = $stat['buy_num'];
            $set['where_id'] = $where['journal_id'];
            Dever::db('journal/info')->update($set);
        }
    }
}
