添加政府,银行大屏,修改政府前后端代码
This commit is contained in:
295
government-backend/controllers/EpidemicRecordController.js
Normal file
295
government-backend/controllers/EpidemicRecordController.js
Normal file
@@ -0,0 +1,295 @@
|
||||
const EpidemicRecord = require('../models/EpidemicRecord')
|
||||
const { Op } = require('sequelize')
|
||||
|
||||
// 获取防疫记录列表
|
||||
exports.getEpidemicRecords = async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10, keyword = '', type = '', status = '', startDate = '', endDate = '' } = req.query
|
||||
|
||||
// 构建查询条件
|
||||
const where = {}
|
||||
|
||||
// 关键词搜索
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ farmName: { [Op.like]: `%${keyword}%` } },
|
||||
{ epidemicStaff: { [Op.like]: `%${keyword}%` } }
|
||||
]
|
||||
}
|
||||
|
||||
// 防疫类型筛选
|
||||
if (type) {
|
||||
where.type = type
|
||||
}
|
||||
|
||||
// 状态筛选
|
||||
if (status) {
|
||||
where.status = status
|
||||
}
|
||||
|
||||
// 日期范围筛选
|
||||
if (startDate && endDate) {
|
||||
where.epidemicDate = {
|
||||
[Op.between]: [startDate, endDate]
|
||||
}
|
||||
} else if (startDate) {
|
||||
where.epidemicDate = {
|
||||
[Op.gte]: startDate
|
||||
}
|
||||
} else if (endDate) {
|
||||
where.epidemicDate = {
|
||||
[Op.lte]: endDate
|
||||
}
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
const offset = (page - 1) * pageSize
|
||||
const { count, rows } = await EpidemicRecord.findAndCountAll({
|
||||
where,
|
||||
order: [['createTime', 'DESC']],
|
||||
limit: parseInt(pageSize),
|
||||
offset: parseInt(offset)
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: {
|
||||
list: rows,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
},
|
||||
message: '获取防疫记录列表成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取防疫记录列表失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 获取防疫记录详情
|
||||
exports.getEpidemicRecordById = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
|
||||
const record = await EpidemicRecord.findByPk(id)
|
||||
|
||||
if (!record) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫记录不存在'
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: record,
|
||||
message: '获取防疫记录详情成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取防疫记录详情失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 创建防疫记录
|
||||
exports.createEpidemicRecord = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
farmName,
|
||||
type,
|
||||
epidemicStaff,
|
||||
phone,
|
||||
epidemicDate,
|
||||
count,
|
||||
vaccineName,
|
||||
area,
|
||||
disinfectant,
|
||||
healthResult,
|
||||
description,
|
||||
notes,
|
||||
status
|
||||
} = req.body
|
||||
|
||||
// 验证必填字段
|
||||
if (!farmName || !type || !epidemicStaff || !phone || !epidemicDate) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '请填写所有必填字段'
|
||||
})
|
||||
}
|
||||
|
||||
// 验证防疫类型
|
||||
const validTypes = ['vaccination', 'disinfection', 'health_check', 'other']
|
||||
if (!validTypes.includes(type)) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '防疫类型无效'
|
||||
})
|
||||
}
|
||||
|
||||
// 验证状态
|
||||
const validStatuses = ['completed', 'pending', 'failed']
|
||||
if (status && !validStatuses.includes(status)) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '状态无效'
|
||||
})
|
||||
}
|
||||
|
||||
// 创建防疫记录
|
||||
const record = await EpidemicRecord.create({
|
||||
farmName,
|
||||
type,
|
||||
epidemicStaff,
|
||||
phone,
|
||||
epidemicDate,
|
||||
count: count || 0,
|
||||
vaccineName,
|
||||
area,
|
||||
disinfectant,
|
||||
healthResult,
|
||||
description,
|
||||
notes,
|
||||
status: status || 'completed'
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: record,
|
||||
message: '创建防疫记录成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('创建防疫记录失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 更新防疫记录
|
||||
exports.updateEpidemicRecord = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
const updateData = req.body
|
||||
|
||||
// 验证防疫类型
|
||||
if (updateData.type) {
|
||||
const validTypes = ['vaccination', 'disinfection', 'health_check', 'other']
|
||||
if (!validTypes.includes(updateData.type)) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '防疫类型无效'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 验证状态
|
||||
if (updateData.status) {
|
||||
const validStatuses = ['completed', 'pending', 'failed']
|
||||
if (!validStatuses.includes(updateData.status)) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '状态无效'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 更新防疫记录
|
||||
const [affectedRows] = await EpidemicRecord.update(updateData, {
|
||||
where: { id }
|
||||
})
|
||||
|
||||
if (affectedRows === 0) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫记录不存在'
|
||||
})
|
||||
}
|
||||
|
||||
// 获取更新后的记录
|
||||
const updatedRecord = await EpidemicRecord.findByPk(id)
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: updatedRecord,
|
||||
message: '更新防疫记录成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('更新防疫记录失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 删除防疫记录
|
||||
exports.deleteEpidemicRecord = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
|
||||
const deletedRows = await EpidemicRecord.destroy({
|
||||
where: { id }
|
||||
})
|
||||
|
||||
if (deletedRows === 0) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫记录不存在'
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '删除防疫记录成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('删除防疫记录失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 批量删除防疫记录
|
||||
exports.batchDeleteEpidemicRecords = async (req, res) => {
|
||||
try {
|
||||
const { ids } = req.body
|
||||
|
||||
if (!ids || !Array.isArray(ids) || ids.length === 0) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '请选择要删除的记录'
|
||||
})
|
||||
}
|
||||
|
||||
const deletedRows = await EpidemicRecord.destroy({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: { deletedCount: deletedRows },
|
||||
message: `成功删除 ${deletedRows} 条防疫记录`
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('批量删除防疫记录失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,16 @@ exports.getSlaughterhouses = async (req, res) => {
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
// 转换状态为中文显示
|
||||
const processedRows = rows.map(row => ({
|
||||
...row.toJSON(),
|
||||
status: row.status === 'active' ? '正常' : row.status === 'inactive' ? '暂停营业' : row.status
|
||||
}));
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: {
|
||||
list: rows,
|
||||
list: processedRows,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
@@ -61,9 +67,15 @@ exports.getSlaughterhouseById = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
// 转换状态为中文显示
|
||||
const processedSlaughterhouse = {
|
||||
...slaughterhouse.toJSON(),
|
||||
status: slaughterhouse.status === 'active' ? '正常' : slaughterhouse.status === 'inactive' ? '暂停营业' : slaughterhouse.status
|
||||
};
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: slaughterhouse,
|
||||
data: processedSlaughterhouse,
|
||||
message: '查询成功'
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -80,6 +92,14 @@ exports.createSlaughterhouse = async (req, res) => {
|
||||
try {
|
||||
const { name, address, contactPerson, contactPhone, licenseNumber, status } = req.body;
|
||||
|
||||
// 验证必填字段
|
||||
if (!name || !address || !contactPerson || !contactPhone || !licenseNumber || !status) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '请填写所有必填字段'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查名称是否重复
|
||||
const existingSlaughterhouse = await Slaughterhouse.findOne({
|
||||
where: { name }
|
||||
@@ -104,20 +124,23 @@ exports.createSlaughterhouse = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
// 转换状态为英文存储
|
||||
const statusValue = status === '正常' ? 'active' : status === '暂停营业' ? 'inactive' : status;
|
||||
|
||||
const slaughterhouse = await Slaughterhouse.create({
|
||||
name,
|
||||
address,
|
||||
contactPerson,
|
||||
contactPhone,
|
||||
licenseNumber,
|
||||
status,
|
||||
status: statusValue,
|
||||
createTime: new Date(),
|
||||
created_by: req.user?.id || null,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 201,
|
||||
code: 200,
|
||||
data: slaughterhouse,
|
||||
message: '新增成功'
|
||||
});
|
||||
@@ -125,7 +148,7 @@ exports.createSlaughterhouse = async (req, res) => {
|
||||
console.error('新增屠宰场失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
message: '服务器内部错误: ' + error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -179,13 +202,16 @@ exports.updateSlaughterhouse = async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 转换状态为英文存储
|
||||
const statusValue = status === '正常' ? 'active' : status === '暂停营业' ? 'inactive' : status;
|
||||
|
||||
await slaughterhouse.update({
|
||||
name,
|
||||
address,
|
||||
contactPerson,
|
||||
contactPhone,
|
||||
licenseNumber,
|
||||
status,
|
||||
status: statusValue,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
@@ -253,9 +279,15 @@ exports.toggleSlaughterhouseStatus = async (req, res) => {
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
// 转换状态为中文显示
|
||||
const processedSlaughterhouse = {
|
||||
...slaughterhouse.toJSON(),
|
||||
status: newStatus === 'active' ? '正常' : '暂停营业'
|
||||
};
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: slaughterhouse,
|
||||
data: processedSlaughterhouse,
|
||||
message: '状态切换成功'
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
394
government-backend/controllers/VaccineController.js
Normal file
394
government-backend/controllers/VaccineController.js
Normal file
@@ -0,0 +1,394 @@
|
||||
const Vaccine = require('../models/Vaccine')
|
||||
const { Op } = require('sequelize')
|
||||
|
||||
// 获取疫苗列表
|
||||
exports.getVaccines = async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10, keyword = '', type = '', status = '' } = req.query
|
||||
|
||||
// 构建查询条件
|
||||
const where = {}
|
||||
|
||||
// 关键词搜索
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ manufacturer: { [Op.like]: `%${keyword}%` } }
|
||||
]
|
||||
}
|
||||
|
||||
// 疫苗类型筛选
|
||||
if (type) {
|
||||
where.type = type
|
||||
}
|
||||
|
||||
// 状态筛选
|
||||
if (status) {
|
||||
where.status = status
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
const offset = (page - 1) * pageSize
|
||||
const { count, rows } = await Vaccine.findAndCountAll({
|
||||
where,
|
||||
order: [['createTime', 'DESC']],
|
||||
limit: parseInt(pageSize),
|
||||
offset: parseInt(offset)
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: {
|
||||
list: rows,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
},
|
||||
message: '获取疫苗列表成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取疫苗列表失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 获取疫苗详情
|
||||
exports.getVaccineById = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
|
||||
const vaccine = await Vaccine.findByPk(id)
|
||||
|
||||
if (!vaccine) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '疫苗不存在'
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: vaccine,
|
||||
message: '获取疫苗详情成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取疫苗详情失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 创建疫苗
|
||||
exports.createVaccine = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
name,
|
||||
type,
|
||||
manufacturer,
|
||||
approvalNumber,
|
||||
specification,
|
||||
price,
|
||||
validDays,
|
||||
storageCondition,
|
||||
notes,
|
||||
stockCount = 0
|
||||
} = req.body
|
||||
|
||||
// 验证必填字段
|
||||
if (!name || !type || !manufacturer || !approvalNumber || !specification || !price || !validDays || !storageCondition) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '请填写所有必填字段'
|
||||
})
|
||||
}
|
||||
|
||||
// 验证疫苗类型
|
||||
const validTypes = ['foot_and_mouth_disease', 'bovine_tuberculosis', 'brucellosis', 'rabies', 'other']
|
||||
if (!validTypes.includes(type)) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '疫苗类型无效'
|
||||
})
|
||||
}
|
||||
|
||||
// 检查批准文号是否已存在
|
||||
const existingVaccine = await Vaccine.findOne({
|
||||
where: { approvalNumber }
|
||||
})
|
||||
|
||||
if (existingVaccine) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该批准文号已存在'
|
||||
})
|
||||
}
|
||||
|
||||
// 创建疫苗
|
||||
const vaccine = await Vaccine.create({
|
||||
name,
|
||||
type,
|
||||
manufacturer,
|
||||
approvalNumber,
|
||||
specification,
|
||||
price: parseFloat(price),
|
||||
validDays: parseInt(validDays),
|
||||
storageCondition,
|
||||
notes,
|
||||
stockCount: parseInt(stockCount),
|
||||
status: stockCount > 0 ? 'valid' : 'low_stock'
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: vaccine,
|
||||
message: '创建疫苗成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('创建疫苗失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 更新疫苗
|
||||
exports.updateVaccine = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
const updateData = req.body
|
||||
|
||||
// 验证疫苗类型
|
||||
if (updateData.type) {
|
||||
const validTypes = ['foot_and_mouth_disease', 'bovine_tuberculosis', 'brucellosis', 'rabies', 'other']
|
||||
if (!validTypes.includes(updateData.type)) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '疫苗类型无效'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 如果更新批准文号,检查是否已存在
|
||||
if (updateData.approvalNumber) {
|
||||
const existingVaccine = await Vaccine.findOne({
|
||||
where: {
|
||||
approvalNumber: updateData.approvalNumber,
|
||||
id: { [Op.ne]: id }
|
||||
}
|
||||
})
|
||||
|
||||
if (existingVaccine) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该批准文号已存在'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 更新疫苗
|
||||
const [affectedRows] = await Vaccine.update(updateData, {
|
||||
where: { id }
|
||||
})
|
||||
|
||||
if (affectedRows === 0) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '疫苗不存在'
|
||||
})
|
||||
}
|
||||
|
||||
// 获取更新后的疫苗
|
||||
const updatedVaccine = await Vaccine.findByPk(id)
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: updatedVaccine,
|
||||
message: '更新疫苗成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('更新疫苗失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 删除疫苗
|
||||
exports.deleteVaccine = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
|
||||
const deletedRows = await Vaccine.destroy({
|
||||
where: { id }
|
||||
})
|
||||
|
||||
if (deletedRows === 0) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '疫苗不存在'
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '删除疫苗成功'
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('删除疫苗失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 批量删除疫苗
|
||||
exports.batchDeleteVaccines = async (req, res) => {
|
||||
try {
|
||||
const { ids } = req.body
|
||||
|
||||
if (!ids || !Array.isArray(ids) || ids.length === 0) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '请选择要删除的疫苗'
|
||||
})
|
||||
}
|
||||
|
||||
const deletedRows = await Vaccine.destroy({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: ids
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: { deletedCount: deletedRows },
|
||||
message: `成功删除 ${deletedRows} 个疫苗`
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('批量删除疫苗失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 疫苗入库
|
||||
exports.stockIn = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
const { count, batchNumber, inDate } = req.body
|
||||
|
||||
if (!count || count <= 0) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '入库数量必须大于0'
|
||||
})
|
||||
}
|
||||
|
||||
const vaccine = await Vaccine.findByPk(id)
|
||||
if (!vaccine) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '疫苗不存在'
|
||||
})
|
||||
}
|
||||
|
||||
// 更新库存数量
|
||||
const newStockCount = vaccine.stockCount + parseInt(count)
|
||||
const newStatus = newStockCount > 0 ? 'valid' : 'low_stock'
|
||||
|
||||
await Vaccine.update({
|
||||
stockCount: newStockCount,
|
||||
status: newStatus
|
||||
}, {
|
||||
where: { id }
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '疫苗入库成功',
|
||||
data: {
|
||||
newStockCount,
|
||||
status: newStatus
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('疫苗入库失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 疫苗出库
|
||||
exports.stockOut = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
const { count, purpose, outDate } = req.body
|
||||
|
||||
if (!count || count <= 0) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '出库数量必须大于0'
|
||||
})
|
||||
}
|
||||
|
||||
const vaccine = await Vaccine.findByPk(id)
|
||||
if (!vaccine) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '疫苗不存在'
|
||||
})
|
||||
}
|
||||
|
||||
if (vaccine.stockCount < count) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '库存不足,无法出库'
|
||||
})
|
||||
}
|
||||
|
||||
// 更新库存数量
|
||||
const newStockCount = vaccine.stockCount - parseInt(count)
|
||||
let newStatus = 'valid'
|
||||
if (newStockCount === 0) {
|
||||
newStatus = 'low_stock'
|
||||
} else if (newStockCount < 100) {
|
||||
newStatus = 'low_stock'
|
||||
}
|
||||
|
||||
await Vaccine.update({
|
||||
stockCount: newStockCount,
|
||||
status: newStatus
|
||||
}, {
|
||||
where: { id }
|
||||
})
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '疫苗出库成功',
|
||||
data: {
|
||||
newStockCount,
|
||||
status: newStatus
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('疫苗出库失败:', error)
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误: ' + error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user