优化项目bug

This commit is contained in:
xuqiuyun
2025-09-16 16:07:32 +08:00
parent 89bc6e8ce2
commit b67f22fd79
22 changed files with 1650 additions and 598 deletions

View File

@@ -1,4 +1,4 @@
const { CattleBatch, IotCattle, Farm, CattleBatchAnimal, User } = require('../models');
const { CattleBatch, IotCattle, Farm, CattleBatchAnimal, User, CattleType, CattleUser, CattlePen } = require('../models');
const { Op } = require('sequelize');
/**
@@ -386,6 +386,9 @@ class CattleBatchController {
const { page = 1, pageSize = 10 } = req.query;
const offset = (page - 1) * pageSize;
console.log('🔍 开始获取批次牛只数据');
console.log('📋 批次信息:', { id, page, pageSize, offset });
// 检查批次是否存在
const batch = await CattleBatch.findByPk(id);
if (!batch) {
@@ -395,31 +398,127 @@ class CattleBatchController {
});
}
// 获取批次中的牛只
console.log('✅ 批次存在:', batch.name);
// 获取批次中的牛只直接通过batchId字段查询
const { count, rows } = await IotCattle.findAndCountAll({
where: { batchId: id },
attributes: [
'id',
'earNumber',
'sex',
'strain',
'varieties',
'birthday',
'parity',
'orgId',
'penId'
],
include: [
{
model: CattleBatchAnimal,
as: 'batchAnimals',
where: { batchId: id },
attributes: ['id', 'joinDate', 'notes']
},
{
model: Farm,
as: 'farm',
attributes: ['id', 'name']
}
],
attributes: ['id', 'earNumber', 'sex', 'strain', 'orgId'],
limit: parseInt(pageSize),
offset: offset,
order: [['earNumber', 'ASC']]
});
console.log(`📊 查询结果: 总记录数=${count}, 返回记录数=${rows.length}`);
// 获取品种和品系映射数据
const typeIds = [...new Set(rows.map(cattle => cattle.varieties).filter(id => id))];
const strainIds = [...new Set(rows.map(cattle => cattle.strain).filter(id => id))];
const penIds = [...new Set(rows.map(cattle => cattle.penId).filter(id => id))];
const typeNames = {};
if (typeIds.length > 0) {
const types = await CattleType.findAll({
where: { id: typeIds },
attributes: ['id', 'name']
});
types.forEach(type => {
typeNames[type.id] = type.name;
});
}
const userNames = {};
if (strainIds.length > 0) {
const users = await CattleUser.findAll({
where: { id: strainIds },
attributes: ['id', 'name']
});
users.forEach(user => {
userNames[user.id] = user.name;
});
}
const penNames = {};
if (penIds.length > 0) {
const pens = await CattlePen.findAll({
where: { id: penIds },
attributes: ['id', 'name']
});
pens.forEach(pen => {
penNames[pen.id] = pen.name;
});
}
// 转换数据格式,添加计算字段
const transformedRows = rows.map(cattle => {
// 计算月龄(基于出生日期)
let ageInMonths = 0;
if (cattle.birthday) {
const birthDate = new Date(cattle.birthday * 1000);
const now = new Date();
ageInMonths = Math.floor((now - birthDate) / (1000 * 60 * 60 * 24 * 30));
}
// 性别转换
const genderMap = { 1: '公', 2: '母', 0: '未知' };
const gender = genderMap[cattle.sex] || '未知';
// 品种转换(动态查询)
const breed = typeNames[cattle.varieties] || `品种ID:${cattle.varieties}`;
// 生理阶段判断
let physiologicalStage = '未知';
if (ageInMonths < 6) {
physiologicalStage = '犊牛';
} else if (ageInMonths < 12) {
physiologicalStage = '育成牛';
} else if (ageInMonths < 24) {
physiologicalStage = '青年牛';
} else if (cattle.sex === 2) {
if (cattle.parity > 0) {
physiologicalStage = '泌乳牛';
} else {
physiologicalStage = '后备母牛';
}
} else {
physiologicalStage = '种公牛';
}
return {
id: cattle.id,
earTag: cattle.earNumber,
breed: breed,
gender: gender,
ageInMonths: ageInMonths,
physiologicalStage: physiologicalStage,
pen: cattle.penId ? (penNames[cattle.penId] || `栏舍ID:${cattle.penId}`) : '未分配栏舍',
farm: cattle.farm
};
});
console.log('🔄 转换后的数据示例:', transformedRows.slice(0, 2));
res.json({
success: true,
data: {
list: rows,
list: transformedRows,
total: count,
page: parseInt(page),
pageSize: parseInt(pageSize)