es.service.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Config, Provide } from '@midwayjs/core';
  2. import { get } from 'lodash';
  3. import { InjectEntityModel } from '@midwayjs/typeorm';
  4. import { Achievement } from '../../entity/platform/achievement.entity';
  5. import { Repository } from 'typeorm';
  6. import { Demand } from '../../entity/platform/demand.entity';
  7. import { Footplate } from '../../entity/platform/footplate.entity';
  8. import { Match } from '../../entity/platform/match.entity';
  9. import { Project } from '../../entity/platform/project.entity';
  10. import { Supply } from '../../entity/platform/supply.entity';
  11. import { Support } from '../../entity/platform/support.entity';
  12. import { Company } from '../../entity/users/company.entity';
  13. import { Expert } from '../../entity/users/expert.entity';
  14. import { Incubator } from '../../entity/users/incubator.entity';
  15. import { School } from '../../entity/users/school.entity';
  16. import { Unit } from '../../entity/users/unit.entity';
  17. import Axios from 'axios';
  18. @Provide()
  19. export class ESService {
  20. @InjectEntityModel(Achievement)
  21. achievement: Repository<Achievement>;
  22. @InjectEntityModel(Demand)
  23. demand: Repository<Demand>;
  24. @InjectEntityModel(Footplate)
  25. footplate: Repository<Footplate>;
  26. @InjectEntityModel(Match)
  27. match: Repository<Match>;
  28. @InjectEntityModel(Project)
  29. project: Repository<Project>;
  30. @InjectEntityModel(Supply)
  31. supply: Repository<Supply>;
  32. @InjectEntityModel(Support)
  33. support: Repository<Support>;
  34. @InjectEntityModel(Company)
  35. company: Repository<Company>;
  36. @InjectEntityModel(Expert)
  37. expert: Repository<Expert>;
  38. @InjectEntityModel(Incubator)
  39. incubator: Repository<Incubator>;
  40. @InjectEntityModel(School)
  41. school: Repository<School>;
  42. @InjectEntityModel(Unit)
  43. unit: Repository<Unit>;
  44. @Config('modulesConfig.es')
  45. esServiceHttpPrefix: any;
  46. /**数据步进量 */
  47. limit = 50;
  48. /**初始化ES */
  49. async initES() {
  50. const initIndexUrl = `${this.esServiceHttpPrefix}/init/indices`;
  51. const result = await Axios.post(initIndexUrl, {}, { responseType: 'json' });
  52. if (result.status !== 200) return;
  53. const tableList = get(result, 'data.data', []);
  54. if (!tableList) return;
  55. for (const index of tableList) {
  56. this.initData(index);
  57. }
  58. }
  59. /**
  60. * 按表,初始化es数据
  61. * 1.获取数据总量
  62. * 2.请求数据
  63. * 3.交由es服务写入
  64. * @param tableName 表名,全小写
  65. */
  66. async initData(tableName) {
  67. const initDataUrl = `${this.esServiceHttpPrefix}/init/data`;
  68. const model = this[tableName];
  69. const builder = model.createQueryBuilder();
  70. const total = await builder.getCount();
  71. let i = 0;
  72. while (i < total - 1) {
  73. try {
  74. const data = await builder.skip(i).take(this.limit).getMany();
  75. await Axios.post(initDataUrl, { index: tableName, data }, { responseType: 'json' });
  76. } catch (error) {
  77. console.error(error);
  78. } finally {
  79. i = i + this.limit;
  80. }
  81. }
  82. }
  83. }