import { Config, Provide } from '@midwayjs/core'; import { get } from 'lodash'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { Achievement } from '../../entity/platform/achievement.entity'; import { Repository } from 'typeorm'; import { Demand } from '../../entity/platform/demand.entity'; import { Footplate } from '../../entity/platform/footplate.entity'; import { Match } from '../../entity/platform/match.entity'; import { Project } from '../../entity/platform/project.entity'; import { Supply } from '../../entity/platform/supply.entity'; import { Support } from '../../entity/platform/support.entity'; import { Company } from '../../entity/users/company.entity'; import { Expert } from '../../entity/users/expert.entity'; import { Incubator } from '../../entity/users/incubator.entity'; import { School } from '../../entity/users/school.entity'; import { Unit } from '../../entity/users/unit.entity'; import Axios from 'axios'; @Provide() export class ESService { @InjectEntityModel(Achievement) achievement: Repository; @InjectEntityModel(Demand) demand: Repository; @InjectEntityModel(Footplate) footplate: Repository; @InjectEntityModel(Match) match: Repository; @InjectEntityModel(Project) project: Repository; @InjectEntityModel(Supply) supply: Repository; @InjectEntityModel(Support) support: Repository; @InjectEntityModel(Company) company: Repository; @InjectEntityModel(Expert) expert: Repository; @InjectEntityModel(Incubator) incubator: Repository; @InjectEntityModel(School) school: Repository; @InjectEntityModel(Unit) unit: Repository; @Config('modulesConfig.es') esServiceHttpPrefix: any; /**数据步进量 */ limit = 50; /**初始化ES */ async initES() { const initIndexUrl = `${this.esServiceHttpPrefix}/init/indices`; const result = await Axios.post(initIndexUrl, {}, { responseType: 'json' }); if (result.status !== 200) return; const tableList = get(result, 'data.data', []); if (!tableList) return; for (const index of tableList) { this.initData(index); } } /** * 按表,初始化es数据 * 1.获取数据总量 * 2.请求数据 * 3.交由es服务写入 * @param tableName 表名,全小写 */ async initData(tableName) { const initDataUrl = `${this.esServiceHttpPrefix}/init/data`; const model = this[tableName]; const builder = model.createQueryBuilder(); const total = await builder.getCount(); let i = 0; while (i < total - 1) { try { const data = await builder.skip(i).take(this.limit).getMany(); await Axios.post(initDataUrl, { index: tableName, data }, { responseType: 'json' }); } catch (error) { console.error(error); } finally { i = i + this.limit; } } } }