import-compare-skn.js 3.96 KB
const path = require('path');
const readline = require('readline');
const {mysqlPool} = require('../libs/mysql');
const fs = require('fs');
const inquirer = require('inquirer');
let skns = require('../data/skns.json');

const date = process.argv[2];
const fileName = date.match(/--d=(.*)/)[1];

const fr = fs.createReadStream(path.join(__dirname, `./imports/${fileName}.csv`));
const rl = readline.createInterface({
  input: fr
});
const importProducts = [];
const failProduct = [];

const importDb = async(appendSkns, inx = 0, success = 0) => {
  if (appendSkns.length - 1 < inx) {
    return Promise.resolve(success);
  }
  const skn = appendSkns[inx];
  const storages = await mysqlPool.query('select id, size_id, product_id from storage A where product_id = :productId and not exists (select 1 from channel_sku_compare B where A.product_id=B.product_id and A.size_id=B.size_id)', {
    productId: skn.productId
  });
  //14321  and not exists (select 1 from channel_sku_compare B where A.product_id=B.product_id and A.size_id=B.size_id)
  const results = await Promise.all(storages.map(async sku => {
    return mysqlPool.insert('insert into `channel_sku_compare` (`product_id`, `sku`, `size_id`, `low_rate`, `high_rate`, `status`, `update_uid`, `channel_price`, `update_time`, `channel_url`) values (:productId, :sku, :sizeId, :lowRate, :highRate, :status, :updateUid, :channelPrice, :updateTime, :channelUrl)', {
      productId: skn.productId,
      sku: sku.id,
      sizeId: sku.size_id,
      lowRate: 0.05,
      highRate: 0.02,
      status: 0,
      updateUid: 0,
      channelPrice: 0,
      updateTime: 0,
      channelUrl: ''
    });
  }));
  success += results.filter(r => r).length;
  return importDb(appendSkns, inx + 1, success);
}

const importData = async() => {
  const appendSkns = importProducts.filter(p => !skns.some(skn => skn.yhId === p.productId));
  if (!appendSkns.length) {
    return console.log('没有需要新增的数据');
  }
  const repeatData = appendSkns.filter(p => skns.some(skn => skn.productId === p.id));

  console.log(repeatData.length)
  if (repeatData.length) {
    const {confirm} = await inquirer.prompt([
      {
        type: 'confirm',
        name: 'confirm',
        message: `数据映射错误,具有重复的毒ID${JSON.stringify(repeatData)},是否覆盖/退出?`
      }
    ]);
    if (confirm) {
      skns = skns.filter(skn => !repeatData.some(d => d.id === skn.productId));
    } else {
      return;
    }
  }
  const {confirm} = await inquirer.prompt([
    {
      type: 'confirm',
      name: 'confirm',
      message: `新增:${appendSkns.length},是否继续?`
    }
  ]);

  if (confirm) {
    const newSkns = skns.concat(appendSkns.map(p => {
      return {"productId": p.id, "yhId": p.productId}
    }));
    const fw = fs.createWriteStream(path.join(__dirname, '../data/skns.json'));

    fw.write(JSON.stringify(newSkns, null, 2));
    fw.end();
    const num = await importDb(appendSkns);

    console.log(`数据库导入:${num}`);
  }
}

const continueImport = async() => {
  const {confirm} = await inquirer.prompt([
    {
      type: 'list',
      name: 'confirm',
      choices: ['继续', '查看不符合内容', '取消'],
      message: `符合格式的行数:${importProducts.length},不符合格式的行数:${failProduct.length},是否继续?`
    }
  ]);

  if (confirm === '查看不符合内容') {
    console.log(failProduct);
    return continueImport();
  } else if (confirm === '取消') {
    return;
  }
  importData();
}

const REG_NUMBER = /^\d+$/;

rl.on('line', line => {
  const splits = line.split(',');
  if (splits.length === 3) {
    const [productId, model, id] = splits;

    if (productId) {
      if (REG_NUMBER.test(productId) && REG_NUMBER.test(id)) {
        importProducts.push({
          productId: +productId,
          id: +id
        });
      } else {
        failProduct.push(splits);
      }
    }
  } else {
    failProduct.push(splits);
  }

});

rl.on('close', async() => {
  continueImport();
});