feat(backend): 开发订单管理和供应商管理功能
- 新增订单管理页面,实现订单列表展示、搜索、分页等功能 - 新增供应商管理页面,实现供应商列表展示、搜索、分页等功能- 添加订单和供应商相关模型及数据库迁移 - 实现订单状态更新和供应商信息编辑功能 - 优化后端路由结构,移除不必要的代码
This commit is contained in:
374
backend/scripts/create_transport_tables.js
Normal file
374
backend/scripts/create_transport_tables.js
Normal file
@@ -0,0 +1,374 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const sequelize = require('../src/config/database');
|
||||
|
||||
// 运输管理模型
|
||||
const Transport = sequelize.define('Transport', {
|
||||
id: {
|
||||
type: DataTypes.BIGINT,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
order_id: {
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: false,
|
||||
comment: '关联订单ID'
|
||||
},
|
||||
driver_id: {
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: false,
|
||||
comment: '司机ID'
|
||||
},
|
||||
vehicle_id: {
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: false,
|
||||
comment: '车辆ID'
|
||||
},
|
||||
start_location: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false,
|
||||
comment: '起始地点'
|
||||
},
|
||||
end_location: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false,
|
||||
comment: '目的地'
|
||||
},
|
||||
scheduled_start_time: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
comment: '计划开始时间'
|
||||
},
|
||||
actual_start_time: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '实际开始时间'
|
||||
},
|
||||
scheduled_end_time: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
comment: '计划结束时间'
|
||||
},
|
||||
actual_end_time: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '实际结束时间'
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('scheduled', 'in_transit', 'completed', 'cancelled'),
|
||||
defaultValue: 'scheduled',
|
||||
comment: '运输状态: scheduled(已安排), in_transit(运输中), completed(已完成), cancelled(已取消)'
|
||||
},
|
||||
estimated_arrival_time: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '预计到达时间'
|
||||
},
|
||||
cattle_count: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
comment: '运输牛只数量'
|
||||
},
|
||||
special_requirements: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
comment: '特殊要求'
|
||||
}
|
||||
}, {
|
||||
tableName: 'transports',
|
||||
timestamps: true,
|
||||
createdAt: 'created_at',
|
||||
updatedAt: 'updated_at'
|
||||
});
|
||||
|
||||
// 车辆管理模型
|
||||
const Vehicle = sequelize.define('Vehicle', {
|
||||
id: {
|
||||
type: DataTypes.BIGINT,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
license_plate: {
|
||||
type: DataTypes.STRING(20),
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
comment: '车牌号'
|
||||
},
|
||||
vehicle_type: {
|
||||
type: DataTypes.STRING(50),
|
||||
allowNull: false,
|
||||
comment: '车辆类型'
|
||||
},
|
||||
capacity: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
comment: '载重能力(公斤)'
|
||||
},
|
||||
driver_id: {
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: false,
|
||||
comment: '司机ID'
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('available', 'in_use', 'maintenance', 'retired'),
|
||||
defaultValue: 'available',
|
||||
comment: '车辆状态: available(可用), in_use(使用中), maintenance(维护中), retired(已退役)'
|
||||
},
|
||||
last_maintenance_date: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '上次维护日期'
|
||||
},
|
||||
next_maintenance_date: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '下次维护日期'
|
||||
},
|
||||
insurance_expiry_date: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '保险到期日期'
|
||||
},
|
||||
registration_expiry_date: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
comment: '注册到期日期'
|
||||
}
|
||||
}, {
|
||||
tableName: 'vehicles',
|
||||
timestamps: true,
|
||||
createdAt: 'created_at',
|
||||
updatedAt: 'updated_at'
|
||||
});
|
||||
|
||||
// 运输跟踪模型
|
||||
const TransportTrack = sequelize.define('TransportTrack', {
|
||||
id: {
|
||||
type: DataTypes.BIGINT,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
transport_id: {
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: false
|
||||
},
|
||||
latitude: {
|
||||
type: DataTypes.DECIMAL(10,8),
|
||||
allowNull: false
|
||||
},
|
||||
longitude: {
|
||||
type: DataTypes.DECIMAL(11,8),
|
||||
allowNull: false
|
||||
},
|
||||
speed: {
|
||||
type: DataTypes.DECIMAL(5,2),
|
||||
allowNull: true
|
||||
},
|
||||
direction: {
|
||||
type: DataTypes.DECIMAL(5,2),
|
||||
allowNull: true
|
||||
},
|
||||
cattle_status: {
|
||||
type: DataTypes.STRING(20),
|
||||
allowNull: true
|
||||
},
|
||||
temperature: {
|
||||
type: DataTypes.DECIMAL(5,2),
|
||||
allowNull: true
|
||||
},
|
||||
humidity: {
|
||||
type: DataTypes.DECIMAL(5,2),
|
||||
allowNull: true
|
||||
},
|
||||
video_url: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true
|
||||
}
|
||||
}, {
|
||||
tableName: 'transport_tracks',
|
||||
timestamps: true,
|
||||
createdAt: 'created_at',
|
||||
updatedAt: false
|
||||
});
|
||||
|
||||
// 同步模型到数据库
|
||||
async function syncModels() {
|
||||
try {
|
||||
// 同步所有模型
|
||||
await Transport.sync({ alter: true });
|
||||
console.log('Transport table created/updated successfully');
|
||||
|
||||
await Vehicle.sync({ alter: true });
|
||||
console.log('Vehicle table created/updated successfully');
|
||||
|
||||
await TransportTrack.sync({ alter: true });
|
||||
console.log('TransportTrack table created/updated successfully');
|
||||
|
||||
console.log('All transport-related tables created/updated successfully');
|
||||
} catch (error) {
|
||||
console.error('Error creating transport tables:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 插入测试数据
|
||||
async function insertTestData() {
|
||||
try {
|
||||
// 先确保必要的外键数据存在(简化处理,实际项目中需要确保有真实的订单、司机、车辆数据)
|
||||
// 这里我们假设订单ID、司机ID、车辆ID分别为1,2,3
|
||||
|
||||
// 插入测试车辆数据
|
||||
const vehicles = await Vehicle.bulkCreate([
|
||||
{
|
||||
license_plate: '京A12345',
|
||||
vehicle_type: '厢式货车',
|
||||
capacity: 5000,
|
||||
driver_id: 1,
|
||||
status: 'available'
|
||||
},
|
||||
{
|
||||
license_plate: '沪B67890',
|
||||
vehicle_type: '专用运牛车',
|
||||
capacity: 8000,
|
||||
driver_id: 2,
|
||||
status: 'in_use'
|
||||
},
|
||||
{
|
||||
license_plate: '粤C11111',
|
||||
vehicle_type: '冷藏车',
|
||||
capacity: 6000,
|
||||
driver_id: 3,
|
||||
status: 'maintenance'
|
||||
}
|
||||
], { returning: true });
|
||||
|
||||
console.log('Test vehicles inserted:', vehicles.length);
|
||||
|
||||
// 插入测试运输数据
|
||||
const transports = await Transport.bulkCreate([
|
||||
{
|
||||
order_id: 1,
|
||||
driver_id: 1,
|
||||
vehicle_id: 1,
|
||||
start_location: '北京市朝阳区仓库',
|
||||
end_location: '天津市滨海新区屠宰场',
|
||||
scheduled_start_time: new Date('2024-01-15 08:00:00'),
|
||||
actual_start_time: new Date('2024-01-15 08:15:00'),
|
||||
scheduled_end_time: new Date('2024-01-15 18:00:00'),
|
||||
status: 'in_transit',
|
||||
estimated_arrival_time: new Date('2024-01-15 18:30:00'),
|
||||
cattle_count: 50,
|
||||
special_requirements: '保持通风'
|
||||
},
|
||||
{
|
||||
order_id: 2,
|
||||
driver_id: 2,
|
||||
vehicle_id: 2,
|
||||
start_location: '上海市浦东新区养殖场',
|
||||
end_location: '杭州市西湖区加工厂',
|
||||
scheduled_start_time: new Date('2024-01-16 06:00:00'),
|
||||
actual_start_time: new Date('2024-01-16 06:10:00'),
|
||||
scheduled_end_time: new Date('2024-01-16 20:00:00'),
|
||||
actual_end_time: new Date('2024-01-16 19:45:00'),
|
||||
status: 'completed',
|
||||
estimated_arrival_time: new Date('2024-01-16 20:30:00'),
|
||||
cattle_count: 80,
|
||||
special_requirements: '温度控制在15-20度'
|
||||
},
|
||||
{
|
||||
order_id: 3,
|
||||
driver_id: 3,
|
||||
vehicle_id: 3,
|
||||
start_location: '广州市天河区基地',
|
||||
end_location: '深圳市南山区配送中心',
|
||||
scheduled_start_time: new Date('2024-01-17 07:00:00'),
|
||||
scheduled_end_time: new Date('2024-01-17 19:00:00'),
|
||||
status: 'scheduled',
|
||||
estimated_arrival_time: new Date('2024-01-17 19:30:00'),
|
||||
cattle_count: 60,
|
||||
special_requirements: '避免急刹车'
|
||||
},
|
||||
{
|
||||
order_id: 4,
|
||||
driver_id: 1,
|
||||
vehicle_id: 1,
|
||||
start_location: '北京市大兴区农场',
|
||||
end_location: '石家庄市裕华区屠宰场',
|
||||
scheduled_start_time: new Date('2024-01-18 09:00:00'),
|
||||
actual_start_time: new Date('2024-01-18 09:20:00'),
|
||||
scheduled_end_time: new Date('2024-01-18 21:00:00'),
|
||||
status: 'in_transit',
|
||||
estimated_arrival_time: new Date('2024-01-18 21:30:00'),
|
||||
cattle_count: 45,
|
||||
special_requirements: '定时检查牛只状态'
|
||||
},
|
||||
{
|
||||
order_id: 5,
|
||||
driver_id: 2,
|
||||
vehicle_id: 2,
|
||||
start_location: '上海市闵行区基地',
|
||||
end_location: '南京市鼓楼区加工厂',
|
||||
scheduled_start_time: new Date('2024-01-19 05:00:00'),
|
||||
actual_start_time: new Date('2024-01-19 05:15:00'),
|
||||
scheduled_end_time: new Date('2024-01-19 19:00:00'),
|
||||
actual_end_time: new Date('2024-01-19 18:50:00'),
|
||||
status: 'completed',
|
||||
estimated_arrival_time: new Date('2024-01-19 19:30:00'),
|
||||
cattle_count: 70,
|
||||
special_requirements: '保持车厢清洁'
|
||||
}
|
||||
], { returning: true });
|
||||
|
||||
console.log('Test transports inserted:', transports.length);
|
||||
|
||||
// 插入测试运输跟踪数据
|
||||
const transportTracks = await TransportTrack.bulkCreate([
|
||||
{
|
||||
transport_id: 1,
|
||||
latitude: 39.9042,
|
||||
longitude: 116.4074,
|
||||
speed: 60.5,
|
||||
direction: 45.0,
|
||||
cattle_status: 'normal',
|
||||
temperature: 18.5,
|
||||
humidity: 65.0
|
||||
},
|
||||
{
|
||||
transport_id: 1,
|
||||
latitude: 39.1234,
|
||||
longitude: 117.5678,
|
||||
speed: 55.2,
|
||||
direction: 90.0,
|
||||
cattle_status: 'normal',
|
||||
temperature: 19.0,
|
||||
humidity: 63.5
|
||||
},
|
||||
{
|
||||
transport_id: 4,
|
||||
latitude: 39.7684,
|
||||
longitude: 116.3210,
|
||||
speed: 62.3,
|
||||
direction: 180.0,
|
||||
cattle_status: 'normal',
|
||||
temperature: 17.8,
|
||||
humidity: 67.2
|
||||
}
|
||||
], { returning: true });
|
||||
|
||||
console.log('Test transport tracks inserted:', transportTracks.length);
|
||||
|
||||
console.log('All test data inserted successfully');
|
||||
} catch (error) {
|
||||
console.error('Error inserting test data:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行脚本
|
||||
async function run() {
|
||||
await syncModels();
|
||||
await insertTestData();
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
run();
|
||||
}
|
||||
|
||||
module.exports = { Transport, Vehicle, TransportTrack, syncModels, insertTestData };
|
||||
52
backend/scripts/create_transport_tables.sql
Normal file
52
backend/scripts/create_transport_tables.sql
Normal file
@@ -0,0 +1,52 @@
|
||||
-- 创建运输管理相关表的SQL脚本
|
||||
|
||||
-- 创建车辆表
|
||||
CREATE TABLE IF NOT EXISTS vehicles (
|
||||
id BIGINT PRIMARY KEY AUTO_INCREMENT,
|
||||
license_plate VARCHAR(20) NOT NULL UNIQUE COMMENT '车牌号',
|
||||
vehicle_type VARCHAR(50) NOT NULL COMMENT '车辆类型',
|
||||
capacity INT NOT NULL COMMENT '载重能力(公斤)',
|
||||
driver_id BIGINT NOT NULL COMMENT '司机ID',
|
||||
status ENUM('available', 'in_use', 'maintenance', 'retired') DEFAULT 'available' COMMENT '车辆状态: available(可用), in_use(使用中), maintenance(维护中), retired(已退役)',
|
||||
last_maintenance_date DATETIME NULL COMMENT '上次维护日期',
|
||||
next_maintenance_date DATETIME NULL COMMENT '下次维护日期',
|
||||
insurance_expiry_date DATETIME NULL COMMENT '保险到期日期',
|
||||
registration_expiry_date DATETIME NULL COMMENT '注册到期日期',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='车辆管理表';
|
||||
|
||||
-- 创建运输表
|
||||
CREATE TABLE IF NOT EXISTS transports (
|
||||
id BIGINT PRIMARY KEY AUTO_INCREMENT,
|
||||
order_id BIGINT NOT NULL COMMENT '关联订单ID',
|
||||
driver_id BIGINT NOT NULL COMMENT '司机ID',
|
||||
vehicle_id BIGINT NOT NULL COMMENT '车辆ID',
|
||||
start_location VARCHAR(255) NOT NULL COMMENT '起始地点',
|
||||
end_location VARCHAR(255) NOT NULL COMMENT '目的地',
|
||||
scheduled_start_time DATETIME NOT NULL COMMENT '计划开始时间',
|
||||
actual_start_time DATETIME NULL COMMENT '实际开始时间',
|
||||
scheduled_end_time DATETIME NOT NULL COMMENT '计划结束时间',
|
||||
actual_end_time DATETIME NULL COMMENT '实际结束时间',
|
||||
status ENUM('scheduled', 'in_transit', 'completed', 'cancelled') DEFAULT 'scheduled' COMMENT '运输状态: scheduled(已安排), in_transit(运输中), completed(已完成), cancelled(已取消)',
|
||||
estimated_arrival_time DATETIME NULL COMMENT '预计到达时间',
|
||||
cattle_count INT NOT NULL COMMENT '运输牛只数量',
|
||||
special_requirements TEXT NULL COMMENT '特殊要求',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='运输管理表';
|
||||
|
||||
-- 创建运输跟踪表
|
||||
CREATE TABLE IF NOT EXISTS transport_tracks (
|
||||
id BIGINT PRIMARY KEY AUTO_INCREMENT,
|
||||
transport_id BIGINT NOT NULL,
|
||||
latitude DECIMAL(10,8) NOT NULL,
|
||||
longitude DECIMAL(11,8) NOT NULL,
|
||||
speed DECIMAL(5,2) NULL,
|
||||
direction DECIMAL(5,2) NULL,
|
||||
cattle_status VARCHAR(20) NULL,
|
||||
temperature DECIMAL(5,2) NULL,
|
||||
humidity DECIMAL(5,2) NULL,
|
||||
video_url VARCHAR(255) NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='运输跟踪表';
|
||||
Reference in New Issue
Block a user