hot.js 2.7 KB
'use strict';

const utils = '../../../utils';
const redis = require(`${utils}/redis`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const camelCase = global.yoho.camelCase;

class Hot extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    list(params, sort) {
        let data = {
            method: 'web.search.forseo',
            sales: params.sales,
            outlets: params.outlets,
            stocknumber: params.stocknumber,
            need_filter: params.need_filter,
            query: params.query,
            type: params.type,
            page: params.page || 1,
            limit: params.limit || 24,
            order: params.order || 0
        };

        if (sort) {
            delete data.query;
            data.sort = sort;
        }

        return this.get({
            data: data,
            param: {
                code: 200
            }
        });
    }

    getSearchKeywordDataById(params) {
        return redis.all([
            ['get', `global:yoho:seo:hot:keywords:id:${params.id}`]
        ]).then(redisData => {
            return redisData;
        });
    }

    index(params) {
        return Promise.all([
            this.getSearchKeywordDataById(params),
        ]).then(result => {
            let resu = {
                list: [],
                fuzzyWord: []
            };

            if (_.get(result, '[0][0]')) {
                let build = [];
                let redisData = JSON.parse(result[0][0]);

                return this.list(Object.assign(params, {query: redisData.name})).then(listData => {
                    let sort = redisData.sort_id;

                    if (!sort || _.get(listData, 'data.total') > 0) {
                        return listData;
                    }

                    return this.list(params, sort);
                }).then(listData => {
                    _.forEach(_.slice(redisData.data, 0, 12), value => {
                        build.push({
                            name: value.keyword,
                            link: helpers.urlFormat(`/mip/hot/${value.id}.html`, null)
                        });
                    });
                    resu.name = redisData.name;
                    resu.fuzzyWord = build;
                    if (_.get(listData, 'data.product_list')) {
                        resu.list = camelCase(listData.data.product_list);
                        _.forEach(resu.list, value => {
                            value.isSoonSoldOut = value.isSoonSoldOut === 'Y';
                        });
                    }
                    return resu;
                });
            } else {
                return resu;
            }
        });
    }
}

module.exports = Hot;