feat: 添加商品价格字段并优化模板分页查询

- 在product和wp_product实体中添加价格字段
- 为模板服务添加分页查询功能
- 更新模板控制器以支持分页参数
- 将Template实体添加到数据源
- 修改QueryBrandDTO的name字段为非必填
This commit is contained in:
tikkhun 2025-11-28 00:19:52 +08:00
parent 3545633f9e
commit 46cfaa24e7
7 changed files with 35 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { Inject, Controller, Get, Post, Put, Del, Body, Param } from '@midwayjs/core';
import { Inject, Controller, Get, Post, Put, Del, Body, Param, Query } from '@midwayjs/core';
import { TemplateService } from '../service/template.service';
import { successResponse, errorResponse } from '../utils/response.util';
import { CreateTemplateDTO, UpdateTemplateDTO } from '../dto/template.dto';
@ -20,17 +20,10 @@ export class TemplateController {
* @description
*/
@ApiOkResponse({ type: [Template], description: '成功获取模板列表' })
@Get('/')
async getTemplateList() {
try {
// 调用服务层获取列表
const data = await this.templateService.getTemplateList();
// 返回成功响应
return successResponse(data);
} catch (error) {
// 返回错误响应
return errorResponse(error.message);
}
@Get('/list')
async getTemplateList(@Query() params: any) {
// 调用服务层获取列表
return this.templateService.getTemplateList(params);
}
/**

View File

@ -34,6 +34,7 @@ import { Subscription } from '../entity/subscription.entity';
import { Site } from '../entity/site.entity';
import { Dict } from '../entity/dict.entity';
import { DictItem } from '../entity/dict_item.entity';
import { Template } from '../entity/template.entity';
const options: DataSourceOptions & SeederOptions = {
type: 'mysql',
@ -79,6 +80,7 @@ const options: DataSourceOptions & SeederOptions = {
Site,
Dict,
DictItem,
Template,
],
migrations: ['src/migration/*.ts'],
seeds: ['src/db/seeds/**/*.ts'],

View File

@ -61,6 +61,11 @@ export class CreateProductDTO {
@ApiProperty()
@Rule(RuleType.string())
humidity: string;
// 商品价格
@ApiProperty({ description: '价格', example: 99.99, required: false })
@Rule(RuleType.number())
price?: number;
}
/**
@ -132,7 +137,7 @@ export class QueryBrandDTO {
pageSize: number; // 每页大小
@ApiProperty({ example: 'ZYN', description: '关键字' })
@Rule(RuleType.string().required())
@Rule(RuleType.string())
name: string; // 搜索关键字(支持模糊查询)
}

View File

@ -42,6 +42,11 @@ export class Product {
@Column({ unique: true })
sku: string;
// 商品价格
@ApiProperty({ description: '价格', example: 99.99 })
@Column({ type: 'decimal', precision: 10, scale: 2, default: 0 })
price: number;
@ManyToMany(() => DictItem, {
cascade: true,
})

View File

@ -25,6 +25,13 @@ export class Template {
@Column('text',{nullable: true,comment: "描述"})
description?: string;
@ApiProperty({
example: true,
description: '是否可删除',
required: true,
})
@Column({ default: true })
deletable: boolean;
@ApiProperty({
example: '2022-12-12 11:11:11',
description: '创建时间',

View File

@ -39,7 +39,7 @@ export class WpProduct {
@Column()
externalProductId: string;
@ApiProperty({ description: 'sku', type: 'string' })
@ApiProperty({ description: '商店sku', type: 'string' })
@Column({ nullable: true })
sku?: string;

View File

@ -17,9 +17,14 @@ export class TemplateService {
*
* @returns {Promise<Template[]>}
*/
async getTemplateList(): Promise<Template[]> {
// 使用 find 方法查询所有模板
return this.templateModel.find();
async getTemplateList(params: { currentPage?: number, pageSize?: number } = {}): Promise<{ items: Template[], total: number }> {
const { currentPage = 1, pageSize = 10 } = params;
// 使用 findAndCount 方法查询所有模板
const [items, total] = await this.templateModel.findAndCount({
skip: (currentPage - 1) * pageSize,
take: pageSize,
});
return { items, total };
}
/**