new api
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user