<?php namespace Channel\Lib;
use Dever;
use Manage\Lib\Auth;
class Manage extends Auth
{
    # 获取渠道列表
    public function getInfo()
    {
        return Dever::db('info', 'channel')->select(array('status' => 1));
    }
    public function formatTime($timevalue)
    {
        if(strpos($timevalue,"-"))
        {
            return strtotime($timevalue);
        }
        else
        {
            return intval(($timevalue- 25569) * 3600 * 24) - 8*3600 + 60;
        }
    }
    # 上传卡密
    public function uploadCard($data)
    {
        if ($data['channel_id'] && $data['goods'] && $data['file']) {
            $goods = explode(',', $data['goods']);
            $file = Dever::load('view', 'upload')->local($data['file']);
            $excel = Dever::load('import', 'excel')->act($file);
            if ($excel) {
                foreach ($excel as $k => $v) {
                    if ($k > 1 && $v['A']) {
                        $update = array();
                        $update['number'] = $v['A'];
                        $info = Dever::db('card', 'channel')->find($update);
                        if ($info) {
                            Dever::error('卡号：' . $update['number'] . '已存在');
                        }
                        $update['pwd'] = $v['B'];
                        $value = $v['C'];
                        $end = $v['D'];
                        if (!strstr($end, '-')) {
                            $end = $this->formatTime($end);
                            $end = date('Y-m-d', $end);
                        }
                        $sku = Dever::db('info_sku', 'goods')->find(array('info_id' => $goods[1], 'value' => $value));
                        if (!$sku) {
                            continue;
                        }
                        $update['seller_id'] = $data['seller_id'];
                        $update['goods_id'] = $goods[1];
                        $update['sku_id'] = $sku['id'];
                        $update['channel_id'] = $data['channel_id'];
                        $update['goods'] = $data['goods'];
                        $update['end'] = $end;
                        Dever::db('card', 'channel')->insert($update);
                    }
                }
            }
        } else {
            Dever::error('请上传文件或者选择商品');
        }
        return $data;
    }

    # 展示卡密统计
    public function showCardStat($where)
    {
        $db = Dever::db('card', 'channel');
        $total = $db->count($where);

        $where['use_status'] = 2;
        $yi = $db->count($where);

        $where['use_status'] = 1;
        $wei = $db->count($where);
        return array
        (
            array
            (
                # 一共24
                'span' => '8',
                'name' => '总数量',
                'value' => $total,
            ),
            array
            (
                'span' => '8',
                'name' => '已用数量',
                'value' => $yi,
            ),
            array
            (
                'span' => '8',
                'name' => '剩余可用数量',
                'value' => $wei,
            ),
        );
    }
}