升级商城逻辑
This commit is contained in:
144
scripts/import_china_regions.js
Normal file
144
scripts/import_china_regions.js
Normal file
@@ -0,0 +1,144 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const mysql = require('mysql2/promise');
|
||||
require('dotenv').config();
|
||||
|
||||
// 数据库配置
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST || '114.55.111.44',
|
||||
user: process.env.DB_USER || 'maov2',
|
||||
password: process.env.DB_PASSWORD || '5fYhw8z6T62b7heS',
|
||||
database: process.env.DB_NAME || 'maov2',
|
||||
};
|
||||
|
||||
// 初始化数据库连接
|
||||
async function initDB() {
|
||||
try {
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
console.log('数据库连接成功');
|
||||
return connection;
|
||||
} catch (error) {
|
||||
console.error('数据库连接失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 递归解析省市区数据
|
||||
function parseRegionData(regions, parentCode = null, level = 1) {
|
||||
const result = [];
|
||||
let sortOrder = 1;
|
||||
|
||||
for (const region of regions) {
|
||||
// 添加当前区域
|
||||
result.push({
|
||||
code: region.code,
|
||||
name: region.name,
|
||||
parent_code: parentCode,
|
||||
level: level,
|
||||
sort_order: sortOrder++
|
||||
});
|
||||
|
||||
// 递归处理子区域
|
||||
if (region.children && region.children.length > 0) {
|
||||
const childrenData = parseRegionData(region.children, region.code, level + 1);
|
||||
result.push(...childrenData);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 导入省市区数据
|
||||
async function importChinaRegions() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
// 读取 JSON 数据文件
|
||||
const jsonFilePath = path.join(__dirname, 'pca-code.json');
|
||||
const jsonData = fs.readFileSync(jsonFilePath, 'utf8');
|
||||
const regionsData = JSON.parse(jsonData);
|
||||
|
||||
console.log('成功读取省市区数据文件');
|
||||
|
||||
// 解析数据
|
||||
const parsedData = parseRegionData(regionsData);
|
||||
console.log(`解析完成,共 ${parsedData.length} 条记录`);
|
||||
|
||||
// 连接数据库
|
||||
connection = await initDB();
|
||||
|
||||
// 清空现有数据
|
||||
await connection.execute('DELETE FROM china_regions');
|
||||
console.log('已清空现有数据');
|
||||
|
||||
// 批量插入数据
|
||||
const batchSize = 100;
|
||||
let insertedCount = 0;
|
||||
|
||||
for (let i = 0; i < parsedData.length; i += batchSize) {
|
||||
const batch = parsedData.slice(i, i + batchSize);
|
||||
const values = batch.map(item => [
|
||||
item.code,
|
||||
item.name,
|
||||
item.parent_code,
|
||||
item.level,
|
||||
item.sort_order
|
||||
]);
|
||||
|
||||
const placeholders = values.map(() => '(?, ?, ?, ?, ?)').join(', ');
|
||||
const flatValues = values.flat();
|
||||
|
||||
await connection.execute(
|
||||
`INSERT INTO china_regions (code, name, parent_code, level, sort_order) VALUES ${placeholders}`,
|
||||
flatValues
|
||||
);
|
||||
|
||||
insertedCount += batch.length;
|
||||
console.log(`已插入 ${insertedCount}/${parsedData.length} 条记录`);
|
||||
}
|
||||
|
||||
// 统计导入结果
|
||||
const [provinceResult] = await connection.execute(
|
||||
'SELECT COUNT(*) as count FROM china_regions WHERE level = 1'
|
||||
);
|
||||
const [cityResult] = await connection.execute(
|
||||
'SELECT COUNT(*) as count FROM china_regions WHERE level = 2'
|
||||
);
|
||||
const [districtResult] = await connection.execute(
|
||||
'SELECT COUNT(*) as count FROM china_regions WHERE level = 3'
|
||||
);
|
||||
const [totalResult] = await connection.execute(
|
||||
'SELECT COUNT(*) as count FROM china_regions'
|
||||
);
|
||||
|
||||
console.log('\n=== 导入完成 ===');
|
||||
console.log(`省份数量: ${provinceResult[0].count}`);
|
||||
console.log(`城市数量: ${cityResult[0].count}`);
|
||||
console.log(`区县数量: ${districtResult[0].count}`);
|
||||
console.log(`总记录数: ${totalResult[0].count}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('导入失败:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('数据库连接已关闭');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果直接运行此脚本
|
||||
if (require.main === module) {
|
||||
importChinaRegions()
|
||||
.then(() => {
|
||||
console.log('省市区数据导入成功!');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('导入过程中发生错误:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { importChinaRegions };
|
||||
Reference in New Issue
Block a user