Browse Source

修改合作伙伴

zs 8 months ago
parent
commit
4402621f09

+ 76 - 0
src/controller/platform/friend.controller.ts

@@ -0,0 +1,76 @@
+import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/core';
+import { BaseController } from '../../frame/BaseController';
+import { ApiQuery, ApiResponse, ApiTags } from '@midwayjs/swagger';
+import { Validate } from '@midwayjs/validate';
+import { omit, pick } from 'lodash';
+import { ErrorCode, ServiceError } from '../../error/service.error';
+import { CVO_friend, FVO_friend, QVO_friend, UVAO_friend } from '../../interface/platform/friend.interface';
+import { FriendService } from '../../service/platform/friend.service';
+import { Context } from '@midwayjs/koa';
+const namePrefix = '合作伙伴';
+@ApiTags(['合作伙伴'])
+@Controller('/friend', { tagName: namePrefix })
+export class FriendController implements BaseController {
+  @Inject()
+  service: FriendService;
+
+  @Inject()
+  ctx: Context;
+
+  @Get('/')
+  @ApiTags('列表查询')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: QVO_friend })
+  async index(@Query() query: object) {
+    const qobj = omit(query, ['skip', 'limit']);
+    const others = pick(query, ['skip', 'limit']);
+    const result = await this.service.query(qobj, others);
+    return result;
+  }
+
+  @Get('/list')
+  async list() {
+    const list = await this.service.list(this.ctx.query);
+    const data = list.data;
+    const total = list.total;
+    return { data, total };
+  }
+
+  @Get('/:id')
+  @ApiTags('单查询')
+  @ApiResponse({ type: FVO_friend })
+  async fetch(@Param('id') id: number) {
+    const data = await this.service.fetch({ id });
+    const result = new FVO_friend(data);
+    return result;
+  }
+
+  @Post('/', { routerName: `创建${namePrefix}` })
+  @ApiTags('创建数据')
+  @Validate()
+  @ApiResponse({ type: CVO_friend })
+  async create(@Body() data: object) {
+    const dbData = await this.service.create(data);
+    const result = new CVO_friend(dbData);
+    return result;
+  }
+
+  @Post('/:id', { routerName: `修改${namePrefix}` })
+  @ApiTags('修改数据')
+  @Validate()
+  @ApiResponse({ type: UVAO_friend })
+  async update(@Param('id') id: number, @Body() data: object) {
+    if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
+    const result = await this.service.update({ id }, data);
+    return result;
+  }
+
+  @Del('/:id', { routerName: `删除${namePrefix}` })
+  @ApiTags('删除数据')
+  @Validate()
+  async delete(@Param('id') id: number) {
+    if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
+    const result = await this.service.delete({ id });
+    return result;
+  }
+}

+ 18 - 0
src/entity/platform/friend.entity.ts

@@ -0,0 +1,18 @@
+import { Entity, Column } from 'typeorm';
+import { BaseModel } from '../../frame/BaseModel';
+
+@Entity('friend')
+export class Friend extends BaseModel {
+  @Column({ type: 'character varying', nullable: true, comment: '编码' })
+  code: string;
+  @Column({ type: 'character varying', nullable: true, comment: '名称' })
+  name: string;
+  @Column({ type: 'jsonb', nullable: true, comment: 'logo', default: [] })
+  file: Array<any>;
+  @Column({ type: 'character varying', nullable: true, comment: '简介' })
+  brief: string;
+  @Column({ type: 'character varying', nullable: true, comment: '上级编码' })
+  parent_code: string;
+  @Column({ type: 'character varying', comment: '是否启用', default: '0' })
+  is_use: string;
+}

+ 82 - 0
src/interface/platform/friend.interface.ts

@@ -0,0 +1,82 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import { ApiProperty } from '@midwayjs/swagger';
+import { dealVO } from '../../frame/VOBase';
+export class FVO_friend {
+  constructor(data: object) {
+    dealVO(this, data);
+  }
+  @ApiProperty({ description: '数据id' })
+  id: number = undefined;
+  @ApiProperty({ description: '编码' })
+  'code': string = undefined;
+  @ApiProperty({ description: '名称' })
+  'name': string = undefined;
+  @ApiProperty({ description: 'logo' })
+  'file': Array<any> = undefined;
+  @ApiProperty({ description: '简介' })
+  'brief': string = undefined;
+  @ApiProperty({ description: '上级编码' })
+  'parent_code': string = undefined;
+  @ApiProperty({ description: '是否使用' })
+  'is_use': string = undefined;
+}
+
+export class QDTO_friend {
+  @ApiProperty({ description: '编码' })
+  'code': string = undefined;
+  @ApiProperty({ description: '名称' })
+  'name': string = undefined;
+  @ApiProperty({ description: '上级编码' })
+  'parent_code': string = undefined;
+
+  @ApiProperty({ description: '是否使用' })
+  'is_use': string = undefined;
+}
+
+export class QVO_friend extends FVO_friend {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class CDTO_friend {
+  @ApiProperty({ description: '编码' })
+  @Rule(RuleType['string']().empty(''))
+  'code': string = undefined;
+  @ApiProperty({ description: '名称' })
+  @Rule(RuleType['string']().empty(''))
+  'name': string = undefined;
+  @ApiProperty({ description: 'logo' })
+  @Rule(RuleType['array']().empty(''))
+  'file': Array<any> = undefined;
+  @ApiProperty({ description: '简介' })
+  @Rule(RuleType['string']().empty(''))
+  'brief': string = undefined;
+  @ApiProperty({ description: '上级编码' })
+  @Rule(RuleType['string']().empty(''))
+  'parent_code': string = undefined;
+  @ApiProperty({ description: '是否使用' })
+  @Rule(RuleType['string']().empty(''))
+  'is_use': string = undefined;
+}
+
+export class CVO_friend extends FVO_friend {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class UDTO_friend extends CDTO_friend {
+  @ApiProperty({ description: '数据id' })
+  @Rule(RuleType['number']().empty(''))
+  id: number = undefined;
+}
+
+export class UVAO_friend extends FVO_friend {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}

+ 29 - 0
src/service/platform/friend.service.ts

@@ -0,0 +1,29 @@
+import { Provide } from '@midwayjs/core';
+import { InjectEntityModel } from '@midwayjs/typeorm';
+import { Friend } from '../../entity/platform/friend.entity';
+import { BaseServiceV2 } from '../../frame/BaseServiceV2';
+import { get, pick } from 'lodash';
+import { IsNull, Equal, Repository } from 'typeorm';
+@Provide()
+export class FriendService extends BaseServiceV2 {
+  @InjectEntityModel(Friend)
+  model: Repository<Friend>;
+
+  // 合作伙伴列表
+  async list(query) {
+    const { skip = 0, limit = 0, parent_code, ...info } = pick(query, ['skip', 'limit', 'code', 'parent_code']);
+    const whereObject: any = { parent_code, ...info };
+    if (!parent_code) whereObject.parent_code = IsNull();
+    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, 'code')) {
+        // 查询下一级
+        const region = await this.model.findOne({ where: { parent_code: Equal(val.code) } });
+        if (region) Object.assign(val, { hasChildren: true });
+      }
+    }
+    return { data, total };
+  }
+}

+ 4 - 0
src/service/util.service.ts

@@ -27,4 +27,8 @@ export class UtilService {
   supplyService: SupplyService;
   @Inject()
   companyService: CompanyService;
+
+  randomStr(len = 6) {
+    return Math.random().toString(36).slice(-len);
+  }
 }