完善政府端

This commit is contained in:
2025-10-09 18:01:06 +08:00
parent f88383425f
commit 1a1abf4c26
39 changed files with 4309 additions and 673 deletions

View File

@@ -0,0 +1,286 @@
const ApprovalProcess = require('../models/ApprovalProcess');
const { Op } = require('sequelize');
// 获取审批流程列表
const getApprovalProcesses = async (req, res) => {
try {
const {
page = 1,
pageSize = 9,
status,
applicant,
type
} = req.query;
const offset = (page - 1) * pageSize;
const where = {};
// 构建查询条件
if (status) {
where.status = status;
}
if (applicant) {
where.applicant = {
[Op.like]: `%${applicant}%`
};
}
if (type) {
where.type = type;
}
const { count, rows } = await ApprovalProcess.findAndCountAll({
where,
limit: parseInt(pageSize),
offset: parseInt(offset),
order: [['createdAt', '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 getApprovalProcessById = async (req, res) => {
try {
const { id } = req.params;
const approvalProcess = await ApprovalProcess.findByPk(id);
if (!approvalProcess) {
return res.status(404).json({
code: 404,
message: '审批流程不存在'
});
}
res.json({
code: 200,
message: '获取成功',
data: approvalProcess
});
} catch (error) {
console.error('获取审批流程详情失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 创建审批流程
const createApprovalProcess = async (req, res) => {
try {
const {
title,
type,
applicant,
phone,
farmName,
quantity,
description,
files,
remarks
} = req.body;
const approvalProcess = await ApprovalProcess.create({
title,
type,
applicant,
phone,
farmName,
quantity,
description,
files,
remarks,
status: 'pending'
});
res.status(201).json({
code: 201,
message: '创建成功',
data: approvalProcess
});
} catch (error) {
console.error('创建审批流程失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 更新审批流程
const updateApprovalProcess = async (req, res) => {
try {
const { id } = req.params;
const {
title,
type,
applicant,
phone,
farmName,
quantity,
description,
files,
remarks
} = req.body;
const approvalProcess = await ApprovalProcess.findByPk(id);
if (!approvalProcess) {
return res.status(404).json({
code: 404,
message: '审批流程不存在'
});
}
await approvalProcess.update({
title,
type,
applicant,
phone,
farmName,
quantity,
description,
files,
remarks
});
res.json({
code: 200,
message: '更新成功',
data: approvalProcess
});
} catch (error) {
console.error('更新审批流程失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 删除审批流程
const deleteApprovalProcess = async (req, res) => {
try {
const { id } = req.params;
const approvalProcess = await ApprovalProcess.findByPk(id);
if (!approvalProcess) {
return res.status(404).json({
code: 404,
message: '审批流程不存在'
});
}
await approvalProcess.destroy();
res.json({
code: 200,
message: '删除成功'
});
} catch (error) {
console.error('删除审批流程失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 审批操作(通过/拒绝)
const processApproval = async (req, res) => {
try {
const { id } = req.params;
const { action, approvalComment, approver } = req.body;
const approvalProcess = await ApprovalProcess.findByPk(id);
if (!approvalProcess) {
return res.status(404).json({
code: 404,
message: '审批流程不存在'
});
}
const status = action === 'approve' ? 'approved' : 'rejected';
await approvalProcess.update({
status,
approvalComment,
approver,
approvalTime: new Date()
});
res.json({
code: 200,
message: action === 'approve' ? '审批通过' : '审批拒绝',
data: approvalProcess
});
} catch (error) {
console.error('审批操作失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 更新审批状态
const updateApprovalStatus = async (req, res) => {
try {
const { id } = req.params;
const { status } = req.body;
const approvalProcess = await ApprovalProcess.findByPk(id);
if (!approvalProcess) {
return res.status(404).json({
code: 404,
message: '审批流程不存在'
});
}
await approvalProcess.update({ status });
res.json({
code: 200,
message: '状态更新成功',
data: approvalProcess
});
} catch (error) {
console.error('更新审批状态失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
module.exports = {
getApprovalProcesses,
getApprovalProcessById,
createApprovalProcess,
updateApprovalProcess,
deleteApprovalProcess,
processApproval,
updateApprovalStatus
};

View File

@@ -0,0 +1,298 @@
const CattleAcademy = require('../models/CattleAcademy');
const { Op } = require('sequelize');
// 获取养牛学院资讯列表
const getCattleAcademyList = async (req, res) => {
try {
const {
page = 1,
pageSize = 10,
title,
category,
status,
author
} = req.query;
const offset = (page - 1) * pageSize;
const where = {};
// 构建查询条件
if (title) {
where.title = {
[Op.like]: `%${title}%`
};
}
if (category) {
where.category = category;
}
if (status !== undefined) {
where.status = status === 'true' || status === true;
}
if (author) {
where.author = {
[Op.like]: `%${author}%`
};
}
const { count, rows } = await CattleAcademy.findAndCountAll({
where,
limit: parseInt(pageSize),
offset: parseInt(offset),
order: [['sort', 'DESC'], ['createdAt', '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 getCattleAcademyById = async (req, res) => {
try {
const { id } = req.params;
const cattleAcademy = await CattleAcademy.findByPk(id);
if (!cattleAcademy) {
return res.status(404).json({
code: 404,
message: '资讯不存在'
});
}
// 增加浏览次数
await cattleAcademy.increment('viewCount');
res.json({
code: 200,
message: '获取成功',
data: cattleAcademy
});
} catch (error) {
console.error('获取养牛学院资讯详情失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 创建养牛学院资讯
const createCattleAcademy = async (req, res) => {
try {
const {
title,
coverImage,
content,
summary,
category,
tags,
sort = 0,
status = true,
author,
publishTime,
isTop = false,
isRecommend = false,
remarks
} = req.body;
const cattleAcademy = await CattleAcademy.create({
title,
coverImage,
content,
summary,
category,
tags,
sort,
status,
author,
publishTime: publishTime || new Date(),
isTop,
isRecommend,
remarks
});
res.status(201).json({
code: 201,
message: '创建成功',
data: cattleAcademy
});
} catch (error) {
console.error('创建养牛学院资讯失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 更新养牛学院资讯
const updateCattleAcademy = async (req, res) => {
try {
const { id } = req.params;
const {
title,
coverImage,
content,
summary,
category,
tags,
sort,
status,
author,
publishTime,
isTop,
isRecommend,
remarks
} = req.body;
const cattleAcademy = await CattleAcademy.findByPk(id);
if (!cattleAcademy) {
return res.status(404).json({
code: 404,
message: '资讯不存在'
});
}
await cattleAcademy.update({
title,
coverImage,
content,
summary,
category,
tags,
sort,
status,
author,
publishTime,
isTop,
isRecommend,
remarks
});
res.json({
code: 200,
message: '更新成功',
data: cattleAcademy
});
} catch (error) {
console.error('更新养牛学院资讯失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 删除养牛学院资讯
const deleteCattleAcademy = async (req, res) => {
try {
const { id } = req.params;
const cattleAcademy = await CattleAcademy.findByPk(id);
if (!cattleAcademy) {
return res.status(404).json({
code: 404,
message: '资讯不存在'
});
}
await cattleAcademy.destroy();
res.json({
code: 200,
message: '删除成功'
});
} catch (error) {
console.error('删除养牛学院资讯失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 切换资讯状态
const toggleCattleAcademyStatus = async (req, res) => {
try {
const { id } = req.params;
const { status } = req.body;
const cattleAcademy = await CattleAcademy.findByPk(id);
if (!cattleAcademy) {
return res.status(404).json({
code: 404,
message: '资讯不存在'
});
}
await cattleAcademy.update({ status });
res.json({
code: 200,
message: '状态更新成功',
data: cattleAcademy
});
} catch (error) {
console.error('切换资讯状态失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 批量更新排序
const updateSort = async (req, res) => {
try {
const { items } = req.body; // [{id, sort}, ...]
for (const item of items) {
await CattleAcademy.update(
{ sort: item.sort },
{ where: { id: item.id } }
);
}
res.json({
code: 200,
message: '排序更新成功'
});
} catch (error) {
console.error('更新排序失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
module.exports = {
getCattleAcademyList,
getCattleAcademyById,
createCattleAcademy,
updateCattleAcademy,
deleteCattleAcademy,
toggleCattleAcademyStatus,
updateSort
};

View File

@@ -0,0 +1,177 @@
const DeviceWarning = require('../models/DeviceWarning');
const { Op, fn, col } = require('sequelize');
const sequelize = require('../config/database');
// 获取设备预警列表
exports.getDeviceWarnings = async (req, res) => {
try {
const { page = 1, pageSize = 20, deviceType, alertType, status, farmerName } = req.query;
const limit = parseInt(pageSize);
const offset = (parseInt(page) - 1) * limit;
const where = {};
if (deviceType) {
where.deviceType = deviceType;
}
if (alertType) {
where.alertType = alertType;
}
if (status) {
where.status = status;
}
if (farmerName) {
where.farmerName = { [Op.like]: `%${farmerName}%` };
}
const { count, rows } = await DeviceWarning.findAndCountAll({
where,
limit,
offset,
order: [['alertTime', 'DESC']],
});
res.status(200).json({
code: 200,
message: '获取成功',
data: {
list: rows,
total: count,
page: parseInt(page),
pageSize: limit,
},
});
} catch (error) {
console.error('获取设备预警列表失败:', error);
res.status(500).json({ code: 500, message: '获取设备预警列表失败', error: error.message });
}
};
// 根据ID获取单个设备预警详情
exports.getDeviceWarningById = async (req, res) => {
try {
const { id } = req.params;
const warning = await DeviceWarning.findByPk(id);
if (!warning) {
return res.status(404).json({ code: 404, message: '设备预警未找到' });
}
res.status(200).json({ code: 200, message: '获取成功', data: warning });
} catch (error) {
console.error('获取设备预警详情失败:', error);
res.status(500).json({ code: 500, message: '获取设备预警详情失败', error: error.message });
}
};
// 创建新的设备预警
exports.createDeviceWarning = async (req, res) => {
try {
const newWarning = await DeviceWarning.create(req.body);
res.status(201).json({ code: 201, message: '创建成功', data: newWarning });
} catch (error) {
console.error('创建设备预警失败:', error);
res.status(500).json({ code: 500, message: '创建设备预警失败', error: error.message });
}
};
// 更新设备预警
exports.updateDeviceWarning = async (req, res) => {
try {
const { id } = req.params;
const [updatedRows] = await DeviceWarning.update(req.body, {
where: { id },
});
if (updatedRows === 0) {
return res.status(404).json({ code: 404, message: '设备预警未找到或无更新' });
}
const updatedWarning = await DeviceWarning.findByPk(id);
res.status(200).json({ code: 200, message: '更新成功', data: updatedWarning });
} catch (error) {
console.error('更新设备预警失败:', error);
res.status(500).json({ code: 500, message: '更新设备预警失败', error: error.message });
}
};
// 删除设备预警
exports.deleteDeviceWarning = async (req, res) => {
try {
const { id } = req.params;
const deletedRows = await DeviceWarning.destroy({
where: { id },
});
if (deletedRows === 0) {
return res.status(404).json({ code: 404, message: '设备预警未找到' });
}
res.status(200).json({ code: 200, message: '删除成功' });
} catch (error) {
console.error('删除设备预警失败:', error);
res.status(500).json({ code: 500, message: '删除设备预警失败', error: error.message });
}
};
// 更新预警状态
exports.updateWarningStatus = async (req, res) => {
try {
const { id } = req.params;
const { status, resolvedBy } = req.body;
const warning = await DeviceWarning.findByPk(id);
if (!warning) {
return res.status(404).json({ code: 404, message: '设备预警未找到' });
}
warning.status = status;
if (status === 'resolved') {
warning.resolvedBy = resolvedBy;
warning.resolvedAt = new Date();
}
await warning.save();
res.status(200).json({ code: 200, message: '状态更新成功', data: warning });
} catch (error) {
console.error('更新预警状态失败:', error);
res.status(500).json({ code: 500, message: '更新预警状态失败', error: error.message });
}
};
// 获取预警统计
exports.getWarningStats = async (req, res) => {
try {
// 分别查询每种设备类型的活跃预警数量
const earTagCount = await DeviceWarning.count({
where: {
deviceType: '智能耳标',
status: 'active'
}
});
const neckbandCount = await DeviceWarning.count({
where: {
deviceType: '智能项圈',
status: 'active'
}
});
const hostCount = await DeviceWarning.count({
where: {
deviceType: '智能主机',
status: 'active'
}
});
const result = {
earTag: earTagCount,
neckband: neckbandCount,
host: hostCount
};
res.status(200).json({ code: 200, message: '获取成功', data: result });
} catch (error) {
console.error('获取预警统计失败:', error);
res.status(500).json({ code: 500, message: '获取预警统计失败', error: error.message });
}
};

View 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
};

View File

@@ -0,0 +1,280 @@
const ProductionMaterialCertification = require('../models/ProductionMaterialCertification');
const { Op } = require('sequelize');
// 获取生资认证列表
const getProductionMaterialCertifications = async (req, res) => {
try {
const {
page = 1,
pageSize = 20,
materialName,
materialType,
manufacturer,
certificationType,
certificationStatus
} = req.query;
const offset = (page - 1) * pageSize;
const where = {};
// 构建查询条件
if (materialName) {
where.materialName = {
[Op.like]: `%${materialName}%`
};
}
if (materialType) {
where.materialType = materialType;
}
if (manufacturer) {
where.manufacturer = {
[Op.like]: `%${manufacturer}%`
};
}
if (certificationType) {
where.certificationType = certificationType;
}
if (certificationStatus) {
where.certificationStatus = certificationStatus;
}
const { count, rows } = await ProductionMaterialCertification.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 getProductionMaterialCertificationById = async (req, res) => {
try {
const { id } = req.params;
const certification = await ProductionMaterialCertification.findByPk(id);
if (!certification) {
return res.status(404).json({
code: 404,
message: '生资认证不存在'
});
}
res.json({
code: 200,
message: '获取成功',
data: certification
});
} catch (error) {
console.error('获取生资认证详情失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 创建生资认证
const createProductionMaterialCertification = async (req, res) => {
try {
const {
materialName,
materialType,
manufacturer,
certificationNumber,
certificationType,
certificationAuthority,
issueDate,
expiryDate,
certificationStatus = 'valid',
description,
specifications,
applicableScope,
contactPerson,
contactPhone,
remarks
} = req.body;
const certification = await ProductionMaterialCertification.create({
materialName,
materialType,
manufacturer,
certificationNumber,
certificationType,
certificationAuthority,
issueDate,
expiryDate,
certificationStatus,
description,
specifications,
applicableScope,
contactPerson,
contactPhone,
remarks
});
res.status(201).json({
code: 201,
message: '创建成功',
data: certification
});
} catch (error) {
console.error('创建生资认证失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 更新生资认证
const updateProductionMaterialCertification = async (req, res) => {
try {
const { id } = req.params;
const {
materialName,
materialType,
manufacturer,
certificationNumber,
certificationType,
certificationAuthority,
issueDate,
expiryDate,
certificationStatus,
description,
specifications,
applicableScope,
contactPerson,
contactPhone,
remarks
} = req.body;
const certification = await ProductionMaterialCertification.findByPk(id);
if (!certification) {
return res.status(404).json({
code: 404,
message: '生资认证不存在'
});
}
await certification.update({
materialName,
materialType,
manufacturer,
certificationNumber,
certificationType,
certificationAuthority,
issueDate,
expiryDate,
certificationStatus,
description,
specifications,
applicableScope,
contactPerson,
contactPhone,
remarks
});
res.json({
code: 200,
message: '更新成功',
data: certification
});
} catch (error) {
console.error('更新生资认证失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 删除生资认证
const deleteProductionMaterialCertification = async (req, res) => {
try {
const { id } = req.params;
const certification = await ProductionMaterialCertification.findByPk(id);
if (!certification) {
return res.status(404).json({
code: 404,
message: '生资认证不存在'
});
}
await certification.destroy();
res.json({
code: 200,
message: '删除成功'
});
} catch (error) {
console.error('删除生资认证失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
// 切换认证状态
const toggleCertificationStatus = async (req, res) => {
try {
const { id } = req.params;
const { status } = req.body;
const certification = await ProductionMaterialCertification.findByPk(id);
if (!certification) {
return res.status(404).json({
code: 404,
message: '生资认证不存在'
});
}
await certification.update({ certificationStatus: status });
res.json({
code: 200,
message: '状态更新成功',
data: certification
});
} catch (error) {
console.error('切换认证状态失败:', error);
res.status(500).json({
code: 500,
message: '服务器内部错误',
error: error.message
});
}
};
module.exports = {
getProductionMaterialCertifications,
getProductionMaterialCertificationById,
createProductionMaterialCertification,
updateProductionMaterialCertification,
deleteProductionMaterialCertification,
toggleCertificationStatus
};