<?php

namespace Agent\Lib;

use Dever;

class Dhorder
{
    # 获取配置
    public $config = array();
    # table
    public $table = 'agent/dh_order';

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

    # 获取公共的where
    public function where($id)
    {
        $where = array();
        $where['mid'] = $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;
        }

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

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

        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, $view = true)
    {
        $info['status_name'] = $this->config['status'][$info['status']];

        $info['cdate'] = date('Y-m-d H:i', $info['cdate']);

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

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


        if ($view && $info['type'] == 1) {
        	$info['ps_info'] = Dever::db('agent/dh_order_ps')->find(array('order_id' => $info['id']));
	        if ($info['ps_info']) {
	            $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
	            $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('agent/dh_order_ps')->config['status'];
	            $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
	            if ($info['ps_info']['service']) {
	                foreach ($info['ps_info']['service'] as $k => $v) {
	                	if (isset($v['order_num']) && $v['order_num']) {
	                		$info['ps_info']['service'][$k]['service_name'] = '商家自送';
		                    if ($v['service_id'] > 0) {
		                        $service = Dever::db('shop/service')->find($v['service_id']);
		                        $info['ps_info']['service'][$k]['service_name'] = $service['name'];
		                    }
	                	} else {
	                		unset($info['ps_info']['service'][$k]);
	                	}
	                }
	            }
	        }

        	$goods_status = Dever::db('agent/dh_order_goods')->config['status'];
	        if ($info['address_id']) {
	            $info['address'] = Dever::db('agent/member_address')->find($info['address_id']);
	        }

	        $info['goods'] = Dever::db('agent/dh_order_goods')->select(array('order_id' => $info['id']));
	        foreach ($info['goods'] as $k => $v) {
	        	$goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
                if (isset($goods_info['sku'])) {
                    $sku = $goods_info['sku']['string'];
                } else {
                    $sku = '';
                }
                $info['goods'][$k] += array
                (
                    'pic' => $goods_info['cover'],
                    'name' => $goods_info['name'],
                    'sku' => $sku,
                    'price' => $goods_info['price'],
                );
	        }
        }
        if ($info['type'] == 2) {
            $info['card'] = Dever::db('card/info')->find($info['type_id']);
        }
        if ($info['type'] == 3) {
            $info['prize'] = Dever::db('setting/prize')->find($info['type_id']);
        }

        return $info;
    }

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

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

    # 得到商品和总价
    public function goods($mid)
    {
        $goods_id = Dever::input('goods_id');
        if (!$goods_id) {
            Dever::alert('请传入商品');
        }
        $goods_id = explode(',', $goods_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);
        $data['list'] = array();
        $i = 0;
        foreach ($goods_id as $k => $v) {
            $n = isset($num[$k]) ? $num[$k] : 1;

            $data['list'][$i] = Dever::db('agent/member_goods')->find(array('mid' => $mid, 'id' => $v));

            if ($data['list'][$i]) {
            	$data['list'][$i]['num'] = $n;

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

            	$total = $data['list'][$i]['total_num'] - $data['list'][$i]['sell_num'];
            	if ($total >= $n) {
            		$data['list'][$i]['buy_num'] = $n;
            	} else {
            		$data['list'][$i]['buy_num'] = $total;
            	}

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

            	$goods_info = Dever::load('goods/lib/info')->getInfoBySku($data['list'][$i]['type_id'], $data['list'][$i]['sku_id']);
                if (isset($goods_info['sku'])) {
                    $sku = $goods_info['sku']['string'];
                } else {
                    $sku = '';
                }
                $data['list'][$i] += array
                (
                    'pic' => $goods_info['cover'],
                    'name' => $goods_info['name'],
                    'sku' => $sku,
                    'price' => $goods_info['price'],
                );

                if ($data['list'][$i]['ku_state'] == 1) {
	                $data['num'] += $data['list'][$i]['buy_num'];
	                $data['price'] += $goods_info['price'] * $data['list'][$i]['buy_num'];
	                $data['name'] = '兑换商品';
	            }

                $i++;
            }
        }

        if ($data['num'] <= 0) {
            Dever::alert('没有商品可以兑换');
        }
        return $data;
    }

    # 确认兑换
	public function action($user, $name, $num, $price, $goods, $address_id, $dh_type, $type, $info, $refer = '')
    {
    	# 获取平台店
    	$shop = Dever::db('shop/info')->find(array('type' => 10));
    	if (!$shop) {
    		Dever::alert('没有平台店');
    	}
        $order_data['shop_id'] = $shop['id'];
        $order_data['mid'] = $user['id'];
        $order_data['mobile'] = $user['mobile'];
        $order_data['address_id'] = $address_id;
        $order_data['name'] = $name;
        $order_data['num'] = $num;
        $order_data['info'] = $info;
        $order_data['price'] = $price;
        $order_data['dh_type'] = $dh_type;
        $order_data['type'] = $type;
        $order_data['status'] = 2;
        $order_data['refund_cash'] = 0;
        $order_data['audit'] = 2;

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

        if (!$id) {
            Dever::alert('兑换失败');
        }

        if ($goods) {
            foreach($goods as $k => $v) {
                if ($v['ku_state'] == 1) {
                    $data['mid'] = $user['id'];
                    $data['order_id'] = $id;
                    $data['shop_id'] = $shop['id'];
                    $data['goods_id'] = $v['type_id'];
                    $data['sku_id'] = $v['sku_id'];
                    $data['price'] = $v['price'];
                    $data['num'] = $v['buy_num'];

                    Dever::db('agent/dh_order_goods')->insert($data);
                }
            }
        }

        # 减少库存 增加销量
        $order_data['id'] = $id;
        $this->updateSell($order_data);
    	return array
        (
            'order_num' => $order_data['order_num'],
            'order_id' => $id,
            'type' => 'dh',
        );
    }

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

        $service = array();
        if ($service_id) {
            $service['service_id'] = $service_id;
        }
        if ($order_num) {
            $service['order_num'] = $order_num;
        }
        if (!$data) {
            $insert = $where;
            if ($service) {
                $insert['service'][] = $service;
                $insert['service'] = Dever::array_encode($insert['service']);
            }
            $insert['status'] = $status;
            if ($status == 2) {
                $insert['qu_date'] = time();
            }
        
            Dever::db('agent/dh_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();
            }
            if ($service) {
                $data['service'] = Dever::array_decode($data['service']);
                $update['service'] = $data['service'];
                $update['service'][] = $service;

                $insert['service'] = Dever::array_encode($update['service']);
            }
            Dever::db('agent/dh_order_ps')->update($update);
        }
    }

    # 减少库存，增加销量
    public function updateSell($order, $type = 1)
    {
        if ($order['type'] == 1) {
            $where['order_id'] = $order['id'];
            $where['status'] = 1;
            $data = Dever::db('agent/dh_order_goods')->select($where);
            if ($data) {

                foreach ($data as $k => $v) {

                    $up['where_mid'] = $order['mid'];
                    $up['where_type'] = $order['type'];
                    $up['where_type_id'] = $v['goods_id'];
                    $up['where_sku_id'] = $v['sku_id'];

                    $info = Dever::db('agent/member_goods')->findData($up);

                    $up['set_sell_num'] = $v['num'];
                    Dever::db('agent/member_goods')->incSell($up);
                    Dever::load('agent/lib/member_set')->log($up['where_mid'], '', $up['where_type'], $up['where_type_id'], $up['where_sku_id'], $up['set_sell_num'], $info['num'], $info['num']-$up['set_sell_num'], '购买商品');
                }
            }
        } elseif ($order['type'] == 3) {
            $up['where_mid'] = $order['mid'];
            $up['where_type'] = $order['type'];
            $up['where_type_id'] = $order['type_id'];
            $up['where_sku_id'] = -1;
            $info = Dever::db('agent/member_goods')->findData($up);
            $up['set_sell_num'] = $order['num'];
            Dever::db('agent/member_goods')->incSell($up);

            Dever::load('agent/lib/member_set')->log($up['where_mid'], '', $up['where_type'], $up['where_type_id'], $up['where_sku_id'], $up['set_sell_num'], $info['num'], $info['num']-$up['set_sell_num'], '兑换名额');
        }
    }

    # 完成
    public function finish($info, $shop_id)
    {
        if ($info && $info['shop_id'] == $shop_id && $info['status'] <= 4) {

            $status = 5;
            $state = Dever::db('agent/dh_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => time()));

            $this->updatePs($info, 3);

            $w['parent_type'] = 3;
            $w['parent_order_id'] = $info['id'];
            $w['status'] = 4;
            $buy_order = Dever::db('shop/buy_order')->select($w);
            if ($buy_order) {
                foreach ($buy_order as $k => $v) {
                    Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
                }
            }
            return 'ok';
        } else {
            Dever::alert('您没有权限核销');
        }
    }

    # 平台订单已收货
    public function yes_api()
    {
        $shop_id = Dever::input('shop_id');
        $order_id = Dever::input('order_id');

        $info = Dever::db('agent/dh_order')->find(array('id' => $order_id));

        if ($info && $info['type'] == 10) {
            $this->finish($info, $shop_id);
        } else {
            Dever::alert('您没有权限核销');
        }

        return 'reload';
    }

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

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

        $config = Dever::db('agent/dh_order')->config;

        $info = Dever::db('agent/dh_order')->one($id);

        if ($info['type'] > 1) {
            return;
        }

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

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

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

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

        $user = Dever::db('agent/member')->find($info['mid']);

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

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

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

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

        $time = array();
        $time[] = array('下单时间', $cdate);

        if ($opertime) {
            if ($info['status'] == 2) {
                $time[] = array('通知时间', $opertime);
            }
            if ($info['status'] == 7) {
                $time[] = array('取消时间', $opertime);
            }
        }

        if ($fdate) {
            $time[] = array('完成时间', $fdate);
        }

        $time[] = array('审核备注', $info['audit_desc']);

        $result = array();

        $result[$info['order_num']] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('订单状态', $status),
                    array('代理商姓名', $user['name']),
                    array('联系电话', $info['mobile']),
                ),

                $time,
            )
        );

        if ($info['address_id'] > 0) {
            $address = Dever::load('agent/address')->getOne($info['mid'], $info['address_id']);

            if ($address) {

                $result['收货地址'] = array
                (
                    'type' => 'info',
                    'content' => array
                    (
                        array
                        (
                            array('联系人', $address['contact']),
                            array('联系电话', $address['mobile']),
                        ),

                        array
                        (
                            array('收货地址', $address['address_info']),
                            array('门牌号', $address['house_number']),
                        ),
                    ),
                );
            }
        }

        $info['ps_info'] = Dever::db('agent/dh_order_ps')->find(array('order_id' => $info['id']));
        if ($info['ps_info']) {
            $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
            $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
            if ($info['ps_info']['qs_date']) {
                $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
            } else {
                $info['ps_info']['qs_date'] = '无';
            }

            $status = Dever::db('agent/dh_order_ps')->config['status'];
            $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
            
            $service = array();
            if ($info['ps_info']['service']) {
                foreach ($info['ps_info']['service'] as $k => $v) {
                	if (isset($v['order_num']) && $v['order_num']) {
                		$info['ps_info']['service'][$k]['service_name'] = '商家自送';
	                    if ($v['service_id'] > 0) {
	                        $service_info = Dever::db('shop/service')->find($v['service_id']);
	                        $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
	                    }

	                    $service[] = array
	                    (
	                        array('名称', $info['ps_info']['service'][$k]['service_name']),
	                        array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
	                    );
                	}
                }
            }

            $result['物流信息'] = array
            (
                'type' => 'info',
                'content' => array
                (
                    array
                    (
                        array('发货时间', $info['ps_info']['cdate']),
                        array('收货时间', $info['ps_info']['qs_date']),
                        array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
                    ),
                ),
            );
            $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
        }


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

        if ($info['refund_cash'] > 0 && $tk) {
            $process = $tk_process[$tk['process']];
            $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
            $result['退款信息'] = array
            (
                'type' => 'info',
                'content' => array
                (
                    array
                    (
                        array('退款金额', $info['refund_cash']),
                        array('数量', $info['num']),
                        array('退款进度', $process),
                    ),

                    array
                    (
                        array('申请时间', $tk['cdate']),
                        array('退款原因', $tk['desc']),
                    ),
                ),
            );
        }

        $body = $refund_body = array();
        $body_total = array();
        $body_total['price'] = 0;
        $body_total['num'] = 0;

        $refund_body_total = array();
        $refund_body_total['price'] = 0;
        $refund_body_total['num'] = 0;

        $goods = Dever::db('agent/dh_order_goods')->select(array('order_id' => $info['id']));
        $goods_status = Dever::db('agent/dh_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('agent/dh_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 = '无';
            }

            if ($info['status'] > 1 && $info['status'] < 7) {
                $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');

                $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
            }

            $d = array
            (
                'pic' => $goods_info['cover'],
                'name' => $goods_info['name'],
                'sku' => $sku,
                'price' => $v['price'],
                'num' => $v['num'],
                'status' => $status,
            );

            if ($tk && $v['status'] == 3) {
                $d['status'] = $tk_status[$tk['status']];
                $refund_body[] = $d;
                $price = $v['price']*$v['num'];
                $refund_body_total['price'] += $price;
                $refund_body_total['num'] += $v['num'];
            } else {
                $body[] = $d;
                $price = $v['price']*$v['num'];
                $body_total['price'] += $price;
                $body_total['num'] += $v['num'];
            }
        }

        if ($body) {
            if ($refund_body_total['price']) {
                $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
            }
            if ($refund_body_total['num']) {
                $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
            }

            $result['商品清单'] = array
            (
                'type' => 'list',
                'content' => $body,
                'total' => $body_total,
            );
        }

        if ($refund_body) {
            $result['退款商品'] = array
            (
                'type' => 'list',
                'content' => $refund_body,
                'total' => $refund_body_total,
            );
        }

        # 平台门店采购单
        $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 3, 'parent_order_id' => $info['id']));
        $source_order_id = array();
        if ($buy_order) {
            $source_order_id = array_keys($buy_order);
        }

        # 平台门店结算单
        if ($source_order_id) {
            $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));

            $col = 'cash';
        }

        if (isset($cash_order) && $cash_order) {
            $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');

            $body = array();
            $config = Dever::db('cash/order')->config;
            foreach ($cash_order as $k => $v) {
                $cdate = date('Y-m-d H:i', $v['cdate']);
                $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
                $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
                $cash = $v[$col];

                $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
                $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';

                $m = '';
                if ($v['audit'] == 1 && $v['audit_type'] == 2) {
                    $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
                    $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
                }

                $d = array
                (
                    $k+1,
                    $v['order_num'],
                    $v['num'],
                    $cash,
                    $config['config_jstype'][$v['jstype']],
                    $config['config_status'][$v['status']],
                    $config['config_audit'][$v['audit']],
                    $operdate,
                    $v['desc'],
                    $m,
                );

                $body[] = $d;
            }

            $result['对账清单'] = array
            (
                'type' => 'table',
                'content' => array
                (
                    'head' => $head,
                    'body' => $body,
                )
            );
                
        }
        

        $head = array
        (
            'name' => '基本信息',
            'btn' => array(),
        );
        $html = Dever::show($head, $result);

        return $html;
    }

    # 第三方确认
    public function tixian()
    {
        $param = array();

        ksort($param);
        $string = array();
        foreach ($param as $k => $v) {
            if (strstr($v, '&') || strstr($v, '@')) {
                $v = urlencode($v);
            }
            $string[] = $k . '=' . $v;
        }
    }
    
    # 发货
    public function notice($info)
    {
        $config = Dever::db('agent/dh_order')->config;
        $state = Dever::db('agent/dh_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
    }

    # 采购订单审核 拆单
    public function audit_commit()
    {
        $order_id = Dever::input('order_id');
        $order = Dever::db('agent/dh_order')->find($order_id);

        if (!$order || ($order && $order['status'] != 2)) {
            Dever::alert('当前订单状态无法拆单');
        }

        $order['type'] = 3;

        $goods = Dever::db('agent/dh_order_goods')->select(array('order_id' => $order_id));

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

        $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);

        $send = array();

        if (!$goods && $data) {
            # 开始生成订单
            $i = $order['order_index'];
            foreach ($data as $k => $v) {
                foreach ($v as $k1 => $v1) {
                    $v1['order_num'] .= '_' . $i;

                    $state = Dever::load('mshop/lib/buy')->createOrder($v1);
                    $send[$state] = $v1;
                }
                $i++;
            }

            Dever::db('agent/dh_order')->update(array('where_id' => $order['id'], 'status' => 3));
        } else {
            Dever::db('agent/dh_order')->update(array('where_id' => $order['id'], 'audit' => 1));
            if ($goods) {
                $string = array();
                foreach ($goods as $k => $v) {
                    $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
                    
                    if ($info['sku_name']) {
                        $info['name'] .= '（'.$info['sku_name'].'）';
                    }
                    $string[] = $info['name'];
                }
                $string = implode('、', $string);
                Dever::alert('商品：'.$string.'库存不足或厂家未生产，请联系供货商备货，该订单进入重新审核状态');
            } else {
                Dever::alert('供货商库存不足，请联系供货商备货，该订单进入重新审核状态');
            }
        }

        if ($send) {
            foreach ($send as $k => $v) {
                Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);

                $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
                Dever::load('cash/lib/order')->up($order_info, 1, 1);
            }
        }

        return 'ok';
    }
}