group-filter-process.js 3.51 KB
const _ = require('lodash');

/**
 * 处理筛选数据
 * @param list
 * @param  string | options
 * @return array 处理之后的筛选数据
 */
exports.processFilter = (list, options) => {
    const filters = {
        classify: []
    };

    const filtersType = {
        brand: {
            name: '所有品牌',
            title: '品牌',
            dataId: 'id',
            subsName: 'brand_name',
            firstSub: 0,
            dataType: 'brand',
            sortNum: '1'
        },
        color: {
            name: '所有颜色',
            title: '颜色',
            dataId: 'color_id',
            subsName: 'color_name',
            firstSub: 0,
            dataType: 'color',
            sortNum: '4'
        },
        discount: {
            name: '所有商品',
            title: '折扣',
            dataId: 'key',
            subsName: 'name',
            firstSub: '0,1',
            dataType: 'p_d',
            sortNum: '7'
        },
        gender: {
            name: '所有性别',
            title: '性别',
            dataId: 'key',
            subsName: 'flag',
            firstSub: '1,2,3',
            dataType: 'gender',
            sortNum: '0'
        },
        sort: {
            name: '所有品类',
            title: '品类',
            dataId: 'relation_parameter',
            subsName: 'category_name',
            firstSub: 0,
            dataType: 'sort',
            sortNum: '3'
        },
        priceRange: {
            name: '所有价格',
            title: '价格',
            dataId: 'key',
            subsName: 'flag',
            firstSub: 0,
            dataType: 'price',
            sortNum: '6'
        },
        size: {
            name: '所有尺码',
            title: '尺码',
            dataId: 'size_id',
            subsName: 'size_name',
            firstSub: 0,
            dataType: 'size',
            sortNum: '5'
        },
        age_level: {
            name: '所有人群',
            title: '人群',
            dataId: 'id',
            subsName: 'name',
            firstSub: 0,
            dataType: 'ageLevel',
            sortNum: '2'
        }
    };

    options = Object.assign({
        gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
        exclude: null // 需要排除的字段
    }, options);

    _.forEach(list, (item) => {
        let classify = {
            subs: []
        };
        let key = item.filterId;

        if (!filtersType[key]) {
            return;
        }

        classify.dataType = filtersType[key].dataType;
        classify.name = filtersType[key].name;
        classify.title = filtersType[key].title;

        classify.subs.push({
            chosed: true,
            dataId: filtersType[key].firstSub,
            name: filtersType[key].name
        });

        // 测试数据
        // let itemList = [];

        // for (let i = 0; i < 12; i++) {
        //     itemList.push({
        //         itemId: '1,3',
        //         itemName: 'MEN'
        //     });
        // }

        let itemList = item.allItemList || item.itemList;

        _.forEach(itemList, (sub) => {
            let subs = {};

            subs.dataId = sub.itemId;
            subs.name = sub.itemName;

            if (options[classify.dataType] + '' === subs.dataId + '') {
                subs.chosed = true;
                classify.subs[0].chosed = false;
            }

            classify.subs.push(subs);
        });
        filters.classify[filtersType[key].sortNum] = classify;
    });
    return filters;
};