2025-09-02 21:59:27 +08:00
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
const Joi = require('joi');
|
2025-09-18 23:51:25 +08:00
|
|
|
|
const { Supplier } = require('../models');
|
|
|
|
|
|
const { Sequelize } = require('sequelize');
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 验证schemas
|
|
|
|
|
|
const supplierCreateSchema = Joi.object({
|
|
|
|
|
|
name: Joi.string().min(2).max(100).required(),
|
|
|
|
|
|
code: Joi.string().min(3).max(20).required(),
|
|
|
|
|
|
contact: Joi.string().min(2).max(50).required(),
|
|
|
|
|
|
phone: Joi.string().pattern(/^1[3-9]\d{9}$/).required(),
|
|
|
|
|
|
address: Joi.string().min(5).max(200).required(),
|
2025-09-18 23:51:25 +08:00
|
|
|
|
businessLicense: Joi.string().max(255).optional(),
|
2025-09-02 21:59:27 +08:00
|
|
|
|
qualificationLevel: Joi.string().valid('A+', 'A', 'B+', 'B', 'C').required(),
|
2025-09-18 23:51:25 +08:00
|
|
|
|
certifications: Joi.array().items(Joi.string()).optional(),
|
2025-09-02 21:59:27 +08:00
|
|
|
|
cattleTypes: Joi.array().items(Joi.string()).min(1).required(),
|
|
|
|
|
|
capacity: Joi.number().integer().min(1).required(),
|
|
|
|
|
|
region: Joi.string().valid('north', 'south', 'east', 'west', 'northeast', 'northwest', 'southeast', 'southwest', 'central').required()
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const supplierUpdateSchema = Joi.object({
|
|
|
|
|
|
name: Joi.string().min(2).max(100),
|
|
|
|
|
|
contact: Joi.string().min(2).max(50),
|
|
|
|
|
|
phone: Joi.string().pattern(/^1[3-9]\d{9}$/),
|
|
|
|
|
|
address: Joi.string().min(5).max(200),
|
2025-09-18 23:51:25 +08:00
|
|
|
|
businessLicense: Joi.string().max(255).optional(),
|
2025-09-02 21:59:27 +08:00
|
|
|
|
qualificationLevel: Joi.string().valid('A+', 'A', 'B+', 'B', 'C'),
|
2025-09-18 23:51:25 +08:00
|
|
|
|
certifications: Joi.array().items(Joi.string()).optional(),
|
2025-09-02 21:59:27 +08:00
|
|
|
|
cattleTypes: Joi.array().items(Joi.string()).min(1),
|
|
|
|
|
|
capacity: Joi.number().integer().min(1),
|
|
|
|
|
|
region: Joi.string().valid('north', 'south', 'east', 'west', 'northeast', 'northwest', 'southeast', 'southwest', 'central'),
|
|
|
|
|
|
status: Joi.string().valid('active', 'inactive', 'suspended')
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 获取供应商列表
|
2025-09-18 23:51:25 +08:00
|
|
|
|
router.get('/', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const {
|
|
|
|
|
|
page = 1,
|
|
|
|
|
|
pageSize = 20,
|
|
|
|
|
|
keyword,
|
|
|
|
|
|
region,
|
|
|
|
|
|
qualificationLevel,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
status
|
2025-09-02 21:59:27 +08:00
|
|
|
|
} = req.query;
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 构建查询条件
|
|
|
|
|
|
const whereConditions = {};
|
|
|
|
|
|
|
|
|
|
|
|
// 状态筛选
|
|
|
|
|
|
if (status) {
|
|
|
|
|
|
whereConditions.status = status;
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
2025-09-18 23:51:25 +08:00
|
|
|
|
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 区域筛选
|
|
|
|
|
|
if (region) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
whereConditions.region = region;
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
2025-09-18 23:51:25 +08:00
|
|
|
|
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 资质等级筛选
|
|
|
|
|
|
if (qualificationLevel) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
whereConditions.qualificationLevel = qualificationLevel;
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
2025-09-18 23:51:25 +08:00
|
|
|
|
|
|
|
|
|
|
// 关键词搜索
|
|
|
|
|
|
if (keyword) {
|
|
|
|
|
|
whereConditions[Sequelize.Op.or] = [
|
|
|
|
|
|
{ name: { [Sequelize.Op.like]: `%${keyword}%` } },
|
|
|
|
|
|
{ code: { [Sequelize.Op.like]: `%${keyword}%` } },
|
|
|
|
|
|
{ contact: { [Sequelize.Op.like]: `%${keyword}%` } }
|
|
|
|
|
|
];
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 分页参数
|
|
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
const limit = parseInt(pageSize);
|
|
|
|
|
|
|
|
|
|
|
|
// 查询数据库
|
|
|
|
|
|
const { rows, count } = await Supplier.findAndCountAll({
|
|
|
|
|
|
where: whereConditions,
|
|
|
|
|
|
offset,
|
|
|
|
|
|
limit,
|
|
|
|
|
|
order: [['created_at', 'DESC']]
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 解析JSON字段
|
|
|
|
|
|
const processedRows = rows.map(row => {
|
|
|
|
|
|
const rowData = row.toJSON();
|
|
|
|
|
|
if (rowData.cattleTypes && typeof rowData.cattleTypes === 'string') {
|
|
|
|
|
|
rowData.cattleTypes = JSON.parse(rowData.cattleTypes);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (rowData.certifications && typeof rowData.certifications === 'string') {
|
|
|
|
|
|
rowData.certifications = JSON.parse(rowData.certifications);
|
|
|
|
|
|
}
|
|
|
|
|
|
return rowData;
|
|
|
|
|
|
});
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
list: processedRows,
|
2025-09-02 21:59:27 +08:00
|
|
|
|
pagination: {
|
|
|
|
|
|
page: parseInt(page),
|
2025-09-18 23:51:25 +08:00
|
|
|
|
pageSize: limit,
|
|
|
|
|
|
total: count,
|
|
|
|
|
|
totalPages: Math.ceil(count / limit)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('获取供应商列表失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '获取供应商列表失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 获取供应商详情
|
2025-09-18 23:51:25 +08:00
|
|
|
|
router.get('/:id', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
2025-09-18 23:51:25 +08:00
|
|
|
|
|
|
|
|
|
|
// 查询数据库
|
|
|
|
|
|
const supplier = await Supplier.findByPk(id);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
if (!supplier) {
|
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '供应商不存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 解析JSON字段
|
|
|
|
|
|
const supplierData = supplier.toJSON();
|
|
|
|
|
|
if (supplierData.cattleTypes && typeof supplierData.cattleTypes === 'string') {
|
|
|
|
|
|
supplierData.cattleTypes = JSON.parse(supplierData.cattleTypes);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (supplierData.certifications && typeof supplierData.certifications === 'string') {
|
|
|
|
|
|
supplierData.certifications = JSON.parse(supplierData.certifications);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
data: supplierData
|
2025-09-02 21:59:27 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('获取供应商详情失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '获取供应商详情失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 创建供应商
|
2025-09-18 23:51:25 +08:00
|
|
|
|
router.post('/', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { error, value } = supplierCreateSchema.validate(req.body);
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '参数验证失败',
|
|
|
|
|
|
errors: error.details.map(detail => detail.message)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查编码是否重复
|
2025-09-18 23:51:25 +08:00
|
|
|
|
const existingSupplier = await Supplier.findOne({ where: { code: value.code } });
|
2025-09-02 21:59:27 +08:00
|
|
|
|
if (existingSupplier) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '供应商编码已存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 检查电话是否重复
|
|
|
|
|
|
const existingPhone = await Supplier.findOne({ where: { phone: value.phone } });
|
|
|
|
|
|
if (existingPhone) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '供应商电话已存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新供应商
|
|
|
|
|
|
const newSupplier = await Supplier.create({
|
2025-09-02 21:59:27 +08:00
|
|
|
|
...value,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
businessLicense: value.businessLicense || '',
|
|
|
|
|
|
certifications: value.certifications ? JSON.stringify(value.certifications) : JSON.stringify([]),
|
|
|
|
|
|
cattleTypes: JSON.stringify(value.cattleTypes),
|
2025-09-02 21:59:27 +08:00
|
|
|
|
rating: 0,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
cooperationStartDate: new Date(),
|
|
|
|
|
|
status: 'active'
|
|
|
|
|
|
});
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '供应商创建成功',
|
|
|
|
|
|
data: newSupplier
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('创建供应商失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '创建供应商失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 更新供应商
|
2025-09-18 23:51:25 +08:00
|
|
|
|
router.put('/:id', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
const { error, value } = supplierUpdateSchema.validate(req.body);
|
|
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '参数验证失败',
|
|
|
|
|
|
errors: error.details.map(detail => detail.message)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 查找供应商
|
|
|
|
|
|
const supplier = await Supplier.findByPk(id);
|
|
|
|
|
|
if (!supplier) {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '供应商不存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 如果更新了电话号码,检查是否重复
|
|
|
|
|
|
if (value.phone && value.phone !== supplier.phone) {
|
|
|
|
|
|
const existingPhone = await Supplier.findOne({ where: { phone: value.phone } });
|
|
|
|
|
|
if (existingPhone) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '供应商电话已存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新供应商信息
|
|
|
|
|
|
await supplier.update({
|
2025-09-02 21:59:27 +08:00
|
|
|
|
...value,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
businessLicense: value.businessLicense !== undefined ? value.businessLicense : undefined,
|
|
|
|
|
|
certifications: value.certifications !== undefined ? JSON.stringify(value.certifications) : undefined,
|
|
|
|
|
|
cattleTypes: value.cattleTypes ? JSON.stringify(value.cattleTypes) : undefined
|
|
|
|
|
|
});
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '供应商更新成功',
|
2025-09-18 23:51:25 +08:00
|
|
|
|
data: supplier
|
2025-09-02 21:59:27 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('更新供应商失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '更新供应商失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 删除供应商
|
2025-09-18 23:51:25 +08:00
|
|
|
|
router.delete('/:id', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
2025-09-18 23:51:25 +08:00
|
|
|
|
|
|
|
|
|
|
// 查找供应商
|
|
|
|
|
|
const supplier = await Supplier.findByPk(id);
|
|
|
|
|
|
if (!supplier) {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '供应商不存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 删除供应商
|
|
|
|
|
|
await supplier.destroy();
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '供应商删除成功'
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('删除供应商失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '删除供应商失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 获取供应商统计信息
|
2025-09-18 23:51:25 +08:00
|
|
|
|
router.get('/stats/overview', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 获取总数和活跃数
|
|
|
|
|
|
const totalSuppliers = await Supplier.count();
|
|
|
|
|
|
const activeSuppliers = await Supplier.count({ where: { status: 'active' } });
|
|
|
|
|
|
|
|
|
|
|
|
// 获取平均评分(排除评分为0的供应商)
|
|
|
|
|
|
const ratingResult = await Supplier.findOne({
|
|
|
|
|
|
attributes: [
|
|
|
|
|
|
[Sequelize.fn('AVG', Sequelize.col('rating')), 'averageRating']
|
|
|
|
|
|
],
|
|
|
|
|
|
where: {
|
|
|
|
|
|
rating: {
|
|
|
|
|
|
[Sequelize.Op.gt]: 0
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
const averageRating = ratingResult ? parseFloat(ratingResult.getDataValue('averageRating')).toFixed(2) : 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 获取总产能
|
|
|
|
|
|
const capacityResult = await Supplier.findOne({
|
|
|
|
|
|
attributes: [
|
|
|
|
|
|
[Sequelize.fn('SUM', Sequelize.col('capacity')), 'totalCapacity']
|
|
|
|
|
|
]
|
|
|
|
|
|
});
|
|
|
|
|
|
const totalCapacity = capacityResult ? capacityResult.getDataValue('totalCapacity') : 0;
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 按等级统计
|
2025-09-18 23:51:25 +08:00
|
|
|
|
const levelStatsResult = await Supplier.findAll({
|
|
|
|
|
|
attributes: [
|
|
|
|
|
|
'qualificationLevel',
|
|
|
|
|
|
[Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
|
|
|
|
|
|
],
|
|
|
|
|
|
group: ['qualificationLevel']
|
|
|
|
|
|
});
|
|
|
|
|
|
const levelStats = levelStatsResult.reduce((stats, item) => {
|
|
|
|
|
|
stats[item.qualificationLevel] = item.getDataValue('count');
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return stats;
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
|
|
// 按区域统计
|
2025-09-18 23:51:25 +08:00
|
|
|
|
const regionStatsResult = await Supplier.findAll({
|
|
|
|
|
|
attributes: [
|
|
|
|
|
|
'region',
|
|
|
|
|
|
[Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
|
|
|
|
|
|
],
|
|
|
|
|
|
group: ['region']
|
|
|
|
|
|
});
|
|
|
|
|
|
const regionStats = regionStatsResult.reduce((stats, item) => {
|
|
|
|
|
|
stats[item.region] = item.getDataValue('count');
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return stats;
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: {
|
|
|
|
|
|
totalSuppliers,
|
|
|
|
|
|
activeSuppliers,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
averageRating: parseFloat(averageRating),
|
2025-09-02 21:59:27 +08:00
|
|
|
|
totalCapacity,
|
|
|
|
|
|
levelStats,
|
|
|
|
|
|
regionStats
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('获取供应商统计信息失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '获取供应商统计信息失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
// 批量操作供应商
|
|
|
|
|
|
router.post('/batch', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
const { ids, action } = req.body;
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
if (!ids || !Array.isArray(ids) || ids.length === 0) {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
message: '请选择要操作的供应商'
|
2025-09-02 21:59:27 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-18 23:51:25 +08:00
|
|
|
|
if (!['activate', 'deactivate', 'delete'].includes(action)) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '无效的操作类型'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
|
case 'activate':
|
2025-09-18 23:51:25 +08:00
|
|
|
|
await Supplier.update(
|
|
|
|
|
|
{ status: 'active' },
|
|
|
|
|
|
{ where: { id: ids } }
|
|
|
|
|
|
);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 'deactivate':
|
2025-09-18 23:51:25 +08:00
|
|
|
|
await Supplier.update(
|
|
|
|
|
|
{ status: 'inactive' },
|
|
|
|
|
|
{ where: { id: ids } }
|
|
|
|
|
|
);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 'delete':
|
2025-09-18 23:51:25 +08:00
|
|
|
|
await Supplier.destroy({
|
|
|
|
|
|
where: {
|
|
|
|
|
|
id: ids
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
2025-09-18 23:51:25 +08:00
|
|
|
|
message: '批量操作成功'
|
2025-09-02 21:59:27 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-09-18 23:51:25 +08:00
|
|
|
|
console.error('批量操作失败:', error);
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '批量操作失败',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|