更新完善页面

This commit is contained in:
董海洋
2026-06-03 14:15:55 +08:00
parent 4b7ae9c933
commit 1675662537
57 changed files with 7625 additions and 883 deletions
+67 -51
View File
@@ -1,13 +1,31 @@
const { query } = require('../config/database')
const { query, transaction } = require('../config/database')
const { paginate } = require('../utils/pagination')
async function ensureStockLogTable() {
await query(`
CREATE TABLE IF NOT EXISTS stock_logs (
id INT NOT NULL AUTO_INCREMENT,
goods_id INT NOT NULL,
change_type VARCHAR(20) NOT NULL COMMENT 'inbound/adjust/purchase/sale',
delta INT NOT NULL,
quantity_after INT NOT NULL,
operator_id INT,
remark VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY goods_id (goods_id),
KEY created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='库存流水表'
`)
}
// 获取库存列表
async function getStockList(ctx) {
const keyword = ctx.query.keyword
const threshold = parseInt(ctx.query.threshold) || 0
let sql = `
SELECT
SELECT
s.id,
s.goods_id,
g.name as goods_name,
@@ -43,64 +61,62 @@ async function getStockList(ctx) {
// 调整库存
async function adjustStock(ctx) {
const goodsId = ctx.params.id
const { quantity, type } = ctx.request.body
const operator = ctx.state.user
if (!operator) {
ctx.status = 401
ctx.body = { code: 401, message: '未登录' }
return
}
const goodsId = parseInt(ctx.params.id)
const { quantity, type, remark } = ctx.request.body || {}
if (!quantity || quantity <= 0) {
ctx.body = {
code: 400,
message: '请输入有效的调整数量'
}
const qty = parseInt(quantity)
if (!qty || qty <= 0 || qty > 100000) {
ctx.body = { code: 400, message: '请输入 1-100000 之间的整数' }
return
}
if (type !== 'add' && type !== 'sub') {
ctx.body = { code: 400, message: 'type 必须是 add 或 sub' }
return
}
// 先检查库存记录是否存在
let stockRecords = await query('SELECT * FROM stock WHERE goods_id = ?', [goodsId])
await ensureStockLogTable()
if (stockRecords.length === 0) {
// 如果库存记录不存在,先创建
if (type === 'add') {
await query(
'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?)',
[goodsId, quantity, '默认仓库']
)
} else {
ctx.body = {
code: 400,
message: '库存不足,无法减少'
}
return
}
} else {
const currentStock = stockRecords[0]
let newQuantity
if (type === 'add') {
newQuantity = currentStock.quantity + quantity
} else {
newQuantity = currentStock.quantity - quantity
if (newQuantity < 0) {
ctx.body = {
code: 400,
message: '库存不足,无法减少'
try {
const newQuantity = await transaction(async (conn) => {
const [rows] = await conn.execute('SELECT * FROM stock WHERE goods_id = ? FOR UPDATE', [goodsId])
let current = rows[0]
if (!current) {
if (type === 'sub') {
throw new Error('库存不足,无法减少')
}
return
await conn.execute(
'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?)',
[goodsId, qty, '默认仓库']
)
return qty
}
}
const delta = type === 'add' ? qty : -qty
const next = current.quantity + delta
if (next < 0) throw new Error('库存不足,无法减少')
await query(
'UPDATE stock SET quantity = ? WHERE goods_id = ?',
[newQuantity, goodsId]
)
}
await conn.execute('UPDATE stock SET quantity = ? WHERE goods_id = ?', [next, goodsId])
await conn.execute(
'UPDATE goods SET stock = ? WHERE id = ?',
[Math.max(0, next), goodsId]
)
await conn.execute(
'INSERT INTO stock_logs (goods_id, change_type, delta, quantity_after, operator_id, remark) VALUES (?, ?, ?, ?, ?, ?)',
[goodsId, 'adjust', delta, next, operator.id, remark || '库存调整']
)
return next
})
// 获取更新后的库存
const updatedStock = await query('SELECT * FROM stock WHERE goods_id = ?', [goodsId])
ctx.body = {
code: 200,
data: updatedStock[0],
message: '库存调整成功'
const [updated] = await query('SELECT * FROM stock WHERE goods_id = ?', [goodsId])
ctx.body = { code: 200, data: updated, message: '库存调整成功', newQuantity }
} catch (error) {
ctx.status = 400
ctx.body = { code: 400, message: error.message || '调整失败' }
}
}