修改小程序,前端,官网跳转路径
This commit is contained in:
268
government-backend/controllers/SlaughterhouseController.js
Normal file
268
government-backend/controllers/SlaughterhouseController.js
Normal file
@@ -0,0 +1,268 @@
|
||||
const { Op } = require('sequelize');
|
||||
const Slaughterhouse = require('../models/Slaughterhouse');
|
||||
|
||||
// 查询屠宰场列表
|
||||
exports.getSlaughterhouses = async (req, res) => {
|
||||
try {
|
||||
const { keyword, status, page = 1, pageSize = 10 } = req.query;
|
||||
|
||||
const where = {};
|
||||
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ contactPerson: { [Op.like]: `%${keyword}%` } },
|
||||
{ contactPhone: { [Op.like]: `%${keyword}%` } },
|
||||
{ licenseNumber: { [Op.like]: `%${keyword}%` } }
|
||||
];
|
||||
}
|
||||
|
||||
if (status) {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
const { count, rows } = await Slaughterhouse.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.getSlaughterhouseById = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const slaughterhouse = await Slaughterhouse.findByPk(id);
|
||||
|
||||
if (!slaughterhouse) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '屠宰场不存在'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: slaughterhouse,
|
||||
message: '查询成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('查询屠宰场详情失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 新增屠宰场
|
||||
exports.createSlaughterhouse = async (req, res) => {
|
||||
try {
|
||||
const { name, address, contactPerson, contactPhone, licenseNumber, status } = req.body;
|
||||
|
||||
// 检查名称是否重复
|
||||
const existingSlaughterhouse = await Slaughterhouse.findOne({
|
||||
where: { name }
|
||||
});
|
||||
|
||||
if (existingSlaughterhouse) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该屠宰场名称已存在'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查许可证号是否重复
|
||||
const existingLicense = await Slaughterhouse.findOne({
|
||||
where: { licenseNumber }
|
||||
});
|
||||
|
||||
if (existingLicense) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该许可证号已存在'
|
||||
});
|
||||
}
|
||||
|
||||
const slaughterhouse = await Slaughterhouse.create({
|
||||
name,
|
||||
address,
|
||||
contactPerson,
|
||||
contactPhone,
|
||||
licenseNumber,
|
||||
status,
|
||||
createTime: new Date(),
|
||||
created_by: req.user?.id || null,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 201,
|
||||
data: slaughterhouse,
|
||||
message: '新增成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('新增屠宰场失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 更新屠宰场
|
||||
exports.updateSlaughterhouse = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { name, address, contactPerson, contactPhone, licenseNumber, status } = req.body;
|
||||
|
||||
const slaughterhouse = await Slaughterhouse.findByPk(id);
|
||||
|
||||
if (!slaughterhouse) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '屠宰场不存在'
|
||||
});
|
||||
}
|
||||
|
||||
// 检查名称是否重复(排除当前屠宰场)
|
||||
if (name && name !== slaughterhouse.name) {
|
||||
const existingSlaughterhouse = await Slaughterhouse.findOne({
|
||||
where: {
|
||||
name,
|
||||
id: { [Op.ne]: id }
|
||||
}
|
||||
});
|
||||
|
||||
if (existingSlaughterhouse) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该屠宰场名称已存在'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 检查许可证号是否重复(排除当前屠宰场)
|
||||
if (licenseNumber && licenseNumber !== slaughterhouse.licenseNumber) {
|
||||
const existingLicense = await Slaughterhouse.findOne({
|
||||
where: {
|
||||
licenseNumber,
|
||||
id: { [Op.ne]: id }
|
||||
}
|
||||
});
|
||||
|
||||
if (existingLicense) {
|
||||
return res.status(400).json({
|
||||
code: 400,
|
||||
message: '该许可证号已存在'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await slaughterhouse.update({
|
||||
name,
|
||||
address,
|
||||
contactPerson,
|
||||
contactPhone,
|
||||
licenseNumber,
|
||||
status,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: slaughterhouse,
|
||||
message: '更新成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('更新屠宰场失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 删除屠宰场
|
||||
exports.deleteSlaughterhouse = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const slaughterhouse = await Slaughterhouse.findByPk(id);
|
||||
|
||||
if (!slaughterhouse) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '屠宰场不存在'
|
||||
});
|
||||
}
|
||||
|
||||
await slaughterhouse.destroy();
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
message: '删除成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('删除屠宰场失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 切换屠宰场状态
|
||||
exports.toggleSlaughterhouseStatus = async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const slaughterhouse = await Slaughterhouse.findByPk(id);
|
||||
|
||||
if (!slaughterhouse) {
|
||||
return res.status(404).json({
|
||||
code: 404,
|
||||
message: '屠宰场不存在'
|
||||
});
|
||||
}
|
||||
|
||||
const newStatus = slaughterhouse.status === 'active' ? 'inactive' : 'active';
|
||||
|
||||
await slaughterhouse.update({
|
||||
status: newStatus,
|
||||
updated_by: req.user?.id || null
|
||||
});
|
||||
|
||||
res.json({
|
||||
code: 200,
|
||||
data: slaughterhouse,
|
||||
message: '状态切换成功'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('切换屠宰场状态失败:', error);
|
||||
res.status(500).json({
|
||||
code: 500,
|
||||
message: '服务器内部错误'
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user