升级商城逻辑
This commit is contained in:
@@ -1,154 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* SQL 语法修复脚本:修复自动替换产生的 SQL 语法错误
|
||||
*/
|
||||
|
||||
class SQLSyntaxFixer {
|
||||
constructor() {
|
||||
this.filesToFix = [
|
||||
'services/matchingService.js',
|
||||
'routes/matchingAdmin.js',
|
||||
'routes/transfers.js',
|
||||
'routes/matching.js'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复单个文件中的 SQL 语法错误
|
||||
* @param {string} filePath - 文件路径
|
||||
*/
|
||||
async fixFile(filePath) {
|
||||
const fullPath = path.join(process.cwd(), filePath);
|
||||
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
console.log(`文件不存在: ${filePath}`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`正在修复文件: ${filePath}`);
|
||||
|
||||
let content = fs.readFileSync(fullPath, 'utf8');
|
||||
let originalContent = content;
|
||||
|
||||
// 1. 修复 WHERE source_type = 'allocation' FROM transfers 的错误顺序
|
||||
content = content.replace(
|
||||
/WHERE source_type = 'allocation' FROM transfers/g,
|
||||
"FROM transfers WHERE source_type = 'allocation'"
|
||||
);
|
||||
|
||||
// 2. 修复多个 WHERE 子句的问题
|
||||
content = content.replace(
|
||||
/FROM transfers WHERE source_type = 'allocation'([\s\S]*?)WHERE/g,
|
||||
"FROM transfers WHERE source_type = 'allocation'$1AND"
|
||||
);
|
||||
|
||||
// 3. 修复 INSERT 语句中的引号问题
|
||||
content = content.replace(
|
||||
/'allocation'/g,
|
||||
"'allocation'"
|
||||
);
|
||||
|
||||
// 4. 修复 JOIN 语句中的表别名问题
|
||||
content = content.replace(
|
||||
/FROM transfers oa WHERE oa\.source_type = 'allocation'/g,
|
||||
"FROM transfers oa WHERE oa.source_type = 'allocation'"
|
||||
);
|
||||
|
||||
// 5. 修复复杂查询中的语法问题
|
||||
content = this.fixComplexQueries(content, filePath);
|
||||
|
||||
if (content !== originalContent) {
|
||||
fs.writeFileSync(fullPath, content);
|
||||
console.log(`✓ 已修复: ${filePath}`);
|
||||
} else {
|
||||
console.log(`- 无需修复: ${filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复复杂查询
|
||||
* @param {string} content - 文件内容
|
||||
* @param {string} filePath - 文件路径
|
||||
* @returns {string} 修复后的内容
|
||||
*/
|
||||
fixComplexQueries(content, filePath) {
|
||||
if (filePath.includes('matchingService.js')) {
|
||||
// 修复 matchingService.js 中的特定查询
|
||||
|
||||
// 修复获取匹配目标的查询
|
||||
content = content.replace(
|
||||
/FROM transfers oa\s+WHERE oa\.source_type = 'allocation'\s+JOIN users u ON oa\.from_user_id = u\.id/g,
|
||||
"FROM transfers oa JOIN users u ON oa.from_user_id = u.id WHERE oa.source_type = 'allocation'"
|
||||
);
|
||||
|
||||
// 修复获取用户待处理分配的查询
|
||||
content = content.replace(
|
||||
/SELECT \* FROM transfers WHERE source_type = 'allocation' WHERE matching_order_id = \? ORDER BY cycle_number, created_at/g,
|
||||
"SELECT * FROM transfers WHERE source_type = 'allocation' AND matching_order_id = ? ORDER BY cycle_number, created_at"
|
||||
);
|
||||
|
||||
// 修复检查周期完成的查询
|
||||
content = content.replace(
|
||||
/SELECT COUNT\(\*\) as count FROM transfers WHERE source_type = 'allocation' WHERE matching_order_id = \? AND cycle_number = \? AND status = "pending"/g,
|
||||
"SELECT COUNT(*) as count FROM transfers WHERE source_type = 'allocation' AND matching_order_id = ? AND cycle_number = ? AND status = 'pending'"
|
||||
);
|
||||
}
|
||||
|
||||
if (filePath.includes('matchingAdmin.js')) {
|
||||
// 修复 matchingAdmin.js 中的查询
|
||||
content = content.replace(
|
||||
/FROM transfers oa WHERE oa\.source_type = 'allocation'\s+JOIN/g,
|
||||
"FROM transfers oa JOIN"
|
||||
);
|
||||
|
||||
// 在 JOIN 后添加 WHERE 条件
|
||||
content = content.replace(
|
||||
/(FROM transfers oa JOIN[\s\S]*?)WHERE(?!.*source_type)/g,
|
||||
"$1WHERE oa.source_type = 'allocation' AND"
|
||||
);
|
||||
}
|
||||
|
||||
if (filePath.includes('matching.js')) {
|
||||
// 修复 matching.js 中的查询
|
||||
content = content.replace(
|
||||
/LEFT JOIN transfers oa ON mo\.id = oa\.matching_order_id WHERE oa\.source_type = 'allocation'/g,
|
||||
"LEFT JOIN transfers oa ON mo.id = oa.matching_order_id AND oa.source_type = 'allocation'"
|
||||
);
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行所有文件的修复
|
||||
*/
|
||||
async fixAllFiles() {
|
||||
console.log('开始修复 SQL 语法错误...');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
for (const filePath of this.filesToFix) {
|
||||
try {
|
||||
await this.fixFile(filePath);
|
||||
} catch (error) {
|
||||
console.error(`修复文件 ${filePath} 失败:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n' + '=' .repeat(60));
|
||||
console.log('✓ SQL 语法修复完成!');
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const fixer = new SQLSyntaxFixer();
|
||||
await fixer.fixAllFiles();
|
||||
}
|
||||
|
||||
// 如果直接运行此脚本
|
||||
if (require.main === module) {
|
||||
main().catch(console.error);
|
||||
}
|
||||
|
||||
module.exports = SQLSyntaxFixer;
|
||||
@@ -1,133 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* 表别名修复脚本:修复 SQL 查询中的表别名问题
|
||||
*/
|
||||
|
||||
class TableAliasFixer {
|
||||
constructor() {
|
||||
this.filesToFix = [
|
||||
'services/matchingService.js',
|
||||
'routes/matchingAdmin.js',
|
||||
'routes/transfers.js',
|
||||
'routes/matching.js'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复单个文件中的表别名问题
|
||||
* @param {string} filePath - 文件路径
|
||||
*/
|
||||
async fixFile(filePath) {
|
||||
const fullPath = path.join(process.cwd(), filePath);
|
||||
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
console.log(`文件不存在: ${filePath}`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`正在修复文件: ${filePath}`);
|
||||
|
||||
let content = fs.readFileSync(fullPath, 'utf8');
|
||||
let originalContent = content;
|
||||
|
||||
// 1. 修复 "FROM transfers WHERE source_type = 'allocation' oa" 的问题
|
||||
content = content.replace(
|
||||
/FROM transfers WHERE source_type = 'allocation' (\w+)/g,
|
||||
"FROM transfers $1 WHERE $1.source_type = 'allocation'"
|
||||
);
|
||||
|
||||
// 2. 修复重复的 source_type 条件
|
||||
content = content.replace(
|
||||
/FROM transfers WHERE source_type = 'allocation' (\w+) AND \1\.source_type = 'allocation'/g,
|
||||
"FROM transfers $1 WHERE $1.source_type = 'allocation'"
|
||||
);
|
||||
|
||||
// 3. 修复 "FROM transfers WHERE source_type = 'allocation'" 后面直接跟其他子句的情况
|
||||
content = content.replace(
|
||||
/FROM transfers WHERE source_type = 'allocation'\s+(JOIN|ORDER|GROUP|LIMIT)/g,
|
||||
"FROM transfers WHERE source_type = 'allocation' $1"
|
||||
);
|
||||
|
||||
// 4. 修复子查询中的问题
|
||||
content = content.replace(
|
||||
/\(SELECT[^)]*FROM transfers WHERE source_type = 'allocation' (\w+)/g,
|
||||
(match, alias) => {
|
||||
return match.replace(
|
||||
`FROM transfers WHERE source_type = 'allocation' ${alias}`,
|
||||
`FROM transfers ${alias} WHERE ${alias}.source_type = 'allocation'`
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// 5. 修复特定的查询模式
|
||||
content = this.fixSpecificPatterns(content, filePath);
|
||||
|
||||
if (content !== originalContent) {
|
||||
fs.writeFileSync(fullPath, content);
|
||||
console.log(`✓ 已修复: ${filePath}`);
|
||||
} else {
|
||||
console.log(`- 无需修复: ${filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复特定的查询模式
|
||||
* @param {string} content - 文件内容
|
||||
* @param {string} filePath - 文件路径
|
||||
* @returns {string} 修复后的内容
|
||||
*/
|
||||
fixSpecificPatterns(content, filePath) {
|
||||
// 修复 SELECT 语句中的表别名问题
|
||||
content = content.replace(
|
||||
/SELECT ([^F]*?) FROM transfers WHERE source_type = 'allocation' (\w+)/g,
|
||||
"SELECT $1 FROM transfers $2 WHERE $2.source_type = 'allocation'"
|
||||
);
|
||||
|
||||
// 修复 UPDATE 语句
|
||||
content = content.replace(
|
||||
/UPDATE transfers WHERE source_type = 'allocation' SET/g,
|
||||
"UPDATE transfers SET"
|
||||
);
|
||||
|
||||
// 修复 WHERE 子句中的条件
|
||||
content = content.replace(
|
||||
/WHERE source_type = 'allocation' AND (\w+)\./g,
|
||||
"WHERE $1.source_type = 'allocation' AND $1."
|
||||
);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行所有文件的修复
|
||||
*/
|
||||
async fixAllFiles() {
|
||||
console.log('开始修复表别名问题...');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
for (const filePath of this.filesToFix) {
|
||||
try {
|
||||
await this.fixFile(filePath);
|
||||
} catch (error) {
|
||||
console.error(`修复文件 ${filePath} 失败:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n' + '=' .repeat(60));
|
||||
console.log('✓ 表别名修复完成!');
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const fixer = new TableAliasFixer();
|
||||
await fixer.fixAllFiles();
|
||||
}
|
||||
|
||||
// 如果直接运行此脚本
|
||||
if (require.main === module) {
|
||||
main().catch(console.error);
|
||||
}
|
||||
|
||||
module.exports = TableAliasFixer;
|
||||
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 };
|
||||
14625
scripts/pca-code.json
Normal file
14625
scripts/pca-code.json
Normal file
File diff suppressed because it is too large
Load Diff
37
scripts/verify_data.js
Normal file
37
scripts/verify_data.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const { initDB, getDB } = require('../database');
|
||||
|
||||
async function verifyData() {
|
||||
try {
|
||||
await initDB();
|
||||
|
||||
// 检查省份数据
|
||||
const [provinces] = await getDB().query('SELECT code, name FROM china_regions WHERE level = 1 ORDER BY code LIMIT 10');
|
||||
console.log('省份数据样本:');
|
||||
provinces.forEach(p => console.log(` ${p.code} - ${p.name}`));
|
||||
|
||||
// 检查城市数据
|
||||
const [cities] = await getDB().query('SELECT code, name, parent_code FROM china_regions WHERE level = 2 ORDER BY code LIMIT 10');
|
||||
console.log('\n城市数据样本:');
|
||||
cities.forEach(c => console.log(` ${c.code} - ${c.name} (${c.parent_code})`));
|
||||
|
||||
// 检查区县数据
|
||||
const [districts] = await getDB().query('SELECT code, name, parent_code FROM china_regions WHERE level = 3 ORDER BY code LIMIT 10');
|
||||
console.log('\n区县数据样本:');
|
||||
districts.forEach(d => console.log(` ${d.code} - ${d.name} (${d.parent_code})`));
|
||||
|
||||
// 统计各级别数量
|
||||
const [stats] = await getDB().query('SELECT level, COUNT(*) as count FROM china_regions GROUP BY level ORDER BY level');
|
||||
console.log('\n各级别统计:');
|
||||
stats.forEach(row => {
|
||||
const levelName = row.level === 1 ? '省份' : row.level === 2 ? '城市' : '区县';
|
||||
console.log(` ${levelName}(level ${row.level}): ${row.count} 个`);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('验证失败:', error);
|
||||
} finally {
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
|
||||
verifyData();
|
||||
Reference in New Issue
Block a user