import { Provide, Inject } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseSysAtlasEntity } from '../../entity/sys/atlas';
import { BaseSysLayersEntity } from '../../entity/sys/layers';
import { BaseService } from '@cool-midway/core';
import { Context } from '@midwayjs/koa';

/**
 * 描述
 */
@Provide()
export class BaseSysLayersService extends BaseService {
  @Inject()
  ctx: Context;

  @InjectEntityModel(BaseSysAtlasEntity)
  baseSysAtlasEntity: Repository<BaseSysAtlasEntity>;

  @InjectEntityModel(BaseSysLayersEntity)
  baseSysLayersEntity: Repository<BaseSysLayersEntity>;

  async delete(ids) {
    if (ids instanceof Array) {
      await this.baseSysLayersEntity.delete(ids);
      ids.forEach(id => {
        this.baseSysAtlasEntity.delete({ layerId: id });
      });
    } else {
      await this.baseSysLayersEntity.delete(ids.split(','));
      ids.split(',').forEach(id => {
        this.baseSysAtlasEntity.delete({ layerId: id });
      });
    }
  }

  async add(param) {
    if (param.parameterSet === '' || param.parameterSet === undefined) {
      param.parameterSet = null;
    } else {
      param.parameterSet = JSON.parse(param.parameterSet);
    }
    let querySet = {};
    querySet['queryType'] = param.mapQuery;
    querySet['querySource'] = param.querySetLi;
    querySet['queryField'] = param.queryField;
    querySet['displayField'] = param.displayField;
    querySet['queryFilter'] = param.queryFilter;
    param.querySet = querySet;
    this.baseSysLayersEntity.save(param);

    return {
      id:
        param instanceof Array
          ? param.map(e => {
              return e.id;
            })
          : param.id,
    };
  }

  async update(param) {
    if (param.parameterSet === '' || param.parameterSet === undefined) {
      param.parameterSet = null;
    } else {
      param.parameterSet = JSON.parse(param.parameterSet);
    }
    let querySet = {};
    querySet['queryType'] = param.mapQuery;
    querySet['querySource'] = param.querySetLi;
    querySet['queryField'] = param.queryField;
    querySet['displayField'] = param.displayField;
    querySet['queryFilter'] = param.queryFilter;
    param.querySet = querySet;
    this.baseSysLayersEntity.save(param);
  }

  /**
   * 根据ID获得信息
   * @param id
   */
  public async info(id) {
    const info = await this.baseSysLayersEntity.findOneBy({
      id,
    });
    if (info) {
      info.parameterSet =
        info.parameterSet != null ? JSON.stringify(info.parameterSet) : '';
      info.mapQuery = info.querySet != null ? info.querySet['queryType'] : '';
      info.querySetLi =
        info.querySet != null ? info.querySet['querySource'] : '';
      info.queryField =
        info.querySet != null ? info.querySet['queryField'] : '';
      info.displayField =
        info.querySet != null ? info.querySet['displayField'] : '';
      info.queryFilter =
        info.querySet != null ? info.querySet['queryFilter'] : '';
      return {
        ...info,
      };
    }
    return {};
  }

  /**
   * 获取ordernum
   * @returns
   */
  async getMaxOrderNum() {
    let num = 1;
    const orderNumEntity = await this.baseSysLayersEntity.find({
      order: { orderNum: 'DESC' },
    });
    if (orderNumEntity.length > 0) {
      num = orderNumEntity[0].orderNum + 1;
    }
    return num;
  }

  /**
   * 获取layerorder
   * @returns
   */
  async getMaxLayerOrder(layerCode) {
    let num = 1;
    const entity = await this.baseSysLayersEntity.find({
      where: { layerCode: layerCode },
      order: { layerOrder: 'DESC' },
    });
    if (entity.length > 0) {
      num = entity[0].layerOrder + 1;
    }
    return num;
  }
}
