import { Supply } from '../../entity/platform/supply.entity';
import { Collection } from '../../entity/platform/collection.entity';
import { User } from '../../entity/system/user.entity';
import { get, pick } from 'lodash';
import { Provide } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Equal, In, Repository } from 'typeorm';
import { BaseService } from '../../frame/BaseService';
@Provide()
export class SupplyService extends BaseService<Supply> {
  @InjectEntityModel(Supply)
  model: Repository<Supply>;
  @InjectEntityModel(Collection)
  cModel: Repository<Collection>;
  @InjectEntityModel(User)
  uModel: Repository<User>;
  // 需求列表
  async list(query) {
    // const { skip = 0, limit = 0, is_use, status, ...condition } = query;
    const skip = get(query, 'skip', 0);
    const limit = get(query, 'limit', 0);
    const is_use = get(query, 'is_use');
    const status = get(query, 'status');
    // const { one, two, thr, four } = condition;
    const one = get(query, 'one');
    const two = get(query, 'two');
    const thr = get(query, 'thr');
    const four = get(query, 'four');

    const whereObject: any = {};
    // if (is_use) whereObject.is_use = Equal(is_use);
    // if (status) whereObject.status = Equal(status);
    if (one) whereObject.field = In(one);
    if (two) whereObject.method = In(two);
    if (thr) whereObject.area = In(thr);
    if (four) whereObject.demand_status = In(four);
    const builder = this.model.createQueryBuilder().setFindOptions({ where: whereObject, skip, take: limit });
    const data = await builder.getMany();
    const total = await builder.getCount();
    // const info: any = { is_use, status };
    // if (one) info.field = { $in: one };
    // if (two) info.method = { $in: two };
    // if (thr) info.area = { $in: thr };
    // if (four) info.demand_status = { $in: four };
    // 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();
        const userData = await this.uModel.findOne({ where: { id: Equal(val.user) } });
        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.findById(id).lean();
    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 };
  }
}
