<?php
namespace Api\Model;

use Think\Model;

header("Content-Type:text/html;CharSet=UTF-8");
class OrderSmiModel extends Model
{
    /**
     * 生成分订单记录 及 商品副表记录
     * @param  [type] $order_id 订单ID
     * @param  [type] $type     订单发出地  1.分仓库, 2.总仓库
     * @param  [type] $goods    商品数组
     * @param  [type] $openid   用户openid
     * @param  [type] $village_id   村ID
     * @param  [type] $order_sn 订单编号
     */
    public function build($order_id, $type, $order_sn, $goods, $openid, $village_id)
    {
        if ($type == 1) {
            // 分仓库订单, 生成 唯一一条 order_smi 分订单记录
            $data = array(
                "order_id" => $order_id,
                "storage_status" => 1,    // 库存状态  1.充足 2.当日不足  (分仓库订单设为 1)
                "smi_sn" => $this->createSn($type, 1, $order_sn)
            );
            $insert_id = $this->add($data);
            // 生成 商品副表记录、补货订单
            foreach ($goods as $key => $value) {
                $good_id = $value->id;    // 商品ID
                $village_expect = M("goodsVillageSet")->where("`village_id`={$village_id} AND `good_id`={$good_id}")->getField("expect_time");    // 该商品 在该村的预计送达天数
                if (!$village_expect) {
                    $village_expect = M("goods")->where("`id`={$good_id}")->getField("village_expect");    // 该商品 分仓库预计送达天数
                    if (!$village_expect) {
                        $row = M("setElse")->find();
                        // 统一设置的预计送达时间
                        if ($row['sure_get'] > 0) {
                            $village_expect = $row['sure_get'];
                        } else {
                            $village_expect = 0;
                        }
                    }
                }
                $expect_time = $_SERVER['REQUEST_TIME'] + $village_expect * 24 * 60 *60;    // 预计送达时间
                $hour = intval(date("H", $_SERVER['REQUEST_TIME']));    // 当前时间小时单位
                if ($hour > 21) {
                    // 晚上 9点后 下单, 预计送达时间 增加 24小时
                    $expect_time += 24 * 60 *60;
                }
                // 订单商品副表
                $data = array(
                    "openid" => $openid,
                    "order_id" => $order_id,
                    "smi_id" => $insert_id,
                    "good_id" => $good_id,
                    "num" => $value->num,
                    "unit_price" => $value->price,
                    "kind_price" => $value->num * $value->price,
                    "expect_time" => $expect_time
                );
                M("orderGood")->add($data);
                // 补货订单
                $data = array(
                    "order_id" => $order_id,
                    "good_id" => $good_id,
                    "supply_sn" => "BH{$order_id}{$good_id}" . date("YmdHis", $_SERVER['REQUEST_TIME']),
                    "num" => $value->num,
                    "village_id" => $village_id,
                    "create_time" => $_SERVER['REQUEST_TIME']
                );
                M("orderSupply")->add($data);
            }
        } elseif ($type == 2) {
            // 总仓库订单, 根据每一类商品的库存 分为一共 2条/1条 order_smi 分订单记录
            $flag_1 = false;    // 为 true 时表明已生成过 库存充足的 总仓库分订单
            $flag_2 = false;    // 为 true 时表明已生成过 库存不足的 总仓库分订单
            // 查看各商品的总仓库库存是否充足
            foreach ($goods as $key => $value) {
                $good_id = $value->id;    // 商品ID
                $num = $value->num;    //购买数量
                $storage = M("goodsStorage")->where("`village_id`=0 AND `good_id`={$good_id}")->getField("storage");    // 库存
                if ($storage > $num) {
                    // 库存充足
                    if (!$flag_1) {
                        // 未生成 库存充足的 总仓库分订单
                        $data = array(
                            "order_id" => $order_id,
                            "storage_status" => 1,    // 库存状态  1.充足 2.当日不足
                            "smi_sn" => $this->createSn($type, 1, $order_sn)
                        );
                        $insert_id_1 = $this->add($data);
                        $flag_1 = true;    // 标记 已生成分订单
                    }
                    // 生成 商品副表记录
                    // 该商品 在该村的预计送达天数
                    $total_expect_enough = M("goodsVillageSet")->where("`village_id`={$village_id} AND `good_id`={$good_id}")->getField("expect_time");
                    if (!$total_expect_enough) {
                        $total_expect_enough = M("goods")->where("`id`={$good_id}")->getField("total_expect_enough");    // 该商品 总仓库库存充足预计送达天数
                        if (!$total_expect_enough) {
                            $row = M("setElse")->find();
                            // 统一设置的预计送达时间
                            if ($row['sure_get'] > 0) {
                                $total_expect_enough = $row['sure_get'];
                            } else {
                                $total_expect_enough = 0;
                            }
                        }
                    }
                    $expect_time = $_SERVER['REQUEST_TIME'] + $total_expect_enough * 24 * 60 *60;    // 预计送达时间
                    $hour = intval(date("H", $_SERVER['REQUEST_TIME']));    // 当前时间小时单位
                    if ($hour > 21) {
                        // 晚上 9点后 下单, 预计送达时间 增加 24小时
                        $expect_time += 24 * 60 *60;
                    }
                    $data = array(
                        "openid" => $openid,
                        "order_id" => $order_id,
                        "smi_id" => $insert_id_1,
                        "good_id" => $good_id,
                        "num" => $value->num,
                        "unit_price" => $value->price,
                        "kind_price" => $value->num * $value->price,
                        "expect_time" => $expect_time
                    );
                    M("orderGood")->add($data);
                } else {
                    // 看库存不足
                    if (!$flag_2) {
                        // 未生成 库存充足的 总仓库分订单
                        $data = array(
                            "order_id" => $order_id,
                            "storage_status" => 2,    // 库存状态  1.充足 2.当日不足
                            "smi_sn" => $this->createSn($type, 2, $order_sn)
                        );
                        $insert_id_2 = $this->add($data);
                        $flag_2 = true;    // 标记 已生成分订单
                    }
                    // 生成 商品副表记录
                    $total_expect_lack = M("goods")->where("`id`={$good_id}")->getField("total_expect_lack");    // 该商品 总仓库库存不足预计送达天数
                    if (!$total_expect_lack) {
                        $total_expect_lack = 0;
                    }
                    $expect_time = $_SERVER['REQUEST_TIME'] + $total_expect_lack * 24 * 60 *60;    // 预计送达时间
                    $hour = intval(date("H", $_SERVER['REQUEST_TIME']));    // 当前时间小时单位
                    if ($hour > 21) {
                        // 晚上 9点后 下单, 预计送达时间 增加 24小时
                        $expect_time += 24 * 60 *60;
                    }
                    $data = array(
                        "openid" => $openid,
                        "order_id" => $order_id,
                        "smi_id" => $insert_id_2,
                        "good_id" => $good_id,
                        "num" => $value->num,
                        "unit_price" => $value->price,
                        "kind_price" => $value->num * $value->price,
                        "expect_time" => $expect_time
                    );
                    M("orderGood")->add($data);
                }
            }
        }
    }

    /**
     * 生成分订单编号
     * @param  [type] $type           订单发出地  1.分仓库, 2.总仓库 (总仓库订单:"订单编号_库存状态", 分仓库订单:订单编号)
     * @param  [type] $storage_status 库存状态  1.充足 2.当日不足  (分仓库订单设为 1)
     * @param  [type] $order_sn       订单编号
     * @return [type]                 分订单编号
     */
    public function createSn($type, $storage_status, $order_sn)
    {
        $str = "";
        $str .= $order_sn;
        if ($type == 2) {
            $str .= $storage_status;
        }
        return $str;
    }
}