153 lines
4.0 KiB
JavaScript
153 lines
4.0 KiB
JavaScript
const { query, transaction } = require('../config/database')
|
|
const { paginate } = require('../utils/pagination')
|
|
|
|
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'
|
|
|
|
if (ctx.query.page) {
|
|
const result = await paginate(query, sql, params, ctx.query.page, ctx.query.pageSize)
|
|
ctx.body = { code: 200, ...result }
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
await transaction(async (conn) => {
|
|
const newPoints = user.points - totalPoints
|
|
await conn.execute('UPDATE users SET points = ? WHERE id = ?', [newPoints, userId])
|
|
await conn.execute('UPDATE points_goods SET stock = stock - ? WHERE id = ?', [qty, goodsId])
|
|
await conn.execute(
|
|
'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
|
|
}
|