import { Achievement } from '../../entity/platform/achievement.entity';
import { Collection } from '../../entity/platform/collection.entity';
import { User } from '../../entity/system/user.entity';
import { get, pick } from 'lodash';
import { BaseService } from '../../frame/BaseService';
import { Provide } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Between, Equal, In, MoreThanOrEqual, Repository } from 'typeorm';
@Provide()
export class AchievementService extends BaseService<Achievement> {
  @InjectEntityModel(Achievement)
  model: Repository<Achievement>;
  @InjectEntityModel(Collection)
  cModel: Repository<Collection>;
  @InjectEntityModel(User)
  uModel: Repository<User>;
  // 成果列表
  async list(query) {
    const { skip = 0, limit = 0, is_use, status, field, ...condition } = pick(query, ['skip', 'limit', 'is_use', 'field', 'status']);
    const { one, two, thr, four, five, six } = pick(query, ['one', 'two', 'thr', 'four', 'five', 'six']);
    const whereObject: any = { field };
    // if (is_use) whereObject.is_use = Equal(is_use);
    // if (status) whereObject.status = Equal(status);
    if (one) whereObject.technology = In(one);
    if (two) whereObject.field = In(two);
    if (thr) whereObject.mature = In(thr);
    if (four) whereObject.sell = In(four);
    if (five) {
      if (five === '1') whereObject.money = Equal('面议');
      if (five === '2') whereObject.money = Between('1', '10');
      if (five === '3') whereObject.money = Between('10', '20');
      if (five === '4') whereObject.money = Between('20', '100');
      if (five === '5') whereObject.money = Between('100', '500');
      if (five === '6') whereObject.money = Between('500', '1000');
      if (five === '7') whereObject.money = MoreThanOrEqual('1000');
    }
    if (six) whereObject.area = In(six);
    const builder = this.model.createQueryBuilder().setFindOptions({ where: whereObject, skip, take: limit });
    const data = await builder.getMany();
    const total = await builder.getCount();
    for (const val of data) {
      if (get(val, 'user')) {
        // 查询成果发布者
        const userData = await this.uModel.findOne({ where: { id: Equal(val.user) } });
        if (userData) Object.assign(val, { userName: userData.nick_name });
      }
    }
    return { data, total };
    // const info: any = { is_use, status };
    // if (one) info.technology = { $in: one };
    // if (two) info.field = { $in: two };
    // if (thr) info.mature = { $in: thr };
    // if (four) info.sell = { $in: four };
    // if (five) {
    //   if (five === '1') info.money = '面议';
    //   if (five === '2') info.money = { $gte: '1', $lte: '10' };
    //   if (five === '3') info.money = { $gte: '10', $lte: '20' };
    //   if (five === '4') info.money = { $gte: '20', $lte: '100' };
    //   if (five === '5') info.money = { $gte: '100', $lte: '500' };
    //   if (five === '6') info.money = { $gte: '500', $lte: '1000' };
    //   if (five === '7') info.money = { $gte: '1000' };
    // }
    // if (six) info.area = { $in: six };
    // const data = await this.model.find(info).skip(skip).limit(limit).lean();
    // for (const val of data) {
    //   if (get(val, 'user')) {
    //     // 查询成果发布者
    //     const userData = await this.uModel.findById(val.user).lean();
    //     if (userData) Object.assign(val, { userName: userData.nick_name });
    //   }
    // }
    // const total = await this.model.count(info);
    // return { data, total };
  }
  // 成果详情
  async detail(id) {
    const user = this.ctx.user;
    const data = { userInfo: {}, is_collection: false };
    const arr = await this.model.findOne({ where: { id: Equal(id) } });
    if (arr && get(arr, 'user')) {
      // 查询成果发布者
      // const userData = await this.uModel.findById(arr.user).lean();
      const userData = await this.uModel.findOne({ where: { id: Equal(arr.user) } });
      if (userData) data.userInfo = { name: userData.nick_name || '', phone: userData.phone || '' };
    }
    if (user && get(user, 'id')) {
      // 查询是否收藏该成果
      // const collection = await this.cModel.findOne({ user: user._id, source: arr.id }).lean();
      const collection = await this.cModel.findOne({ where: { user: Equal(user.id), source: Equal(arr.id) } });
      if (collection) data.is_collection = true;
    }
    return { ...arr, ...data };
  }
}
