修改政府端前端,银行端小程序和后端接口
This commit is contained in:
246
government-backend/controllers/epidemicAgencyController.js
Normal file
246
government-backend/controllers/epidemicAgencyController.js
Normal file
@@ -0,0 +1,246 @@
|
||||
const EpidemicAgency = require('../models/EpidemicAgency');
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
// 查询防疫机构列表
|
||||
exports.getEpidemicAgencies = async (req, res) => {
|
||||
try {
|
||||
const { keyword, status, type, page = 1, pageSize = 10 } = req.query;
|
||||
|
||||
const where = {};
|
||||
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ director: { [Op.like]: `%${keyword}%` } },
|
||||
{ phone: { [Op.like]: `%${keyword}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
if (status) {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
if (type) {
|
||||
where.type = type;
|
||||
}
|
||||
|
||||
const { count, rows } = await EpidemicAgency.findAndCountAll({
|
||||
where,
|
||||
offset: (page - 1) * pageSize,
|
||||
limit: parseInt(pageSize),
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
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: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 查询单个防疫机构详情
|
||||
exports.getEpidemicAgencyById = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const agency = await EpidemicAgency.findByPk(id);
|
||||
|
||||
if (!agency) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫机构不存在'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: agency,
|
||||
message: '查询成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('查询防疫机构详情失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 新增防疫机构
|
||||
exports.createEpidemicAgency = async (req, res) => {
|
||||
try {
|
||||
const { name, director, phone, address, email, type, status, establishmentDate, description } = req.body;
|
||||
|
||||
const existingAgency = await EpidemicAgency.findOne({
|
||||
where: { name }
|
||||
});
|
||||
|
||||
if (existingAgency) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该机构名称已存在'
|
||||
});
|
||||
}
|
||||
|
||||
const agency = await EpidemicAgency.create({
|
||||
name,
|
||||
director,
|
||||
phone,
|
||||
address,
|
||||
email,
|
||||
type,
|
||||
status,
|
||||
establishmentDate,
|
||||
description,
|
||||
created_by: req.user?.id || null,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 201,
|
||||
data: agency,
|
||||
message: '新增成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('新增防疫机构失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 更新防疫机构
|
||||
exports.updateEpidemicAgency = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { name, director, phone, address, email, type, status, establishmentDate, description } = req.body;
|
||||
|
||||
const agency = await EpidemicAgency.findByPk(id);
|
||||
|
||||
if (!agency) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫机构不存在'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查名称是否重复(排除当前机构)
|
||||
if (name && name !== agency.name) {
|
||||
const existingAgency = await EpidemicAgency.findOne({
|
||||
where: {
|
||||
name,
|
||||
id: { [Op.ne]: id }
|
||||
}
|
||||
});
|
||||
|
||||
if (existingAgency) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该机构名称已存在'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await agency.update({
|
||||
name,
|
||||
director,
|
||||
phone,
|
||||
address,
|
||||
email,
|
||||
type,
|
||||
status,
|
||||
establishmentDate,
|
||||
description,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: agency,
|
||||
message: '更新成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('更新防疫机构失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 删除防疫机构
|
||||
exports.deleteEpidemicAgency = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const agency = await EpidemicAgency.findByPk(id);
|
||||
|
||||
if (!agency) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫机构不存在'
|
||||
});
|
||||
}
|
||||
|
||||
await agency.destroy();
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '删除成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除防疫机构失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 切换防疫机构状态
|
||||
exports.toggleEpidemicAgencyStatus = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const agency = await EpidemicAgency.findByPk(id);
|
||||
|
||||
if (!agency) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '防疫机构不存在'
|
||||
});
|
||||
}
|
||||
|
||||
const newStatus = agency.status === 'active' ? 'inactive' : 'active';
|
||||
|
||||
await agency.update({
|
||||
status: newStatus,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: agency,
|
||||
message: '状态切换成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('切换防疫机构状态失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
214
government-backend/controllers/smartCollarController.js
Normal file
214
government-backend/controllers/smartCollarController.js
Normal file
@@ -0,0 +1,214 @@
|
||||
const { Op } = require('sequelize');
|
||||
const SmartCollar = require('../models/SmartCollar');
|
||||
|
||||
// 获取智能项圈列表
|
||||
const getSmartCollars = async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10, collarId = '', status = '' } = req.query;
|
||||
|
||||
const whereCondition = {};
|
||||
|
||||
// 项圈编号搜索
|
||||
if (collarId) {
|
||||
whereCondition.collar_id = { [Op.like]: `%${collarId}%` };
|
||||
}
|
||||
|
||||
// 状态筛选
|
||||
if (status) {
|
||||
whereCondition.status = status;
|
||||
}
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const { count, rows } = await SmartCollar.findAndCountAll({
|
||||
where: whereCondition,
|
||||
offset,
|
||||
limit: parseInt(pageSize),
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
// 格式化数据以便前端使用
|
||||
const formattedData = rows.map(collar => ({
|
||||
id: collar.id,
|
||||
key: collar.id.toString(),
|
||||
collarId: collar.collar_id,
|
||||
name: collar.name,
|
||||
status: collar.status,
|
||||
battery: collar.battery,
|
||||
remark: collar.remark,
|
||||
createdAt: collar.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: collar.updated_at.toLocaleString('zh-CN')
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: formattedData,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
message: '获取智能项圈列表成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取智能项圈列表失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取智能项圈列表失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 新增智能项圈
|
||||
const createSmartCollar = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
collarId,
|
||||
name,
|
||||
status = 'inactive',
|
||||
battery = 100,
|
||||
remark = ''
|
||||
} = req.body;
|
||||
|
||||
// 检查项圈编号是否已存在
|
||||
const existingCollar = await SmartCollar.findOne({ where: { collar_id: collarId } });
|
||||
if (existingCollar) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '该智能项圈编号已存在'
|
||||
});
|
||||
}
|
||||
|
||||
const collar = await SmartCollar.create({
|
||||
collar_id: collarId,
|
||||
name,
|
||||
status,
|
||||
battery,
|
||||
remark
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: {
|
||||
id: collar.id,
|
||||
collarId: collar.collar_id,
|
||||
name: collar.name,
|
||||
status: collar.status,
|
||||
battery: collar.battery,
|
||||
remark: collar.remark,
|
||||
createdAt: collar.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: collar.updated_at.toLocaleString('zh-CN')
|
||||
},
|
||||
message: '新增智能项圈成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('新增智能项圈失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '新增智能项圈失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 编辑智能项圈
|
||||
const updateSmartCollar = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const {
|
||||
collarId,
|
||||
name,
|
||||
status,
|
||||
battery,
|
||||
remark
|
||||
} = req.body;
|
||||
|
||||
// 查找要编辑的智能项圈
|
||||
const collar = await SmartCollar.findByPk(id);
|
||||
if (!collar) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '未找到该智能项圈'
|
||||
});
|
||||
}
|
||||
|
||||
// 如果修改了项圈编号,检查新编号是否已存在
|
||||
if (collarId && collarId !== collar.collar_id) {
|
||||
const existingCollar = await SmartCollar.findOne({ where: { collar_id: collarId } });
|
||||
if (existingCollar) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '该智能项圈编号已存在'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 更新智能项圈信息
|
||||
await collar.update({
|
||||
collar_id: collarId,
|
||||
name,
|
||||
status,
|
||||
battery,
|
||||
remark
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: collar.id,
|
||||
collarId: collar.collar_id,
|
||||
name: collar.name,
|
||||
status: collar.status,
|
||||
battery: collar.battery,
|
||||
remark: collar.remark,
|
||||
createdAt: collar.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: collar.updated_at.toLocaleString('zh-CN')
|
||||
},
|
||||
message: '编辑智能项圈成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('编辑智能项圈失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '编辑智能项圈失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 删除智能项圈
|
||||
const deleteSmartCollar = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// 查找要删除的智能项圈
|
||||
const collar = await SmartCollar.findByPk(id);
|
||||
if (!collar) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '未找到该智能项圈'
|
||||
});
|
||||
}
|
||||
|
||||
// 删除智能项圈
|
||||
await collar.destroy();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除智能项圈成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除智能项圈失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '删除智能项圈失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSmartCollars,
|
||||
createSmartCollar,
|
||||
updateSmartCollar,
|
||||
deleteSmartCollar
|
||||
};
|
||||
214
government-backend/controllers/smartEarmarkController.js
Normal file
214
government-backend/controllers/smartEarmarkController.js
Normal file
@@ -0,0 +1,214 @@
|
||||
const { Op } = require('sequelize');
|
||||
const SmartEarmark = require('../models/SmartEarmark');
|
||||
|
||||
// 获取智能耳标列表
|
||||
const getSmartEarmarks = async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10, earmarkId = '', status = '' } = req.query;
|
||||
|
||||
const whereCondition = {};
|
||||
|
||||
// 耳标编号搜索
|
||||
if (earmarkId) {
|
||||
whereCondition.earmark_id = { [Op.like]: `%${earmarkId}%` };
|
||||
}
|
||||
|
||||
// 状态筛选
|
||||
if (status) {
|
||||
whereCondition.status = status;
|
||||
}
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const { count, rows } = await SmartEarmark.findAndCountAll({
|
||||
where: whereCondition,
|
||||
offset,
|
||||
limit: parseInt(pageSize),
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
// 格式化数据以便前端使用
|
||||
const formattedData = rows.map(earmark => ({
|
||||
id: earmark.id,
|
||||
key: earmark.id.toString(),
|
||||
earmarkId: earmark.earmark_id,
|
||||
name: earmark.name,
|
||||
status: earmark.status,
|
||||
battery: earmark.battery,
|
||||
remark: earmark.remark,
|
||||
createdAt: earmark.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: earmark.updated_at.toLocaleString('zh-CN')
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: formattedData,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
message: '获取智能耳标列表成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取智能耳标列表失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取智能耳标列表失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 新增智能耳标
|
||||
const createSmartEarmark = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
earmarkId,
|
||||
name,
|
||||
status = 'inactive',
|
||||
battery = 100,
|
||||
remark = ''
|
||||
} = req.body;
|
||||
|
||||
// 检查耳标编号是否已存在
|
||||
const existingEarmark = await SmartEarmark.findOne({ where: { earmark_id: earmarkId } });
|
||||
if (existingEarmark) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '该智能耳标编号已存在'
|
||||
});
|
||||
}
|
||||
|
||||
const earmark = await SmartEarmark.create({
|
||||
earmark_id: earmarkId,
|
||||
name,
|
||||
status,
|
||||
battery,
|
||||
remark
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: {
|
||||
id: earmark.id,
|
||||
earmarkId: earmark.earmark_id,
|
||||
name: earmark.name,
|
||||
status: earmark.status,
|
||||
battery: earmark.battery,
|
||||
remark: earmark.remark,
|
||||
createdAt: earmark.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: earmark.updated_at.toLocaleString('zh-CN')
|
||||
},
|
||||
message: '新增智能耳标成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('新增智能耳标失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '新增智能耳标失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 编辑智能耳标
|
||||
const updateSmartEarmark = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const {
|
||||
earmarkId,
|
||||
name,
|
||||
status,
|
||||
battery,
|
||||
remark
|
||||
} = req.body;
|
||||
|
||||
// 查找要编辑的智能耳标
|
||||
const earmark = await SmartEarmark.findByPk(id);
|
||||
if (!earmark) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '未找到该智能耳标'
|
||||
});
|
||||
}
|
||||
|
||||
// 如果修改了耳标编号,检查新编号是否已存在
|
||||
if (earmarkId && earmarkId !== earmark.earmark_id) {
|
||||
const existingEarmark = await SmartEarmark.findOne({ where: { earmark_id: earmarkId } });
|
||||
if (existingEarmark) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '该智能耳标编号已存在'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 更新智能耳标信息
|
||||
await earmark.update({
|
||||
earmark_id: earmarkId,
|
||||
name,
|
||||
status,
|
||||
battery,
|
||||
remark
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: earmark.id,
|
||||
earmarkId: earmark.earmark_id,
|
||||
name: earmark.name,
|
||||
status: earmark.status,
|
||||
battery: earmark.battery,
|
||||
remark: earmark.remark,
|
||||
createdAt: earmark.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: earmark.updated_at.toLocaleString('zh-CN')
|
||||
},
|
||||
message: '编辑智能耳标成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('编辑智能耳标失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '编辑智能耳标失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 删除智能耳标
|
||||
const deleteSmartEarmark = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// 查找要删除的智能耳标
|
||||
const earmark = await SmartEarmark.findByPk(id);
|
||||
if (!earmark) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '未找到该智能耳标'
|
||||
});
|
||||
}
|
||||
|
||||
// 删除智能耳标
|
||||
await earmark.destroy();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除智能耳标成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除智能耳标失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '删除智能耳标失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSmartEarmarks,
|
||||
createSmartEarmark,
|
||||
updateSmartEarmark,
|
||||
deleteSmartEarmark
|
||||
};
|
||||
214
government-backend/controllers/smartHostController.js
Normal file
214
government-backend/controllers/smartHostController.js
Normal file
@@ -0,0 +1,214 @@
|
||||
const { Op } = require('sequelize');
|
||||
const SmartHost = require('../models/SmartHost');
|
||||
|
||||
// 获取智能主机列表
|
||||
const getSmartHosts = async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10, hostId = '', status = '' } = req.query;
|
||||
|
||||
const whereCondition = {};
|
||||
|
||||
// 主机编号搜索
|
||||
if (hostId) {
|
||||
whereCondition.host_id = { [Op.like]: `%${hostId}%` };
|
||||
}
|
||||
|
||||
// 状态筛选
|
||||
if (status) {
|
||||
whereCondition.status = status;
|
||||
}
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const { count, rows } = await SmartHost.findAndCountAll({
|
||||
where: whereCondition,
|
||||
offset,
|
||||
limit: parseInt(pageSize),
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
// 格式化数据以便前端使用
|
||||
const formattedData = rows.map(host => ({
|
||||
id: host.id,
|
||||
key: host.id.toString(),
|
||||
hostId: host.host_id,
|
||||
name: host.name,
|
||||
ipAddress: host.ip_address,
|
||||
status: host.status,
|
||||
remark: host.remark,
|
||||
createdAt: host.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: host.updated_at.toLocaleString('zh-CN')
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: formattedData,
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
message: '获取智能主机列表成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取智能主机列表失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取智能主机列表失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 新增智能主机
|
||||
const createSmartHost = async (req, res) => {
|
||||
try {
|
||||
const {
|
||||
hostId,
|
||||
name,
|
||||
ipAddress,
|
||||
status = 'inactive',
|
||||
remark = ''
|
||||
} = req.body;
|
||||
|
||||
// 检查主机编号是否已存在
|
||||
const existingHost = await SmartHost.findOne({ where: { host_id: hostId } });
|
||||
if (existingHost) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '该智能主机编号已存在'
|
||||
});
|
||||
}
|
||||
|
||||
const host = await SmartHost.create({
|
||||
host_id: hostId,
|
||||
name,
|
||||
ip_address: ipAddress,
|
||||
status,
|
||||
remark
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: {
|
||||
id: host.id,
|
||||
hostId: host.host_id,
|
||||
name: host.name,
|
||||
ipAddress: host.ip_address,
|
||||
status: host.status,
|
||||
remark: host.remark,
|
||||
createdAt: host.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: host.updated_at.toLocaleString('zh-CN')
|
||||
},
|
||||
message: '新增智能主机成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('新增智能主机失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '新增智能主机失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 编辑智能主机
|
||||
const updateSmartHost = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const {
|
||||
hostId,
|
||||
name,
|
||||
ipAddress,
|
||||
status,
|
||||
remark
|
||||
} = req.body;
|
||||
|
||||
// 查找要编辑的智能主机
|
||||
const host = await SmartHost.findByPk(id);
|
||||
if (!host) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '未找到该智能主机'
|
||||
});
|
||||
}
|
||||
|
||||
// 如果修改了主机编号,检查新编号是否已存在
|
||||
if (hostId && hostId !== host.host_id) {
|
||||
const existingHost = await SmartHost.findOne({ where: { host_id: hostId } });
|
||||
if (existingHost) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '该智能主机编号已存在'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 更新智能主机信息
|
||||
await host.update({
|
||||
host_id: hostId,
|
||||
name,
|
||||
ip_address: ipAddress,
|
||||
status,
|
||||
remark
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: host.id,
|
||||
hostId: host.host_id,
|
||||
name: host.name,
|
||||
ipAddress: host.ip_address,
|
||||
status: host.status,
|
||||
remark: host.remark,
|
||||
createdAt: host.created_at.toLocaleString('zh-CN'),
|
||||
updatedAt: host.updated_at.toLocaleString('zh-CN')
|
||||
},
|
||||
message: '编辑智能主机成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('编辑智能主机失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '编辑智能主机失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 删除智能主机
|
||||
const deleteSmartHost = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
// 查找要删除的智能主机
|
||||
const host = await SmartHost.findByPk(id);
|
||||
if (!host) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '未找到该智能主机'
|
||||
});
|
||||
}
|
||||
|
||||
// 删除智能主机
|
||||
await host.destroy();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '删除智能主机成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除智能主机失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '删除智能主机失败',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getSmartHosts,
|
||||
createSmartHost,
|
||||
updateSmartHost,
|
||||
deleteSmartHost
|
||||
};
|
||||
Reference in New Issue
Block a user