<?php
namespace Api\Model;

use Think\Model;

header("Content-Type:text/html;CharSet=UTF-8");
class OrderModel extends Model
{
    /**
     * 获取5种类型订单 待付款 待发货 待收货 已送达 已完成
     * @param  string $openid 用户标识
     * @return array
     */
    public function typesOfOrder($openid)
    {
        $list = array(
            "all" => array(),
            "notPaid" => array(),
            "notSended" => array(),
            "notGetted" => array(),
            "cancel" => array()
        );
        // 订单
        $orderList = M("order")
            ->where("`openid`='{$openid}'")
            ->order("`id` DESC")
            ->select();
        // 循环订单
        foreach ($orderList as $key => $value) {
            $orderList[$key]['create_time'] = date("Y年m月d日 H:i", $value['create_time']);
            $order_id = $value['id'];
            // 读取分订单
            $branch = M("orderSmi")->where("`order_id`={$order_id}")->select();
            foreach ($branch as $kb => $vb) {
                $smi_id = $vb['id'];
                // 商品副表
                $goods = M("orderGood")
                    ->join("og LEFT JOIN `am_goods` g ON og.`good_id`=g.`id`")
                    ->where("og.`smi_id`={$smi_id}")
                    ->field("og.*,g.`goods_name`,g.`thumb`,g.`des`")
                    ->select();
                $count = 0;
                $expect_time = 0;
                foreach ($goods as $kg => $vg) {
                    $count += $vg['num'];
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$vg['thumb']}";
                    if ($vg['expect_time'] > $expect_time) {
                        $expect_time = $vg['expect_time'];
                    }
                }
                unset($kg, $vg);
                if (!$goods) {
                    $goods = array();
                }
                $branch[$kb]['expect_time'] = date("n月j日", $expect_time);
                $branch[$kb]['goods'] = $goods;
            }
            unset($kb, $vb);
            $orderList[$key]['branch'] = $branch;
            $orderList[$key]['count'] = $count;
            // 订单分拨
            switch ($value['status']) {
                case '1':
                    $list['notPaid'][] = $orderList[$key];
                    break;
                case '2':
                    $list['notSended'][] = $orderList[$key];
                    break;
                case '3':
                    $list['notGetted'][] = $orderList[$key];
                    break;
                case '4':
                    $list['notGetted'][] = $orderList[$key];
                    break;
                case '-1':
                    $list['cancel'][] = $orderList[$key];
                    break;
            }
            $list['all'][] = $orderList[$key];
        }
        unset($key, $value);
        return $list;
    }

    /**
     * 通过ID获取一条订单记录
     * @param  int    $orderid 订单ID
     * @return array
     */
    public function findByID($orderid)
    {
        if ($orderid) {
            return $this->where("`id` = $orderid")->find();
        } else {
            return false;
        }
    }

    /**
     * 生成订单号
     * @param  string $openid 用户标识
     * @return string
     */
    public function createSn($openid = "")
    {
        if ($openid) {
            $userUid = M("users")->where("`openid` = '$openid'")->getField("uid");
            return "SN" . $userUid . date("YnjHis", $_SERVER['REQUEST_TIME']);
        } else {
            return "SN" . date("YnjHis") . rand(0, 999);
        }
    }

    /**
     * 订单中的商品列表
     * @param  string $orderid 订单ID
     * @return array
     */
    public function getGoods($orderid)
    {
        if ($orderid) {
            $list = M("ordergood")
                ->where("`orderid`={$orderid}")
                ->field("`id`,`goodid`,`num`")
                ->select();
            if (is_array($list)) {
                return $list;
            } else {
                file_put_contents("./Logs/errorLog.log", date("Y-m-d H:i:s ", $_SERVER['REQUEST_TIME']) . "订单中的商品列表: " . M("ordergood")->getlastsql() . "\r\n", FILE_APPEND);
                file_put_contents("./Logs/errorLog.log", date("Y-m-d H:i:s ", $_SERVER['REQUEST_TIME']) . "订单中的商品列表: " . serialize($list) . "\r\n\r\n", FILE_APPEND);
                return array();
            }
        } else {
            file_put_contents("./Logs/errorLog.log", date("Y-m-d H:i:s ", $_SERVER['REQUEST_TIME']) . "订单中的商品列表: no orderid" . "\r\n\r\n", FILE_APPEND);
            return false;
        }
    }
}