feat: 添加重复投标项目清理脚本并更新相关逻辑
更新依赖版本,移除调试日志,修改静态文件排除规则,将投标项目查重逻辑从URL改为标题,并添加清理重复投标项目的脚本
This commit is contained in:
@@ -48,7 +48,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
"@eslint/js": "^9.18.0",
|
"@eslint/js": "^9.18.0",
|
||||||
"@nestjs/cli": "^11.0.0",
|
"@nestjs/cli": "^11.0.14",
|
||||||
"@nestjs/schematics": "^11.0.0",
|
"@nestjs/schematics": "^11.0.0",
|
||||||
"@nestjs/testing": "^11.0.1",
|
"@nestjs/testing": "^11.0.1",
|
||||||
"@types/express": "^5.0.0",
|
"@types/express": "^5.0.0",
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export class AiService {
|
|||||||
|
|
||||||
投标项目标题列表:
|
投标项目标题列表:
|
||||||
${JSON.stringify(bids.map(b => b.title), null, 2)}`;
|
${JSON.stringify(bids.map(b => b.title), null, 2)}`;
|
||||||
this.logger.log('发给AI的内容',prompt);
|
// this.logger.log('发给AI的内容',prompt);
|
||||||
const completion = await this.openai.chat.completions.create({
|
const completion = await this.openai.chat.completions.create({
|
||||||
model: 'mimo-v2-flash-free',
|
model: 'mimo-v2-flash-free',
|
||||||
// max_tokens: 32768,
|
// max_tokens: 32768,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import { AiModule } from './ai/ai.module';
|
|||||||
ScheduleModule.forRoot(),
|
ScheduleModule.forRoot(),
|
||||||
ServeStaticModule.forRoot({
|
ServeStaticModule.forRoot({
|
||||||
rootPath: join(__dirname, '..', 'frontend', 'dist'),
|
rootPath: join(__dirname, '..', 'frontend', 'dist'),
|
||||||
exclude: ['/api/:path(*)'],
|
exclude: ['/api'],
|
||||||
}),
|
}),
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
DatabaseModule,
|
DatabaseModule,
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ export class BidsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createOrUpdate(data: Partial<BidItem>) {
|
async createOrUpdate(data: Partial<BidItem>) {
|
||||||
// Use URL or a hash of URL to check for duplicates
|
// Use title or a hash of title to check for duplicates
|
||||||
let item = await this.bidRepository.findOne({ where: { url: data.url } });
|
let item = await this.bidRepository.findOne({ where: { title: data.title } });
|
||||||
if (item) {
|
if (item) {
|
||||||
Object.assign(item, data);
|
Object.assign(item, data);
|
||||||
return this.bidRepository.save(item);
|
return this.bidRepository.save(item);
|
||||||
|
|||||||
77
src/scripts/remove-duplicates.ts
Normal file
77
src/scripts/remove-duplicates.ts
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import { NestFactory } from '@nestjs/core';
|
||||||
|
import { AppModule } from '../app.module';
|
||||||
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
import { BidItem } from '../bids/entities/bid-item.entity';
|
||||||
|
import { CustomLogger } from '../common/logger/logger.service';
|
||||||
|
|
||||||
|
async function removeDuplicates() {
|
||||||
|
const app = await NestFactory.createApplicationContext(AppModule);
|
||||||
|
|
||||||
|
// 设置自定义 logger
|
||||||
|
const logger = await app.resolve(CustomLogger);
|
||||||
|
app.useLogger(logger);
|
||||||
|
logger.setContext('RemoveDuplicatesScript');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 获取 BidItem 的 repository
|
||||||
|
const bidItemRepository = app.get<Repository<BidItem>>(getRepositoryToken(BidItem));
|
||||||
|
|
||||||
|
logger.log('开始查找重复的title...');
|
||||||
|
|
||||||
|
// 查找所有重复的title
|
||||||
|
const duplicates = await bidItemRepository
|
||||||
|
.createQueryBuilder('bid')
|
||||||
|
.select('bid.title', 'title')
|
||||||
|
.addSelect('COUNT(*)', 'count')
|
||||||
|
.groupBy('bid.title')
|
||||||
|
.having('COUNT(*) > 1')
|
||||||
|
.getRawMany();
|
||||||
|
|
||||||
|
if (duplicates.length === 0) {
|
||||||
|
logger.log('没有发现重复的title');
|
||||||
|
await app.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.log(`发现 ${duplicates.length} 个重复的title`);
|
||||||
|
|
||||||
|
let totalDeleted = 0;
|
||||||
|
|
||||||
|
// 对每个重复的title,只保留最晚创建的一条记录
|
||||||
|
for (const duplicate of duplicates) {
|
||||||
|
const title = duplicate.title;
|
||||||
|
const count = duplicate.count;
|
||||||
|
|
||||||
|
logger.log(`处理重复title: "${title}" (共 ${count} 条)`);
|
||||||
|
|
||||||
|
// 获取该title的所有记录,按创建时间降序排列
|
||||||
|
const items = await bidItemRepository
|
||||||
|
.createQueryBuilder('bid')
|
||||||
|
.where('bid.title = :title', { title })
|
||||||
|
.orderBy('bid.createdAt', 'DESC')
|
||||||
|
.getMany();
|
||||||
|
|
||||||
|
// 保留第一条(最晚创建的),删除其余的
|
||||||
|
const itemsToDelete = items.slice(1);
|
||||||
|
|
||||||
|
if (itemsToDelete.length > 0) {
|
||||||
|
const idsToDelete = itemsToDelete.map(item => item.id);
|
||||||
|
const deleteResult = await bidItemRepository.delete(idsToDelete);
|
||||||
|
totalDeleted += deleteResult.affected || 0;
|
||||||
|
logger.log(` 删除了 ${deleteResult.affected} 条重复记录,保留ID: ${items[0].id} (最晚创建)`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.log(`总共删除了 ${totalDeleted} 条重复记录`);
|
||||||
|
|
||||||
|
await app.close();
|
||||||
|
process.exit(0);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('删除重复记录失败:', error);
|
||||||
|
await app.close();
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
removeDuplicates();
|
||||||
Reference in New Issue
Block a user