<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class VillageSecondController extends Controller
{
    /**
     * 总仓库配送订单 列表
     */
    public function homeOrderList()
    {
        $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();
            $order = "o.`id` DESC";
            $where = "o.`village_id`={$village_id} AND o.`status` > 1 AND o.`status` < 5 AND o.`type`=2 AND o.`create_time` < {$limitTime}";
            // $where = "o.`village_id`={$village_id} AND o.`status` > 1 AND o.`status` < 5 AND o.`type`=2";
            $list = M("order")
                ->join("o LEFT JOIN `am_users` u ON o.`group_openid`=u.`openid`")
                ->where($where)
                ->field("o.`id`,o.`order_sn` AS `sn`,o.`status`,o.`create_time`,u.`name` AS `group`")
                ->order($order)
                ->select();
            if (!$list) {
                $list = array();
            }
            foreach ($list as $ko => $vo) {
                if ($vo['create_time'] > 0) {
                    $list[$ko]['create_time'] = date("Y-m-d H:i", $vo['create_time']);
                }
                $order_id = $vo['id'];
                // 查询分订单
                $result = M("orderSmi")
                    ->join("os LEFT JOIN `am_users` u ON os.`dispatcher`=u.`openid`")
                    ->where("`order_id`={$order_id}")
                    ->field("os.`id`,os.`smi_sn` AS `sn`,u.`name` AS `dispatcher`")
                    ->select();
                if (!$result) {
                    $result = array();
                }
                $list[$ko]['branch'] = $result;
            }
            unset($ko, $vo);
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }

    /**
     * 总仓库配送订单 分派
     */
    public function homeOrderAssign()
    {
        $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 {
            $result = M("orderSmi")->where("`id`={$id}")->setField("dispatcher", $dispatcher_openid);
            if (!$result) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                $data = array("status" => 1, "msg" => "操作成功");
            }
        }
        apiReturn($data);
    }

    /**
     * 总仓库配送订单 详情
     */
    public function homeOrderInfo()
    {
        $id = $_GET['id'];    // am_order 表 的ID
        if (!$id || $id == "undefined" || $id == "null") {
            $data = array("status" => -1, "msg" => "订单ID有误");
        } else {
            $order = M("order")->where("`id`={$id}")->find();
            $order['sn'] = $order['order_sn'];
            $group_openid = $order['group_openid'];
            $grouper = M("users")->where("`openid`='{$group_openid}'")->find();
            $order['group'] = $grouper['name'];
            if ($order['create_time'] > 0) {
                $order['create_time'] = date("Y-m-d H:i", $order['create_time']);
            }
            $branch = M("orderSmi")->where("`order_id`={$id}")->select();
            if (!$branch) {
                $branch = array();
            }
            foreach ($branch as $kb => $vb) {
                $branch[$kb]['sn'] = $vb['smi_sn'];
                $dispatcher = M("users")->where("`openid`='{$vb['dispatcher']}'")->find();
                $branch[$kb]['dispatcher'] = $dispatcher['name'];
                $goods = M("orderGood")
                ->where("`smi_id`={$vb['id']}")
                ->join("og LEFT JOIN `am_goods` g ON og.`good_id`=g.`id`")
                ->field("og.*,g.`thumb`,g.`goods_name`,g.`goods_price`")
                ->select();
                if (!$goods) {
                    $goods = array();
                }
                foreach ($goods as $kg => $vg) {
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$vg['thumb']}";
                    if ($vg['expect_time'] > 0) {
                        $goods[$kg]['expect_time'] = date("n月j日", $vg['expect_time']);
                    }
                }
                unset($kg, $vg);
                $branch[$kb]['goods'] = $goods;
            }
            unset($kb, $vb);
            $order['branch'] = $branch;
            $data = array("status" => 1, "result" => $order);
        }
        apiReturn($data);
    }

    /**
     * 本村宴席订单
     */
    public function villageDinner()
    {
        $village_id = $_GET['village_id'];    // 村ID
        if (!$village_id || $village_id == "undefined" || $village_id == "null" || !is_numeric($village_id)) {
            $data = array("status" => -1, "msg" => "村ID有误");
        } else {
            // 村长宴席佣金比例
            $villageSet = M("regionVillage")->where("`id`={$village_id}")->find();
            $is_self = $villageSet['is_self'];
            if ($is_self == 2) {
                // 用村设置的比例计算佣金
                $village_percent = $villageSet['village_percent'];
            } elseif ($is_self == 1) {
                // 用后台设置的比例计算佣金
                $set = M("setFund")->find();
                $village_percent = $set['village_percent'];
            }
            $list = M("dinner")
                ->where("`village_id`={$village_id}")
                ->field("`id`,`dinner_sn`,`total_fee`,`status`,`create_time`,`dinner_time`")
                ->order("`id` DESC")
                ->select();
            if (!$list) {
                $list = array();
            }
            // 待确认  待付款  进行中  已结束
            $notSured = $notPaid = $going = $finished = array();
            foreach ($list as $key => $value) {
                $list[$key]['yongjin'] = number_format($value['total_fee'] * $village_percent / 100, 2, ".", "");
                if ($value['create_time']) {
                    $list[$key]['create_time'] = date("Y-m-d", $value['create_time']);
                }
                if ($value['dinner_time']) {
                    $list[$key]['dinner_time'] = date("Y-m-d", $value['dinner_time']);
                }
                $dinner_id = $value['id'];
                // 已选套餐
                if ($dinner_id) {
                    $row = M("dinnerDish")
                        ->join("dd LEFT JOIN `am_dish` d ON dd.`dish_id`=d.`id`")
                        ->where("`dinner_id`={$dinner_id}")
                        ->field("d.`name`")
                        ->find();
                    $dish_name = $row['name'];
                    if (!$dish_name) {
                        $dish_name = "";
                    }
                    $list[$key]['dish_name'] = $dish_name;
                }
                if ($value['status'] == 1) {
                    // 待确认
                    array_push($notSured, $list[$key]);
                } elseif ($value['status'] == 2) {
                    // 待付款
                    array_push($notPaid, $list[$key]);
                } elseif ($value['status'] > 2 && $value['status'] < 6) {
                    // 进行中
                    array_push($going, $list[$key]);
                } elseif ($value['status'] == 6) {
                    // 已结束
                    array_push($finished, $list[$key]);
                }
            }
            unset($key, $value);
            $data = array(
                "status" => 1,
                "all" => $list,
                "notSured" => $notSured,
                "notPaid" => $notPaid,
                "going" => $going,
                "finished" => $finished
            );
        }
        apiReturn($data);
    }

    /**
     * 村宴席订单详情
     */
    public function villageDinnerInfo()
    {
        $dinner_id = $_GET['dinner_id'];    // 宴席订单ID
        if (!$dinner_id || $dinner_id == "undefined" || $dinner_id == "null" || !is_numeric($dinner_id)) {
            $data = array("status" => -1, "msg" => "宴席订单ID有误");
        } else {
            // 订单信息
            $info = M("dinner")
                ->where("`id`={$dinner_id}")
                ->field("`id`,`total_fee`,`pre_fee`,`end_fee`,`table_num`,`name`,`tel`,`address`,`dinner_time`,`status`,`dinner_sn`,`create_time`,`village_id`")
                ->find();
            if ($info['dinner_time'] > 0) {
                $info['dinner_time'] = date("n月j日", $info['dinner_time']);
            }
            if ($info['create_time'] > 0) {
                $info['create_time'] = date("Y-m-d H:i", $info['create_time']);
            }
            // 村管理员电话
            $village_id = $info['village_id'];
            $villager = M("users")->where("`village_id` AND `identity`=2")->find();
            $village_tel = $villager['mobile'];
            $info['village_tel'] = $village_tel;
            // 已选套餐
            $row = M("dinnerDish")
                ->join("dd LEFT JOIN `am_dish` d ON dd.`dish_id`=d.`id`")
                ->where("dd.`dinner_id`={$dinner_id}")
                ->field("d.`name`,dd.`id`")
                ->find();
            $dish_name = $row['name'];    // 套餐名称
            $dinner_dish_id = $row['id'];    // 订单套餐副表ID
            if (!$dish_name) {
                $dish_name = "";
            }
            $info['dish_name'] = $dish_name;
            // 套餐食材
            $goods = M("dinnerDishGood")
                ->join("ddg LEFT JOIN `am_goods` g ON ddg.`good_id`=g.`id`")
                ->where("ddg.`dinner_dish_id`={$dinner_dish_id}")
                ->field("g.`goods_name`,g.`id`,ddg.`goods_price`,ddg.`num`")
                ->select();
            if (!$goods) {
                $goods = array();
            }
            $goods_total = 0;    // 套餐食材总价
            foreach ($goods as $key => $value) {
                $goods_price = $value['goods_price'];
                $num = $value['num'];
                $goods_total = strval($goods_total) + strval(strval($goods_price) * $num);
            }
            unset($key, $value);
            $info['goods_count'] = count($goods);    // 套餐食材类数
            $info['goods_total'] = $goods_total;    // 套餐食材总价
            // 退余货食材
            $back = M("dinnerGoodReturn")
                ->join("ddr LEFT JOIN `am_goods` g ON ddr.`good_id`=g.`id`")
                ->where("ddr.`dinner_id`={$dinner_id}")
                ->field("g.`goods_name`,g.`id`,ddr.`goods_price`,ddr.`num`")
                ->select();
            if (!$back) {
                $back = array();
            }
            $back_goods_total = 0;    // 退余货食材总价
            // 循环原食材数组
            $back_goods = array();
            foreach ($back as $kb => $vb) {
                $goods_price = $vb['goods_price'];
                $num = $vb['num'];
                $back_goods_total = strval($back_goods_total) + strval(strval($goods_price) * $num);
                // 循环输出的数组，有商品ID相同的元素则加数组数量，没有则插入到输出的数组中
                $flag = false;
                foreach ($back_goods as $kbg => $vbg) {
                    if ($vbg['id'] == $vb['id']) {
                        // 输出数组中存在同一商品
                        $back_goods[$kbg]['num'] += $vb['num'];
                        $flag = true;
                    }
                }
                unset($kbg, $kpg);
                if (!$flag) {
                    array_push($back_goods, $vb);
                }
            }
            unset($kb, $vb);
            $info['back_goods_count'] = count($back_goods);    // 退余货食材类数
            $info['back_goods_total'] = $back_goods_total;    // 套餐食材总价
            // 用户增购食材
            $plus = M("dinnerDishGood")
                ->join("ddg LEFT JOIN `am_goods` g ON ddg.`good_id`=g.`id`")
                ->where("ddg.`dinner_dish_id`=0 AND `dinner_id`={$dinner_id}")
                ->field("g.`goods_name`,g.`id`,ddg.`goods_price`,ddg.`num`")
                ->select();
            $plus_goods_total = 0;    // 增购食材总价
            // 循环原食材数组
            $plus_goods = array();
            foreach ($plus as $kp => $vp) {
                $goods_price = $vp['goods_price'];
                $num = $vp['num'];
                $plus_goods_total = strval($plus_goods_total) + strval(strval($goods_price) * $num);
                // 循环输出的数组，有商品ID相同的元素则加数组数量，没有则插入到输出的数组中
                $flag = false;
                foreach ($plus_goods as $kpg => $vpg) {
                    if ($vpg['id'] == $vp['id']) {
                        // 输出数组中存在同一商品
                        $plus_goods[$kpg]['num'] += $vp['num'];
                        $flag = true;
                    }
                }
                unset($kpg, $kpg);
                if (!$flag) {
                    array_push($plus_goods, $vp);
                }
            }
            unset($kp, $vp);
            $info['plus_goods_count'] = count($plus_goods);    // 增购食材类数
            $info['plus_goods_total'] = $plus_goods_total;    // 套餐食材总价
            // 返回数据
            $data = array(
                "status" => 1,
                "info" => $info,
                "goods" => $goods,
                "back_goods" => $back_goods,
                "plus_goods" => $plus_goods
            );
        }
        apiReturn($data);
    }

    /**
     * 村宴席订单详情 (用于退余货)
     */
    public function villageDinnerInfoII()
    {
        $dinner_id = $_GET['dinner_id'];    // 宴席订单ID
        if (!$dinner_id || $dinner_id == "undefined" || $dinner_id == "null" || !is_numeric($dinner_id)) {
            $data = array("status" => -1, "msg" => "宴席订单ID有误");
        } else {
            // 订单信息
            $info = M("dinner")
                ->where("`id`={$dinner_id}")
                ->field("`id`,`total_fee`,`pre_fee`,`end_fee`,`table_num`,`name`,`tel`,`address`,`dinner_time`,`status`,`dinner_sn`,`create_time`,`village_id`")
                ->find();
            if ($info['dinner_time'] > 0) {
                $info['dinner_time'] = date("n月j日", $info['dinner_time']);
            }
            if ($info['create_time'] > 0) {
                $info['create_time'] = date("Y-m-d H:i", $info['create_time']);
            }
            // 村管理员电话
            $village_id = $info['village_id'];
            $villager = M("users")->where("`village_id` AND `identity`=2")->find();
            $village_tel = $villager['mobile'];
            $info['village_tel'] = $village_tel;
            // 已选套餐
            $row = M("dinnerDish")
                ->join("dd LEFT JOIN `am_dish` d ON dd.`dish_id`=d.`id`")
                ->where("dd.`dinner_id`={$dinner_id}")
                ->field("d.`name`,dd.`id`")
                ->find();
            $dish_name = $row['name'];    // 套餐名称
            $dinner_dish_id = $row['id'];    // 订单套餐副表ID
            if (!$dish_name) {
                $dish_name = "";
            }
            $info['dish_name'] = $dish_name;
            // 套餐食材
            $goods = M("dinnerDishGood")
                ->join("ddg LEFT JOIN `am_goods` g ON ddg.`good_id`=g.`id`")
                ->where("ddg.`dinner_dish_id`={$dinner_dish_id}")
                ->field("g.`goods_name`,g.`id`,ddg.`goods_price`,ddg.`num`")
                ->select();
            if (!$goods) {
                $goods = array();
            }
            $goods_total = 0;    // 套餐食材总价
            foreach ($goods as $key => $value) {
                $goods_price = $value['goods_price'];
                $num = $value['num'];
                $goods_total = strval($goods_total) + strval(strval($goods_price) * $num);
                $good_id = $value['id'];
                // 加上增购的数量，减去退余货的数量
                // 增购数量
                $this_add = M("dinnerDishGood")
                    ->where("`good_id`={$good_id} AND `dinner_id`={$dinner_id} AND `dinner_dish_id`=0")
                    ->field("`good_id`,`num`")
                    ->select();
                foreach ($this_add as $kta => $vta) {
                    if ($vta['good_id'] == $good_id) {
                        $num += $vta['num'];
                    }
                }
                unset($kta, $vta);
                // 退余货数量
                $this_back = M("dinnerGoodReturn")
                    ->where("`good_id`={$good_id} AND `dinner_id`={$dinner_id}")
                    ->field("`good_id`,`num`")
                    ->select();
                foreach ($this_back as $ktb => $vtb) {
                    if ($vtb['good_id'] == $good_id) {
                        $num -= $vtb['num'];
                    }
                }
                unset($ktb, $vtb);
                $goods[$key]['num'] = $num;
            }
            unset($key, $value);
            $info['goods_count'] = count($goods);    // 套餐食材类数
            $info['goods_total'] = $goods_total;    // 套餐食材总价
            // 返回数据
            $data = array(
                "status" => 1,
                "info" => $info,
                "goods" => $goods,
                "back_goods" => $back_goods,
                "plus_goods" => $plus_goods
            );
        }
        apiReturn($data);
    }

    /**
     * 拒绝预订 (取消订单)
     */
    public function villageDinnerReject()
    {
        $dinner_id = $_GET['dinner_id'];    // 宴席订单ID
        if (!$dinner_id || $dinner_id == "undefined" || $dinner_id == "null" || !is_numeric($dinner_id)) {
            $data = array("status" => -1, "msg" => "宴席订单ID有误");
        } else {
            $saveData = array(
                "status" => -1,
                "cancel_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("dinner")->where("`id`={$dinner_id}")->save($saveData);
            if (!$result) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                $row = M("dinner")->where("`id`={$dinner_id}")->find();
                $dinner_sn = $row['dinner_sn'];
                $form_id = $row['form_id'];
                $openid = $row['openid'];
                // 发送模板消息
                vendor("WxPayPubHelper.WxPlugin");
                $WxPlugin = new \WxPlugin();
                $params['form_id'] = $form_id;
                $params['touser'] = $openid;
                $params['template_id'] = "Sse96UtkKyu6q326k-g5ziNugTOMDy24w4y3bgPV5P0";
                $params['data'] = array(
                    "keyword1" => array("value" => "宴席订单取消", "color" => "#000000"),
                    "keyword2" => array("value" => $dinner_sn, "color" => "#000000"),
                    "keyword3" => array("value" => "您的宴席订单已取消", "color" => "#000000")
                );
                $result = $WxPlugin->XcxSend($params);
                if (!$result) {
                    file_put_contents("./Logs/templateMsg/orderArrive.log", JSON($result), FILE_APPEND);
                }
                $data = array("status" => 1, "msg" => "操作成功");
            }
        }
        apiReturn($data);
    }

    /**
     * 确认预订
     */
    public function villageDinnerSure()
    {
        $dinner_id = $_GET['dinner_id'];    // 宴席订单ID
        if (!$dinner_id || $dinner_id == "undefined" || $dinner_id == "null" || !is_numeric($dinner_id)) {
            $data = array("status" => -1, "msg" => "宴席订单ID有误");
        } else {
            $saveData = array(
                "status" => 2,
                "sure_time" => $_SERVER['REQUEST_TIME']
            );
            $result = M("dinner")->where("`id`={$dinner_id}")->save($saveData);
            if (!$result) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                $row = M("dinner")->where("`id`={$dinner_id}")->find();
                $dinner_sn = $row['dinner_sn'];
                $form_id = $row['form_id'];
                $openid = $row['openid'];
                // 发送模板消息
                vendor("WxPayPubHelper.WxPlugin");
                $WxPlugin = new \WxPlugin();
                $params['form_id'] = $form_id;
                $params['touser'] = $openid;
                $params['template_id'] = "Sse96UtkKyu6q326k-g5ziNugTOMDy24w4y3bgPV5P0";
                $params['data'] = array(
                    "keyword1" => array("value" => "宴席订单确认", "color" => "#000000"),
                    "keyword2" => array("value" => $dinner_sn, "color" => "#000000"),
                    "keyword3" => array("value" => "您的宴席订单已确认，请尽快支付定金", "color" => "#000000")
                );
                $result = $WxPlugin->XcxSend($params);
                if (!$result) {
                    file_put_contents("./Logs/templateMsg/orderArrive.log", JSON($result), FILE_APPEND);
                }
                $data = array("status" => 1, "msg" => "操作成功");
            }
        }
        apiReturn($data);
    }

    /**
     * 确认收取尾款
     */
    public function dinnerMoneyGet()
    {
        $dinner_id = $_GET['dinner_id'];    // 宴席订单ID
        $cook_openid = $_GET['cook_openid'];    // 厨师openid
        $group_openid = $_GET['group_openid'];    // 组管理员openid
        if (!$dinner_id || $dinner_id == "undefined" || $dinner_id == "null" || !is_numeric($dinner_id)) {
            $data = array("status" => -1, "msg" => "宴席订单ID有误");
        } elseif (!$cook_openid || $cook_openid == "undefined" || $cook_openid == "null") {
            $data = array("status" => -1, "msg" => "厨师openid有误");
        } elseif (!$group_openid || $group_openid == "undefined" || $group_openid == "null") {
            $data = array("status" => -1, "msg" => "组管理员openid有误");
        } else {
            $saveData = array(
                "status" => 4,
                "cook_openid" => $cook_openid,
                "group_openid" => $group_openid
            );
            $result = M("dinner")->where("`id`={$dinner_id}")->save($saveData);
            if (!$result) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                $data = array("status" => 1, "msg" => "操作成功");
            }
        }
        apiReturn($data);
    }

    /**
     * 村管理员减少商品(多余商品退货)
     */
    public function overBack()
    {
        $dinner_id = $_GET['dinner_id'];    // 宴席订单ID
        $goods = json_decode($_GET['goods']);
        if (!$dinner_id || $dinner_id == "undefined" || $dinner_id == "null") {
            $data = array("status" => -1, "msg" => "宴席订单ID有误");
        } elseif (!$goods || !is_array($goods) || !$goods[0]) {
            $data = array("status" => -1, "msg" => "商品数组有误");
        } else {
            $priceCut = 0;    // 减价数值初始化
            foreach ($goods as $key => $value) {
                $good_id = $value->id;    // 商品ID
                $num = $value->minus_num;    // 订单中商品的增加数量
                // 查询商品价格
                $goods_price = M("goods")->where("`id`={$good_id}")->getField("goods_price");
                if (!$goods_price) {
                    $goods_price = 0;
                }
                if ($num > 0) {
                    // 生成 多余商品退货单
                    $addData = array(
                        "dinner_id" => $dinner_id,
                        "goods_price" => $goods_price,
                        "good_id" => $good_id,
                        "num" => $num
                    );
                    $insert_id = M("dinnerGoodReturn")->add($addData);
                    if ($insert_id) {
                        // 库存操作
                        // 总仓库库存增加
                        M("goodsStorage")->where("`good_id`={$good_id} AND `village_id`=0")->lock()->setInc("storage", $num);
                        // 生成总仓库入库记录
                        $data = array(
                            "choice" => 2,
                            "type" => 5,
                            "order_id" => 0,
                            "dinner_id" => $dinner_id,
                            "good_id" => $good_id,
                            "change_num" => $num,
                            "create_time" => $_SERVER['REQUEST_TIME']
                        );
                        M("storageTotal")->add($data);
                        // 查询商品价格
                        $price = M("goods")->where("`id`={$good_id}")->getField("goods_price");
                        if (!$price) {
                            $price = 0;
                        } else {
                            $price = strval($price);
                        }
                        // 减价数值累加
                        $priceCut = strval($priceCut) + strval(strval($num) * strval($goods_price));
                    }
                }
            }
            unset($key, $value);
            // 订单减价
            M("dinner")->where("`id`={$dinner_id}")->lock()->setDec("total_fee", $priceCut);
            M("dinner")->where("`id`={$dinner_id}")->lock()->setDec("end_fee", $priceCut);
            // 返回信息
            $data = array("status" => 1, "msg" => "操作成功");
        }
        apiReturn($data);
    }

    /**
     * 新增套餐
     */
    public function addDish()
    {
        $village_id = $_GET['village_id'];
        $name = $_GET['name'];
        $describe = $_GET['describe'];
        $thumb = $_GET['thumb'];
        $goods = json_decode($_GET['goods']);
        $caipin = json_decode($_GET['caipin']);
        // 检测商品数组是否正常
        $flagGoods = true;
        $countGoods = 0;
        foreach ($goods as $kg => $vg) {
            if (!$vg->id) {
                $flagGoods = false;
                break;
            }
            if ($vg->num) {
                $countGoods++;
            }
        }
        unset($kg, $vg);
        // 检测菜品数组是否正常
        $flagCaipin = true;
        foreach ($caipin as $kc => $vc) {
            if (!$vc->name || !$vc->thumb) {
                $flagCaipin = false;
                break;
            }
        }
        unset($kc, $vc);
        if (!$village_id || $village_id == "undefined" || $village_id == "null" || !is_numeric($village_id)) {
            $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 || !is_array($goods) || !$goods[0] || !$flagGoods) {
            $data = array("status" => -1, "msg" => "食材有误");
        } elseif (!$caipin || !is_array($caipin) || !$caipin[0] || !$flagCaipin) {
            $data = array("status" => -1, "msg" => "菜品有误");
        } elseif ($countGoods == 0) {
            $data = array("status" => -1, "msg" => "没有提交食材");
        } else {
            // 生成 dish 记录
            $addData = array(
                "village_id" => $village_id,
                "name" => $name,
                "thumb" => str_replace("\"", "", $thumb),
                "describe" => $describe
            );
            $dish_id = M("dish")->add($addData);
            if (!$dish_id) {
                $data = array("status" => -1, "msg" => "系统繁忙");
            } else {
                // 生成 食材副表
                foreach ($goods as $key => $value) {
                    $good_id = $value->id;
                    $num = $value->num;
                    if ($good_id > 0 && $num > 0) {
                        $addData = array(
                            "dish_id" => $dish_id,
                            "good_id" => $good_id,
                            "num" => $num
                        );
                        M("dishGood")->add($addData);
                    }
                }
                unset($key, $value);
                // 生成 菜品副表
                foreach ($caipin as $key => $value) {
                    $name = $value->name;
                    $thumb = $value->thumb;
                    $addData = array(
                        "dish_id" => $dish_id,
                        "name" => $name,
                        "thumb" => $thumb
                    );
                    M("dishCaipin")->add($addData);
                }
                unset($key, $value);
                $data = array("status" => 1, "msg" => "操作成功");
            }
        }
        apiReturn($data);
    }
}