import { Achievement } from '../../entity/platform/achievement.entity';
import { isArray } from 'lodash';
import { Provide } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Between, Equal, In, MoreThanOrEqual, Repository } from 'typeorm';
import { BaseServiceV2 } from '../../frame/BaseServiceV2';
@Provide()
export class AchievementService extends BaseServiceV2 {
  @InjectEntityModel(Achievement)
  model: Repository<Achievement>;

  getQueryColumnsOpera() {
    const obj = {
      name: this.Opera.Like,
      industry: this.Opera.In,
      field: this.Opera.In,
      sell: this.Opera.In,
      mature: this.Opera.In,
      area: this.Opera.Json,
      tags: this.Opera.Json,
    };
    return obj;
  }

  // 加浏览量
  async fetchBrowse(data) {
    const { num = 0, id } = data;
    await this.model.update({ id }, { num: num + 1 });
  }

  // 处理金钱
  async money(filter) {
    const { money, ...info } = filter;
    if (money) {
      if (!isArray(money)) {
        let money_title;
        if (money === '1') money_title = Equal('面议');
        if (money === '2') money_title = Between('1', '10');
        if (money === '3') money_title = Between('10', '20');
        if (money === '4') money_title = Between('20', '100');
        if (money === '5') money_title = Between('100', '500');
        if (money === '6') money_title = Between('500', '1000');
        if (money === '7') money_title = MoreThanOrEqual('1000');
        info.money = money_title;
      } else {
        const moneylist = [];
        for (const val of money) {
          if (val === '1') moneylist.push(Equal('面议'));
          if (val === '2') moneylist.push(Between('1', '10'));
          if (val === '3') moneylist.push(Between('10', '20'));
          if (val === '4') moneylist.push(Between('20', '100'));
          if (val === '5') moneylist.push(Between('100', '500'));
          if (val === '6') moneylist.push(Between('500', '1000'));
          if (val === '7') moneylist.push(MoreThanOrEqual('1000'));
        }
        info.money = In(moneylist);
      }
    }
    return info;
  }
}
