Order.php 14.3 KB
<?php

namespace Home;

use Configs\CacheConfig;
use Plugin\Cache;
use LibModels\Wap\Home\IndexData;
use LibModels\Wap\Home\OrderData;
use Plugin\Helpers;

/**
 * Description of Order
 *
 */
class OrderModel
{

    const CODE_PAYMENT = '04cf5abaa7c20178325a07c4a833782c'; //支付订单资源码
    const CODE_STROLL = 'a7989369aa86681c678bc40f171b8f1d'; //随便逛逛url地址资源码
    const CODE_LOGISTIC_BANNER = '1fc9b2484fcd559049f2f7e0db313f20'; // 物流详情banner资源码

    /**
     * 订单相关数据处理
     *
     * payment_type:
      '1' => array(//在线支付
      0 => '待付款',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      ),
      '2' => array(//货到付款
      0 => '备货中',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      ),
      '3' => array(//现金支付
      0 => '待付款',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      ),
      '4' => array(//抵消支付
      0 => '待付款',
      1 => '已付款',
      2 => '已付款',
      3 => '已付款',
      4 => '已发货',
      5 => '已发货',
      6 => '交易成功'
      )
     */

    public static function getOrder($type, $page, $limit, $gender, $yh_channel, $uid)
    {
        $result = array();
        //调用接口获得数据
        $data = OrderData::getOrderData($type, $page, $limit, $gender, $yh_channel, $uid);
        // 判断是否还有数据, 没有数据则返回空
        if (isset($data['data']['page_total']) && $page > $data['data']['page_total']) {
            return $result;
        }
        //检查数据返回是否正常,正常则处理数据
        if (!empty($data['data']['order_list'])) {
            $count = 0;
            foreach ($data['data']['order_list'] as $key => $vo) {
                // 订单件数清零
                $count = 0;
                //根据订单status判断订单处于什么状态。
                $result[$key] = self::getOrderStatus($vo);
                //订单号,支付状态,订单商品数量,订单总价格
                $result[$key]['orderNum'] = $vo['order_code'];
                $result[$key]['orderStatus'] = $vo['status_str'];
                $result[$key]['sumCost'] = $vo['amount'];
                // 如果运费大于0,会显示运费
                if (floatval($vo['shipping_cost']) > 0) {
                    $result[$key]['shippingCost'] = $vo['shipping_cost'];
                }
                //类内调用格式化订单商品数据方法
                $result[$key]['goods'] = Helpers::formatOrderGoods($vo['order_goods'], $count);
                $result[$key]['detailUrl'] = Helpers::url('/home/orderdetail', array('order_code' => $vo['order_code']));
                $result[$key]['count'] = $count;
                //倒计时时间
                if (isset($vo['counter_flag']) && $vo['counter_flag'] == 'Y') {
                    $result[$key]['leftTime'] = $vo['pay_lefttime']*1000;
                }
            }
        }

        return $result;
    }

    /**
     * 查看物流
     *
     * @param int $orderCode 订单编号
     * @param int $uid 用户ID
     * @return array
     */
    public static function Logistics($orderCode, $uid)
    {
        $result = array();

        do {
            if (!isset($orderCode) || !is_numeric($uid)) {
                break;
            }

            // 获取物流详情页banner
            $banner = self::getLogisterBanner();
            if ($banner) {
                $result['banner'] = $banner;
            }

            $logistics = OrderData::LogisticsData($orderCode, $uid);
            if (empty($logistics['data'])) {
                break;
            }
            $result['logisticUrl'] = $logistics['data']['url'];
            $result['logisticImg'] = strtr($logistics['data']['logo'], array('http://' => '//'));
            $result['logisticCompany'] = $logistics['data']['caption'];
            $result['logisticNumber'] = $logistics['data']['express_number'];

            $build = array();
            foreach ($logistics['data']['express_detail'] as $value) {
                $build['status'] = $value['accept_address'];
                $build['date'] = $value['acceptTime'];
                $result['logisticDetail'][] = $build;
            }
        }
        while (false);

        return $result;
    }

