import-compare-skn.js
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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,
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();
});