xlsx-service.js 1.35 KB
/**
 * Created by TaoHuang on 2017/5/17.
 */
const Context = require('../framework/context');
const xlsx2json = require('xlsx2json');
const Api = require('../common/api');
const _ = require('lodash');

const xlsxRead = (fileName) => {
    return xlsx2json(fileName, {
        dataStartingRow: 2,
        mapping: {
            productSkn: 'A',
            productSku: 'B',
            number: 'c'
        }
    });
};

class XlsxService extends Context {
    constructor() {
        super();
        this.api = this.instance(Api);
    }

    handle(fileName) {
        return xlsxRead(fileName).then((jsonArray) => {
            let data = _.first(jsonArray);

            if (!data) {
                return {code: 500, message: '数据为空'};
            }

            if (data.length > 1000) {
                return {code: 500, message: '数据超出限制'};
            }

            data = data.filter(d => d.productSkn && d.productSku && d.number);

            return this.postJitStore(data);
        });
    }

    postJitStore(params) {
        let data = {
            body: {
                brandAuthData: null,
                authLevel: true,
                storageType: 3,
                virtualInventoryBos: params
            },
            path: '/erp/importJitStorage'
        };

        return Promise.resolve(data);
    }
}

module.exports = XlsxService;