Browse Source

修改聊天记录

zs 1 year ago
parent
commit
2fd3f3f087

+ 75 - 0
src/controller/core/chat.controller.ts

@@ -0,0 +1,75 @@
+import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';
+import { BaseController } from 'free-midway-component';
+import { ChatService } from '../../service/core/chat.service';
+import { CDTO_chat, CVO_chat, FVO_chat, QDTO_chat, QVO_chat, UDTO_chat, UVAO_chat } from '../../interface/core/chat.interface';
+import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
+import { Validate } from '@midwayjs/validate';
+@ApiTags(['聊天记录表'])
+@Controller('/chat')
+export class ChatController extends BaseController {
+  @Inject()
+  service: ChatService;
+
+  @Post('/')
+  @Validate()
+  @ApiResponse({ type: CVO_chat })
+  async create(@Body() data: CDTO_chat) {
+    const dbData = await this.service.create(data);
+    const result = new CVO_chat(dbData);
+    return result;
+  }
+  @Get('/')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: QVO_chat })
+  async query(@Query() filter: QDTO_chat, @Query('skip') skip: number, @Query('limit') limit: number) {
+    const list = await this.service.query(filter, { skip, limit });
+    const data = [];
+    for (const i of list) {
+      const newData = new QVO_chat(i);
+      data.push(newData);
+    }
+    const total = await this.service.count(filter);
+    return { data, total };
+  }
+
+  @Get('/read')
+  @ApiQuery({ name: 'query' })
+  async user(@Query() filter: any) {
+    const result = await this.service.read(filter);
+    return result;
+  }
+
+  @Get('/:id')
+  @ApiResponse({ type: FVO_chat })
+  async fetch(@Param('id') id: string) {
+    const data = await this.service.fetch(id);
+    const result = new FVO_chat(data);
+    return result;
+  }
+
+  @Post('/:id')
+  @Validate()
+  @ApiResponse({ type: UVAO_chat })
+  async update(@Param('id') id: string, @Body() body: UDTO_chat) {
+    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.');
+  }
+}

+ 21 - 0
src/entity/core/chat.entity.ts

@@ -0,0 +1,21 @@
+import { modelOptions, prop } from '@typegoose/typegoose';
+import { BaseModel } from 'free-midway-component';
+@modelOptions({
+  schemaOptions: { collection: 'chat' },
+})
+export class Chat extends BaseModel {
+  @prop({ required: false, index: true, zh: '用户' })
+  user: string;
+  @prop({ required: false, index: false, zh: '业务员' })
+  admin: string;
+  @prop({ required: false, index: false, zh: '发言人' })
+  speaker: string;
+  @prop({ required: false, index: false, zh: '内容' })
+  content: string;
+  @prop({ required: false, index: false, zh: '创建时间' })
+  create_time: string;
+  @prop({ required: false, index: false, zh: '类型', default: '0' })
+  type: string;
+  @prop({ required: false, index: false, zh: '是否已读', default: '0' })
+  is_read: string;
+}

+ 93 - 0
src/interface/core/chat.interface.ts

@@ -0,0 +1,93 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import { ApiProperty } from '@midwayjs/swagger';
+import { SearchBase } from 'free-midway-component';
+import get = require('lodash/get');
+const dealVO = (cla, data) => {
+  for (const key in cla) {
+    const val = get(data, key);
+    if (val || val === 0) cla[key] = val;
+  }
+};
+export class FVO_chat {
+  constructor(data: object) {
+    dealVO(this, data);
+  }
+  @ApiProperty({ description: '数据id' })
+  _id: string = undefined;
+  @ApiProperty({ description: '用户' })
+  'user': string = undefined;
+  @ApiProperty({ description: '业务员' })
+  'admin': string = undefined;
+  @ApiProperty({ description: '发言人' })
+  'speaker': string = undefined;
+  @ApiProperty({ description: '内容' })
+  'content': string = undefined;
+  @ApiProperty({ description: '创建时间' })
+  'create_time': string = undefined;
+  @ApiProperty({ description: '类型' })
+  'type': string = undefined;
+  @ApiProperty({ description: '是否已读' })
+  'is_read': string = undefined;
+}
+
+export class QDTO_chat extends SearchBase {
+  constructor() {
+    const like_prop = [];
+    const props = ['user'];
+    const mapping = [];
+    super({ like_prop, props, mapping });
+  }
+  @ApiProperty({ description: '用户' })
+  'user': string = undefined;
+}
+
+export class QVO_chat extends FVO_chat {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class CDTO_chat {
+  @ApiProperty({ description: '用户' })
+  @Rule(RuleType['string']().empty(''))
+  'user': string = undefined;
+  @ApiProperty({ description: '业务员' })
+  @Rule(RuleType['string']().empty(''))
+  'admin': string = undefined;
+  @ApiProperty({ description: '发言人' })
+  @Rule(RuleType['string']().empty(''))
+  'speaker': string = undefined;
+  @ApiProperty({ description: '内容' })
+  @Rule(RuleType['string']().empty(''))
+  'content': string = undefined;
+  @ApiProperty({ description: '创建时间' })
+  @Rule(RuleType['string']().empty(''))
+  'create_time': string = undefined;
+  @ApiProperty({ description: '类型' })
+  @Rule(RuleType['string']().empty(''))
+  'type': string = undefined;
+  @ApiProperty({ description: '是否已读' })
+  @Rule(RuleType['string']().empty(''))
+  'is_read': string = undefined;
+}
+
+export class CVO_chat extends FVO_chat {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class UDTO_chat extends CDTO_chat {
+  @ApiProperty({ description: '数据id' })
+  @Rule(RuleType['string']().empty(''))
+  _id: string = undefined;
+}
+
+export class UVAO_chat extends FVO_chat {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}

+ 25 - 0
src/service/core/chat.service.ts

@@ -0,0 +1,25 @@
+import { Provide } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import { FrameworkErrorEnum, ServiceError, BaseService } from 'free-midway-component';
+import { Chat } from '../../entity/core/chat.entity';
+type modelType = ReturnModelType<typeof Chat>;
+@Provide()
+export class ChatService extends BaseService<modelType> {
+  @InjectEntityModel(Chat)
+  model: modelType;
+
+  // 该用户全部已读
+  async read(filter) {
+    const { user } = filter;
+    if (!user) throw new ServiceError('没有用户信息!', FrameworkErrorEnum.NOT_FOUND_DATA);
+    const list: any = await this.model.find({ user, speaker: user, is_read: '0' }).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';
+  }
+}