<?php namespace Seller\Api;
use Dever;
class Order
{
    private $info;
    public function signature()
    {
        $token = $this->token();
        $request = Dever::input();
        unset($request['l']);
        $result = \Dever\Helper\Secure::get($request, $token);
        $string = '';
        foreach ($result as $k => $v) {
            $string .= $k . '=' . $v . '&';
        }
        $string = rtrim($string, '&');
        return array('param' => $result, 'string' => $string);
    }

    # 检测加密是否正确
    public function check_secure(){}
    public function check_token(){return $this->token();}
    public function check()
    {
        return 'ok';
    }

    # 队列提交订单 不做任何数据库操作 先不实现
    //public function submit_commit(){}
    public function submit_secure(){}
    public function submit_token(){return $this->token();}
    public function submit()
    {
        # 余额保存在redis中
        # 商户信息和折扣也保存在redis中
        # 扣款
        # 收单
        # 按照一个商户一个进程去跑，保证单商户单应用
        return $this->act();
    }

    # 查询订单
    public function info_secure(){}
    public function info_token(){return $this->token();}
    public function info()
    {
        $order_num = Dever::input('order_num');
        if (!$order_num) {
            Dever::error('请传入订单号');
        }
        $info = Dever::db('order', 'seller')->find(array('seller_order_num' => $order_num));
        if (!$info) {
            $info = Dever::db('order_log', 'seller')->find(array('seller_order_num' => $order_num));
            if (!$info) {
                Dever::error('订单不存在');
            }
        }
        $result['system_order_num'] = $info['order_num'];
        $result['order_num'] = $info['seller_order_num'];
        $result['order_status'] = $info['status'];
        $result['cash'] = $info['cash'];
        $result['num'] = $info['num'];
        $result['price'] = $info['price'];
        $result['cdate'] = date('Y-m-d H:i:s', $info['cdate']);
        return $result;
    }

    # 查询账户
    public function account_secure(){}
    public function account_token(){return $this->token();}
    public function account()
    {
        $result['name'] = $this->info['name'];
        $result['yue'] = $this->info['cash'] + $this->info['credit'];
        $result['cash'] = $this->info['cash'];
        $result['credit'] = $this->info['credit'];
        $result['payCash'] = $this->info['p_cash'];
        return $result;
    }

    # 直接提交订单
    private function act()
    {
        if (!$this->info) {
            $this->token();
        }
        $code = Dever::input('code');
        if (!$code) {
            Dever::error('请传入商品编码');
        }
        $account = Dever::input('account');
        if (!$account) {
            Dever::error('请传入账号');
        }
        $order = Dever::input('order');
        if (!$order) {
            Dever::error('请传入订单号');
        }

        $sku = Dever::db('info_sku', 'goods')->find(array('code' => $code));
        if (!$sku) {
            Dever::error('商品编码不存在');
        }
        if ($sku['key'] == -1) {
            $cash = Dever::input('cash');
            if (!$cash) {
                Dever::error('请传入面值');
            }
            $sku['value'] = $cash;
        }
        $goods = Dever::db('info', 'goods')->find($sku['info_id']);
        if (!$goods) {
            Dever::error('商品不存在');
        }
        if ($goods['status'] == 2) {
            Dever::error('商品已下架');
        }
        $num = 1;
        Dever::load('order', 'seller')->add($this->info, $goods, $sku, $account, $order, $num);
        return array
        (
            'order_status' => 1,
        );
    }

    # 获取token
    private function token()
    {
        $appkey = Dever::input('appkey');
        if (!$appkey) {
            Dever::error('appkey错误');
        }
        /*
        $this->info = Dever::cache('seller_' . $appkey);
        if (!$this->info) {
            $this->info = Dever::db('info', 'seller')->find(array('appkey' => $appkey));
        }*/
        $this->info = Dever::db('info', 'seller')->find(array('appkey' => $appkey));
        if (!$this->info) {
            Dever::error('商户不存在');
        }
        if ($this->info && $this->info['status'] == 2) {
            Dever::error('商户已封禁');
        }
        return $this->info['appsecret'];
    }
}