136 lines
4.5 KiB
JavaScript
136 lines
4.5 KiB
JavaScript
|
|
const sqlite3 = require('sqlite3').verbose();
|
|||
|
|
const db = new sqlite3.Database('./data/cashier.db');
|
|||
|
|
|
|||
|
|
db.serialize(() => {
|
|||
|
|
// 插入订单项数据
|
|||
|
|
// 获取订单ID
|
|||
|
|
db.get("SELECT id FROM orders WHERE order_no = 'T001'", (err, row) => {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('查询订单ID失败:', err);
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!row) {
|
|||
|
|
console.log('未找到订单T001');
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const orderId = row.id;
|
|||
|
|
console.log('订单T001的ID:', orderId);
|
|||
|
|
|
|||
|
|
// 检查是否已存在订单项
|
|||
|
|
db.get("SELECT COUNT(*) as count FROM order_items WHERE order_id = ?", [orderId], (err, row) => {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('查询订单项失败:', err);
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (row.count > 0) {
|
|||
|
|
console.log('订单项已存在,跳过插入');
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 插入订单项
|
|||
|
|
const orderItems = [
|
|||
|
|
{ order_id: orderId, product_id: 1, quantity: 2, unit_price: 3.5, total_price: 7.0 }, // 可口可乐 x2
|
|||
|
|
{ order_id: orderId, product_id: 4, quantity: 1, unit_price: 5, total_price: 5.0 }, // 旺旺雪饼 x1
|
|||
|
|
{ order_id: orderId, product_id: 8, quantity: 1, unit_price: 8.5, total_price: 8.5 } // 舒肤佳香皂 x1
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
const itemStmt = db.prepare('INSERT INTO order_items (order_id, product_id, quantity, unit_price, total_price, discount_amount, created_at) VALUES (?, ?, ?, ?, ?, 0, "2025-11-11 10:30:00")');
|
|||
|
|
orderItems.forEach(item => {
|
|||
|
|
itemStmt.run(item.order_id, item.product_id, item.quantity, item.unit_price, item.total_price, function(err) {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('插入订单项失败:', err);
|
|||
|
|
} else {
|
|||
|
|
console.log('插入订单项成功,ID:', this.lastID);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
itemStmt.finalize();
|
|||
|
|
|
|||
|
|
// 插入其他订单的订单项
|
|||
|
|
insertOtherOrderItems(db);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
function insertOtherOrderItems(db) {
|
|||
|
|
// 获取订单T002的ID
|
|||
|
|
db.get("SELECT id FROM orders WHERE order_no = 'T002'", (err, row) => {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('查询订单T002 ID失败:', err);
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!row) {
|
|||
|
|
console.log('未找到订单T002');
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const orderId = row.id;
|
|||
|
|
console.log('订单T002的ID:', orderId);
|
|||
|
|
|
|||
|
|
// 插入订单项
|
|||
|
|
const orderItems = [
|
|||
|
|
{ order_id: orderId, product_id: 2, quantity: 1, unit_price: 3.5, total_price: 3.5 }, // 百事可乐 x1
|
|||
|
|
{ order_id: orderId, product_id: 3, quantity: 1, unit_price: 8.5, total_price: 8.5 } // 乐事薯片 x1
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
const itemStmt = db.prepare('INSERT INTO order_items (order_id, product_id, quantity, unit_price, total_price, discount_amount, created_at) VALUES (?, ?, ?, ?, ?, 0, "2025-11-11 11:45:00")');
|
|||
|
|
orderItems.forEach(item => {
|
|||
|
|
itemStmt.run(item.order_id, item.product_id, item.quantity, item.unit_price, item.total_price, function(err) {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('插入订单项失败:', err);
|
|||
|
|
} else {
|
|||
|
|
console.log('插入订单项成功,ID:', this.lastID);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
itemStmt.finalize();
|
|||
|
|
|
|||
|
|
// 获取订单T003的ID
|
|||
|
|
db.get("SELECT id FROM orders WHERE order_no = 'T003'", (err, row) => {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('查询订单T003 ID失败:', err);
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!row) {
|
|||
|
|
console.log('未找到订单T003');
|
|||
|
|
db.close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const orderId = row.id;
|
|||
|
|
console.log('订单T003的ID:', orderId);
|
|||
|
|
|
|||
|
|
// 插入订单项
|
|||
|
|
const orderItems = [
|
|||
|
|
{ order_id: orderId, product_id: 1, quantity: 5, unit_price: 3.5, total_price: 17.5 }, // 可口可乐 x5
|
|||
|
|
{ order_id: orderId, product_id: 9, quantity: 1, unit_price: 20, total_price: 20 } // 红塔山香烟 x1
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
const itemStmt = db.prepare('INSERT INTO order_items (order_id, product_id, quantity, unit_price, total_price, discount_amount, created_at) VALUES (?, ?, ?, ?, ?, 0, "2025-11-11 12:15:00")');
|
|||
|
|
orderItems.forEach(item => {
|
|||
|
|
itemStmt.run(item.order_id, item.product_id, item.quantity, item.unit_price, item.total_price, function(err) {
|
|||
|
|
if (err) {
|
|||
|
|
console.error('插入订单项失败:', err);
|
|||
|
|
} else {
|
|||
|
|
console.log('插入订单项成功,ID:', this.lastID);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
itemStmt.finalize();
|
|||
|
|
|
|||
|
|
db.close();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
}
|