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<Achievement>;
  @InjectEntityModel(Demand)
  demand: Repository<Demand>;
  @InjectEntityModel(Footplate)
  footplate: Repository<Footplate>;
  @InjectEntityModel(Match)
  match: Repository<Match>;
  @InjectEntityModel(Project)
  project: Repository<Project>;
  @InjectEntityModel(Supply)
  supply: Repository<Supply>;
  @InjectEntityModel(Support)
  support: Repository<Support>;
  @InjectEntityModel(Company)
  company: Repository<Company>;
  @InjectEntityModel(Expert)
  expert: Repository<Expert>;
  @InjectEntityModel(Incubator)
  incubator: Repository<Incubator>;
  @InjectEntityModel(School)
  school: Repository<School>;
  @InjectEntityModel(Unit)
  unit: Repository<Unit>;
  @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');
    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;
      }
    }
  }
}
