完善政府端
This commit is contained in:
242
government-backend/controllers/epidemicActivityController.js
Normal file
242
government-backend/controllers/epidemicActivityController.js
Normal file
@@ -0,0 +1,242 @@
|
||||
const EpidemicActivity = require('../models/EpidemicActivity');
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
// 获取防疫活动列表
|
||||
const getEpidemicActivities = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
activityName,
|
||||
livestockCategory,
|
||||
diseaseCategory,
|
||||
activityStatus
|
||||
} = req.query;
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const where = {};
|
||||
|
||||
// 构建查询条件
|
||||
if (activityName) {
|
||||
where.activityName = {
|
||||
[Op.like]: `%${activityName}%`
|
||||
};
|
||||
}
|
||||
if (livestockCategory) {
|
||||
where.livestockCategory = livestockCategory;
|
||||
}
|
||||
if (diseaseCategory) {
|
||||
where.diseaseCategory = diseaseCategory;
|
||||
}
|
||||
if (activityStatus) {
|
||||
where.activityStatus = activityStatus;
|
||||
}
|
||||
|
||||
const { count, rows } = await EpidemicActivity.findAndCountAll({
|
||||
where,
|
||||
limit: parseInt(pageSize),
|
||||
offset: parseInt(offset),
|
||||
order: [['updatedAt', 'DESC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: {
|
||||
list: rows,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize)
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取防疫活动列表失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 根据ID获取防疫活动
|
||||
const getEpidemicActivityById = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const activity = await EpidemicActivity.findByPk(id);
|
||||
|
||||
if (!activity) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫活动不存在'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '获取成功',
|
||||
data: activity
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取防疫活动详情失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 创建防疫活动
|
||||
const createEpidemicActivity = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
activityName,
|
||||
livestockCategory,
|
||||
diseaseCategory,
|
||||
vaccineUsed,
|
||||
vaccineBatch,
|
||||
preventionDate,
|
||||
activityStatus = 'active'
|
||||
} = req.body;
|
||||
|
||||
const activity = await EpidemicActivity.create({
|
||||
activityName,
|
||||
livestockCategory,
|
||||
diseaseCategory,
|
||||
vaccineUsed,
|
||||
vaccineBatch,
|
||||
preventionDate,
|
||||
activityStatus
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
code: 201,
|
||||
message: '创建成功',
|
||||
data: activity
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('创建防疫活动失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 更新防疫活动
|
||||
const updateEpidemicActivity = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const {
|
||||
activityName,
|
||||
livestockCategory,
|
||||
diseaseCategory,
|
||||
vaccineUsed,
|
||||
vaccineBatch,
|
||||
preventionDate,
|
||||
activityStatus
|
||||
} = req.body;
|
||||
|
||||
const activity = await EpidemicActivity.findByPk(id);
|
||||
if (!activity) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫活动不存在'
|
||||
});
|
||||
}
|
||||
|
||||
await activity.update({
|
||||
activityName,
|
||||
livestockCategory,
|
||||
diseaseCategory,
|
||||
vaccineUsed,
|
||||
vaccineBatch,
|
||||
preventionDate,
|
||||
activityStatus
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '更新成功',
|
||||
data: activity
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('更新防疫活动失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 删除防疫活动
|
||||
const deleteEpidemicActivity = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const activity = await EpidemicActivity.findByPk(id);
|
||||
|
||||
if (!activity) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫活动不存在'
|
||||
});
|
||||
}
|
||||
|
||||
await activity.destroy();
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '删除成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除防疫活动失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 切换活动状态
|
||||
const toggleActivityStatus = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const activity = await EpidemicActivity.findByPk(id);
|
||||
|
||||
if (!activity) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫活动不存在'
|
||||
});
|
||||
}
|
||||
|
||||
const newStatus = activity.activityStatus === 'active' ? 'inactive' : 'active';
|
||||
await activity.update({ activityStatus: newStatus });
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '状态更新成功',
|
||||
data: activity
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('切换活动状态失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getEpidemicActivities,
|
||||
getEpidemicActivityById,
|
||||
createEpidemicActivity,
|
||||
updateEpidemicActivity,
|
||||
deleteEpidemicActivity,
|
||||
toggleActivityStatus
|
||||
};
|
||||
Reference in New Issue
Block a user