<?php namespace Place_source\Lib;
use Dever;
use Place;
class Info
{
    # 获取基本信息
    public function get($info, $cate = false, $view = false)
    {
        if (is_numeric($info)) {
            $info = Dever::db('info', 'place_source')->find($info, array('col' => 'id,name,info,pic,content,status,spec_type,cate_child_id,cate_parent_id,type'));
            if (!$info) {
                Dever::error('资源不存在');
            }
            if ($info['status'] != 1) {
                Dever::error('资源不存在');
            }
        }

        # 获取封面
        $info['cover'] = '';
        $pic = explode(',', $info['pic']);
        unset($info['pic']);
        if (isset($pic[0])) {
            $info['cover'] = $pic[0];
        }

        # 获取最低价格
        $info['sku'] = $this->getSku($info, $cate);

        # 查看详情
        if ($view) {
            $info['pic'] = $pic;
            $info['content'] = htmlspecialchars_decode($info['content']);

            # 查看是否专享
            $info['vip'] = $this->getVip($info, $info['sku']);
        } else {
            if (isset($info['content'])) {
                unset($info['content']);
            }
        }
        return $info;
    }

    public function getSkuList($source_id, $sku_id, $buy_num = 1)
    {
        $info = Dever::db('info', 'place_source')->find($source_id, array('col' => 'id,name,cate_child_id,cate_parent_id,status,type'));
        if (!$info) {
            Dever::error('资源不存在');
        }
        if ($info['status'] != 1) {
            Dever::error('资源不存在');
        }

        $cate_id = $info['cate_child_id'] ? $info['cate_child_id'] : $info['cate_parent_id'];
        $cate = Dever::db('cate', 'place_source')->find($cate_id);

        $where = array('info_id' => $source_id);
        $data = Dever::load('sku', 'api')->getList($where, $sku_id, 'place_source');
        if ($data['sku']) {
            $data['sku'] = $this->getPrice($data['sku'], $cate, $buy_num);
        }
        if ($data['price']) {
            foreach ($data['price'] as &$v) {
                $v = $this->getPrice($v, $cate, $buy_num, false);
            }
        }
        $data['vip'] = $this->getVip($info, $data['sku']);
        $data['source'] = $info;
        return $data;
    }

    public function getSku($info, $cate)
    {
        if (!$cate) {
            $cate_id = $info['cate_child_id'] ? $info['cate_child_id'] : $info['cate_parent_id'];
            $cate = Dever::db('cate', 'place_source')->find($cate_id);
        }

        $where = array('info_id' => $info['id']);
        $sku_id = Dever::input('sku_id');
        if ($sku_id) {
            $where['id'] = $sku_id;
        }
        $sku = Dever::load('sku', 'api')->getPrice($where, $info['spec_type'], 'place_source');
        return $this->getPrice($sku, $cate);
    }

    public function getPrice($sku, $cate, $buy_num = 1, $score = true)
    {
        $result['id'] = $sku['id'];
        if (isset($sku['name'])) {
            $result['name'] = $sku['name'];
        }
        if (isset($sku['pic'])) {
            $result['pic'] = $sku['pic'];
        }
        $result['buy_num'] = $buy_num;
        $result['num'] = $sku['num'];
        $result['price'] = $sku['price'];
        if ($cate['buy']) {
            $result['m_price'] = $result['price'];
            $result['price'] = number_format($result['m_price'] * ($cate['buy']/100), 2);
        }
        if (Place::$uid) {
            # 根据角色信息重新获取价格
            $user_role = Dever::db('user_role', 'place_user')->select(array('uid' => Place::$uid));
            if ($user_role) {
                $result['level'] = array();
                foreach ($user_role as $v) {
                    $result['level'][] = $v['level_id'];
                }
                $profit = Dever::db('role_level_profit', 'place_user')->find(array('level_id' => array('in', $result['level'])), array('order' => 'buy asc'));
                $result['price'] = number_format($result['price'] * ($profit['buy']/100), 2);
            }
        }
        $result['num'] = $result['num']*$buy_num;
        $result['price'] = round($result['price']*$buy_num, 2);

        if ($score) {
            $result['score'] = '';
            $result['score_location'] = 1;
            if ($result['price'] <= 0) {
                $result['price'] = '免费';
                $result['button'] = '';
            } else {
                $result['button'] = '立即购买';
                if ($cate['score_id']) {
                    if (empty($cate['score'])) {
                        $cate['score'] = Dever::db('info', 'place_score')->find($cate['score_id']);
                    }
                    $result['score'] = $cate['score']['symbol'];
                    $result['score_location'] = $cate['score']['symbol_location'];
                }
            }
        }
        
        return $result;
    }

