feat: add meValue to product entity and request, update mapper and service to support new energy calculation logic

This commit is contained in:
ilhamtahir committed 2026-02-02 00:28:36 +08:00
1 parent 2dfc1dafec
commit 89acdb26d3
6 files changed
+49 -9

No files matched your search

+6
View File
@@ -31,6 +31,12 @@ async function bootstrap() {
app.useGlobalPipes(new GlobalValidationPipe());
app.useGlobalFilters(new GlobalExceptionFilter());
// Enable CORS for frontend applications
app.enableCors({
origin: ['http://localhost:5173', 'http://localhost:5174', 'http://localhost:5175'],
credentials: true,
});
await app.listen(process.env.APP_PORT || 3000);
}
bootstrap();
@@ -60,4 +60,11 @@ export class CreateProductRequest {
})
@ApiProperty()
moisture: number;
@IsOptional()
@ApiProperty({
description: 'ME值(kcal/g),直接配置的代谢能,可选。如果提供则优先使用,否则通过营养成分计算',
required: false,
})
meValue?: number;
}
+9
View File
@@ -56,6 +56,15 @@ export class Product extends BaseEntity {
})
moisture: number;
// ME值(kcal/g),直接配置的代谢能,可选字段
// 如果配置了此值,优先使用;否则通过营养成分计算
@Column({
type: 'double',
nullable: true,
comment: 'ME值(kcal/g),直接配置的代谢能',
})
meValue: number | null;
@OneToMany(
() => FeedingPlanProduct,
(feedingPlanProduct) => feedingPlanProduct.feedingPlan,
+19 -8
View File
@@ -60,14 +60,24 @@ export class ProductMapper extends BaseMapper<Product, ProductVo> {
}
toVo(entity: Product): ProductVo {
// 计算ME值(每克产品的能量)
const mePerGram = this.calculateME(
entity.protein,
entity.fat,
entity.fiber,
entity.ash,
entity.moisture,
);
// ME值计算逻辑:
// 1. 如果配置了meValue,优先使用配置值
// 2. 否则通过营养成分计算
let mePerGram: number;
if (entity.meValue !== null && entity.meValue !== undefined) {
// 使用直接配置的ME值(kcal/g)
mePerGram = entity.meValue;
} else {
// 通过营养成分计算ME值
mePerGram = this.calculateME(
entity.protein,
entity.fat,
entity.fiber,
entity.ash,
entity.moisture,
);
}
// 计算整包产品的总能量
const totalCalories = mePerGram * entity.totalWeight;
@@ -84,6 +94,7 @@ export class ProductMapper extends BaseMapper<Product, ProductVo> {
ash: entity.ash,
moisture: entity.moisture,
totalWeight: entity.totalWeight,
meValue: entity.meValue,
totalCalories: Number(totalCalories.toFixed(2)), // 保留2位小数
};
}
@@ -37,6 +37,7 @@ export class ProductService {
product.ash = updateProductRequest.ash;
product.moisture = updateProductRequest.moisture;
product.totalWeight = updateProductRequest.totalWeight;
product.meValue = updateProductRequest.meValue ?? null;
return this.productRepository.save(product);
}
+7 -1
View File
@@ -38,7 +38,13 @@ export class ProductVo extends BaseVo {
moisture: number;
@ApiProperty({
description: '产品总能量(kcal),基于ME算法计算',
description: 'ME值(kcal/g),直接配置的代谢能,可选',
required: false,
})
meValue: number | null;
@ApiProperty({
description: '产品总能量(kcal),基于ME算法计算或直接配置',
})
totalCalories: number;
}