import { Chat } from '../../entity/platform/chat.entity';
// import { get } from 'lodash';
import { User } from '../../entity/system/user.entity';
import { Provide, App } from '@midwayjs/core';
import { Application } from '@midwayjs/koa';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService } from '../../frame/BaseService';
/**写不下去了。聊天问题让她自己弄吧 */
@Provide()
export class ChatService extends BaseService<Chat> {
  @InjectEntityModel(Chat)
  model: Repository<Chat>;

  @App('webSocket')
  wsApp: Application | any;

  @InjectEntityModel(User)
  uModel: Repository<User>;

  // 实时发送消息
  async createAfter(body) {
    // const sender = await this.uModel.findById(body.sender_id).lean();
    // const receiver = await this.uModel.findById(body.receiver_id).lean();
    // const data = { sender_url: get(sender, 'icon'), sender_name: get(sender, 'nick_name'), receiver_url: get(receiver, 'icon'), receiver_name: get(receiver, 'nick_name'), ...body };
    // this.wsApp.clients.forEach(ws => {
    //   ws.send(JSON.stringify(data));
    // });
  }

  // 聊天
  async chat(filter) {
    // const user = this.ctx.user;
    // const list = await this.model.aggregate([
    //   { $match: { ...filter, $or: [{ sender_id: get(user, '_id') }, { receiver_id: get(user, '_id') }] } },
    //   { $sort: { send_time: -1 } },
    //   { $group: { _id: { sender_id: '$sender_id', receiver_id: '$receiver_id' }, other: { $first: '$$ROOT' } } },
    //   { $replaceRoot: { newRoot: '$other' } },
    // ]);
    // const data = [];
    // for (const val of list) {
    //   if (val.sender_id !== get(user, '_id')) {
    //     const userData = await this.uModel.findById(val.sender_id).lean();
    //     data.push({
    //       _id: get(userData, '_id'),
    //       nick_name: get(userData, 'nick_name'),
    //       icon: get(userData, 'icon'),
    //       send_time: val.send_time,
    //       content: val.content,
    //       type: val.type,
    //       is_read: val.is_read,
    //     });
    //   }
    // }
    // return data;
  }
  // 聊天记录
  // async query(filter) {
  // const userInfo = this.ctx.user;
  // const { skip = 0, limit = 0, user, ...condition } = filter;
  // const list = await this.model
  //   .find({
  //     ...condition,
  //     ...{ $or: [{ $and: [{ sender_id: user }, { receiver_id: get(userInfo, '_id') }] }, { $and: [{ sender_id: get(userInfo, '_id') }, { receiver_id: user }] }] },
  //   })
  //   .skip(skip)
  //   .limit(limit)
  //   .sort({ send_time: 1 })
  //   .lean();
  // const data = [];
  // for (const val of list) {
  //   const sender = await this.uModel.findById(val.sender_id).lean();
  //   const receiver = await this.uModel.findById(val.receiver_id).lean();
  //   data.push({
  //     sender_url: get(sender, 'icon'),
  //     sender_name: get(sender, 'nick_name'),
  //     receiver_url: get(receiver, 'icon'),
  //     receiver_name: get(receiver, 'nick_name'),
  //     ...val,
  //   });
  // }
  // return data;
  // }

  // 该用户全部已读
  async read(filter) {
    // const { sender_id, receiver_id } = filter;
    // const list: any = await this.model.find({ $or: [{ $and: [{ sender_id }, { receiver_id }] }, { $and: [{ sender_id: receiver_id }, { receiver_id: sender_id }] }] }).lean();
    // const chat = list.map(i => {
    //   return i._id;
    // });
    // for (const val of chat) {
    //   await this.model.updateOne({ _id: val }, { is_read: '1' });
    // }
    // return 'ok';
  }
}
