OrderModel.js 12.6 KB
'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const OrderData = require('./OrderData');
const ChannelConfig = require('./ChannelConfig');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
const _ = require('lodash');
const Image = require('../../../utils/images');

/**
 * 获取我的订单列表数据-分页
 * @param type $uid
 * @param type $page
 * @param type $limit
 * @param type $type 获取订单类型 type=1全部,type=2待付款,type=3待发货,type=4待收货,type=5待评论(已成功) 7取消
 */
const getOrders = ($uid, $page, $limit, $type, $isPage)=>{

    return co(function *() {
        $isPage = $isPage || false;
        let getOrderDescStr = {
            1: '您还没有任何订单',
            5: '您目前还没有成功的订单',
            7: '您还没有任何取消的订单'
        };
        let $descStr = getOrderDescStr[$type] || '';
        let $orders = {empty: $descStr};
        let $orderInfo = yield OrderData.getUserOrders($uid, $page, $limit, $type);
        if ($orderInfo.data && $orderInfo.data.order_list) {
            $orders = {};
            $orderInfo.data.order_list.forEach(function($orderV, $orderK) {
                $orders[$orderK] = {};
                $orders[$orderK]['orderNum'] = $orderV['order_code']; // 订单标识
                $orders[$orderK]['orderTime'] = new Date($orderV['create_time']);
                let $statusInfo = getOrderStatus($orderV['is_cancel'], $orderV['status'], $orderV['payment_type'], $orderV['payment_status']);

                // 订单状态
                if ($statusInfo['cancel']) {
                    $orders[$orderK]['cancel'] = $statusInfo['cancel'];
                }
                else {
                    if ($statusInfo['keyName']) {
                        $orders[$orderK][$statusInfo['keyName']] = true;

                        // 已发货,物流信息
                        if ($statusInfo['keyName'] == 'shipped') {
                            let $expressInfo = getExpressInfo($orderV['order_code'], $uid, $orderV['payment_type'], $orderV['create_time']);
                            $orders[$orderK]['logistics'] = $expressInfo['logistics'];
                        }
                    }
                }

                // 订单商品相关信息
                let $opRefundStatus = true; // 订单不可操作退换货
                if ($orderV['order_goods'] && $orderV['order_goods']) {
                    let $goods = {}, $refundFlag = {};
                    $orderV['order_goods'].forEach(function($goval, $gokey) {
                        $goods[$gokey] = {};
                        $goods[$gokey]['href'] = helpers.getUrlBySkc($goval['product_id'], $goval['goods_id'], $goval['cn_alphabet']);
                        $goods[$gokey]['thumb'] = $goval['goods_image'] && $goval['goods_image'] ? Image.getImageUrl($goval['goods_image'], 100, 100) : '';
                        $goods[$gokey]['name'] = $goval['product_name'];
                        $goods[$gokey]['color'] = $goval['color_name'];
                        $goods[$gokey]['size'] = $goval['size_name'];
                        $goods[$gokey]['price'] = $goval['goods_price'];
                        let $buyNum = Number($goval['buy_number']);
                        let $refundNum = Number($goval['refund_num']);
                        $goods[$gokey]['count'] = $buyNum;
                        let $refundStatus = ($refundNum > 0) ? true : false; // 只要发生一件退换,退换过的标记
                        $goods[$gokey]['refundStatus'] = $refundStatus;
                        $refundFlag = (($buyNum == $refundNum) && $refundNum > 0) ? 'finished' : 'unfinished'; // 某一件商品全部退换结束
                        $goods[$gokey]['arrivalDate'] = $goval['expect_arrival_time'];
                        let $goodsTagName = getGoodsTag($orderV['attribute'], $goval['goods_type']);
                        if ($goodsTagName) {
                            $goods[$gokey][$goodsTagName] = true;
                        }
                        $orders[$orderK]['goods'] = $goods;
                    });

                    if ($refundFlag.indexOf('unfinished')) {
                        $opRefundStatus = false;
                    }
                    $orders[$orderK]['pay'] = $orderV['amount']; // 付款数
                    $orders[$orderK]['fregit'] = $orderV['shipping_cost']; // 邮费
                }

                // 操作
                $orders[$orderK]['operation'] = getOperateInfo($orderV['attribute'], $orderV['is_cancel'], $orderV['status'], $orderV['payment_status'], $orderV['update_time'], $orderV['order_type'], $orderV['refund_status'], $orderV['payment_type'], $orderV['order_code'], $opRefundStatus);

            });
            if ($isPage) {
                $orders['pager']['total'] = $orderInfo['data']['total'];
                $orders['pager']['pageTotal'] = $orderInfo['data']['page_total'];
                $orders['pager']['page'] = $orderInfo['data']['page'];
            }
            return $orders;
        }
    })();
};
const getOrderStatus = ($isCancel, $status, $payType, $payStatus)=>{
    // 初始化:未取消,待付款
    let $ret = {cancel: false, keyName: 'noPay', statusStr: '待付款'};
    if ($isCancel == 'Y') {
        $ret = {cancel: true, statusStr: '已取消'};
    } else {
        switch ($status) {
            case 0:
                // '订单已成功,等待付款'
                if ($payType != 2 && $payStatus == 'N') {
                    $ret['keyName'] = 'noPay';
                    $ret['statusStr'] = '待付款';
                }

                // '订单已付款,等待备货中'
                else if ($payType != 2 && $payStatus == 'Y') {
                    $ret['keyName'] = 'paid';
                    $ret['statusStr'] = '备货中';
                }

                // '订单已成功,等待备货中'-货到付款
                else if ($payType == 2 && $payStatus == 'N') {
                    $ret['keyName'] = 'complete';
                    $ret['statusStr'] = '备货中';
                }
                break;
            case 1:
            case 2:
            case 3:
                // '订单已付款,等待备货中'
                $ret['keyName'] = 'paid';
                $ret['statusStr'] = '备货中';
                break;
            case 4:
            case 5:
                // '订单已发货'
                $ret['keyName'] = 'shipped';
                $ret['statusStr'] = '待收货';
                break;
            case 6:
                // '交易完成';
                $ret['keyName'] = 'reback';
                $ret['statusStr'] = '交易完成';
                break;
        }
    }
    return $ret;
};
const getExpressInfo = ($orderCode, $uid, $paymetType, $createTime, $isDetail)=>{
    return co(function * () {
        $isDetail = $isDetail || false;
        let $result = {};
        $result['logisticsUrl'] = '';
        $result['logisticsImg'] = '';
        $result['logisticsCompany'] = '';
        $result['courierNumbe'] = '';

        if ($paymetType == 1) {
            if ($isDetail) {
                $result['logistics'] = [new Date($createTime), ' ', '您的订单已提交,等待付款'];
            }
            else {
                $result['logistics'] = new Date($createTime) + ' ' + '您的订单已提交,等待付款';
            }
        }
        if ($paymetType == 2) {
            if ($isDetail) {
                $result['logistics'] = [new Date($createTime), ' ', '您的订单已提交,等待审核'];
            }
            else {
                $result['logistics'] = new Date($createTime) + ' ' + '您的订单已提交,等待审核';
            }
        }

        // 有物流
        if ($orderCode && _.isNumber($uid)) {
            let $logistics = yield OrderData.getLogisticsData($orderCode, $uid);
            if ($logistics['data']) {
                $result['logisticsUrl'] = $logistics['data']['url'] ? helper.getUrlSafe($logistics['data']['url']) : '';
                $result['logisticsImg'] = $logistics['data']['logo'] ? $logistics['data']['logo'] : '';
                $result['logisticsCompany'] = $logistics['data']['caption'] ? $logistics['data']['caption'] : '';
                $result['courierNumbe'] = $logistics['data']['express_number'] ? $logistics['data']['express_number'] : '';
                let $expressDetail = $logistics['data']['express_detail'] ? $logistics['data']['express_detail'] : {};
                if ($expressDetail) {
                    let $logisticsTmp = $result['logistics'][0]; // 暂存
                    $result['logistics'] = {};
                    $expressDetail.forEach(function($value) {
                        let $pos = $value['accept_address'].indexOf(' ') / 3;
                        let $city = $value['accept_address'].substr(0, $pos);
                        let $exInfo = $value['accept_address'].substr($pos);
                        if ($isDetail) {
                            $result['logistics'] = [$value['acceptTime'], $city, $exInfo];
                        }
                        else {
                            $result['logistics'] = $value['acceptTime'] + $city + $exInfo;
                        }
                    });

                    // 把最初的处理放最后
                    $result['logistics'] = $logisticsTmp;
                }
            }
        }
        return $result;
    })();

};
const getGoodsTag = ($attribute, $goodsType)=>{
    let $goodsTagName = '';
    switch ($goodsType) {

        // 赠品
        case 'gift':
            $goodsTagName = 'freebie';
            break;

        // 加价购
        case 'price_gift':
            $goodsTagName = 'advanceBuy';
            break;

        // 预售
        case 'advance':
            $goodsTagName = 'preSaleGood';
            break;

        // outlet
        case 'outlet':
            $goodsTagName = '';
            break;

        // 免单
        case 'free':
            $goodsTagName = '';
            break;

        // 电子
        case 'ticket':
            $goodsTagName = '';
            break;
        default:
            break;
    }

    // 虚拟
    if ($attribute == 3) {
        $goodsTagName = 'virtualGood';
    }
    return $goodsTagName;
};
const getOperateInfo = ($attribute, $isCancel, $status, $payStatus, $updateTime, $orderType, $refundStatus, $paymentType, $orderCode, $opRefundStatus)=>{
    // 查看订单
    let $orderDetailUrl = helpers.urlFormat('/home/orders/detail', {orderCode: $orderCode});

        // 查看二维码
    let $ticketUrl = helpers.urlFormat('/home/orders/tickets', {orderCode: $orderCode});

        // 立即付款
    let $payUrl = helpers.urlFormat('/shopping/pay', {orderCode: $orderCode});

        // 取消订单
    let $cancelOrderUrl = 'javascript:void(0)';

        // 确认订单
    let $confirmOrderUrl = 'javascript:void(0)';

        // 申请换货
    let $exchangeUrl = helpers.urlFormat('/home/returns/exchangeRequest', {orderCode: $orderCode});

        // 申请退货
    let $refundUrl = helpers.urlFormat('/home/returns/refundrequest', {orderCode: $orderCode});

    let $operation = {};

        // 立即付款
    if ($payStatus == 'N' && $paymentType != 2 && $isCancel == 'N') {
        $operation = {payNow: true, href: $payUrl};
    }

    $operation = {href: $orderDetailUrl, name: '查看订单'};

        // 查看订单,虚拟订单查看二维码
    if ($attribute == 3) {
        if ($payStatus == 'Y') {
            $operation = {href: $ticketUrl, name: '查看二维码'};
        }
    }

        // 取消订单
    if ($status < 3 && $isCancel == 'N' && $orderType != 5 && $payStatus == 'N') {
        $operation = {href: $cancelOrderUrl, name: '取消订单', cancelOrder: true};
    }

        // 确认收货
    if ($status >= 4 && $status < 6 && $refundStatus == 0 && $attribute != 3 && $isCancel == 'N') {
        $operation = {href: $confirmOrderUrl, name: '确认收货', confirmReceived: true};
    }

        // 换货
    let $time = (Date.now() - $updateTime);
    let $orderExchangeLimitTime = ChannelConfig.$exchangeDay;
    if ($status >= 6 && $time < 86400 * $orderExchangeLimitTime && $attribute != 3 && $isCancel == 'N') {
        $operation = {href: $exchangeUrl, name: '申请换货', optDis: $opRefundStatus};
    }

        // 退货
    let $orderRefundLimitTime = ChannelConfig.$refundDay;
    if ($status >= 6 && $time < 86400 * $orderRefundLimitTime && $attribute != 3 && $isCancel == 'N') {
        $operation = {href: $refundUrl, name: '申请退货', optDis: $opRefundStatus};
    }
    return $operation;
};


module.exports = {
    getOrders
};