<?php

namespace Stat\Lib;

use Dever;

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

        $seller = Dever::db('code/seller')->state();
        $journal = Dever::db('journal/info')->state(array('buy' => 1));

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

    private function total($seller, $info, $where)
    {
    	$day = date('Y-m-d', $where['start']);
        $sql = 'select * from wonderful_code_info where seller_id = '.$seller['id'].' and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
        $code = Dever::db('code/info')->fetchAll($sql);

        $code_num = $code_yes_num = $code_no_num = $code_drop_num = 0;

        if ($code) {
            foreach ($code as $k => $v) {
                $code_num++;
                if ($v['type'] <= 2) {
                    # 未使用
                    $code_no_num++;
                } elseif ($v['type'] == 3) {
                    # 已使用
                    $code_yes_num++;
                } elseif ($v['type'] == 4) {
                    # 已作废
                    $code_drop_num++;
                }
            }
        }

        $this->update($seller['id'], $info['cate_id'], $info['id'], $day, $code_num, $code_yes_num, $code_no_num, $code_drop_num);
    }

    public function update($seller_id, $cate_id, $info_id, $day, $code_num, $code_yes_num, $code_no_num, $code_drop_num)
    {
        $update = array();
        $update['day_string'] = $day;
        $update['day_int'] = Dever::maketime($day);
        $update['code_num'] = $code_num;
        $update['code_yes_num'] = $code_yes_num;
        $update['code_no_num'] = $code_no_num;
        $update['code_drop_num'] = $code_drop_num;
        $update['seller_id'] = $seller_id;
        $update['cate_id'] = $cate_id;
        $update['journal_id'] = $info_id;
        $update['cdate'] = time();

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

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