feat: add meValue to product entity and request, update mapper and service to support new energy calculation logic
This commit is contained in:
1 parent
2dfc1dafec
commit
89acdb26d3
6 files changed
+49
-9
No files matched your search
@@ -31,6 +31,12 @@ async function bootstrap() {
|
|||||||
app.useGlobalPipes(new GlobalValidationPipe());
|
app.useGlobalPipes(new GlobalValidationPipe());
|
||||||
app.useGlobalFilters(new GlobalExceptionFilter());
|
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);
|
await app.listen(process.env.APP_PORT || 3000);
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
@@ -60,4 +60,11 @@ export class CreateProductRequest {
|
|||||||
})
|
})
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
moisture: number;
|
moisture: number;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'ME值(kcal/g),直接配置的代谢能,可选。如果提供则优先使用,否则通过营养成分计算',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
meValue?: number;
|
||||||
}
|
}
|
||||||
@@ -56,6 +56,15 @@ export class Product extends BaseEntity {
|
|||||||
})
|
})
|
||||||
moisture: number;
|
moisture: number;
|
||||||
|
|
||||||
|
// ME值(kcal/g),直接配置的代谢能,可选字段
|
||||||
|
// 如果配置了此值,优先使用;否则通过营养成分计算
|
||||||
|
@Column({
|
||||||
|
type: 'double',
|
||||||
|
nullable: true,
|
||||||
|
comment: 'ME值(kcal/g),直接配置的代谢能',
|
||||||
|
})
|
||||||
|
meValue: number | null;
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => FeedingPlanProduct,
|
() => FeedingPlanProduct,
|
||||||
(feedingPlanProduct) => feedingPlanProduct.feedingPlan,
|
(feedingPlanProduct) => feedingPlanProduct.feedingPlan,
|
||||||
|
|||||||
@@ -60,14 +60,24 @@ export class ProductMapper extends BaseMapper<Product, ProductVo> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toVo(entity: Product): ProductVo {
|
toVo(entity: Product): ProductVo {
|
||||||
// 计算ME值(每克产品的能量)
|
// ME值计算逻辑:
|
||||||
const mePerGram = this.calculateME(
|
// 1. 如果配置了meValue,优先使用配置值
|
||||||
entity.protein,
|
// 2. 否则通过营养成分计算
|
||||||
entity.fat,
|
let mePerGram: number;
|
||||||
entity.fiber,
|
|
||||||
entity.ash,
|
if (entity.meValue !== null && entity.meValue !== undefined) {
|
||||||
entity.moisture,
|
// 使用直接配置的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;
|
const totalCalories = mePerGram * entity.totalWeight;
|
||||||
@@ -84,6 +94,7 @@ export class ProductMapper extends BaseMapper<Product, ProductVo> {
|
|||||||
ash: entity.ash,
|
ash: entity.ash,
|
||||||
moisture: entity.moisture,
|
moisture: entity.moisture,
|
||||||
totalWeight: entity.totalWeight,
|
totalWeight: entity.totalWeight,
|
||||||
|
meValue: entity.meValue,
|
||||||
totalCalories: Number(totalCalories.toFixed(2)), // 保留2位小数
|
totalCalories: Number(totalCalories.toFixed(2)), // 保留2位小数
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export class ProductService {
|
|||||||
product.ash = updateProductRequest.ash;
|
product.ash = updateProductRequest.ash;
|
||||||
product.moisture = updateProductRequest.moisture;
|
product.moisture = updateProductRequest.moisture;
|
||||||
product.totalWeight = updateProductRequest.totalWeight;
|
product.totalWeight = updateProductRequest.totalWeight;
|
||||||
|
product.meValue = updateProductRequest.meValue ?? null;
|
||||||
return this.productRepository.save(product);
|
return this.productRepository.save(product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,13 @@ export class ProductVo extends BaseVo {
|
|||||||
moisture: number;
|
moisture: number;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: '产品总能量(kcal),基于ME算法计算',
|
description: 'ME值(kcal/g),直接配置的代谢能,可选',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
meValue: number | null;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: '产品总能量(kcal),基于ME算法计算或直接配置',
|
||||||
})
|
})
|
||||||
totalCalories: number;
|
totalCalories: number;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user