<?php namespace Place\Lib;
use Dever;
use Place;
class Vip
{
    protected $type = 'vip';
    protected $name = '会员';
    protected $data;
    protected $info;
    public function __construct()
    {
        if (!$this->data) {
            $where['status'] = 1;
            $this->data = Dever::db($this->type, 'place')->select($where);
            foreach ($this->data as $k => $v) {
                $this->info[$v['id']] = $v['name'];
            }
        }
    }

    public function getInfo()
    {
        $data = array();
        if (Place::$user[$this->type . '_id'] > 0) {
            $data['info'] = Dever::db($this->type, 'place')->find(Place::$user[$this->type . '_id']);
            $data['price'] = Dever::db($this->type . '_price', 'place')->select(array($this->type . '_id' => $data['info']['id']), array('col' => 'per,total,type'));
            if ($data['price']) {
                foreach ($data['price'] as &$v) {
                    if ($v['total'] > 0) {
                        if ($v['type'] == 1) {
                            $v['total'] = '每日可下载共'.$v['total'].'次';
                        } elseif ($v['type'] == 2) {
                            $v['total'] = '每日可使用共'.$v['total'].'次';
                        } elseif ($v['type'] == 3) {
                            $v['total'] = '每次最多购买'.$v['total'].'个';
                        }
                    }
                    
                    $v['per'] = $v['per'] . '%';
                    $v['name'] = Dever::db('resource', 'place')->value('type', $v['type']);
                }
            }
        }
        return $data;
    }

    # 获取所有数据
    public function getData()
    {
        $cur = Place::$user[$this->type . '_id'] ?? 0;
        $user = array();
        if ($cur > 0) {
            $user = Dever::db($this->type, 'place')->find($cur);
        }
        $data['list'] = $this->data;
        if ($data['list']) {
            foreach ($data['list'] as &$v) {
                # 判断哪个已经买过了，不用再买了
                $v['sprice'] = $v['price'];
                # 1是可以购买
                $v['buy'] = 1;
                if ($user && $user['level'] >= $v['level']) {
                    $v['buy'] = 2;
                } elseif ($v['pay'] == 1) {
                    $v['price'] -= $user['price'];
                }
            }
        }
        return $data;
    }

    # 获取列表
    public function getList($state = true)
    {
        $data = array();
        if ($state) {
            $data = array
            (
                0 => array
                (
                    'id' => -1,
                    'name' => '无' . $this->name,
                ),
            );
        }
        return array_merge($data, $this->data);
    }

    # 获取购买资源的价格列表
    public function getResourcePrice($price, $info_id, $app, $type)
    {
        $result = array();
        $result['list'] = array();
        $result['alone'] = array();
        if ($info = $this->info) {
            $type_id = $this->type . '_id';
            $table = $this->type . '_price';
            $result['name'] = Place::$info[$this->type . '_name'];
            $list = array();
            $alone = array();
            $func = Dever::load('price', 'place');
            $sets = Dever::db($table, $app)->select(array('info_id' => $info_id));
            if ($sets) {
                foreach ($sets as $set) {
                    $set['value'] = $func->getValue($set['value'], $price, $type);
                    $set['text'] = $func->getText($set['value'], $type);
                    if ($set[$type_id]) {
                        $set[$type_id] = explode(',', $set[$type_id]);
                        foreach ($set[$type_id] as $id) {
                            if (isset($info[$id])) {
                                $list[$id] = array($info[$id], $set['value'], $set['text']);
                                unset($info[$id]);
                                if ($set['status'] == 1) {
                                    $alone[$id] = $list[$id];
                                }
                            }
                        }
                    } elseif ($info) {
                        foreach ($info as $id => $name) {
                            if (empty($list[$id])) {
                                $list[$id] = array($name, $set['value'], $set['text']);
                                unset($info[$id]);
                            }
                            if ($set['status'] == 1) {
                                $alone[$id] = $list[$id];
                            }
                        }
                    }
                }
            }
            if ($info) {
                foreach ($info as $id => $name) {
                    if (empty($list[$id])) {
                        $info = Dever::db($table, 'place')->find(array($type_id => $id, 'type' => $type));
                        if ($info) {
                            $value = $func->getValue($info['per'], $price, 3);
                            $text = $func->getText($value, $type);
                            $list[$id] = array($name, $value, $text);
                        }
                    }
                }
            }
            $result['list'] = $list;
            $result['alone'] = $alone;
        }
        return $result;
    }

    # 购买会员
    public function pay()
    {
        if (!Place::$uid) {
            Dever::error('请先登录', 300);
        }
        $db = Dever::db($this->type, 'place');
        $id = Dever::input('id', 'is_numeric', 'ID');
        $info = $db->find($id);
        if (!$info) {
            Dever::error('信息不存在');
        }
        $cur = Place::$user[$this->type . '_id'];
        if ($cur == $info['id']) {
            Dever::error('您已购买过' . $info['name']);
        }
        if ($cur > 0) {
            $user = $db->find($cur);
            if ($user && $user['level'] >= $info['level']) {
                Dever::error('您已购买过');
            }
            if ($info['pay'] == 1) {
                $info['price'] -= $user['price'];
            }
        }

        $prefix = strtoupper(substr($this->type, 0, 2));
        $order = array();
        $order['uid'] = Place::$uid;
        $order['name'] = $info['name'];
        $order['cash'] = $info['price'];
        $order['order_num'] = Dever::load('util', 'api')->createNumber($prefix, 'place/'.$this->type.'_order');
        $order['notify'] = 'place/'.$this->type.'.success|' . Dever::input('p') . '|' . $order['order_num'];
        $data['pay'] = Dever::load('account', 'place')->pay($order);
        if ($data['pay'] && empty($data['pay']['link'])) {
            $order['status'] = 1;
            $order[$this->type.'_id'] = $info['id'];
            $data['order_id'] = Dever::db($this->type . '_order', 'place')->insert($order);
            $data['order_num'] = $order['order_num'];
        }
        return $data;
    }

    # 支付成功
    public function success($place, $order_num, $status, $body = array())
    {
        Dever::input('authorization', 'is_string', '入口码', $place);
        $order = Dever::db($this->type.'_order', 'place')->find(array('order_num' => $order_num));
        if ($order && $order['status'] == 1) {
            if ($status == 1) {
                $update['status'] = 2;
            } else {
                $update['status'] = 3;
            }
            
            $update['pdate'] = time();
            $state = Dever::db($this->type.'_order', 'place')->update($order['id'], $update);
            if (!$state) {
                return '订单更新失败';
            }
            Dever::db('user', 'place')->update($order['uid'], array($this->type.'_id' => $order[$this->type.'_id']));
        }
    }
}