product_cache.js 2.99 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 _ from 'lodash';
import {
    ProductCache,
    TempProductImport
} from '../../models'

const r = new Router();

const productCache = {
    async query(ctx) {
        if (await TempProductImport.count({}) > 0) {
            await TempProductImport.remove({});
        }
        await ctx.render('action/product_cache');
    },
    async import(ctx) {
        if (await TempProductImport.count({}) > 0) {
            await TempProductImport.remove({});
        }
        let excelFile = ctx.request.body._files && ctx.request.body._files.excelFile;
        if (excelFile) {
            let sheets = xlsx.parse(fs.readFileSync(excelFile.path));
            let data = ProductCache.rendExcel(sheets);
            await TempProductImport.insert(data);
            let count = await TempProductImport.count({});
            ctx.body = {
                code: 200,
                count
            }
            return;
        }
        ctx.body = {
            code: 400
        }
    },
    async clear(ctx) {
        let type = ctx.request.body.type;
        if (await TempProductImport.count({}) > 0) {
            let data = await TempProductImport.findAll();
            if (type) {
                if (type === '1') {
                    ProductCache.removePriceCache(data)
                } else if (type === '2') {
                    ProductCache.removeProductCache(data)
                }
                ctx.body = {
                    code: 200
                }
                return;
            }
        } else {
            ProductCache._broadcast('请导入商品');
        }
        ctx.body = {
            code: 400
        }
    },
    async downloadProductUrl(ctx) {
        if (await TempProductImport.count({}) > 0) {
            let data = await TempProductImport.findAll();
            let productUrls = await ProductCache.generateProductUrl(data);
            let domains = {'PC': 'http://www.yohobuy.com', 'WAP': 'https://m.yohobuy.com'};
            let resultData = [];
            _.forEach(domains, (value, key) => {
                let rows = [['URL']];
                _.forEach(productUrls, url => {
                    rows.push([value + url]);
                });
                resultData.push({name: key, data: rows});
            });
            var buffer = xlsx.build(resultData);
            ctx.status = 200;
            ctx.set('Content-disposition', 'attachment; filename=output.xlsx');
            ctx.set('Content-type', 'application/vnd.ms-excel');
            ctx.body = buffer;
            return;
        } else {
            ProductCache._broadcast('请导入商品');
        }
        ctx.body = {
            code: 400
        }
    }
}

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

export default r;