CouponsModel.js 2.84 KB
'use strict';


const Promise = require('bluebird');
const co = Promise.coroutine;

const UserData=require('./UserData');
const helpers = global.yoho.helpers;


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

const getCouponsList=(uid,type,page,limit)=>{
    return co(function*(){
        let couponsInfo= yield UserData.getCouponsList(uid,type,page,limit);
        let result=[];
        if(!couponsInfo.data.couponList){
            return result;
        }
        let coupons=couponsInfo.data.couponList;
        if(coupons){
            coupons.forEach(function(item,i){
                result[i]={};
                result[i].id=item.couponId;
                result[i].code=item.couponCode;
                // 格式化有效日期 "couponValidity": "2016.03.15-2016.03.31"
                let dates=item.couponValidity.split('-');


                result[i].beginTime=dates[0].replace('.','-');
                result[i].endTime=dates[1].replace('.','-');

                if(!item.couponImageUrl){
                    result[i].img='//static.yohobuy.com/images/v2/activity/default_coupon.jpg';
                }else{
                    result[i].img=item.couponImageUrl;
                }
                if((item.overTime-Date.now())<259200){
                    result[i].endSoon=true;
                }else{
                    result[i].endSoon=false;
                }
                result[i].value=item.couponValue.toFixed(2);
                result[i].validity=item.couponValidity;
                result[i].useRemark=item.couponDetailInfomation;



                if(type===USED){
                    result[i].orderNum=item.orderCode?item.orderCode:'';
                    result[i].orderDetailUrl=helpers.urlFormat('/home/orders/detail',{orderCode:item.orderCode||''});
                    result[i].orderSum=item.orderPrice.toFixed(2)?item.orderPrice:0;
                    result[i].payment=item.actuallyPaid.toFixed(2)?item.actuallyPaid:0;
                    let data=result[i].usedTime?moment(result[i].usedTime).format("YYYY-MM-DD"):0;
                    if(data){
                        result[i].useTime=new Date(date).getTime();
                    }else{
                        result[i].useTime='';
                    }
                }else{
                    result[i].status=item.isValidity?'可使用' : '已过期';
                }
                if(item.couponType){
                    result[i].type=item.couponType;
                    if(item.couponType==5){
                        result[i].value='免邮';
                    }
                }
            });
        }
        return {list:result,pager:{
            total:couponsInfo.data.total,
            pageTotal:couponsInfo.data.totalPageNum,
            page:page
        }};
    })();
}

module.exports = {
    getCouponsList,
    UNUSED,
    USED,
    INVALID
};