76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
'use strict';
|
|
const balanceUtil = require('./util.js');
|
|
|
|
module.exports = {
|
|
/**
|
|
* 编辑资金流水
|
|
* @url admin/finance/balance/sys/update 前端调用的url参数地址
|
|
*/
|
|
main: async (event) => {
|
|
let { data = {}, userInfo, util } = event;
|
|
let { vk } = util;
|
|
const transaction = await vk.baseDao.startTransaction();
|
|
|
|
try {
|
|
if (vk.pubfn.isNull(data._id)) {
|
|
throw new Error('流水ID不能为空');
|
|
}
|
|
const oldLog = await vk.baseDao.findByWhereJson({
|
|
db: transaction,
|
|
dbName: 'jjx-balance-log',
|
|
whereJson: {
|
|
_id: data._id
|
|
}
|
|
});
|
|
if (!oldLog) {
|
|
throw new Error('流水不存在');
|
|
}
|
|
|
|
const description = String(data.description || '').trim();
|
|
if (!description) {
|
|
throw new Error('说明不能为空');
|
|
}
|
|
const newLog = {
|
|
description,
|
|
amount: balanceUtil.normalizeAmount(data.amount),
|
|
type: balanceUtil.normalizeType(data.type),
|
|
logTime: balanceUtil.normalizeTime(data.logTime),
|
|
operatorId: userInfo && userInfo.uid ? userInfo.uid : oldLog.operatorId || '',
|
|
source: oldLog.source || data.source || 'manual'
|
|
};
|
|
|
|
const oldChange = balanceUtil.buildBalanceChange(oldLog);
|
|
const newChange = balanceUtil.buildBalanceChange(newLog);
|
|
const delta = Number((newChange - oldChange).toFixed(2));
|
|
|
|
await balanceUtil.checkBalanceAvailable({
|
|
transaction,
|
|
vk,
|
|
changeAmount: delta
|
|
});
|
|
|
|
await vk.baseDao.updateById({
|
|
db: transaction,
|
|
dbName: 'jjx-balance-log',
|
|
id: data._id,
|
|
dataJson: newLog
|
|
});
|
|
|
|
await balanceUtil.updateBalanceWithDelta({
|
|
transaction,
|
|
vk,
|
|
delta,
|
|
lastLogId: data._id
|
|
});
|
|
|
|
await transaction.commit();
|
|
return { code: 0, msg: '' };
|
|
} catch (err) {
|
|
return await balanceUtil.rollbackTransaction({
|
|
transaction,
|
|
err
|
|
});
|
|
}
|
|
}
|
|
};
|