<?php

# 销售支付

namespace Shop\Lib;

use Dever;

class Sell
{
    # 1是用户,2是门店
    public $type = 1;
    # 1是列表,2是详情
    public $view = 1;
    # 获取配置
    public $config = array();
    # table
    public $table = 'shop/sell_order';

    public function __construct()
    {
        $this->config = Dever::db($this->table)->config;
    }

    # 设置订单的类型
    public function set($type, $view)
    {
        $this->type = $type;
        $this->view = $view;

        return $this;
    }

    # 获取公共的where
    public function where($id)
    {
        $where = array();
        if ($this->type == 1) {
            $where['uid'] = $id;
        } elseif ($this->type == 2) {
            $where['shop_id'] = $id;
        }
        if (!$where) {
            Dever::alert('参数错误');
        }

        return $where;
    }

    # 获取订单列表
    public function getList($id)
    {
        $result = array();
        $where = $this->where($id);
        $mobile = Dever::input('mobile');
        if ($mobile) {
            $where['mobile'] = $mobile;
        }
        $status = Dever::input('status');
        if ($status) {
            $where['status'] = $status;
        }

        $method = Dever::input('method');
        if ($method) {
            $where['method'] = $method;
        }

        $pay_method = Dever::input('pay_method');
        if ($pay_method) {
            $where['pay_method'] = $pay_method;
        }

        $result['search_value'] = $where;
        $result['search_value']['day'] = $day = Dever::input('day');
        if ($day) {
            $where['start'] = Dever::maketime($day . ' 00:00:00');
            $where['end'] = Dever::maketime($day . ' 23:59:59');
        }

        $result['order'] = Dever::db($this->table)->getAll($where);

        if ($result['order']) {
            foreach ($result['order'] as $k => $v) {
                $result['order'][$k] = $this->getInfo($v);
            }
        }

        $result['search'] = array();
        $result['search']['status'] = array
        (
            array('value' => 1, 'name' => '待支付'),
            array('value' => 2, 'name' => '待处理'),
        );
        if ($method == 1) {
            $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
        } elseif ($method == 2) {
            $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
        } else {
            $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
            $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
        }
        $result['search']['status'][] = array('value' => 8, 'name' => '退款');
        $result['search']['status'][] = array('value' => '5,6', 'name' => '已完成');
        $result['search']['status'][] = array('value' => 7, 'name' => '已取消');


        $result['search']['method'] = array
        (
            array('value' => 1, 'name' => '自提'),
            array('value' => 2, 'name' => '配送')
        );
        $result['search']['pay_method'] = array
        (
            array('value' => 1, 'name' => '平台结算'),
            array('value' => 2, 'name' => '门店代下单')
        );

        return $result;
    }

    # 查看详情
    public function getView($id, $order_id, $show = true)
    {
        $where = $this->where($id);
        $where['id'] = $order_id;

        $result = Dever::db($this->table)->find($where);

        if (!$result) {
            Dever::alert('订单不存在');
        }

        if ($show) {
            $result = $this->getInfo($result);
        }

        return $result;
    }

    # 获取订单详细信息
    public function getInfo($info)
    {
        if ($info['status'] == 1) {
            # 15分钟内支付,900秒
            $m = 9000;
            # 支付倒计时
            $info['time'] = time() - $info['cdate'];
            if ($info['time'] >= $m) {
                # 已过期,自动取消
                $info['time'] = -1;
                Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
                $info['status'] = 11;
            } else {
                $info['time'] = $m - $info['time'];
            }
        }

        $info['status_name'] = $this->config['status'][$info['status']];
        $info['method_name'] = $this->config['method'][$info['method']];
        $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
        $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];

        if (!$info['coupon_cash']) {
            $info['coupon_cash'] = 0;
        }
        if (!$info['refund_cash']) {
            $info['refund_cash'] = 0;
        }

        if ($this->type == 1) {
            if ($info['status'] == 2) {
                $info['status_name'] = '支付成功';
            }
            if ($info['status'] == 3) {
                if ($info['method'] == 1) {
                    $info['status_name'] = '已配货';
                } else {
                    $info['status_name'] = '配送中';
                }
            }
        } elseif ($this->type == 2) {
            if ($info['status'] == 2) {
                $info['status_name'] = '待处理';
            }
            if ($info['status'] == 3) {
                if ($info['method'] == 1) {
                    $info['status_name'] = '待自提';
                } else {
                    $info['status_name'] = '配送中';
                }
            }
        }

