Order.php 17.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
<?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;
            }
        }

        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'] = $logistics['data']['logo'];
            $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);
            print_r($orderDetail);
            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']);
                $result['goods'] = Helpers::formatOrderGoods($orderDetail['data']['order_goods'], $count, true);
//                $result['sumPrice'] = $orderDetail['data']['goods_total_amount']; // 商品总金额
//                $result['salePrice'] = self::filterOrderPrice($orderDetail['data']['promotion_amount']); // 活动金额
//                $result['freight'] = $orderDetail['data']['shipping_cost']; // 运费
//                $result['coupon'] = self::filterOrderPrice($orderDetail['data']['coupons_amount']); // 优惠券
                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';
            }
            //相关金额(商品总金额,活动金额,运费,优惠券,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));
            }
            //测试
//            $result['isJit'] = true;
//            $result['jitDetailUrl'] = Helpers::url('/cart/index/jitDetail', array('orderCode' => $orderCode, 'sessionKey' => $sessionKey));
        }

        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)
    {
        $result['logisticsUrl'] = Helpers::url('/home/logistic', array('order_code' => $order['order_code']));

        if ($showLogistics && isset($order['express_company']['caption'])) {
            $result['logisticsCompany'] = $order['express_company']['caption'];
            $result['logisticsNum'] = isset($order['express_number']) ? $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;
        }
    }

    /**
     * 获取拆单信息数据
     * @param type $uid 用户id
     * @param type $cartType 购物车类型
     * @param type $skuList 商品信息
     * @return boolean
     */
    public static function getPackageInfo($orderCode, $uid, $sessionKey)
    {
        $result = array();
        $carpay = OrderData::viewOrderData($orderCode, $uid, $sessionKey);
//        print_r($carpay);
        do {
//            if (!$carpay || $carpay['code'] != 200 || empty($carpay['data']['package_list'])) {
//                break;
//            }
//            $package = $carpay['data'];
//            // 拆单数据
//            if (isset($package['package_list']) && !empty($package['package_list'])) {
//                $result['jitDetailPage'] = true;
//                $packageList = $package['data']['package_list'];
//                foreach ($packageList as $pk => $pv) {
//                    $result['packages'][$pk]['packageType'] = $pk + 1;
//                    $result['packages'][$pk]['dispatchType'] = ($pv['supplier_id'] == 0) ? '总仓库' : '异地调拨'; //仓库
//                    $goodList = $pv['goods_list'];
//                    foreach ($goodList as $glk => $glv) {
//                        $result['packages'][$pk]['goods'][$glk]['thumb'] = Images::getImageUrl($glv['goods_images'], 235, 314);
//                        $tag = isset($glv['goods_type']) ? $glv['goods_type'] : '';
//                        switch ($tag) {
//                            case 'price_gift' :
//                                //加价购
//                                $result['packages'][$pk]['goods'][$glk]['isAdd'] = true;
//                                break;
//                            case 'gift' :
//                                //赠品
//                                $result['packages'][$pk]['goods'][$glk]['isGift'] = true;
//                                break;
//                            default:
//                                break;
//                        }
//                    }
//                    $result['packages'][$pk]['expressCost'] = $pv['shopping_cost'];//运费
//                    $result['packages'][$pk]['discount'] = $pv['shopping_cut_cost'];//已优惠
//                }
//            }
            if (!$carpay || $carpay['code'] != 200 || empty($carpay['data']['shopping_cart_data'])) {
                break;
            }
            $package = $carpay['data'];
            // 拆单数据
            if (isset($package['shopping_cart_data']) && !empty($package['shopping_cart_data'])) {
                $result['jitDetailPage'] = true;
                $packageList = $package['shopping_cart_data']['package_list'];
                foreach ($packageList as $pk => $pv) {
                    $result['packages'][$pk]['packageType'] = $pk + 1;
                    $result['packages'][$pk]['dispatchType'] = ($pv['supplier_id'] == 0) ? '总仓库' : '异地调拨'; //仓库
                    $goodList = $pv['goods_list'];
                    foreach ($goodList as $glk => $glv) {
                        $result['packages'][$pk]['goods'][$glk]['thumb'] = Images::getImageUrl($glv['goods_images'], 235, 314);
                        $tag = isset($glv['goods_type']) ? $glv['goods_type'] : '';
                        switch ($tag) {
                            case 'price_gift' :
                                //加价购
                                $result['packages'][$pk]['goods'][$glk]['isAdd'] = true;
                                break;
                            case 'gift' :
                                //赠品
                                $result['packages'][$pk]['goods'][$glk]['isGift'] = true;
                                break;
                            default:
                                break;
                        }
                    }
                    $result['packages'][$pk]['expressCost'] = $pv['shopping_cost']; //运费
                    $result['packages'][$pk]['discount'] = $pv['shopping_cut_cost']; //已优惠
                }
            }
        }
        while (false);
        return $result;
    }

}