    /**
     * 获取物流详情页banner
     *
     * @return array|bool
     */
    private static function getLogisterBanner()
    {
        $result = false;

        if (USE_CACHE) {
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get(CacheConfig::KEY_CODE_LOGISTIC_BANNER, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 调用接口获取数据
        $banner = IndexData::getBannerStart(self::CODE_LOGISTIC_BANNER);
        if (isset($banner['code']) && $banner['code'] == 200 && !empty($banner['data'])) {
            $result = array();
            // 处理数据
            foreach ($banner['data'] as $val) {
                foreach ($val['data'] as $single) {
                    $result['url'] = Helpers::getFilterUrl($single['url']);
                    $result['img'] = Helpers::getImageUrl($single['src'], 640, 200);
                }
            }
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get(CacheConfig::KEY_CODE_LOGISTIC_BANNER, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set(CacheConfig::KEY_CODE_LOGISTIC_BANNER, $result);
            }
        }

        return $result;
    }

    /**
     * 获取订单详情信息
     *
     * @param string $orderCode 订单号
     * @param int $uid 用户ID
     * @param string $sessionKey 用户的会话
     * @return array
     */
    public static function orderDetail($orderCode, $uid, $sessionKey)
    {
        $result = array();
        if (is_numeric($orderCode) && is_numeric($uid) && is_string($sessionKey)) {
            $orderDetail = OrderData::viewOrderData($orderCode, $uid, $sessionKey);
            if (isset($orderDetail['data']['order_code'])) {
                $count = 0;
                $result = self::getOrderStatus($orderDetail['data'], true); // 订单状态
                $result['name'] = $orderDetail['data']['user_name'];
                $result['phoneNum'] = $orderDetail['data']['mobile'];
                $result['address'] = $orderDetail['data']['area'] . $orderDetail['data']['address'];
                $result['orderStatus'] = $orderDetail['data']['status_str'];
                $result['orderNum'] = $orderDetail['data']['order_code'];
                $result['orderTime'] = date('Y-m-d H:i:s', $orderDetail['data']['create_time']);
                //倒计时时间
                if (isset($orderDetail['data']['counter_flag']) && $orderDetail['data']['counter_flag'] == 'Y') {
                    $result['leftTime'] = $orderDetail['data']['pay_lefttime']*1000;
                }
                $result['goods'] = Helpers::formatOrderGoods($orderDetail['data']['order_goods'], $count, true);
                if (isset($orderDetail['data']['promo_code_amount'])) {
                    $result['promo_code_amount'] = self::filterOrderPrice($orderDetail['data']['promo_code_amount']); // 优惠码
                }
                $result['yohoCoin'] = $orderDetail['data']['yoho_give_coin']; // YOHO币返回
                $result['price'] = $orderDetail['data']['amount']; // 实付金额
                $result['goodsAmount'] = $orderDetail['data']['payment_amount']; // 商品总金额没有人民币符号
                $result['orderCount'] = $count; // 订单总件数
                $result['isPay'] = $orderDetail['data']['payment_status'] === 'Y';
                //判断是否可以修改地址
                $result['changeable'] = $orderDetail['data']['can_update_delivery_address'] == 'Y' ? 'true' : 'false';
                //判断是否有关联订单
                $result['relation'] = $orderDetail['data']['relate_order_code'] == 'Y' ? 'true' : 'false';
                //修改地址跳转链接
                $result['url'] = Helpers::url('/home/addressModify', array('orderCode' => $orderCode, 'relation' => $result['relation']));
            }
            //相关金额(商品总金额,活动金额,运费,优惠券,YOHO币)
            if (isset($orderDetail['data']['promotion_formulas']) && $orderDetail['data']['promotion_formulas']) {
                foreach ($orderDetail['data']['promotion_formulas'] as $pricek => $pricev) {
                    $result['orderBalance'][$pricek] = array('promotion' => $pricev['promotion'], 'account' => $pricev['promotion_amount']);
                }
            }
            //为支付的拆单配送信息
            if (isset($orderDetail['data']['is_multi_package']) && $orderDetail['data']['is_multi_package'] == 'Y') {
                $result['isJit'] = true;
                $result['jitDetailUrl'] = Helpers::url('/cart/index/jitDetail', array('orderCode' => $orderCode, 'sessionKey' => $sessionKey));
            }
        }
        //取消订单原因列表
        $resons = OrderData::closeReasons();
        $result['cancelReason'] = isset($resons['data']) ? $resons['data'] : '';
        return $result;
    }

    /**
     * 根据type值设置nav属性
     */
    public static function getNavs($type)
    {
        $navType = array(1 => '全部', 2 => '待付款', 3 => '待发货', 4 => '待收货');
        $nav = array();
        foreach ($navType as $key => $value) {
            $act = false;
            if ($type == $key) {
                $act = true;
            }
            $nav[] = array(
                'name' => $value,
                'typeId' => $key,
                'active' => $act,
                'url' => Helpers::url('/home/orders', array('type' => $key))
            );
        }

        return $nav;
    }

    /**
     * 获取订单状态
     *
     * @param array $order 订单
     * @param bool $showLogistics 控制是否显示物流信息
     * @return array
     */
    private static function getOrderStatus($order, $showLogistics = false)
    {
        $result = array();

        //根据订单status判断订单处于什么状态。
        do {
            //订单取消状态 = Y 时,跳出判断订单状态循环,并设置订单状态为已取消。
            if ($order['is_cancel'] === 'Y') {
                $result['canceled'] = true;
                break;
            }

            // 先判断订单付款方式,根据不同的付款方式计算订单状态。(注:货到付款没有待付款状态)
            // 支付方式为非货到付款时,计算订单状态。
            if ($order['payment_type'] != 2) {
                switch ($order['status']) {
                    case 0: // 待付款
                        $result['unpaid'] = true;
                        $result['payUrl'] = Helpers::url('/home/orders/pay', array('order_code' => $order['order_code']));
                        break;
                    //未发货&未收货 状态,统一合并到待收货状态。
                    case 1:
                    case 2:
                    case 3:
                        //已付款状态不给查看物流URL
                        $result['unreceived'] = true;
                        break;
                    case 4:
                    case 5:
                        //已发货状态,给查看物流URL
                        $result['unreceived'] = true;
                        self::assignExpressInfo($showLogistics, $order, $result);
                        break;
                    case 6:
                        $result['completed'] = true;
                        // 已成功订单,给查看物流URL
                        self::assignExpressInfo($showLogistics, $order, $result);
                        break;
                    default:
                        break;
                }
                break;
            }

            // 订单为货到付款订单时,计算订单状态。(货到付款没有待付款状态)
            switch ($order['status']) {
                case 0: // 备货中
                    $result['unpaid'] = true;
                    break;
                case 1:
                case 2:
                case 3:
                    //备货中、已付款状态不给查看物流链接
                    $result['unreceived'] = true;
                    break;
                case 4:
                case 5:
                    //待收货状态,给查看物流url
                    $result['unreceived'] = true;
                    self::assignExpressInfo($showLogistics, $order, $result);
                    break;
                case 6:
                    $result['completed'] = true;
                    self::assignExpressInfo($showLogistics, $order, $result);
                    break;
                default:
                    break;
            }
        }
        while (false);

        return $result;
    }

    /**
     * 获取快递有关信息
     * @param boolean $showLogistics 是否显示
     * @param array $order 订单信息
     * @param array $result
     */
    private static function assignExpressInfo($showLogistics, $order, &$result)
    {
        if ($showLogistics && isset($order['express_company']['caption']) && isset($order['express_number'])) {
            $result['logisticsUrl'] = Helpers::url('/home/logistic', array('order_code' => $order['order_code']));
            $result['logisticsCompany'] = $order['express_company']['caption'];
            $result['logisticsNum'] =  $order['express_number'];
        }
    }

    /**
     * 过滤掉为0的价格数据
     *
     * @param $price
     * @return string 过滤之后的价格
     */
    private static function filterOrderPrice($price)
    {
        if (is_string($price) && strstr($price, '¥0.00') !== false) {
            return '';
        }
        else {
            return $price;
        }
    }

}