product_cache.js 1.41 KB
/**
 *
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 16/10/19
 */

'use strict';

import Router from 'koa-router';
import xlsx from 'node-xlsx';
import fs from 'fs';
import {
    ProductCache
} from '../../models'

const r = new Router();

const productCache = {
    async query(ctx) {
        await ctx.render('action/product_cache');
    },
    async clear(ctx) {
        let excelFile = ctx.request.body._files && ctx.request.body._files.excelFile;
        let type = ctx.request.body.type;
        if (excelFile && type) {
            let sheets = xlsx.parse(fs.readFileSync(excelFile.path));
            if (sheets.length) {
                let rows = sheets[0].data;
                if (rows.length > 1) {
                    let data =  [];
                    for (var i = 1; i < rows.length; i++) {
                        var row = rows[i];
                        if (row.length) {
                            data.push(row[0]);
                        }
                    }
                    if (type === '1') {
                        ProductCache.removePriceCache(data)
                    } else if (type === '2') {
                        ProductCache.removeProductCache(data)
                    }
                    
                }
            }
        }
        ctx.body = {
            code: 200
        }
    }
}

r.get('/query', productCache.query);
r.post('/clear', productCache.clear);

export default r;