<?php namespace Place\Lib;
use Dever;
use Place;
class Account
{
    # 下单操作
    public function pay($order)
    {
        $account = Dever::input('account', 'is_string', '账户', 'wechat');
        $env = Dever::input('env', 'is_numeric', '运行环境', 3);
        $result = Dever::load('util', 'api')->openid($account, $env, Place::$uid);
        if (isset($result['openid'])) {
            $order['openid'] = $result['openid'];
        } elseif (isset($result['link'])) {
            return $result;
        }

        $account = 'pay_' . $account;
        $project = 'api';

        if (Place::$info['pay_account'] > 0) {
            $info = $this->getInfo(Place::$info['pay_account']);
            if ($info) {
                $account = $info;
                $project = 'place';
                $cert = Dever::db('account_cert', 'place')->find(array('account_id' => $info['id'], 'platform_cert_id' => 2));
                if (!$cert || ($cert && $cert['edate'] <= time())) {
                    Dever::load('account', 'api')->run($account, 'task', array(), 1, 'run', $project);
                }
            }
        }

        $order['cash'] = $order['cash']*100;
        $order['time_expire'] = time() + 3600;

        return Dever::load('account', 'api')->run($account, 'order', $order, $env, 'run', $project);
    }

    # 获取账户信息
    public function getInfo($id)
    {
        $info = Dever::db('account', 'place')->find($id);
        if ($info) {
            if ($info['type'] == 1) {
                $key = 'pay_wechat';
            } else {
                Dever::error('错误信息');
            }
            $account = Dever::db('account', 'api')->find(array('key' => $key));
            $info['app_id'] = $account['app_id'];
            $info['platform_id'] = $account['platform_id'];
            return $info;
        }
    }
}