Coupons.php 3.47 KB
<?php
/**
 * Created by PhpStorm.
 * User: yl3016
 * Date: 16/3/21
 * Time: 下午5:10
 */

namespace Home;

use LibModels\Web\Home\UserData;
use WebPlugin\Helpers;

class CouponsModel
{

    const UNUSED = 'notuse';
    const USED = 'use';
    const INVALID = 'overtime';

    public static function getCouponsList($uid, $type, $page, $limit)
    {
        $couponsInfo = UserData::getCouponsList($uid, $type, $page, $limit);
        $result = array();
        if (empty($couponsInfo['data']['couponList'])) {
            return $result;
        }

        $coupons = $couponsInfo['data']['couponList'];

        if ($coupons) {
            foreach ($coupons as $k => $v) {
                $result[$k]['id'] = $v['couponId'];
                $result[$k]['code'] = $v['couponCode'];

                // 格式化有效日期 "couponValidity": "2016.03.15-2016.03.31"
                $dates = explode('-', $v['couponValidity']);
                $result[$k]['beginTime'] = date('Y-m-d', date_timestamp_get(date_create_from_format('Y.m.d', $dates[0])));
                $result[$k]['endTime'] = date('Y-m-d', date_timestamp_get(date_create_from_format('Y.m.d', $dates[1])));
                if (empty($v['couponImageUrl'])) {
                    $result[$k]['img'] = 'http://static.yohobuy.com/images/v2/activity/default_coupon.jpg';
                } else {
                    $result[$k]['img'] = $v['couponImageUrl'];
                }

                // 3天之内即将要过期
                if (($v['overTime'] - time()) < 259200) {
                    $result[$k]['endSoon'] = true;
                } else {
                    $result[$k]['endSoon'] = false;
                }

                $result[$k]['value'] = sprintf("¥%.2f", $v['couponValue']);
                $result[$k]['validity'] = $v['couponValidity'];
                $result[$k]['useRemark'] = $v['couponDetailInfomation'];

                if ($type == self::USED) {
                    $result[$k]['orderNum'] = isset($v['orderCode']) ? $v['orderCode'] : '';
                    $result[$k]['orderDetailUrl'] = Helpers::url('/home/orders/detail', array('orderCode' => isset($v['orderCode']) ? $v['orderCode'] : ''));
                    $result[$k]['orderSum'] = sprintf("¥%.2f", isset($v['orderPrice']) ? $v['orderPrice'] : 0);
                    $result[$k]['payment'] = sprintf("¥%.2f", isset($v['actuallyPaid']) ? $v['actuallyPaid'] : 0);
                    $date = date_create_from_format('Y-m-d H:i:s', isset($v['usedTime']) ? $v['usedTime'] : 0);
                    if ($date) {
                        $result[$k]['useTime'] = date('Y-m-d H:i', date_timestamp_get($date));
                    } else {
                        $result[$k]['useTime'] = '';
                    }
                } else {
                    $result[$k]['status'] = $v['isValidity'] ? '可使用' : '已过期';
                }

                if (isset($v['couponType'])) {
                    $result[$k]['type'] = $v['couponType'];
                    // 免邮券面值显示‘免邮’
                    if ($v['couponType'] == 5) {
                        $result[$k]['value'] = '免邮';
                    }
                }
            }
            $result['pager']['total'] = $couponsInfo['data']['total'];
            $result['pager']['pageTotal'] = $couponsInfo['data']['totalPageNum'];
            $result['pager']['page'] = $page;
        }

        return $result;
    }
}