coupon-service.js 3.97 KB

const _ = require('lodash');
const CouponApi = require('./coupon-api');

const helpers = global.yoho.helpers;
const setPager = require(`${global.utils}/pager`).setPager;
const handleFilterUrl = require(`${global.utils}/parameter`).fullParamToMinPath;

const COUPON_BASE_URI = '/home/coupons';
const COUPON_TYPE = {
    UNUSED: 'notuse',
    USED: 'use',
    INVALID: 'overtime'
};

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    _setShowNum(num) {
        num = num > 0 ? num : 0;
        num = num > 99 ? '99+' : num;

        return num ? `(${num})` : '';
    }

    _setCouponTabs(nums, type) {
        nums = nums || {};

        return [
            {
                active: type === COUPON_TYPE.UNUSED,
                url: helpers.urlFormat(COUPON_BASE_URI),
                name: `可用${this._setShowNum(nums[COUPON_TYPE.UNUSED])}`
            },
            {
                active: type === COUPON_TYPE.USED,
                url: helpers.urlFormat(COUPON_BASE_URI, {type: COUPON_TYPE.USED}),
                name: `已使用${this._setShowNum(nums[COUPON_TYPE.USED])}`
            },
            {
                active: type === COUPON_TYPE.INVALID,
                url: helpers.urlFormat(COUPON_BASE_URI, {type: COUPON_TYPE.INVALID}),
                name: `已失效${this._setShowNum(nums[COUPON_TYPE.INVALID])}`
            }
        ];
    }

    getCouponData(uid, params) {
        params = params || {};

        const page = parseInt(`0${params.page}`, 10) || 1;
        const type = params.type || COUPON_TYPE.UNUSED;
        const couponApiCase = new CouponApi(this.ctx);
        let resData = {};

        return Promise.all([
            couponApiCase.getCouponList(uid, params.type, page, params),
            couponApiCase.getCouponNum(uid)
        ]).then(result => {
            Object.assign(resData, _.get(result, '[0].data', {}));

            if (resData.couponList && resData.couponList.length) {
                _.forEach(resData.couponList, value => {
                    if (type === COUPON_TYPE.USED) {
                        value.isUsed = true;
                    } else if (type === COUPON_TYPE.INVALID) {
                        if (value.is_overtime === 'Y') {
                            value.isOvertime = true;
                        } else {
                            value.isUseless = true;
                        }
                    } else {
                        value.toUse = handleFilterUrl('/list', {
                            cpc_id: value.coupon_id,
                            coupon_code: value.coupon_code,
                            phrase: `以下商品可使用【${value.coupon_name}】优惠券`
                        });
                    }
                });
            } else {
                resData.couponList = false;
            }


            resData.tabs = this._setCouponTabs(_.get(result, '[1].data', {}), type);

            if (_.get(resData, 'tabs[0].active') && resData.filters) {
                const filterUrl = helpers.urlFormat(COUPON_BASE_URI,
                    Object.assign({}, params, {filter: '__filter__', page: 1}));
                let filter = params.filter || 0;

                _.forEach(resData.filters, value => {
                    if ((+filter === +value.filter_id)) {
                        value.active = true;
                        value.url = 'javascript:;'; // eslint-disable-line
                    } else {
                        value.url = filterUrl.replace('__filter__', value.filter_id);
                    }
                });
            } else {
                resData.filters = false;
            }

            let total = _.get(resData, 'total', 0);
            let pageTotal = _.get(resData, 'pageNum') || 1;

            resData.pager = Object.assign({
                count: total,
                curPage: page,
                totalPages: pageTotal
            }, setPager(pageTotal, params));

            return resData;
        });

    }
};