job,挖矿收益计算
This commit is contained in:
156
src/jobs/jobs.js
156
src/jobs/jobs.js
@@ -22,22 +22,26 @@ const system = require('../modules/system/system.model');
|
|||||||
const { createAction } = require('../modules/action/action.handler');
|
const { createAction } = require('../modules/action/action.handler');
|
||||||
const { createNewFund } = require('../modules/fund/fund.handler');
|
const { createNewFund } = require('../modules/fund/fund.handler');
|
||||||
|
|
||||||
const tokenReleaseRate = 0.2;
|
const tokenReleaseRate = 0.02;
|
||||||
async function awardMine(user, fund, i) {
|
async function awardMine(user, fund) {
|
||||||
const released = fund.releasedAmout || 0;
|
if (!user || !fund || fund.fundList.length === 0) return 0;
|
||||||
if (released === fund.releasedAmout) {
|
let sum = 0;
|
||||||
await Fund.findByIdAndUpdate(fund.id, {
|
for (let i = 0, len = fund.fundList.length; i < len; ++i) {
|
||||||
$set: {
|
const released = fund.releasedAmout || 0;
|
||||||
[`fundList.${i}.status`]: 2 //标记失效
|
if (released === fund.releasedAmout) {
|
||||||
}
|
await Fund.findByIdAndUpdate(fund.id, {
|
||||||
});
|
$set: {
|
||||||
return 0;
|
[`fundList.${i}.status`]: 2 //标记失效
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
sum += +fund.fundList[i].amount * tokenReleaseRate;
|
||||||
}
|
}
|
||||||
const rewardMine = +fund.amount * tokenReleaseRate;
|
return sum;
|
||||||
return rewardMine;
|
|
||||||
}
|
}
|
||||||
function awardInvite(user, fund, allInviteReward) {
|
function awardInvite(user, fund, allInviteReward) {
|
||||||
const rewardInvite = allInviteReward - (fund.option.usedGroupReward || 0); // 求增量: 总额 - 已领取总额(fundSum)
|
const rewardInvite = allInviteReward - (fund.option && fund.option.usedGroupReward || 0); // 求增量: 总额 - 已领取总额(fundSum)
|
||||||
// 挖矿奖励 = 充值金额奖励 + 社区奖励
|
// 挖矿奖励 = 充值金额奖励 + 社区奖励
|
||||||
return rewardInvite;
|
return rewardInvite;
|
||||||
}
|
}
|
||||||
@@ -46,71 +50,81 @@ const doReward = async (user, fund) => {
|
|||||||
// 1.对于充值的fundList的每一项,按照2%进行发放,累加到asset.vic,添加action,类型为1
|
// 1.对于充值的fundList的每一项,按照2%进行发放,累加到asset.vic,添加action,类型为1
|
||||||
// 2.对于社团成员进行人头数目,按照10vic/人进行奖励,累加到asset.vic,添加action,类型为2
|
// 2.对于社团成员进行人头数目,按照10vic/人进行奖励,累加到asset.vic,添加action,类型为2
|
||||||
if (!user || !fund) return 0;
|
if (!user || !fund) return 0;
|
||||||
const fundlist = fund.fundList;
|
const allInviteReward = (+user.group.length || 0) * 10;
|
||||||
for (let i = 0, len = fundlist.length; i < len; ++i) {
|
const rewardMine = await awardMine(user, fund);
|
||||||
const fund = fundlist[i];
|
const rewardInvite = awardInvite(user, fund, allInviteReward);
|
||||||
const allInviteReward = (+user.group.length || 0) * 10;
|
const reward = rewardMine + rewardInvite;
|
||||||
const rewardMine = awardMine(user, fund, i);
|
|
||||||
const rewardInvite = awardInvite(user, fund, allInviteReward);
|
reward && await Fund.findByIdAndUpdate(fund.id, {
|
||||||
const reward = rewardMine + rewardInvite;
|
$inc: {
|
||||||
await Fund.findByIdAndUpdate(fund.id, {
|
'asset.vic': reward,
|
||||||
$inc: {
|
},
|
||||||
'asset.vic': reward,
|
$set: {
|
||||||
},
|
'option.usedGroupReward': rewardInvite, // 本次的奖励值累加到已领取团队奖励总和内
|
||||||
|
'asset.usdt': allInviteReward, // 用来记录所有社群奖励总额
|
||||||
|
'lastMine': new Date().toISOString()
|
||||||
|
}
|
||||||
|
}).then(() => {
|
||||||
|
rewardMine > 0 && createAction(1, {
|
||||||
|
userId: user.id,
|
||||||
|
tokenType: 'vic',
|
||||||
|
amount: rewardMine,
|
||||||
|
remain: fund.asset
|
||||||
|
}).save();
|
||||||
|
rewardInvite > 0 && createAction(2, {
|
||||||
|
userId: user.id,
|
||||||
|
tokenType: 'vic',
|
||||||
|
amount: rewardInvite,
|
||||||
|
remain: fund.asset
|
||||||
|
}).save();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const mock = {
|
||||||
|
touch: () => {console.log('touch');},
|
||||||
|
done: () => {console.log('done');},
|
||||||
|
};
|
||||||
|
async function mine (job = mock, done = mock) {
|
||||||
|
//todo: 程序内检查是否可以执行,保证不会启动时错误的自动执行一次
|
||||||
|
mylog.info('[Schedule Job] 开始定时计算作业...');
|
||||||
|
let currentIndex = await system.findOne({
|
||||||
|
key: 'userIndex4job'
|
||||||
|
}).exec();
|
||||||
|
currentIndex = currentIndex.value;
|
||||||
|
if (typeof currentIndex !== 'number') {
|
||||||
|
currentIndex = 0;
|
||||||
|
await new system({
|
||||||
|
key: 'userIndex4job',
|
||||||
|
value: currentIndex
|
||||||
|
}).save();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量更新,每次十个
|
||||||
|
const userFundList = await Fund.find().sort('_id').limit(10).skip(currentIndex);
|
||||||
|
// job.touch();
|
||||||
|
mylog.info('touch');
|
||||||
|
if (!userFundList || !Array.isArray(userFundList)) return 0;
|
||||||
|
for (let i = 0, len = userFundList.length; i < len; ++i) {
|
||||||
|
const fund = userFundList[i]; // 一条数据库fund表记录
|
||||||
|
if (fund.lastMine && new Date(fund.lastMine).getDate() === new Date().getDate()) {
|
||||||
|
mylog.info('已经计算过...跳过');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const user = await User.findById(fund.userId).exec();
|
||||||
|
await doReward(user, fund);
|
||||||
|
// job.touch();
|
||||||
|
await system.updateOne({ key: 'userIndex4job' }, {
|
||||||
$set: {
|
$set: {
|
||||||
'option.usedGroupReward': rewardInvite, // 本次的奖励值累加到已领取团队奖励总和内
|
value: currentIndex
|
||||||
'asset.usdt': allInviteReward // 用来记录所有社群奖励总额
|
|
||||||
}
|
}
|
||||||
}).then(() => {
|
|
||||||
rewardMine > 0 && createAction(1, {
|
|
||||||
userId: fund.userId,
|
|
||||||
tokenType: 'vic',
|
|
||||||
amount: rewardMine,
|
|
||||||
remain: fund.asset
|
|
||||||
}).save();
|
|
||||||
rewardInvite > 0 && createAction(2, {
|
|
||||||
userId: fund.userId,
|
|
||||||
tokenType: 'vic',
|
|
||||||
amount: rewardInvite,
|
|
||||||
remain: fund.asset
|
|
||||||
}).save();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
mylog.info('收益计算分配完成');
|
||||||
|
// done('收益计算分配完成');
|
||||||
|
}
|
||||||
const jobs = [
|
const jobs = [
|
||||||
{
|
{
|
||||||
name: 'mine',
|
name: 'mine',
|
||||||
processor: async (job, done) => {
|
processor: mine,
|
||||||
//todo: 程序内检查是否可以执行,保证不会启动时错误的自动执行一次
|
|
||||||
mylog.info('[Schedule Job] 开始定时计算作业...');
|
|
||||||
let currentIndex = await system.findOne({
|
|
||||||
key: 'userIndex4job'
|
|
||||||
}).exec();
|
|
||||||
if (typeof currentIndex !== 'number') {
|
|
||||||
currentIndex = 0;
|
|
||||||
await new system({
|
|
||||||
key: 'userIndex4job',
|
|
||||||
value: currentIndex
|
|
||||||
}).save();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 批量更新,每次十个
|
|
||||||
const userFundList = await Fund.find().sort('_id').limit(10).skip(currentIndex);
|
|
||||||
job.touch();
|
|
||||||
if (!userFundList || !Array.isArray(userFundList)) return 0;
|
|
||||||
currentIndex += userFundList.length;
|
|
||||||
for (let i = 0, len = userFundList.length; i < len; ++i) {
|
|
||||||
const fundItem = userFundList[i]; // 一条数据库fund表记录
|
|
||||||
const funds = fundItem.fundList;
|
|
||||||
if (!funds || !Array.isArray(funds)) continue;
|
|
||||||
const user = await User.findOne({ id: fundItem.userId }).exec();
|
|
||||||
doReward(user, funds);
|
|
||||||
job.touch();
|
|
||||||
}
|
|
||||||
mylog.info('收益计算分配完成');
|
|
||||||
done('收益计算分配完成');
|
|
||||||
},
|
|
||||||
type: 'every',
|
type: 'every',
|
||||||
slot: '0 0 3 * * *',
|
slot: '0 0 3 * * *',
|
||||||
priority: 'highest',
|
priority: 'highest',
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ module.exports = {
|
|||||||
const actions = await Action.find({ userId, type: 1, createdAt: { $gt: start, $lt: now } });
|
const actions = await Action.find({ userId, type: 1, createdAt: { $gt: start, $lt: now } });
|
||||||
// 计算昨日收益
|
// 计算昨日收益
|
||||||
let revenueLastday = calRevenueLastday(actions);
|
let revenueLastday = calRevenueLastday(actions);
|
||||||
// 筛选有效矿晶
|
// 筛选有效矿晶
|
||||||
let activeFund = getActiveFund(fund.fundList);
|
let activeFund = getActiveFund(fund.fundList);
|
||||||
return res.json({
|
return res.json({
|
||||||
code: 0,
|
code: 0,
|
||||||
@@ -241,7 +241,7 @@ module.exports = {
|
|||||||
basic: {
|
basic: {
|
||||||
activeFund,
|
activeFund,
|
||||||
revenueLastday,
|
revenueLastday,
|
||||||
revenueAll: fund.asset && fund.asset['vic']
|
revenueAll: fund.asset && +fund.asset['vic'] - +fund.asset['usdt'] // 总金额 - 社区奖励 = 挖矿奖励
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -262,7 +262,7 @@ module.exports = {
|
|||||||
basic: {
|
basic: {
|
||||||
activeFund, //有效矿晶
|
activeFund, //有效矿晶
|
||||||
revenueLastday, // 昨日收益
|
revenueLastday, // 昨日收益
|
||||||
revenueAll: newFund.asset['vic'] // 累计总收益
|
revenueAll: +newFund.asset['vic'] - +newFund.asset['usdt'] // 累计总收益 - 社区奖励 = 挖矿奖励
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -297,7 +297,7 @@ module.exports = {
|
|||||||
data: {
|
data: {
|
||||||
activeFund,
|
activeFund,
|
||||||
revenueLastday,
|
revenueLastday,
|
||||||
revenueAll: fund.asset && fund.asset['vic']
|
revenueAll: fund.asset && +fund.asset['vic'] - +fund.asset['usdt'] // 总金额 - 社区奖励 = 挖矿奖励
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ const FundSchema = new mongoose.Schema({
|
|||||||
default: 0
|
default: 0
|
||||||
},
|
},
|
||||||
lastMine: {
|
lastMine: {
|
||||||
type: String
|
type: String,
|
||||||
|
default: new Date().toISOString()
|
||||||
},
|
},
|
||||||
lastDeposit: {
|
lastDeposit: {
|
||||||
type: String
|
type: String
|
||||||
|
|||||||
Reference in New Issue
Block a user