        if ($info['method'] == 2) {
            $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
            if ($info['ps_info']) {
                $info['ps_info']['service_name'] = '商家自送';
                $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
                if ($info['ps_info']['qu_date']) {
                    $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
                }
                if ($info['ps_info']['qs_date']) {
                    $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
                }

                $status = Dever::db('shop/sell_order_ps')->config['status'];
                $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
                
                if ($info['ps_info']['service_id'] > 0) {
                    //暂时还没有
                }
            }
        }

        $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
        if ($info['uid'] && $info['uid'] > 0) {
            $info['user'] = Dever::load('passport/api')->info($info['uid']);
        }
        
        $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
        $info['cdate'] = date('Y-m-d H:i', $info['cdate']);

        if ($info['operdate']) {
            $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
        }

        $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
        if ($info['address_id']) {
            $info['address'] = Dever::db('passport/address')->find($info['address_id']);
        }
        if ($this->view == 2) {

            foreach ($info['goods'] as $k => $v) {
                $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
                $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
            }

            if ($info['invoice_id']) {
                $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
            }

            if ($info['user_coupon_id']) {
                $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
                if ($coupon) {
                    $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
                }
            }
        } else {
            foreach ($info['goods'] as $k => $v) {
                $goods = Dever::db('goods/info')->one($v['goods_id']);
                $info['goods'][$k]['name'] = $goods['name'];
                $info['goods'][$k]['cover'] = $goods['cover'];
                $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
            }
        }