    # 获取专享
    private function getVip($info, &$sku)
    {
        $sku['buy'] = 1;
        $result = array();
        $vip = Dever::db('role_price', 'place_source')->select(array('info_id' => $info['id'], 'status' => 1));
        if ($vip) {
            $sku['buy'] = 2;
            foreach ($vip as $k => $v) {
                if ($v['role_id']) {
                    if (empty($result[$v['role_id']])) {
                        $result[$v['role_id']] = Dever::db('role', 'place_user')->find($v['role_id'], array('col' => 'id,name'));
                    }
                    $w = array('role_id' => $v['role_id']);
                    if ($v['role_level_id']) {
                        $w['id'] = array('in', $v['role_level_id']);
                    }
                    $result[$v['role_id']]['level'] = Dever::db('role_level', 'place_user')->select($w, array('col' => 'id,name'));

                    if ($result[$v['role_id']]['level'] && isset($sku['level'])) {
                        foreach ($result[$v['role_id']]['level'] as $k1 => $v1) {
                            if (in_array($v1['id'], $sku['level'])) {
                                $sku['buy'] = 1;
                            }
                        }
                    }
                }
            }
        }
        return $result;
    }

    # 下单
    public function pay_commit(){}
    public function pay($info)
    {
        $stock = Dever::load('stock', 'place_source')->yue($info['source']['id'], $info['sku']['id']);
        if ($stock !== false) {
            if ($stock < $info['sku']['buy_num']) {
                Dever::error('库存不足');
            }
        }
        $order = array();
        $order['uid'] = Place::$uid;
        $order['name'] = $info['source']['name'];
        $order['cash'] = $info['sku']['price'];
        if ($info['source']['type'] == 1) {
            $order['address_id'] = Dever::input('address_id', 'is_numeric', '收货地址');
        }
        $order['order_num'] = Dever::load('util', 'api')->createNumber('SO', 'place_order/source');
        $order['notify'] = 'place_source/info.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['source_id'] = $info['source']['id'];
            $order['num'] = $info['sku']['buy_num'];
            $data['order_id'] = Dever::db('source', 'place_order')->insert($order);
            $data['order_num'] = $order['order_num'];
            if (isset($info['detail'])) {
                Dever::load('source_log', 'place_order')->up(1, $order['uid'], $data['order_id'], '下单成功');
                $detail['order_id'] = $data['order_id'];
                $detail['source_id'] = $order['source_id'];
                $detail['name'] = $order['name'];
                $detail['sku_id'] = $data['sku']['id'];
                $detail['sku_name'] = $data['sku']['name'];
                $detail['pic'] = $data['sku']['pic'];
                $detail['cash'] = $data['sku']['price'];
                $detail['num'] = $data['sku']['buy_num'];
                $detail['pic'] = $data['sku']['pic'];
                Dever::db('source_detail', 'place_order')->insert($detail);
                if ($stock !== false) {
                    $state = Dever::load('stock', 'place_source')->sell($detail['source_id'], $detail['sku_id'], $detail['num']);
                    if (!$state) {
                        Dever::error('库存不足');
                    }
                }
                # 上线这里注释掉
                $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('source', 'place_order')->find(array('order_num' => $order_num));
        if ($order && $order['status'] == 1) {
            if ($status == 1) {
                $update['status'] = 2;
                $msg = '支付成功';
                
            } else {
                $update['status'] = 7;
                $msg = '支付失败';
            }
            $update['pdate'] = time();
            $state = Dever::db('source', 'place_order')->update($order['id'], $update);
            Dever::load('source/log', 'place_order')->up(1, $order['uid'], $data['order_id'], $msg);
            if ($state && $update['status'] == 7) {
                # 恢复库存
                $this->resell($order['id']);
            }
        }
    }

    # 恢复库存
    public function resell($id)
    {
        $result = Dever::db('source_detail', 'place_order')->select(array('order_id' => $id));
        if ($result) {
            foreach ($result as $v) {
                $state = Dever::load('stock', 'place_source')->sell($v['source_id'], $v['sku_id'], $v['num']*-1);
            }
        }  
    }
}