|
@@ -0,0 +1,124 @@
|
|
|
+import { Body, Controller, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';
|
|
|
+import { BaseController, FrameworkErrorEnum, ServiceError } from 'free-midway-component';
|
|
|
+import { ChatRecordService } from '../service/chatRecord.service';
|
|
|
+import {
|
|
|
+ CreateDTO_chatRecord,
|
|
|
+ CreateVO_chatRecord,
|
|
|
+ FetchVO_chatRecord,
|
|
|
+ QueryDTO_chatRecord,
|
|
|
+ QueryVO_chatRecord,
|
|
|
+ ReadDTO,
|
|
|
+ UpdateDTO_chatRecord,
|
|
|
+ UpdateVO_chatRecord,
|
|
|
+} from '../interface/chatRecord.interface';
|
|
|
+import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
|
|
|
+import { Validate } from '@midwayjs/validate';
|
|
|
+import * as dayjs from 'dayjs';
|
|
|
+// import CustomParseFormat = require('dayjs/plugin/customParseFormat');
|
|
|
+// dayjs.extend(CustomParseFormat);
|
|
|
+import { RoomService } from '../service/room.service';
|
|
|
+import get = require('lodash/get');
|
|
|
+import { MqSender } from '../service/mq/mqSender.service';
|
|
|
+@ApiTags(['聊天记录'])
|
|
|
+@Controller('/chatRecord')
|
|
|
+export class ChatRecordController extends BaseController {
|
|
|
+ @Inject()
|
|
|
+ service: ChatRecordService;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ roomService: RoomService;
|
|
|
+
|
|
|
+ @Inject()
|
|
|
+ mqSender: MqSender;
|
|
|
+ @Post('/')
|
|
|
+ @Validate()
|
|
|
+ @ApiResponse({ type: CreateVO_chatRecord })
|
|
|
+ async create(@Body() data: CreateDTO_chatRecord) {
|
|
|
+ data.time = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ let result;
|
|
|
+ if (data.room) {
|
|
|
+ result = await this.service.create(data);
|
|
|
+ } else {
|
|
|
+ // 先创建room,再创建聊天记录
|
|
|
+ const { customer, shop, speaker } = data;
|
|
|
+ const roomData: any = {};
|
|
|
+ if (customer) {
|
|
|
+ roomData.customer = customer;
|
|
|
+ roomData.shop = speaker;
|
|
|
+ } else if (shop) {
|
|
|
+ roomData.customer = speaker;
|
|
|
+ roomData.shop = shop;
|
|
|
+ } else throw new ServiceError('缺少参数', FrameworkErrorEnum.BAD_BODY);
|
|
|
+ delete data.customer;
|
|
|
+ delete data.shop;
|
|
|
+ const roomId = await this.roomService.checkRoomIsExist(roomData);
|
|
|
+ if (roomId) {
|
|
|
+ data.room = roomId;
|
|
|
+ } else {
|
|
|
+ roomData.last_chat = data.content;
|
|
|
+ roomData.last_person = speaker;
|
|
|
+ roomData.last_time = data.time;
|
|
|
+ const roomDbData = await this.roomService.create(roomData);
|
|
|
+ if (!roomDbData) throw new ServiceError('房间创建失败', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
+ data.room = get(roomDbData, '_id');
|
|
|
+ }
|
|
|
+ result = await this.service.create(data);
|
|
|
+ }
|
|
|
+ // mq 发送消息
|
|
|
+ const room = await this.roomService.fetch(data.room);
|
|
|
+ let receiver;
|
|
|
+ if (get(room, 'customer') === get(data, 'speaker')) receiver = get(room, 'shop');
|
|
|
+ else receiver = get(room, 'customer');
|
|
|
+ await this.mqSender.toSendMsg(receiver, data);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ @Get('/')
|
|
|
+ @ApiQuery({ name: 'query' })
|
|
|
+ @ApiResponse({ type: QueryVO_chatRecord })
|
|
|
+ async query(@Query() filter: QueryDTO_chatRecord, @Query('skip') skip: number, @Query('limit') limit: number) {
|
|
|
+ const data = await this.service.query(filter, { skip, limit });
|
|
|
+ const total = await this.service.count(filter);
|
|
|
+ return { data, total };
|
|
|
+ }
|
|
|
+
|
|
|
+ @Post('/read')
|
|
|
+ @Validate()
|
|
|
+ async read(@Body() body: ReadDTO) {
|
|
|
+ await this.service.toRead(body.ids);
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ @Get('/:id')
|
|
|
+ @ApiResponse({ type: FetchVO_chatRecord })
|
|
|
+ async fetch(@Param('id') id: string) {
|
|
|
+ const data = await this.service.fetch(id);
|
|
|
+ const result = new FetchVO_chatRecord(data);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // @Post('/:id')
|
|
|
+ @Validate()
|
|
|
+ @ApiResponse({ type: UpdateVO_chatRecord })
|
|
|
+ async update(@Param('id') id: string, @Body() body: UpdateDTO_chatRecord) {
|
|
|
+ const result = await this.service.updateOne(id, body);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // @Del('/:id')
|
|
|
+ @Validate()
|
|
|
+ async delete(@Param('id') id: string) {
|
|
|
+ await this.service.delete(id);
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+ async createMany(...args: any[]) {
|
|
|
+ throw new Error('Method not implemented.');
|
|
|
+ }
|
|
|
+
|
|
|
+ async updateMany(...args: any[]) {
|
|
|
+ throw new Error('Method not implemented.');
|
|
|
+ }
|
|
|
+
|
|
|
+ async deleteMany(...args: any[]) {
|
|
|
+ throw new Error('Method not implemented.');
|
|
|
+ }
|
|
|
+}
|