Compare commits
2
Commits
d856dc1887
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89acdb26d3 | ||
|
|
2dfc1dafec |
No files matched your search
@@ -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;
|
||||
}
|
||||
@@ -26,26 +26,45 @@ export class Product extends BaseEntity {
|
||||
@Column()
|
||||
totalWeight: number;
|
||||
|
||||
//蛋白质
|
||||
@Column()
|
||||
//蛋白质(百分比,如0.6表示60%)
|
||||
@Column({
|
||||
type: 'double',
|
||||
})
|
||||
protein: number;
|
||||
|
||||
// 脂肪
|
||||
@Column()
|
||||
// 脂肪(百分比)
|
||||
@Column({
|
||||
type: 'double',
|
||||
})
|
||||
fat: number;
|
||||
|
||||
// 粗纤维
|
||||
@Column()
|
||||
// 粗纤维(百分比)
|
||||
@Column({
|
||||
type: 'double',
|
||||
})
|
||||
fiber: number;
|
||||
|
||||
// 粗灰分
|
||||
@Column()
|
||||
// 粗灰分(百分比)
|
||||
@Column({
|
||||
type: 'double',
|
||||
})
|
||||
ash: number;
|
||||
|
||||
// 水分
|
||||
@Column()
|
||||
// 水分(百分比)
|
||||
@Column({
|
||||
type: 'double',
|
||||
})
|
||||
moisture: number;
|
||||
|
||||
// ME值(kcal/g),直接配置的代谢能,可选字段
|
||||
// 如果配置了此值,优先使用;否则通过营养成分计算
|
||||
@Column({
|
||||
type: 'double',
|
||||
nullable: true,
|
||||
comment: 'ME值(kcal/g),直接配置的代谢能',
|
||||
})
|
||||
meValue: number | null;
|
||||
|
||||
@OneToMany(
|
||||
() => FeedingPlanProduct,
|
||||
(feedingPlanProduct) => feedingPlanProduct.feedingPlan,
|
||||
|
||||
@@ -6,7 +6,82 @@ import { productCategoryLabels } from '@/pet-feeder/enum/product-category';
|
||||
|
||||
@Injectable()
|
||||
export class ProductMapper extends BaseMapper<Product, ProductVo> {
|
||||
/**
|
||||
* 计算产品的代谢能(ME - Metabolizable Energy)
|
||||
* 基于营养成分计算每克产品的能量值
|
||||
*
|
||||
* @param protein 蛋白质含量(百分比,如0.4表示40%)
|
||||
* @param fat 脂肪含量(百分比)
|
||||
* @param fiber 粗纤维含量(百分比)
|
||||
* @param ash 粗灰分含量(百分比)
|
||||
* @param moisture 水分含量(百分比)
|
||||
* @returns ME值 (kcal/g)
|
||||
*/
|
||||
private calculateME(
|
||||
protein: number,
|
||||
fat: number,
|
||||
fiber: number,
|
||||
ash: number,
|
||||
moisture: number,
|
||||
): number {
|
||||
// 步骤1: 计算干物质
|
||||
const dryMatter = 1 - moisture;
|
||||
|
||||
// 步骤2: 计算NEF(无氮浸出物)
|
||||
const nef =
|
||||
1 -
|
||||
protein * (1 - moisture) -
|
||||
fat * (1 - moisture) -
|
||||
fiber * (1 - moisture) -
|
||||
ash * (1 - moisture) -
|
||||
moisture;
|
||||
|
||||
// 步骤3: 计算各营养成分质量
|
||||
const proteinMass = protein * dryMatter;
|
||||
const fatMass = fat * dryMatter;
|
||||
const fiberMass = fiber * dryMatter;
|
||||
|
||||
// 步骤4: 计算干物质中粗纤维含量占比
|
||||
const fiberPercentageInDryMatter = (fiberMass / dryMatter) * 100;
|
||||
|
||||
// 步骤5: 计算GE(总能)
|
||||
const ge = 5.7 * proteinMass + 9.4 * fatMass + 4.1 * (nef + fiberMass);
|
||||
|
||||
// 步骤6: 计算能量消化率
|
||||
const digestibility = 87.9 - 0.88 * fiberPercentageInDryMatter;
|
||||
|
||||
// 步骤7: 计算DE(消化能)
|
||||
const de = (ge * digestibility) / 100;
|
||||
|
||||
// 步骤8: 计算ME(代谢能)
|
||||
const me = de - 0.77 * proteinMass;
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
toVo(entity: Product): ProductVo {
|
||||
// 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;
|
||||
|
||||
return {
|
||||
...super.toVo(entity),
|
||||
name: entity.name,
|
||||
@@ -19,6 +94,8 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,4 +36,15 @@ export class ProductVo extends BaseVo {
|
||||
|
||||
@ApiProperty()
|
||||
moisture: number;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'ME值(kcal/g),直接配置的代谢能,可选',
|
||||
required: false,
|
||||
})
|
||||
meValue: number | null;
|
||||
|
||||
@ApiProperty({
|
||||
description: '产品总能量(kcal),基于ME算法计算或直接配置',
|
||||
})
|
||||
totalCalories: number;
|
||||
}
|
||||
Reference in new issue
Block a user