FavoriteModel.js 8.18 KB
'use strict';

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

const path=require('path');
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;

const FavoriteData=require('./FavoriteData');

const TABS=[
        {type:'product',name:'商品收藏'},
        {type:'brand',name:'品牌收藏'},
        {type:'article',name:'文章收藏'}
    ];

const getFavoriteTabs=type=>{
    type=type||'product';
    return TABS.map((item)=>{
        if(item.type===type){
            item.active=true;
        }
        item.url=helpers.urlFormat('/home/favorite',{type:item['type']});
        return item;
    })
}

const favoriteProductList=(uid,page,limit,type,sort,subscribe,reduction,promotion)=>{
    return co(function*(){
        let data={},product={},result={
            sort:{},reduction:{},filter:{},goods:{},pager:{}
        };
        product=yield FavoriteData.getFavoriteProductList(uid);
        if(product.data.category_list){
            result.sort= getSortInfo(product.data.category_list,sort);
        }
        result.reduction=yield redutionCount(uid);
        let productList=[];

        if(product.data.product_list){
            product.data.product_list.forEach(function(product){
                if(
                    (reduction==='Y'&&promotion=='Y'&&product.is_price_down=='Y'&&promotion=='Y')||
                    (sort&&product.category_id==sort)||
                    (subscribe&&product.is_subscribe_reduction=='Y')||
                    (reduction=='Y'&&product.is_price_down=='Y')||
                    (promotion=='Y'&&product.is_join_promotion=='Y')
                    ){
                    productList.push(product);
                }
            });
            productList=product.data.product_list;
        }
        if(reduction=='N'&&promotion=='N'){
            result.filter={
                reductionUrl:helpers.urlFormat('/home/favorite',{is_reduction:'Y'}),
                reductionChecked:'',
                activityUrl:helpers.urlFormat('/home/favorite',{is_promotion:'Y'}),
                activityChecked:''
            };
        }else
        if(reduction=='N'&&promotion=='Y'){
            result.filter={
                reductionUrl:helpers.urlFormat('/home/favorite',{is_reduction:'Y',is_promotion:'Y'}),
                reductionChecked:'',
                activityUrl:helpers.urlFormat('/home/favorite',{is_promotion:'Y'}),
                activityChecked:''
            };
        }else
        if(reduction=='Y'&&promotion=='N'){
            result.filter={
                reductionUrl:helpers.urlFormat('/home/favorite',{is_reduction:'Y',is_promotion:'Y'}),
                reductionChecked:'',
                activityUrl:helpers.urlFormat('/home/favorite',{is_promotion:'Y',is_promotion:'Y'}),
                activityChecked:''
            }
        }else{
            result.filter={
                reductionUrl:helpers.urlFormat('/home/favorite',{is_reduction:'Y',is_promotion:'Y'}),
                reductionChecked:'',
                activityUrl:helpers.urlFormat('/home/favorite',{is_promotion:'Y',is_promotion:'Y'}),
                activityChecked:''
            }
        }
        let total=productList;
        let pageTotal=Math.ceil(total/limit);
        result.pager=getPager(page,total,pageTotal);
        result.goods=getGoodsInfo(productList,page,limit);
        return result;
    })();
}

const favoriteBrandList=(uid,page,limit,type)=>{
    return co(function*(){
        let result={
            brands:{
                empty:'您没有收藏品牌',
                pager:{}
            }
        };
        let brand=yield FavoriteData.favoriteBrandData(uid,page,limit);
        if(!brand.data||!brand.data.page_total){
            return result;
        }
        if(brand.data.page_total<page){
            page=brand.data.page_total;
            brand=yield FavoriteData.favoriteBrandData(uid,page,limit);
        }
        if(!brand.data.brand_list){
            return result;
        }
        let brands=[];
        brand.data.brand_list.forEach((item,i)=>{
            brands.push({
                id:item.brand_id,
                brandOrShopType:item.brandOrShopType||'',
                shop_id:item.shop_id||'',
                img:helpers.image(item.brand_ico,100,100),
                url:helpers.urlFormat('',{shopId:item.shop_id||''},item.brand_domain),
                name:item.brand_name,
                naCount:item.new_product_num,
                colCount:item.brand_favorite_num
            });
        });
        result.brands=brands;
        let total=brand.data.total||0;
        let pageTotal=brand.data.page_total||0;
        page=brand.data.page||0;
        result.pager=getPager(page,total,pageTotal);
        return result;
    })();
}

const favoriteArticleList=(uid,udid,page,limit,type)=>{
 return co(function*(){
    let result={articles:[],pager:{}};
    let article=yield FavoriteData.favoriteArticleData(uid,udid,page,limit);
    if(!article.data&&!article.data.data){
        article.data.data.forEach((item,i)=>{
            result.articles.push({
                id:item.id,
                name:item.title,
                img:helpers.image(item.src,146,96),
                desc:item.intro,
                url:helpers.urlFormat('/'+item.id+'.html','','guang')
            });
        });

        let total=article.data.total||0;
        let pageTotal=article.data.totalPage||0;
        let page=article.data.page||0;

        result.pager=getPager(page,total,pageTotal);
    }else{
        result.articles={empty:'你尚未收藏任何文章!'};
    }
    return result;
 })();
}

const getPager=(page,total,totalPage,size,type)=>{
    let result={};
    if(page&&total&&totalPage){
        result={
            count:total,
            curPage:page,
            totalPages:totalPage,
            hasCheckAll:true
        };
    }
    return result;
}
const getGoodsInfo=(data,page,limit)=>{
    let result=[];
    let begin=(page-1)*limit;

    if(!data){
        data=data.slice(begin,limit);
        data.forEach((item,i)=>{
            let obj={
                skn:item.product_id,
                img:helpers.img(item.image,100,100),
                name:item.product_name,
                url:helpers.getUrlBySkc(item.product_id,item.goodsId,item.cnAlphabet),
                price:item.sales_price,
                priceDown:item.price_down,
                buyNow:helpers.getUrlBySkc(item.product_id,item.goodsId,item.cnAlphabet),
                soldOut:item.storage==0?true:'',
                hadNoticed:item.is_subscribe_reduction=='Y'?true:'',
                count:item.promotion_list?item.promotion_list.length:0
            }

            if(item.promotion_list){
                item.promotion_list.forEach(function(item1){
                    obj.activites.list.push({
                        type:item1.promotion_type,
                        name:item1.promotion_title
                    });
                });
            }
            result.push(obj);
        });
    }else{
        result={empty:'您没有收藏商品'};
    }
    return result;
}
const redutionCount=(uid)=>{
    return co(function*(){
        let result={count:0,url:'/home/favorite/reduction',phone:''};
        let data=yield FavoriteData.redutionCount(uid);
        if(data.data.num){
            result.count=+data.data.num;
            result.phone=data.data.mobile;
        }
        return result;
    })();
}
const getSortInfo=(categoryList,sort)=>{
    let result={default:{},all:[]};
    let defaultCategory={name:'全部',url:helpers.urlFormat('/home/favorite'),count:0,focus:''};
    categoryList.forEach(function(category){
        result.all.push({
            name:category.category_name,
            url:helpers.urlFormat('/home/favorite',{sort_id:category.category_id}),
            count:category.num,
            focus:category.category_id==sort?true:''
        });
        defaultCategory.count+=category.num;
        defaultCategory.focus=sort==0?true:'';
    });
    result.all.unshift(defaultCategory);
    result.default=result.all.slice(result.all,0,7);
    return result;
}


module.exports ={
    getFavoriteTabs,
    favoriteProductList,
    favoriteArticleList
}