<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class VillageManageController extends Controller
{
    /**
     * 村管理员 申请
     */
    public function newVillager()
    {
        $village_id = $_GET['village_id'];
        $openid = $_GET['openid'];
        $name = I('name');
        $mobile = I('mobile');
        $address = I('address');
        $idcard = I('idcard');
        $idcard_face = str_replace("\"", "", $_GET['idcard_face']);
        $idcard_back = str_replace("\"", "", $_GET['idcard_back']);
        $form_id = $_GET['form_id'];
        // 手机号码正则
        $pattern = "/^1[3|4|5|7|8][0-9]{9}$/";
        $matchMobile = preg_match($pattern, $mobile);  //匹配正确返回 1,错误返回 0
        // 身份证号码正则
        $isIDCard1 = "/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/"; 
        $isIDCard2 = "/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/";
        $match_1 = preg_match($isIDCard1, $idcard);  //匹配正确返回 1,错误返回 0
        $match_2 = preg_match($isIDCard2, $idcard);  //匹配正确返回 1,错误返回 0
        // 验证信息
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID有误");
        } elseif (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid有误");
        } elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "姓名有误");
        } elseif (!$mobile || $mobile == "undefined" || $mobile == "null") {
            $data = array("status" => -1, "msg" => "手机号码有误");
        } elseif (!$address || $address == "undefined" || $address == "null") {
            $data = array("status" => -1, "msg" => "地址有误");
        } elseif (!$idcard || $idcard == "undefined" || $idcard == "null") {
            $data = array("status" => -1, "msg" => "身份证号码有误");
        } elseif ($match_1 == 0 && $match_2 == 0) {
            $data = array("status" => -1, "msg" => "请输入正确的身份证号码");
        } elseif (!$idcard_face || $idcard_face == "undefined" || $idcard_face == "null") {
            $data = array("status" => -1, "msg" => "身份证正面照有误");
        } elseif (!$idcard_back || $idcard_back == "undefined" || $idcard_back == "null") {
            $data = array("status" => -1, "msg" => "身份证反面照有误");
        } elseif (!$form_id || $form_id == "undefined" || $form_id == "null") {
            $data = array("status" => -1, "msg" => "form_id有误");
        } elseif ($matchMobile == 0) {
            $data = array("status" => -1, "msg" => "请填写正确手机号码");
        } else {
            // 查看该村是否已有村管理员
            $existNum = M("users")->where("`village_id`={$village_id} AND `identity`=2")->count();
            if ($existNum > 0) {
                $data = array("status" => -1, "msg" => "已有村管理员！");
            } else {
                // 查看该用户是否有未审核申请
                $notPassed = M("usersVillage")->where("`openid`='{$openid}' AND `status`=1")->count();
                if ($notPassed > 0) {
                    $data = array("status" => -1, "msg" => "您有未审核申请");
                } else {
                    $addData = array(
                        "village_id" => $village_id,
                        "openid" => $openid,
                        "name" => $name,
                        "mobile" => $mobile,
                        "address" => $address,
                        "idcard" => $idcard,
                        "idcard_face" => $idcard_face,
                        "idcard_back" => $idcard_back,
                        "form_id" => $form_id,
                        "create_time" => $_SERVER['REQUEST_TIME']
                    );
                    $insert_id = M("usersVillage")->add($addData);
                    if ($insert_id) {
                        $data = array("status" => 1, "msg" => "申请成功！请等待审核");
                    } else {
                        $data = array("status" => -1, "msg" => "系统繁忙");
                    }
                }
            }
        }
        apiReturn($data);
    }

    /**
     * 组管理员任命
     */
    public function newGrouper()
    {
        $openid = $_GET['openid'];
        $name = I("name");
        $mobile = I("mobile");
        $address = I("address");
        // 手机号码正则
        $pattern = "/^1[3|4|5|7|8][0-9]{9}$/";
        $matchMobile = preg_match($pattern, $mobile);  //匹配正确返回 1,错误返回 0
        // 验证信息
        if (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid有误");
        } elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "姓名有误");
        } elseif (!$mobile || $mobile == "undefined" || $mobile == "null") {
            $data = array("status" => -1, "msg" => "电话有误");
        } elseif (!$address || $address == "undefined" || $address == "null") {
            $data = array("status" => -1, "msg" => "地址有误");
        } elseif ($matchMobile == 0) {
            $data = array("status" => -1, "msg" => "请填写正确手机号码");
        } else {
            $saveData = array(
                "name" => $name,
                "mobile" => $mobile,
                "address" => $address,
                "identity" => 3,
                "modify_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("users")->where("`openid`='{$openid}'")->save($saveData);
            if ($result) {
                $data = array("status" => 1, "msg" => "操作成功");
            } else {
                $data = array("status" => -1, "msg" => "系统繁忙");
            }
        }
        apiReturn($data);
    }

    /**
     * 厨师任命
     */
    public function newCook()
    {
        $openid = $_GET['openid'];
        $name = I("name");
        $mobile = I("mobile");
        $address = I("address");
        // 手机号码正则
        $pattern = "/^1[3|4|5|7|8][0-9]{9}$/";
        $matchMobile = preg_match($pattern, $mobile);  //匹配正确返回 1,错误返回 0
        // 验证信息
        if (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid有误");
        } elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "姓名有误");
        } elseif (!$mobile || $mobile == "undefined" || $mobile == "null") {
            $data = array("status" => -1, "msg" => "电话有误");
        } elseif (!$address || $address == "undefined" || $address == "null") {
            $data = array("status" => -1, "msg" => "地址有误");
        } elseif ($matchMobile == 0) {
            $data = array("status" => -1, "msg" => "请填写正确手机号码");
        } else {
            $saveData = array(
                "name" => $name,
                "mobile" => $mobile,
                "address" => $address,
                "identity" => 4,
                "modify_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("users")->where("`openid`='{$openid}'")->save($saveData);
            if ($result) {
                $data = array("status" => 1, "msg" => "操作成功");
            } else {
                $data = array("status" => -1, "msg" => "系统繁忙");
            }
        }
        apiReturn($data);
    }

    /**
     * 组配送员任命
     */
    public function newDispatcher()
    {
        $openid = $_GET['openid'];
        $name = I("name");
        $mobile = I("mobile");
        $address = I("address");
        // 手机号码正则
        $pattern = "/^1[3|4|5|7|8][0-9]{9}$/";
        $matchMobile = preg_match($pattern, $mobile);  //匹配正确返回 1,错误返回 0
        // 验证信息
        if (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid有误");
        } elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "姓名有误");
        } elseif (!$mobile || $mobile == "undefined" || $mobile == "null") {
            $data = array("status" => -1, "msg" => "电话有误");
        } elseif (!$address || $address == "undefined" || $address == "null") {
            $data = array("status" => -1, "msg" => "地址有误");
        } elseif ($matchMobile == 0) {
            $data = array("status" => -1, "msg" => "请填写正确手机号码");
        } else {
            $saveData = array(
                "name" => $name,
                "mobile" => $mobile,
                "address" => $address,
                "identity" => 5,
                "modify_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("users")->where("`openid`='{$openid}'")->save($saveData);
            if ($result) {
                $data = array("status" => 1, "msg" => "操作成功");
            } else {
                $data = array("status" => -1, "msg" => "系统繁忙");
            }
        }
        apiReturn($data);
    }

    /**
     * 注销村内身份
     */
    public function clearIdentity()
    {
        $openid = $_GET['openid'];
        if (!$openid || $openid == "undefined" || $openid == "null") {
            $data = array("status" => -1, "msg" => "openid有误");
        } else {
            $result = M("users")->where("`openid`='{$openid}'")->setField("identity", 1);
            if ($result) {
                $data = array("status" => 1, "msg" => "操作成功");
            } else {
                $data = array("status" => -1, "msg" => "系统繁忙");
            }
        }
        apiReturn($data);
    }

    /**
     * 选择新的组管理员/厨师/组配送员
     */
    public function customers()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            $list = M("users")
                ->where("`village_id`={$village_id} AND `identity`=1")
                ->field("`openid`,`nickname`,`avatarurl`,`gender`")
                ->select();
            if (!$list) {
                $list = array();
            }
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }

    /**
     * 查看组管理员列表
     */
    public function groupers()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            $list = M("users")
                ->where("`village_id`={$village_id} AND `identity`=3")
                ->field("`openid`,`name`,`mobile`,`address`")
                ->select();
            if (!$list) {
                $list = array();
            }
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }

    /**
     * 查看厨师列表
     */
    public function cooks()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            $list = M("users")
                ->where("`village_id`={$village_id} AND `identity`=4")
                ->field("`openid`,`name`,`mobile`,`address`")
                ->select();
            if (!$list) {
                $list = array();
            }
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }

    /**
     * 查看组配送员列表
     */
    public function dispatchers()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            $list = M("users")
                ->where("`village_id`={$village_id} AND `identity`=5")
                ->field("`openid`,`name`,`mobile`,`address`")
                ->select();
            if (!$list) {
                $list = array();
            }
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }

    /**
     * 查询所有食材
     */
    public function dishGoods()
    {
        $list = M("goods")
            ->where("`is_material`=2")
            ->field("`id`,`thumb`,`goods_name` AS `name`,`goods_price` AS `price`")
            ->order("`sort` ASC,`id` DESC")
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['thumb'];
        }
        if (!$list) {
            $list = array();
        }
        apiReturn($list);
    }

    /**
     * 添加新套餐
     */
    public function newDish()
    {
        $village_id = $_GET['village_id'];
        $name = I('name');
        $describe = I('describe');
        $thumb = str_replace("\"", "", $_GET['thumb']);
        $goods = unserialize($_GET['goods']);
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "名称 有误");
        } elseif (!$describe || $describe == "undefined" || $describe == "null") {
            $data = array("status" => -1, "msg" => "描述 有误");
        } elseif (!$thumb || $thumb == "undefined" || $thumb == "null") {
            $data = array("status" => -1, "msg" => "缩略图 有误");
        } elseif (!$goods || $goods == "undefined" || $goods == "null") {
            $data = array("status" => -1, "msg" => "材料商品 有误");
        } else {
            $dishData = array(
                "village_id" => $village_id,
                "name" => $name,
                "describe" => $describe,
                "thumb" => $thumb
            );
            $insert_id = M("dish")->add($dishData);
            if ($insert_id) {
                foreach ($goods as $key => $value) {
                    $dishGood = array(
                        "dish_id" => $insert_id,
                        "good_id" => $value->id,
                        "price" => $value->price,
                        "num" => $value->num
                    );
                    M("dishGood")->add($dishGood);
                }
                $data = array("status" => 1, "msg" => "添加成功");
            } else {
                $data = array("status" => -1, "msg" => "系统繁忙");
            }
        }
        apiReturn($data);
    }

    /**
     * 查询本村套餐
     */
    public function dishList()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            $resource = M("dish")
                ->where("`village_id`={$village_id}")
                ->field("`id`,`name`,`describe`,`thumb`")
                ->order("`sort` ASC")
                ->select();
            $list = array();
            foreach ($resource as $key => $value) {
                // 查询套餐总价
                $res = M("dishGood")
                    ->join("dg LEFT JOIN `am_goods` g ON dg.`good_id`=g.`id`")
                    ->where("dg.`dish_id`={$value['id']}")
                    ->field("g.`goods_price` AS `price`,dg.`num`")
                    ->select();
                $price = 0;
                foreach ($res as $kr => $vr) {
                    $price += $vr['price'] * $vr['num'];
                }
                // 入栈
                array_push(
                    $list,
                    array(
                        "id" => $value['id'],
                        "name" => $value['name'],
                        "describe" => $value['describe'],
                        "thumb" => "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . str_replace("\"", "", $value['thumb']),
                        "price" => $price 
                    )
                );
            }
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }

    /**
     * 查询某个套餐的名称、描述
     */
    public function dishName()
    {
        $id = $_GET['id'];
        if (!$id || $id == "undefined" || $id == "null") {
            $data = array("status" => -1, "msg" => "套餐ID 有误");
        } else {
            $info = M("dish")->where("`id`={$id}")->find();
            $info['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . str_replace("\"", "", $info['thumb']);
            $data = array("status" => 1, "result" => $info);
        }
        apiReturn($data);
    }

    /**
     * 查询某个套餐的材料商品
     */
    public function oneDishGoods()
    {
        $id = $_GET['id'];
        if (!$id || $id == "undefined" || $id == "null" || !is_numeric($id)) {
            $data = array("status" => -1, "msg" => "套餐ID 有误");
        } else {
            // 所有材料信息
            $list = M("dishGood")
                ->join("dg LEFT JOIN `am_goods` g ON dg.`good_id`=g.`id`")
                ->where("dg.`dish_id`={$id}")
                ->field("g.`id`,g.`thumb` AS `img`,g.`goods_name` AS `name`,g.`goods_price` AS `price`,dg.`num`")
                ->select();
            foreach ($list as $key => $value) {
                $list[$key]['img'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$value['img']}";
            }
            unset($key, $value);
            if (!$list) {
                $list = array();
            }
            // 查询菜品
            $caipin = M("dishCaipin")
                ->where("`dish_id`={$id}")
                ->field("`name`,`thumb`")
                ->select();
            foreach ($caipin as $kc => $vc) {
                $caipin[$kc]['thumbView'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$vc['thumb']}";
            }
            unset($kc, $vc);
            $data = array("status" => 1, "result" => $list, "caipin" => $caipin);
        }
        apiReturn($data);
    }

    /**
     * 补货订单 列表
     */
    public function supplyList()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            // 获取数据刷新时间, 没有则定为 21:00
            $limitTime = D("Smi/SetElse")->getLimitTime();
            $list = M("orderSupply")
                ->join("os LEFT JOIN `am_goods` g ON os.`good_id`=g.`id`")
                ->join("LEFT JOIN `am_storage_dpc` dpc ON os.`deliveryman`=dpc.`id`")
                ->where("os.`village_id`={$village_id} AND os.`create_time`<{$limitTime} AND os.`status`>1")
                ->order("os.`status` ASC, os.`id` ASC")
                ->field("os.*,g.`goods_name`,g.`thumb`,g.`des`,g.`goods_price`,dpc.`name` AS `deliveryman`,dpc.`mobile` AS `deliveryman_mobile`")
                ->select();
            if (!$list) {
                $list = array();
            }
            $notGetted = $getted = array();
            foreach ($list as $kl => $vl) {
                $list[$kl]['total_fee'] = strval(strval($vl['goods_price']) * strval($vl['num']));
                $list[$kl]['create_time'] = date("Y-m-d H:i", $vl['create_time']);
                $list[$kl]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$vl['thumb']}";
                switch ($vl['status']) {
                    case '2':
                        array_push($notGetted, $list[$kl]);
                        break;
                    case '3':
                        array_push($getted, $list[$kl]);
                        break;
                }
            }
            unset($kl, $vl);
            $data = array(
                "status" => 1,
                "notGetted" => $notGetted,
                "getted" => $getted
            );
        }
        apiReturn($data);
    }

    /**
     * 补货订单 确认到货
     */
    public function supplyGet()
    {
        $id = $_GET['id'];
        if (!$id || $id == "undefined" || $id == "null") {
            $data = array("status" => -1, "msg" => "ID 有误");
        } else {
            $saveData = array(
                "status" => 3,
                "arrive_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("orderSupply")->where("`id`={$id}")->save($saveData);
            if (!$result) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                $row = M("orderSupply")->where("`id`={$id}")->find();
                $village_id = $row['village_id'];
                $good_id = $row['good_id'];
                $num = $row['num'];
                $order_id = $row['order_id'];
                // 补回村分仓库库存
                $result = M("goodsStorage")->where("`village_id`={$village_id} AND `good_id`={$good_id}")->lock()->setInc("storage", $num);
                if ($result) {
                    // 生成村分仓库入库记录
                    $addData = array(
                        "choice" => 2,
                        "type" => 3,
                        "order_id" => $order_id,
                        "dinner_id" => 0,
                        "good_id" => $good_id,
                        "change_num" => $num,
                        "create_time" => $_SERVER['REQUEST_TIME']
                    );
                    M("storageVillage")->add($addData);
                }
                $data = array("status" => 1, "msg" => "操作成功");
            }
        }
        apiReturn($data);
    }

    /**
     * 补货订单 详情
     */
    public function supplyInfo()
    {
        $id = $_GET['id'];
        if (!$id || $id == "undefined" || $id == "null") {
            $data = array("status" => -1, "msg" => "订单ID有误");
        } else {
            $row = M("orderSupply")->where("`id`={$id}")->find();
            $good = M("goods")->where("`id`={$row['good_id']}")->field("`id` AS `good_id`,`thumb`,`goods_name` AS `name`")->find();
            $good['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $good['thumb'];
            $result = array(
                "id" => $row['id'],
                "sn" => $row['supply_sn'],
                "status" => $row['status'],
                "create_time" => date("Y年m月d日 H:i", $row['create_time']),
                "good" => array(
                    "good_id" => $good['good_id'],
                    "thumb" => $good['thumb'],
                    "name" => $good['name'],
                    "num" => $row['num']
                )
            );
            $date = array(
                "status" => 1,
                "result" => $result
            );
        }
        apiReturn($data);
    }

    /**
     * 分仓库配送订单 列表
     */
    public function branchOrderList()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "undefined" || $village_id == "null") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            // 获取数据刷新时间, 没有则定为 21:00
            $limitTime = D("Smi/SetElse")->getLimitTime();
            $list = M("orderSmi")
                ->join("os LEFT JOIN `am_order` o ON os.`order_id`=o.`id`")
                ->join("LEFT JOIN `am_users` u ON o.`group_openid`=u.`openid`")
                ->where("o.`village_id`={$village_id} AND o.`create_time`<{$limitTime} AND o.`status`>1 AND o.`status`< 5 AND `type`=1")
                ->order("os.`id` ASC")
                ->field("os.`id`,o.`order_sn` AS `sn`,o.`status`,os.`dispatcher`,o.`create_time`,u.`name` AS `group`")
                ->select();
            foreach ($list as $key => $value) {
                $list[$key]['dispatcher'] = M("users")->where("`openid`='{$value['dispatcher']}'")->getField("name");
                $list[$key]['create_time'] = date("Y年m月d日 H:i", $value['create_time']);
            }
            if (!$list) {
                $list = array();
            }
            $data = array(
                "status" => 1,
                "result" => $list
            );
        }
        apiReturn($data);
    }

    /**
     * 分仓库配送订单 详情
     */
    public function branchOrderInfo()
    {
        $id = $_GET['id'];
        if (!$id || $id == "undefined" || $id == "null") {
            $data = array("status" => -1, "msg" => "分订单ID有误");
        } else {
            $resource = M("order")
                ->join("o LEFT JOIN `am_order_smi` os ON o.`id`=os.`order_id`")
                ->join("LEFT JOIN `am_users` u ON os.`dispatcher`=u.`openid`")
                ->join("LEFT JOIN `am_order_good` og ON os.`id`=og.`smi_id`")
                ->join("LEFT JOIN `am_goods` g ON og.`good_id`=g.`id`")
                ->where("os.`id`={$id}")
                ->field("o.`order_sn` AS `sn`,o.`create_time`,os.`id`,os.`smi_sn`,os.`status`,u.`name` AS `dispatcher`,og.`smi_id`,og.`good_id`,og.`num`,og.`expect_time`,g.`thumb`,g.`goods_name`,g.`goods_price`")
                ->select();
            if ($resource[0]['sn']) {
                // 订单编号
                $sn = $resource[0]['sn'];
            }
            if ($resource[0]['create_time']) {
                $create_time = $resource[0]['create_time'];
            }
            $goodList = array();
            $branchList = array();
            foreach ($resource as $kr => $vr) {
                $resource[$kr]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $vr['thumb'];
                // 组装 商品数组
                array_push($goodList, array(
                    "id" => $resource[$kr]['good_id'],
                    "thumb" => $resource[$kr]['thumb'],
                    "goods_name" => $resource[$kr]['goods_name'],
                    "goods_price" => $resource[$kr]['goods_price'],
                    "num" => $resource[$kr]['num'],
                    "smi_id" => $resource[$kr]['smi_id'],
                    "expect_time" => date("n月j日", $resource[$kr]['expect_time'])
                ));
                // 组装 分订单数组
                $arr = array(
                    "id" => $resource[$kr]['id'],
                    "sn" => $resource[$kr]['smi_sn'],    // 分订单编号
                    "status" => $resource[$kr]['status'],
                    "dispatcher" => $resource[$kr]['dispatcher']
                );
                if (!in_array($arr, $branchList)) {
                    array_push($branchList, $arr);
                }
            }
            unset($kr, $vr);
            // 分订单数组 吸收 商品数组
            foreach ($branchList as $kb => $vb) {
                $branchList[$kb]['goods'] = array();
                $id = $vb['id'];
                foreach ($goodList as $kg => $vg) {
                    $smi_id = $vg['smi_id'];
                    if ($smi_id == $id) {
                        array_push($branchList[$kb]['goods'], array(
                            "id" => $vg['good_id'],
                            "thumb" => $vg['thumb'],
                            "goods_name" => $vg['goods_name'],
                            "goods_price" => $vg['goods_price'],
                            "num" => $vg['num'],
                            "expect_time" => $vg['expect_time']
                        ));
                    }
                }
                unset($kg, $vg);
            }
            unset($kb, $vb);
            // 订单信息
            $order = M("order")->where("`id`={$id}")->find();
            // 组管理员
            $group = M("users")->where("`openid`='{$order['group_openid']}'")->getField("name");
            $data = array(
                "status" => 1,
                "result" => array(
                    "sn" => $sn,
                    "name" => $order['name'],
                    "tel" => $order['tel'],
                    "address" => $order['address'],
                    "group" => $group,
                    "create_time" => date("Y年m月d日 H:i", $create_time),
                    "branch" => $branchList
                )
            );
        }
        apiReturn($data);
    }

    /**
     * 分仓库配送订单 分派
     */
    public function branchOrderAssign()
    {
        $id = $_GET['id'];
        $dispatcher_openid = $_GET['dispatcher_openid'];
        if (!$id || $id == "undefined" || $id == "null") {
            $data = array("status" => -1, "msg" => "分订单ID有误");
        } elseif (!$dispatcher_openid || $dispatcher_openid == "undefined" || $dispatcher_openid == "null") {
            $data = array("status" => -1, "msg" => "配送员openid有误");
        } else {
            // 分订单状态修改
            $saveData = array(
                "status" => 3,
                "dispatcher" => $dispatcher_openid,
                "send_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("orderSmi")->where("`id`={$id}")->save($saveData);
            if (!$result) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                // 订单状态修改
                $order_id = M("orderSmi")->where("`id`={$id}")->getField("order_id");
                if ($order_id) {
                    M("order")->where("`id`={$order_id}")->setField("status", 3);
                }
                $data = array("status" => 1, "msg" => "分派成功");
            }
        }
        apiReturn($data);
    }
}