        return $info;
    }

    # 取消订单
    public function cancel($id, $order_id)
    {
        $data = $this->getView($id, $order_id, false);
        if ($data['status'] == 1) {
            $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
            if ($state) {
                if ($data['user_coupon_id']) {
                    Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
                }

                $this->updateSell($data, 2);
            }

            return 'ok';
        } else {
            Dever::alert('当前订单状态不允许取消');
        }
    }

    # 获取优惠券
    public function coupon(&$data, $type = 1)
    {
        $user_coupon_id = Dever::input('user_coupon_id');
        $coupon_id = Dever::input('coupon_id');

        $data['user_coupon_id'] = 0;
        $data['coupon_id'] = 0;
        $data['coupon_cash'] = 0;

        if ($type == 1 && isset($data['uid']) && $data['uid'] > 0) {
            # 默认选择优惠券
            if ($data['price'] <= 0) {
                Dever::alert('付款价格错误');
            }
            # 查找符合要求的优惠券
            $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));

            if ($coupon) {
                foreach ($coupon as $k => $v) {
                    $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
                    if ($coupon_info) {
                        if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
                            # 门店专属券 不能给别的门店用
                            continue;
                        }

                        $kou = false;
                        if ($coupon_info['type'] == 1) {
                            # 满减券
                            if ($data['price'] >= $coupon_info['total_cash']) {
                                $kou = true;
                            }
                        } else {
                            $kou = true;
                        }

                        if ($kou) {
                            $coupon_info['user_coupon_id'] = $v['id'];
                            $coupon_info['uid'] = $v['uid'];
                            $coupon_info['shop_id'] = $v['shop_id'];
                            $coupon_info['edate'] = date('Y-m-d', $v['edate']);
                            $data['coupon'][] = $coupon_info;

                            if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
                                $data['user_coupon_id'] = $v['id'];
                                $data['coupon_id'] = $coupon_info['id'];
                                $data['coupon_cash'] = $coupon_info['cash'];
                            }
                        }
                    }
                }
            }
        }

        if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
            # 用户自己使用优惠券
            if ($data['price'] <= 0) {
                Dever::alert('付款价格错误');
            }
            $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
            if (!$coupon) {
                Dever::alert('优惠券不可用');
            }
            if (time() > $coupon['edate']) {
                Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
                Dever::alert('优惠券已过期');
            }

            $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
            if (!$goods_coupon) {
                Dever::alert('优惠券不可用');
            }

            if ($coupon['shop_id'] != $data['shop']['id']) {
                if ($goods_coupon['method'] == 3) {
                    Dever::alert('优惠券不可用');
                } else {
                    $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
                    if (!$coupon_info) {
                        Dever::alert('优惠券不可用');
                    }
                }
            }

            if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
                Dever::alert('优惠券不可用');
            }

            $data['user_coupon_id'] = $user_coupon_id;
            $data['coupon_id'] = $goods_coupon['id'];
            $data['coupon_cash'] = $goods_coupon['cash'];
        } elseif ($coupon_id && $type == 3) {
            # 店里直接给用优惠券
            if ($data['price'] <= 0) {
                Dever::alert('付款价格错误');
            }
            $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $coupon_id));
            if (!$coupon_info) {
                Dever::alert('优惠券不可用');
            }

            $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
            if (!$goods_coupon) {
                Dever::alert('优惠券不可用');
            }

            if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
                Dever::alert('优惠券不可用');
            }

            $data['user_coupon_id'] = -1;
            $data['coupon_id'] = $goods_coupon['id'];
            $data['coupon_cash'] = $goods_coupon['cash'];
        }

        if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
            # 这里还要计算每个商品的优惠金额
            if (isset($data['list']) && $data['list']) {
                foreach ($data['list'] as $k => $v) {
                    $data['list'][$k]['coupon_id'] = $data['coupon_id'];
                    $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
                    $data['list'][$k]['coupon_cash'] = round($v['price']/$data['price'], 2) * $data['coupon_cash'];
                }
            }
            $data['price'] -= $data['coupon_cash'];
            if ($data['price'] < 0) {
                $data['price'] = 0;
            }
        }

        return $data;
    }

    # 得到商品和总价
    public function goods(&$data)
    {
        # 1自提,2配送
        $data['method'] = Dever::input('method', 1);
        $data['pay_method'] = Dever::input('pay_method');
        $card = Dever::input('card');
        $pwd = Dever::input('pwd');

        if ($data['pay_method'] == 3 && $card && $pwd) {
            $data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
            if (!$data['card']) {
                Dever::alert('卡号/密码错误');
            }
            if (time() > $data['card']['edate']) {
                Dever::alert('礼品卡已失效');
            }
            $card_info = Dever::db('goods/card')->find($data['card']['card_id']);
            if (!$card_info) {
                Dever::alert('卡号/密码错误');
            }
            $goods = Dever::array_decode($card_info['goods']);

            $goods_id = array();
            $num = array();
            $sku_id = array();
            foreach ($goods as $k => $v) {
                $goods_id[] = $v['goods_id'];
                $num[] = $v['num'];
                $sku_id[] = -1;
            }
        } else {
            $goods_id = Dever::input('goods_id');
            if (!$goods_id) {
                Dever::alert('请传入商品');
            }
            $goods_id = explode(',', $goods_id);

            $sku_id = Dever::input('price_id');
            if ($sku_id) {
                $sku_id = explode(',', $sku_id);
            }
            
            $num = Dever::input('num');
            if (!$num) {
                Dever::alert('请传入商品数量');
            }
            $num = explode(',', $num);
        }

        $data['price'] = 0;
        $data['num'] = 0;
        $data['name'] = array();
        $count = count($goods_id);
        # 计算总价格
        foreach ($goods_id as $k => $v) {
            $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
            $n = isset($num[$k]) ? $num[$k] : 1;
            $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
            $data['list'][$k]['num'] = $n;

            # 2是库存不足
            $data['list'][$k]['ku_state'] = 1;

            # 验证是否有货
            $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);

            if ($total <= 0) {
                $data['list'][$k]['ku_state'] = 2;
                $data['list'][$k]['buy_num'] = 0;
            }

            if ($data['list'][$k]['ku_state'] == 1) {
                $data['list'][$k]['buy_num'] = $n;
                $data['num'] += $n;
                $data['price'] += $data['list'][$k]['price'] * $n;
                $data['name'][] = $data['list'][$k]['name'];
            }
        }

        $data['name'] = implode(',', $data['name']);

        return $data;
    }

    # 发起支付
	public function pay($method, $pay_method, $pay_type, $user, $shop, $name, $num, $goods, $price, $cash, $address_id, $invoice_id, $info, $card, $coupon_id, $user_coupon_id, $coupon_cash, $cart = false, $system_source = 5, $refer = '')
    {
        if (!$user) {
            Dever::alert('错误的用户信息');
        }

        $uid = $user['id'];
        
        $order_data['shop_id'] = $shop['id'];
        $order_data['uid'] = $uid;
        $order_data['mobile'] = $user['mobile'];
        $order_data['address_id'] = $address_id;
        $order_data['invoice_id'] = $invoice_id;
        $order_data['name'] = $name;
        $order_data['num'] = $num;
        $order_data['info'] = $info;
        $order_data['price'] = $price;
        $order_data['kou_cash'] = $cash;

        $order_data['method'] = $method;
        $order_data['pay_method'] = $pay_method;
        $order_data['pay_type'] = $pay_type;

        if ($card) {
        	$order_data['card_code_id'] = $card['id'];
        }
        if ($user_coupon_id) {
            $order_data['user_coupon_id'] = $user_coupon_id;
        }
        if ($coupon_id) {
            $order_data['coupon_id'] = $coupon_id;
        }
        if ($coupon_cash) {
            $order_data['coupon_cash'] = $coupon_cash;
        }

        $order_data['order_num'] = $this->getOrderId();
        $id = Dever::db('shop/sell_order')->insert($order_data);

        if (!$id) {
            Dever::alert('支付失败');
        }

        foreach($goods as $k => $v) {
            if ($v['ku_state'] == 1) {
                $data['uid'] = $uid;
                $data['order_id'] = $id;
                $data['goods_id'] = $v['id'];
                $data['sku_id'] = $v['sku_id'];
                $data['price'] = $v['price'] * $v['buy_num'];
                $data['num'] = $v['buy_num'];

                if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
                    $data['user_coupon_id'] = $v['user_coupon_id'];
                }
                if (isset($v['coupon_id']) && $v['coupon_id']) {
                    $data['coupon_id'] = $v['coupon_id'];
                }
                if (isset($v['coupon_cash']) && $v['coupon_cash']) {
                    $data['coupon_cash'] = $v['coupon_cash'];
                } else {
                    $data['coupon_cash'] = 0;
                }

                Dever::db('shop/sell_order_goods')->insert($data);
            }
        }

        # 减少库存 增加销量
        $order_data['id'] = $id;
        $this->updateSell($order_data);

        if ($card || $price <= 0 || $pay_type == 2) {

        	$param['pay_product_id'] = $id;
        	$param['pay_order_id'] = $order_data['order_num'];
        	$param['pay_status'] = 2;
        	$param['pay_cash'] = $price;
        	$param['pay_msg'] = '';
        	$this->success($param);

            if ($cart) {
                # 来自购物车,要清空一下
                $this->dropCart($cart, $uid, $goods, $shop['id']);
            }
            # 使用优惠券
            $this->useCoupon($coupon_id, $user_coupon_id);

            if ($card) {
                # 更新礼品卡状态
                $card_update['where_id'] = $card['id'];
                $card_update['status'] = 2;
                $card_update['uid'] = $uid;
                $card_update['shop_id'] = $shop['id'];
                $card_update['order_id'] = $id;
                $card_update['ddate'] = time();
                Dever::db('goods/card_code')->update($card_update);
            }
        	return array
            (
                'order' => '',
                'order_num' => $order_data['order_num'],
                'order_id' => $id,
                'type' => 'pay',
            );
        }

        $param = array
        (
            'project_id' => 1,
            'channel_id' => 1,
            'system_source' => $system_source,
            'uid' => $uid,
            'name' => $order_data['name'],
            'cash' => $price,
            'product_id' => $id,
            'order_id' => $order_data['order_num'],
            'refer' => $refer,
        );

        $receipt = Dever::input('receipt');
        if ($receipt) {
            $param['receipt'] = $receipt;
        }

        //$result = Dever::load('pay/api.pay', $param);
        $result['type'] = 'test';
        $result['order'] = '';
        if ($cart) {
            # 来自购物车,要清空一下
            $this->dropCart($cart, $uid, $goods, $shop['id']);
        }
        # 使用优惠券
        $this->useCoupon($coupon_id, $user_coupon_id);
        $result['order_num'] = $order_data['order_num'];
        $result['order_id'] = $id;
        return $result;
    }

    public function dropCart($cart, $uid, $goods, $shop_id)
    {
        $where['uid'] = $uid;
        $where['shop_id'] = $shop_id;
        foreach ($goods as $k => $v) {
            $where['goods_id'] = $v['id'];
            if ($cart == 1) {
                Dever::db('shop/cart')->delete($where);
            } else {
                unset($where['uid']);
                Dever::db('shop/shop_cart')->delete($where);
            }
        }
    }

    public function useCoupon($coupon_id, $user_coupon_id)
    {
        if ($user_coupon_id) {
            Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
        }

        # 这里可以记录一下使用日志
    }

    # 再次发起支付
    public function rpay($id, $system_source = 5, $refer = '')
    {
    	$order = Dever::db('shop/sell_order')->find($id);

    	if (!$order) {
    		Dever::alert('订单信息错误');
    	}

    	if ($order['status'] > 1) {
    		Dever::alert('订单已支付或已取消');
    	}

    	$param = array
        (
            'project_id' => 1,
            'channel_id' => 1,
            'system_source' => $system_source,
            'uid' => $order['uid'],
            'name' => $order['name'],
            'cash' => $order['price'],
            'product_id' => $id,
            'order_id' => $order['order_num'],
            'refer' => $refer,
        );

        $receipt = Dever::input('receipt');
        if ($receipt) {
            $param['receipt'] = $receipt;
        }

        $result = Dever::load('pay/api.pay', $param);
        $result['order_num'] = $order['order_num'];
        $result['order_id'] = $id;
        return $result;
    }

    # 支付成功回调 安全加密 设置token
    public function success_secure_api_token()
    {
        $project_id = Dever::input('pay_project_id');
        $info = Dever::db('pay/project')->one($project_id);
        if ($info) {
            return $info['key'];
        }
        return 'shop_dever_2020';
    }

    # 支付成功回调 安全加密
    public function success_secure_api($param = array())
    {
        $this->success($param);
    }

    # 支付成功回调
    public function success($param = array())
    {
        $send = $param ? $param : Dever::preInput('pay_');
        $product_id = $send['pay_product_id'];
        $order_id = $send['pay_order_id'];
        $status = $send['pay_status'];
        $cash = $send['pay_cash'];
        $msg = $send['pay_msg'];

        $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
        if ($order && $order['status'] == 1) {

            if ($status == 2) {
                # 生成自提码或者取件码
                $update['code'] = $this->getCode();
                if ($order['method'] == 1) {
                    $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
                } else {
                    $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
                    # 分配配送信息
                    //$this->updatePs($order);
                }

                # 发消息
                if (Dever::project('message')) {
                    $shop = Dever::db('shop/info')->one($order['shop_id']);
                    $msg_param['type'] = 1;//消息类型1是订单消息
                    $msg_param['id'] = $order['id'];
                    $msg_param['name'] = $shop['name'];
                    $msg_param = Dever::json_encode($msg_param);
                    Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
                }
            }

            $update['status'] = 2;
            $update['where_id'] = $order['id'];
            Dever::db('shop/sell_order')->update($update);
        }

        return 'ok';
    }

    # 添加配送信息
    public function updatePs($order, $status = 1)
    {
        $where['order_id'] = $order['id'];
        $data = Dever::db('shop/sell_order_ps')->find($where);

        if (!$data) {
            $insert = $where;
            $insert['service_id'] = -1;
            $insert['price'] = 0;
            $insert['status'] = $status;
            $insert['num'] = 1;
            if ($status == 2) {
                $insert['qu_date'] = time();
            }
        
            Dever::db('shop/sell_order_ps')->insert($insert);
        } else {
            $update = $where;
            $update['where_id'] = $data['id'];
            $update['status'] = $status;
            if ($status == 2) {
                $update['qu_date'] = time();
            }
            if ($status == 3) {
                $update['qs_date'] = time();
            }
            Dever::db('shop/sell_order_ps')->update($update);
        }
    }

    # 减少库存,增加销量
    public function updateSell($order, $type = 1)
    {
        $where['order_id'] = $order['id'];
        $where['status'] = 1;
        $data = Dever::db('shop/sell_order_goods')->select($where);
        if ($data) {
            Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
        }
    }

    # 生成自提码
    public function getCode()
    {
        $where['code'] = Dever::rand(6, 0);
        $state = Dever::db('shop/sell_order')->one($where);
        if (!$state) {
            return $where['code'];
        } else {
            return $this->getCode();
        }
    }

    # 生成订单号
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('G');
        $state = Dever::db('shop/sell_order')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }

    # 展示订单详情
    public function show()
    {
        $id = Dever::input('order_id');

        $config = Dever::db('shop/sell_order')->config;

        $info = Dever::db('shop/sell_order')->one($id);

        $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
        $tk_process = Dever::db('shop/sell_order_refund')->config['process'];

        $status = $config['status'][$info['status']];

        $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));

        $shop = Dever::db('shop/info')->find($info['shop_id']);

        $user = Dever::db('passport/user')->find($info['uid']);

        if (!$user['truename']) {
            $user['truename'] = $user['username'];
        }

        
        $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';

        $html .= '<tr>
          <td>订单号</td>
          <td>'.$this->table(false, array(array($info['order_num']))).'</td>

        </tr>';

        $html .= '<tr>
          <td>门店信息</td>
          <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>

        </tr>';

        $html .= '<tr>
          <td>用户信息</td>
          <td>'.$this->table(false, array(array('昵称:' . $user['username'], '联系电话:' . $user['mobile']))).'</td>
        </tr>';

        if ($tk && $tk['process'] == 1) {
            $status = '申请' . $tk_status[$tk['status']];
            $process = $tk_process[$tk['process']];
            $process = '';
        }

        if ($info['info']) {
            $html .= '<tr>
              <td>订单备注</td>
              <td>'.$this->table(false, array(array($info['info']))).'</td>

            </tr>';
        }

        if (!$info['refund_cash']) {
            $info['refund_cash'] = 0;
        }

        $html .= '<tr>
          <td>订单信息</td>
          <td>'.$this->table(array('金额', '退款金额', '数量', '支付方式', '配送方式', '订单状态'), array(array($info['price'], $info['refund_cash'], $info['num'],$config['pay_method'][$info['pay_method']], $config['method'][$info['method']], $status))).'</td>
        </tr>';

        if ($info['address_id'] > 0) {
            $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
            $html .= '<tr>
              <td>收货地址</td>
              <td>'.$this->table(array('省份', '城市', '地区','地址', '门牌号', '姓名', '手机号'), array(array($address['province_name'], $address['city_name'], $address['county_name'], $address['address'], $address['house_number'], $address['contact'], $address['mobile']))).'</td>
        </tr>';
        }

        if ($info['invoice_id'] > 0) {
            $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
            $html .= '<tr>
              <td>发票信息</td>
              <td>'.$this->table(array('发票类型', '抬头', '税号','手机号', '邮箱'), array(array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email']))).'</td>
        </tr>';
        }

        

        $html .= '</tbody></table>';

        $html .= '[商品清单]:';

        $head = array('名称', '属性', '价格', '数量', '状态');

        $body = array();
        $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
        $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
        foreach ($goods as $k => $v) {
            $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
            $status = $goods_status[$v['status']];
            $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
            if ($tk && $tk['process'] == 1) {
                $status = '申请' . $tk_status[$tk['status']];
                //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
            }
            if (isset($goods_info['sku'])) {
                $sku = $goods_info['sku']['string'];
            } else {
                $sku = '无';
            }
            $body[$k] = array
            (
                $goods_info['name'],
                $sku,
                $v['price'],
                $v['num'],
                $status,
            );
        }
        $html .= $this->table($head, $body);

        return $html;
    }

    private function table($head, $data)
    {
        $html = '';
        if ($head) {
            $html = '<table class="layui-table">';

            $html .= '<thead><tr>';
            foreach ($head as $k => $v) {
                $html .= '<th>'.$v.'</th>';
            }
            $html .= '</tr></thead>';

            $html .= '<tbody>';
            foreach ($data as $k => $v) {
                $html .= '<tr>';
                foreach ($v as $k1 => $v1) {
                    $html .= '<td>'.$v1.'</td>';
                }
                $html .= '</tr>';
            }

            $html .= '</tbody>';

            $html .= '</table>';
        } else {
            foreach ($data as $k => $v) {
                $html .= '';
                foreach ($v as $k1 => $v1) {
                    $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                $html .= '';
            }
        }
        return $html;
    }
}