Order.php 6.34 KB
<?php

namespace home;

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

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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

    /**
     * 订单相关数据处理
     */
    public function getOrder($type = 1, $page = 1, $limit, $gender, $yh_channel, $uid) {
        $result = array();
        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_HOME_ORDER_ORDER;
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }
        //调用接口获得数据
        $data = OrderData::getOrderData($type, $page, $limit, $gender, $yh_channel, $uid);
        //检查数据返回是否正常,正常则处理数据
        if ($data['code'] == 200 && isset($data['data'])) {
            foreach ($data['data']['order_list'] as $key => $vo) {
                //订单号,支付状态,订单商品数量,订单总价格
                $result[$key]['orderNum'] = $vo['order_code'];
                $result[$key]['orderStatus'] = $vo['status_str'];
                $result[$key]['count'] = count($vo['order_goods']);
                $result[$key]['sumCost'] = $vo['amount'] + $vo['shipping_cost'];
                //订单商品列表数据
                $result[$key]['goods'] = self::formatOrderGoods($vo['order_goods']);
                //订单status判断订单处于什么状态。
                do {
                    //订单取消状态
                    if ($vo['is_cancel'] === 'Y') {
                        $result[$key]['canceled'] = true;
                        break;
                    }
                    //支付方式不是货到付款时,计算订单状态
                    if ($vo['payment_type'] != 2) {
                        switch ($vo['status']) {
                            case 0:
                                $result[$key]['unpaid'] = true;
                                break;
                            case 1:
                            case 2:
                            case 3:
                            case 4:
                            case 5:
                                $result[$key]['unreceived'] = true;
                                $result[$key]['logisticsUrl'] = "暂无logisticsUrl数据";
                                break;
                            case 6:
                                $result[$key]['completed'] = true;
                                break;
                            default:
                                break;
                        }
                    } elseif ($vo['payment_type'] == 2) {
                        //订单为货到付款订单时,订单没有未支付状态
                        switch ($vo['status']) {
                            case 0 || 1 || 2 || 3 || 4 || 5:
                                $result[$key]['unreceived'] = true;
                                //此处备注,接口没有返回logisticsUrl数据
                                $resault[$key]['logisticsUrl'] = "备注:暂无logisticsUrl数据";
                                break;
                            case 6:
                                $result[$key]['completed'] = true;
                                break;
                            default:
                                break;
                        }
                    }
                } while (false);
            }
        }

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

    //格式化订单商品
    static function formatOrderGoods($orderGoods) {
        $arr = array();
        foreach ($orderGoods as $key => $vo) {
            $arr[$key]['thumb'] = Helpers::getImageUrl($vo['goods_image'], 90, 120);
            $arr[$key]['name'] = $vo['product_name'];
            $arr[$key]['color'] = $vo['color_name'];
            $arr[$key]['size'] = $vo['size_name'];
            $arr[$key]['price'] = $vo['goods_price'];
            $arr[$key]['count'] = $vo['buy_number'];
            if ($vo['goods_type'] == 'gift') {
                $arr[$key]['gift'] = true;
            } elseif ($vo['goods_type'] == 'price_gift') {
                $arr[$key]['advanceBuy'] = true;
            }
        }
        return $arr;
    }

    //根据type值设置nav属性
    public function getNavs($type) {
        $nav = array(
            array(
                'name' => '全部',
                'typeId' => '1',
                'url' => '/home/order?type=1'
            ),
            array(
                'name' => '待付款',
                'typeId' => '2',
                'url' => '/home/order?type=2'
            ),
            array(
                'name' => '待发货',
                'typeId' => '3',
                'url' => '/home/order?type=3'
            ),
            array(
                'name' => '待收货',
                'typeId' => '4',
                'url' => '/home/order?type=4'
            )
        );
        foreach ($nav as $key => $vo) {
            switch ($type) {
                case 1:
                    if ($vo['typeId'] == 1) {
                        $nav[$key]['active'] = true;
                    }
                    break;
                case 2:
                    if ($vo['typeId'] == 2) {
                        $nav[$key]['active'] = true;
                    }
                    break;
                case 3:
                    if ($vo['typeId'] == 3) {
                        $nav[$key]['active'] = true;
                    }
                    break;
                case 4:
                    if ($vo['typeId'] == 4) {
                        $nav[$key]['active'] = true;
                    }
                    break;
                default:
                    break;
            }
        }
        return $nav;
    }

}