API/src/dto/product.dto.ts

187 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ApiProperty } from '@midwayjs/swagger';
import { Rule, RuleType } from '@midwayjs/validate';
/**
* 属性输入DTO
*/
export class AttributeInputDTO {
@ApiProperty({ description: '属性字典标识', example: 'brand' })
@Rule(RuleType.string())
dictName?: string;
@ApiProperty({ description: '属性值', example: 'ZYN' })
@Rule(RuleType.string())
value?: string;
@ApiProperty({ description: '属性ID', example: 1 })
@Rule(RuleType.number())
id?: number;
@ApiProperty({ description: '属性名称', example: 'ZYN' })
@Rule(RuleType.string())
name?: string;
@ApiProperty({ description: '属性显示名称', example: 'ZYN' })
@Rule(RuleType.string())
title?: string;
}
/**
* DTO 用于创建产品
*/
export class CreateProductDTO {
@ApiProperty({
example: 'ZYN 6MG WINTERGREEN',
description: '产品名称',
required: true,
})
@Rule(RuleType.string().required().empty({ message: '产品名称不能为空' }))
name: string;
@ApiProperty({ example: '产品描述', description: '产品描述' })
@Rule(RuleType.string())
description: string;
@ApiProperty({ description: '产品 SKU', required: false })
@Rule(RuleType.string())
sku?: string;
@ApiProperty({ description: '分类ID (DictItem ID)', required: false })
@Rule(RuleType.number())
categoryId?: number;
// 通用属性输入(中文注释:通过 attributes 统一提交品牌/口味/强度/尺寸/干湿等)
@ApiProperty({ description: '属性列表', type: 'array' })
@Rule(RuleType.array().required())
attributes: AttributeInputDTO[];
// 商品价格
@ApiProperty({ description: '价格', example: 99.99, required: false })
@Rule(RuleType.number())
price?: number;
// 促销价格
@ApiProperty({ description: '促销价格', example: 99.99, required: false })
@Rule(RuleType.number())
promotionPrice?: number;
// 中文注释:商品类型(默认 simplebundle 需手动设置组成)
@ApiProperty({ description: '商品类型', enum: ['simple', 'bundle'], default: 'simple', required: false })
@Rule(RuleType.string().valid('simple', 'bundle').default('simple'))
type?: string;
// 中文注释:仅当 type 为 'bundle' 时,才需要提供 components
@ApiProperty({ description: '产品组成', type: 'array', required: false })
@Rule(
RuleType.array()
.items(
RuleType.object({
sku: RuleType.string().required(),
quantity: RuleType.number().required(),
})
)
.when('type', {
is: 'bundle',
then: RuleType.array().required(),
})
)
components?: { sku: string; quantity: number }[];
}
/**
* DTO 用于更新产品
*/
export class UpdateProductDTO {
@ApiProperty({ example: 'ZYN 6MG WINTERGREEN', description: '产品名称' })
@Rule(RuleType.string())
name?: string;
@ApiProperty({ example: '产品描述', description: '产品描述' })
@Rule(RuleType.string())
description?: string;
@ApiProperty({ description: '产品 SKU', required: false })
@Rule(RuleType.string())
sku?: string;
@ApiProperty({ description: '分类ID (DictItem ID)', required: false })
@Rule(RuleType.number())
categoryId?: number;
// 商品价格
@ApiProperty({ description: '价格', example: 99.99, required: false })
@Rule(RuleType.number())
price?: number;
// 促销价格
@ApiProperty({ description: '促销价格', example: 99.99, required: false })
@Rule(RuleType.number())
promotionPrice?: number;
// 属性更新(中文注释:可选,支持增量替换指定字典的属性项)
@ApiProperty({ description: '属性列表', type: 'array', required: false })
@Rule(RuleType.array())
attributes?: AttributeInputDTO[];
// 中文注释商品类型更新simple 或 bundle
@ApiProperty({ description: '商品类型', enum: ['simple', 'bundle'], required: false })
@Rule(RuleType.string().valid('simple', 'bundle'))
type?: string;
}
/**
* DTO 用于创建分类属性绑定
*/
export class CreateCategoryAttributeDTO {
@ApiProperty({ description: '分类字典项ID', example: 1 })
@Rule(RuleType.number().required())
categoryItemId: number;
@ApiProperty({ description: '属性字典ID列表', example: [2, 3] })
@Rule(RuleType.array().items(RuleType.number()).required())
attributeDictIds: number[];
}
/**
* DTO 用于分页查询产品
*/
export class QueryProductDTO {
@ApiProperty({ description: '当前页', example: 1 })
@Rule(RuleType.number().default(1))
current: number;
@ApiProperty({ description: '每页数量', example: 10 })
@Rule(RuleType.number().default(10))
pageSize: number;
@ApiProperty({ description: '搜索关键字', required: false })
@Rule(RuleType.string())
name?: string;
@ApiProperty({ description: '分类ID', required: false })
@Rule(RuleType.number())
categoryId?: number;
@ApiProperty({ description: '品牌ID', required: false })
@Rule(RuleType.number())
brandId?: number;
}
/**
* DTO 用于设置产品组成
*/
export class SetProductComponentsDTO {
@ApiProperty({ description: '产品组成', type: 'array', required: true })
@Rule(
RuleType.array()
.items(
RuleType.object({
sku: RuleType.string().required(),
quantity: RuleType.number().required(),
})
)
.required()
)
components: { sku: string; quantity: number }[];
}