|
|
@@ -19,6 +19,8 @@ const {
|
|
|
dataSign
|
|
|
} = require('../../plugin/Lepao/Crypto')
|
|
|
const generateGyrFromPath = require('../../plugin/Lepao/generateGyrFromPath')
|
|
|
+const { syncAccountInfo } = require('./syncAccountInfo')
|
|
|
+const { insertLedgerRecord } = require('./CountLedger')
|
|
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
@@ -423,6 +425,23 @@ class Worker {
|
|
|
/* ================= 业务 ================= */
|
|
|
|
|
|
initHandlers() {
|
|
|
+ this.register('lepao.syncAccountInfo', async (req) => {
|
|
|
+ const studentNum = req?.student_num
|
|
|
+ if (!studentNum) {
|
|
|
+ throw new Error('同步乐跑账号失败:缺少 student_num')
|
|
|
+ }
|
|
|
+ const syncResult = await syncAccountInfo({
|
|
|
+ studentNum,
|
|
|
+ logger: this.logger
|
|
|
+ })
|
|
|
+ if (!syncResult.ok) {
|
|
|
+ const err = new Error(syncResult.msg || '同步乐跑账号失败')
|
|
|
+ err.retryable = false
|
|
|
+ throw err
|
|
|
+ }
|
|
|
+ return syncResult.data
|
|
|
+ })
|
|
|
+
|
|
|
/* ---------------- 开始乐跑 ---------------- */
|
|
|
this.register('lepao.startRun', async (req, ctx) => {
|
|
|
const traceId = ctx.traceId
|
|
|
@@ -744,10 +763,51 @@ class Worker {
|
|
|
}
|
|
|
|
|
|
this.logger.info(`${account || uuid}开始扣减乐跑次数`)
|
|
|
- const useLepaoCountSql = 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?'
|
|
|
- const r = await db.query(useLepaoCountSql, [uuid])
|
|
|
- if (!r || r.affectedRows !== 1) {
|
|
|
- throw new Error('扣减乐跑次数失败:数据库更新失败')
|
|
|
+ const conn = await db.connect()
|
|
|
+ try {
|
|
|
+ await conn.beginTransaction()
|
|
|
+ const [userRows] = await conn.execute(
|
|
|
+ 'SELECT lepao_count FROM users WHERE uuid = ? FOR UPDATE',
|
|
|
+ [uuid]
|
|
|
+ )
|
|
|
+ if (!userRows || userRows.length !== 1) {
|
|
|
+ await conn.rollback()
|
|
|
+ throw new Error('扣减乐跑次数失败:用户不存在')
|
|
|
+ }
|
|
|
+
|
|
|
+ const beforeCount = Number(userRows[0].lepao_count || 0)
|
|
|
+ if (beforeCount < 1) {
|
|
|
+ await conn.rollback()
|
|
|
+ throw new Error('用户乐跑次数不足,请购买乐跑次数后重试!')
|
|
|
+ }
|
|
|
+
|
|
|
+ const [r] = await conn.execute(
|
|
|
+ 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?',
|
|
|
+ [uuid]
|
|
|
+ )
|
|
|
+ if (!r || r.affectedRows !== 1) {
|
|
|
+ await conn.rollback()
|
|
|
+ throw new Error('扣减乐跑次数失败:数据库更新失败')
|
|
|
+ }
|
|
|
+
|
|
|
+ await insertLedgerRecord({
|
|
|
+ executor: conn,
|
|
|
+ userUuid: uuid,
|
|
|
+ delta: -1,
|
|
|
+ balanceBefore: beforeCount,
|
|
|
+ balanceAfter: beforeCount - 1,
|
|
|
+ bizType: 'run_consume',
|
|
|
+ bizId: consumeKey
|
|
|
+ })
|
|
|
+
|
|
|
+ await conn.commit()
|
|
|
+ } catch (error) {
|
|
|
+ try { await conn.rollback() } catch (_) { }
|
|
|
+ throw error
|
|
|
+ } finally {
|
|
|
+ if (conn?.connection && typeof conn.connection.release === 'function' && typeof conn?.release === 'function') {
|
|
|
+ conn.release()
|
|
|
+ }
|
|
|
}
|
|
|
this.logger.info(`${account || uuid}扣减乐跑次数完成`)
|
|
|
|
|
|
@@ -777,10 +837,46 @@ class Worker {
|
|
|
}
|
|
|
|
|
|
this.logger.info(`${account || uuid}开始返还乐跑次数`)
|
|
|
- const sql = 'UPDATE users SET lepao_count = lepao_count + 1 WHERE uuid = ?'
|
|
|
- const r = await db.query(sql, [uuid])
|
|
|
- if (!r || r.affectedRows !== 1) {
|
|
|
- throw new Error('返还乐跑次数失败:数据库更新失败')
|
|
|
+ const conn = await db.connect()
|
|
|
+ try {
|
|
|
+ await conn.beginTransaction()
|
|
|
+ const [userRows] = await conn.execute(
|
|
|
+ 'SELECT lepao_count FROM users WHERE uuid = ? FOR UPDATE',
|
|
|
+ [uuid]
|
|
|
+ )
|
|
|
+ if (!userRows || userRows.length !== 1) {
|
|
|
+ await conn.rollback()
|
|
|
+ throw new Error('返还乐跑次数失败:用户不存在')
|
|
|
+ }
|
|
|
+
|
|
|
+ const beforeCount = Number(userRows[0].lepao_count || 0)
|
|
|
+ const [r] = await conn.execute(
|
|
|
+ 'UPDATE users SET lepao_count = lepao_count + 1 WHERE uuid = ?',
|
|
|
+ [uuid]
|
|
|
+ )
|
|
|
+ if (!r || r.affectedRows !== 1) {
|
|
|
+ await conn.rollback()
|
|
|
+ throw new Error('返还乐跑次数失败:数据库更新失败')
|
|
|
+ }
|
|
|
+
|
|
|
+ await insertLedgerRecord({
|
|
|
+ executor: conn,
|
|
|
+ userUuid: uuid,
|
|
|
+ delta: 1,
|
|
|
+ balanceBefore: beforeCount,
|
|
|
+ balanceAfter: beforeCount + 1,
|
|
|
+ bizType: 'run_refund',
|
|
|
+ bizId: refundKey
|
|
|
+ })
|
|
|
+
|
|
|
+ await conn.commit()
|
|
|
+ } catch (error) {
|
|
|
+ try { await conn.rollback() } catch (_) { }
|
|
|
+ throw error
|
|
|
+ } finally {
|
|
|
+ if (conn?.connection && typeof conn.connection.release === 'function' && typeof conn?.release === 'function') {
|
|
|
+ conn.release()
|
|
|
+ }
|
|
|
}
|
|
|
this.logger.info(`${account || uuid}返还乐跑次数完成`)
|
|
|
await Redis.set(refundKey, '1', { EX: 3600 })
|