This commit is contained in:
董海洋
2026-05-26 09:18:48 +08:00
parent 12b582ec64
commit ff40282dc1
19 changed files with 703 additions and 8 deletions
+143
View File
@@ -0,0 +1,143 @@
const { query } = require('../config/database')
async function getPointsGoods(ctx) {
let sql = 'SELECT * FROM points_goods WHERE 1=1'
const params = []
if (ctx.query.visible === '1') {
sql += ' AND is_show = 1'
}
sql += ' ORDER BY points ASC'
const goods = await query(sql, params)
ctx.body = {
code: 200,
data: goods
}
}
async function getPointsGoodsById(ctx) {
const id = parseInt(ctx.params.id)
const goods = await query('SELECT * FROM points_goods WHERE id = ?', [id])
if (goods.length > 0) {
ctx.body = { code: 200, data: goods[0] }
} else {
ctx.body = { code: 404, message: '积分商品不存在' }
}
}
async function createPointsGoods(ctx) {
const { name, points, stock, image, description } = ctx.request.body
if (!name || points === undefined) {
ctx.body = { code: 400, message: '请填写商品名称和所需积分' }
return
}
const result = await query(
'INSERT INTO points_goods (name, points, stock, image, description) VALUES (?, ?, ?, ?, ?)',
[name, parseInt(points) || 0, parseInt(stock) || 0, image || '', description || '']
)
ctx.body = {
code: 200,
message: '添加成功',
data: { id: result.insertId }
}
}
async function updatePointsGoods(ctx) {
const id = parseInt(ctx.params.id)
const { name, points, stock, image, description } = ctx.request.body
if (!name || points === undefined) {
ctx.body = { code: 400, message: '请填写商品名称和所需积分' }
return
}
const result = await query(
'UPDATE points_goods SET name = ?, points = ?, stock = ?, image = ?, description = ? WHERE id = ?',
[name, parseInt(points) || 0, parseInt(stock) || 0, image || '', description || '', id]
)
if (result.affectedRows > 0) {
ctx.body = { code: 200, message: '更新成功' }
} else {
ctx.body = { code: 404, message: '积分商品不存在' }
}
}
async function deletePointsGoods(ctx) {
const id = parseInt(ctx.params.id)
const result = await query('DELETE FROM points_goods WHERE id = ?', [id])
if (result.affectedRows > 0) {
ctx.body = { code: 200, message: '删除成功' }
} else {
ctx.body = { code: 404, message: '积分商品不存在' }
}
}
async function exchangePointsGoods(ctx) {
const { userId, goodsId, quantity } = ctx.request.body
if (!userId || !goodsId) {
ctx.body = { code: 400, message: '参数不完整' }
return
}
const qty = quantity || 1
const users = await query('SELECT * FROM users WHERE id = ? AND status = 1', [userId])
if (users.length === 0) {
ctx.body = { code: 404, message: '用户不存在' }
return
}
const user = users[0]
const goods = await query('SELECT * FROM points_goods WHERE id = ? AND is_show = 1', [goodsId])
if (goods.length === 0) {
ctx.body = { code: 404, message: '积分商品不存在' }
return
}
const goodsItem = goods[0]
if (goodsItem.stock < qty) {
ctx.body = { code: 400, message: '库存不足' }
return
}
const totalPoints = goodsItem.points * qty
if (user.points < totalPoints) {
ctx.body = { code: 400, message: '积分不足' }
return
}
const newPoints = user.points - totalPoints
await query('UPDATE users SET points = ? WHERE id = ?', [newPoints, userId])
await query('UPDATE points_goods SET stock = stock - ? WHERE id = ?', [qty, goodsId])
await query(
'INSERT INTO points_logs (user_id, type, amount, description) VALUES (?, ?, ?, ?)',
[userId, 'spend', totalPoints, `兑换「${goodsItem.name}」x${qty}`]
)
ctx.body = {
code: 200,
message: '兑换成功',
data: {
remainingPoints: newPoints
}
}
}
module.exports = {
getPointsGoods,
getPointsGoodsById,
createPointsGoods,
updatePointsGoods,
deletePointsGoods,
exchangePointsGoods
}
+42
View File
@@ -0,0 +1,42 @@
const { query } = require('../config/database')
async function getPriceList(ctx) {
const orderId = ctx.params.orderId
const orders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
if (orders.length === 0) {
ctx.body = { code: 404, message: '订单不存在' }
return
}
const order = orders[0]
let items = []
try {
const cartData = typeof order.cart === 'string' ? JSON.parse(order.cart) : order.cart
items = Array.isArray(cartData) ? cartData : (cartData.items || cartData.list || [])
} catch (e) {
items = []
}
ctx.body = {
code: 200,
data: {
orderNo: order.id,
createTime: order.created_at,
items: items.map(item => ({
name: item.name,
quantity: item.quantity || item.count || 1,
price: item.price || 0,
unit: item.unit || '件',
subtotal: (item.price || 0) * (item.quantity || item.count || 1)
})),
total: order.total_price,
remark: ''
}
}
}
module.exports = {
getPriceList
}
+144
View File
@@ -0,0 +1,144 @@
const { query } = require('../config/database')
async function getPurchases(ctx) {
let sql = `
SELECT p.*,
(SELECT COUNT(*) FROM purchase_items WHERE purchase_id = p.id) as items_count
FROM purchases p
WHERE 1=1`
const params = []
if (ctx.query.keyword) {
sql += ' AND (p.supplier_name LIKE ?)'
params.push(`%${ctx.query.keyword}%`)
}
sql += ' ORDER BY p.created_at DESC'
const purchases = await query(sql, params)
ctx.body = {
code: 200,
data: purchases
}
}
async function getPurchaseById(ctx) {
const id = parseInt(ctx.params.id)
const purchases = await query('SELECT * FROM purchases WHERE id = ?', [id])
if (purchases.length === 0) {
ctx.body = { code: 404, message: '采购单不存在' }
return
}
const purchase = purchases[0]
const items = await query(
`SELECT pi.*, g.name as goods_name
FROM purchase_items pi
LEFT JOIN goods g ON pi.goods_id = g.id
WHERE pi.purchase_id = ?`,
[id]
)
purchase.items = items
purchase.items_count = items.length
ctx.body = {
code: 200,
data: purchase
}
}
async function createPurchase(ctx) {
const { supplier_id, items, remarks } = ctx.request.body
if (!supplier_id || !items || items.length === 0) {
ctx.body = { code: 400, message: '请选择供应商和采购商品' }
return
}
const suppliers = await query('SELECT * FROM suppliers WHERE id = ?', [supplier_id])
if (suppliers.length === 0) {
ctx.body = { code: 404, message: '供应商不存在' }
return
}
const supplier = suppliers[0]
let total = 0
for (const item of items) {
total += (item.purchase_price || 0) * (item.quantity || 0)
}
const purchaseResult = await query(
'INSERT INTO purchases (supplier_id, supplier_name, total, remarks) VALUES (?, ?, ?, ?)',
[supplier_id, supplier.name, total, remarks || '']
)
const purchaseId = purchaseResult.insertId
for (const item of items) {
const goods = await query('SELECT name FROM goods WHERE id = ?', [item.goods_id])
const goodsName = goods.length > 0 ? goods[0].name : ''
await query(
'INSERT INTO purchase_items (purchase_id, goods_id, goods_name, quantity, purchase_price) VALUES (?, ?, ?, ?, ?)',
[purchaseId, item.goods_id, goodsName, item.quantity || 0, item.purchase_price || 0]
)
}
ctx.body = {
code: 200,
message: '采购单创建成功',
data: { id: purchaseId }
}
}
async function inboundPurchase(ctx) {
const id = parseInt(ctx.params.id)
const purchases = await query('SELECT * FROM purchases WHERE id = ?', [id])
if (purchases.length === 0) {
ctx.body = { code: 404, message: '采购单不存在' }
return
}
const purchase = purchases[0]
if (purchase.status === 1) {
ctx.body = { code: 400, message: '该采购单已入库' }
return
}
const items = await query('SELECT * FROM purchase_items WHERE purchase_id = ?', [id])
for (const item of items) {
const existing = await query('SELECT * FROM stock WHERE goods_id = ?', [item.goods_id])
if (existing.length > 0) {
await query(
'UPDATE stock SET quantity = quantity + ? WHERE goods_id = ?',
[item.quantity, item.goods_id]
)
} else {
await query(
'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?)',
[item.goods_id, item.quantity, '默认仓库']
)
}
await query(
'UPDATE goods SET stock = stock + ? WHERE id = ?',
[item.quantity, item.goods_id]
)
}
await query('UPDATE purchases SET status = 1 WHERE id = ?', [id])
ctx.body = {
code: 200,
message: '入库成功'
}
}
module.exports = {
getPurchases,
getPurchaseById,
createPurchase,
inboundPurchase
}
+30
View File
@@ -0,0 +1,30 @@
const { query } = require('../config/database')
async function getTodayStats(ctx) {
const today = new Date()
const todayStart = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')} 00:00:00`
const todayEnd = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')} 23:59:59`
const orderResult = await query(
'SELECT COUNT(*) as orderCount, COALESCE(SUM(total_price), 0) as totalSales FROM orders WHERE created_at >= ? AND created_at <= ? AND status IN ("paid", "completed")',
[todayStart, todayEnd]
)
const customerResult = await query(
'SELECT COUNT(DISTINCT user_id) as customerCount FROM orders WHERE created_at >= ? AND created_at <= ?',
[todayStart, todayEnd]
)
ctx.body = {
code: 200,
data: {
sales: orderResult[0].totalSales,
orders: orderResult[0].orderCount,
customers: customerResult[0].customerCount
}
}
}
module.exports = {
getTodayStats
}
+95
View File
@@ -0,0 +1,95 @@
const { query } = require('../config/database')
async function getSuppliers(ctx) {
let sql = 'SELECT * FROM suppliers WHERE 1=1'
const params = []
if (ctx.query.keyword) {
sql += ' AND (name LIKE ? OR contact LIKE ? OR phone LIKE ?)'
params.push(`%${ctx.query.keyword}%`, `%${ctx.query.keyword}%`, `%${ctx.query.keyword}%`)
}
sql += ' ORDER BY id DESC'
const suppliers = await query(sql, params)
ctx.body = {
code: 200,
data: suppliers
}
}
async function getSupplierById(ctx) {
const id = parseInt(ctx.params.id)
const suppliers = await query('SELECT * FROM suppliers WHERE id = ?', [id])
if (suppliers.length > 0) {
ctx.body = { code: 200, data: suppliers[0] }
} else {
ctx.body = { code: 404, message: '供应商不存在' }
}
}
async function createSupplier(ctx) {
const { name, contact, phone, address } = ctx.request.body
if (!name) {
ctx.body = { code: 400, message: '请输入供应商名称' }
return
}
const result = await query(
'INSERT INTO suppliers (name, contact, phone, address) VALUES (?, ?, ?, ?)',
[name, contact || '', phone || '', address || '']
)
ctx.body = {
code: 200,
message: '添加成功',
data: { id: result.insertId }
}
}
async function updateSupplier(ctx) {
const id = parseInt(ctx.params.id)
const { name, contact, phone, address } = ctx.request.body
if (!name) {
ctx.body = { code: 400, message: '请输入供应商名称' }
return
}
const result = await query(
'UPDATE suppliers SET name = ?, contact = ?, phone = ?, address = ? WHERE id = ?',
[name, contact || '', phone || '', address || '', id]
)
if (result.affectedRows > 0) {
ctx.body = { code: 200, message: '更新成功' }
} else {
ctx.body = { code: 404, message: '供应商不存在' }
}
}
async function deleteSupplier(ctx) {
const id = parseInt(ctx.params.id)
try {
const result = await query('DELETE FROM suppliers WHERE id = ?', [id])
if (result.affectedRows > 0) {
ctx.body = { code: 200, message: '删除成功' }
} else {
ctx.body = { code: 404, message: '供应商不存在' }
}
} catch (error) {
console.error('删除供应商失败:', error)
ctx.body = { code: 500, message: '删除失败' }
}
}
module.exports = {
getSuppliers,
getSupplierById,
createSupplier,
updateSupplier,
deleteSupplier
}