<?php namespace Place_user\Lib;
use Dever;
use Place;
class Role
{
    # 获取等级的权益
    public function profit($level_id)
    {
        $profit = Dever::db('role_level_profit', 'place_user')->select(array('level_id' => $level_id, 'status' => 1));
        if ($profit) {
            $result = array();
            foreach ($profit as $v) {
                if ($v['type']) {
                    list($type, $type_id) = explode(',', $v['type']);
                    if ($type == 1) {
                        $channel = Dever::db('channel', 'place_source')->find($type_id);
                        $name = $channel['name'];
                    } elseif ($type == 2) {
                        $role = Dever::db('role', 'place_user')->find($type_id);
                        $name = $role['name'];
                    }
                    $desc = array();
                    if ($v['buy']) {
                        $desc[] = '购买折扣' . $v['buy'] . '%';
                    }
                    if ($v['fee']) {
                        $desc[] = '佣金' . $v['fee'] . '%';
                    }
                    if ($v['total']) {
                        $desc[] = '每日上限' . $v['total'] . '次';
                    }
                    $desc = implode('，', $desc);
                    $result[] = $name . '：' . $desc;
                }
            }
            return $result;
        }
        return false;
    }

    # 获取购买资源的价格列表
    public function getSourcePrice($price, $info_id, $type)
    {
        $sets = Dever::db('user_role', 'place_user')->select(array('info_id' => $info_id));

        $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, 'place_user')->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 getPrice()
    {
        $role_id = Dever::input('role_id', 'is_numeric', '角色ID');
        $role = Dever::db('role', 'place_user')->find(array('id' => $role_id, 'status' => 1));
        if (!$role) {
            Dever::error('角色信息错误');
        }
        $level_id = Dever::input('level_id', 'is_numeric', '等级ID');
        $level = Dever::db('role_level', 'place_user')->find(array('id' => $level_id, 'role_id' => $role_id, 'status' => 1), array('col' => 'id,name,level,price,role_id,pay'));
        if (!$level) {
            Dever::error('角色等级信息错误');
        }
        $level['role_name'] = $role['name'];
        if (Place::$uid) {
            $user = Dever::db('user_role', 'place_user')->find(array('uid' => Place::$uid, 'role_id' => $role['id'], 'level_id' => $level['id']));
            if ($user && $user['edate'] <= time()) {
                # 存在，并且未过期
                if ($level['pay'] == 1 && $level['id'] != $user['level_id']) {
                    # 买过，补差价
                    $user_level = Dever::db('role_level', 'place_user')->find($user['level_id']);
                    $level['price'] -= $user_level['price'];
                    $level['price'] = $level['price'] * $user['num'];
                }
            }
        }
        return $level;
    }

    # 购买会员
    public function pay_commit(){}
    public function pay()
    {
        $level = $this->getPrice();
        $order = array();
        $order['uid'] = Place::$uid;
        $order['name'] = $level['role_name'] . '-' . $level['name'];
        $order['cash'] = $level['price'];
        $order['order_num'] = Dever::load('util', 'api')->createNumber('VI', 'place_order/role');
        $order['notify'] = 'place_user/role.success|' . Dever::input('p') . '|' . $order['order_num'];
        $data['pay'] = Dever::load('account', 'place')->pay($order);
        //if ($data['pay'] && empty($data['pay']['link'])) {
        if ($data['pay']) {
            $order['status'] = 1;
            $order['role_id'] = $level['role_id'];
            $order['level_id'] = $level['id'];
            $data['order_id'] = Dever::db('role', 'place_order')->insert($order);
            $data['order_num'] = $order['order_num'];
            # 上线这里注释掉
            $this->success(Dever::input('p'), $data['order_num'], 1);
        }
        return $data;
    }

    # 支付成功
    public function success($place, $order_num, $status, $body = array())
    {
        Dever::input('authorization', 'is_string', '入口码', $place);
        $order = Dever::db('role', 'place_order')->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('role', 'place_order')->update($order['id'], $update);
            if (!$state) {
                return '订单更新失败';
            }
            if ($status == 1) {
                $where = array('uid' => $order['uid'], 'role_id' => $order['role_id'], 'level_id' => $order['level_id']);
                $user = Dever::db('user_role', 'place_user')->find($where);
                $level = Dever::db('role_level', 'place_user')->find($order['level_id']);
                $where['level_num'] = $level['level'];
                $where['edate'] = $level['day']*86400;
                if ($user) {
                    $where['edate'] += $user['edate'];
                    $where['num'] = $user['num'] + 1;
                    Dever::db('user_role', 'place_user')->update($user['id'], $where);
                } else {
                    $where['edate'] += time();
                    Dever::db('user_role', 'place_user')->insert($where);
                }
            }
        }
    }
}