This commit is contained in:
vastxie
2024-07-07 13:09:08 +08:00
parent 086e5aed3c
commit 4fef3663e4
1131 changed files with 11143 additions and 10769 deletions

View File

@@ -25,7 +25,6 @@ const accountLog_entity_1 = require("./accountLog.entity");
const balance_entity_1 = require("./balance.entity");
const userBalance_entity_1 = require("./userBalance.entity");
const date_1 = require("../../common/utils/date");
const whiteList_entity_1 = require("../chat/whiteList.entity");
const chatGroup_entity_1 = require("../chatGroup/chatGroup.entity");
const chatLog_entity_1 = require("../chatLog/chatLog.entity");
const midjourney_entity_1 = require("../midjourney/midjourney.entity");
@@ -34,7 +33,7 @@ const salesUsers_entity_1 = require("../sales/salesUsers.entity");
const user_entity_1 = require("../user/user.entity");
const fingerprint_entity_1 = require("./fingerprint.entity");
let UserBalanceService = class UserBalanceService {
constructor(balanceEntity, userBalanceEntity, accountLogEntity, cramiPackageEntity, configEntity, userEntity, salesUsersEntity, whiteListEntity, fingerprintLogEntity, chatGroupEntity, chatLogEntity, midjourneyEntity, salesService, globalConfigService) {
constructor(balanceEntity, userBalanceEntity, accountLogEntity, cramiPackageEntity, configEntity, userEntity, salesUsersEntity, fingerprintLogEntity, chatGroupEntity, chatLogEntity, midjourneyEntity, salesService, globalConfigService) {
this.balanceEntity = balanceEntity;
this.userBalanceEntity = userBalanceEntity;
this.accountLogEntity = accountLogEntity;
@@ -42,7 +41,6 @@ let UserBalanceService = class UserBalanceService {
this.configEntity = configEntity;
this.userEntity = userEntity;
this.salesUsersEntity = salesUsersEntity;
this.whiteListEntity = whiteListEntity;
this.fingerprintLogEntity = fingerprintLogEntity;
this.chatGroupEntity = chatGroupEntity;
this.chatLogEntity = chatLogEntity;
@@ -88,17 +86,28 @@ let UserBalanceService = class UserBalanceService {
model4Count = model4Count + configMap.registerSendModel4Count;
drawMjCount = drawMjCount + configMap.registerSendDrawMjCount;
}
if (configMap.registerSendStatus === 1 && configMap.firstRegisterSendStatus === 1 && userId <= configMap.firstRegisterSendRank) {
if (configMap.registerSendStatus === 1 &&
configMap.firstRegisterSendStatus === 1 &&
userId <= configMap.firstRegisterSendRank) {
model3Count = model3Count + configMap.firstRregisterSendModel3Count;
model4Count = model4Count + configMap.firstRregisterSendModel4Count;
drawMjCount = drawMjCount + configMap.firstRregisterSendDrawMjCount;
}
await this.saveRecordRechargeLog({ userId, rechargeType: balance_constant_1.RechargeType.REG_GIFT, model3Count, drawMjCount, model4Count });
await this.saveRecordRechargeLog({
userId,
rechargeType: balance_constant_1.RechargeType.REG_GIFT,
model3Count,
drawMjCount,
model4Count,
});
if (invitedId) {
if (Number(configMap.inviteSendStatus) === 1) {
model3Count = model3Count + Number(configMap.invitedGuestSendModel3Count);
model4Count = model4Count + Number(configMap.invitedGuestSendModel4Count);
drawMjCount = drawMjCount + Number(configMap.invitedGuestSendDrawMjCount);
model3Count =
model3Count + Number(configMap.invitedGuestSendModel3Count);
model4Count =
model4Count + Number(configMap.invitedGuestSendModel4Count);
drawMjCount =
drawMjCount + Number(configMap.invitedGuestSendDrawMjCount);
await this.saveRecordRechargeLog({
userId,
rechargeType: balance_constant_1.RechargeType.INVITE_GIFT,
@@ -120,7 +129,13 @@ let UserBalanceService = class UserBalanceService {
});
}
}
await this.userBalanceEntity.save({ userId, model3Count, model4Count, drawMjCount, useTokens: 0 });
await this.userBalanceEntity.save({
userId,
model3Count,
model4Count,
drawMjCount,
useTokens: 0,
});
}
catch (error) {
console.log('error: ', error);
@@ -136,11 +151,25 @@ let UserBalanceService = class UserBalanceService {
if (role === 'visitor') {
return this.validateVisitorBalance(req, type, amount);
}
const res = await this.configEntity.findOne({ where: { configKey: 'vxNumber' } });
const res = await this.configEntity.findOne({
where: { configKey: 'vxNumber' },
});
const vxNumber = res ? res.configVal : '---';
const memberKey = type === 1 ? 'memberModel3Count' : type === 2 ? 'memberModel4Count' : type === 3 ? 'memberDrawMjCount' : null;
const baseKey = type === 1 ? 'model3Count' : type === 2 ? 'model4Count' : type === 3 ? 'drawMjCount' : null;
if (b.packageId && b[memberKey] < amount) {
const memberKey = type === 1
? 'memberModel3Count'
: type === 2
? 'memberModel4Count'
: type === 3
? 'memberDrawMjCount'
: null;
const baseKey = type === 1
? 'model3Count'
: type === 2
? 'model4Count'
: type === 3
? 'drawMjCount'
: null;
if (b.packageId && b[memberKey] + b[baseKey] < amount) {
if (b[baseKey] < amount) {
throw new common_1.HttpException(`您的账户余额不足,如果想继续体验服务,请联系管理员或购买专属套餐 `, common_1.HttpStatus.PAYMENT_REQUIRED);
}
@@ -152,21 +181,33 @@ let UserBalanceService = class UserBalanceService {
}
async validateVisitorBalance(req, type, amount) {
const { id } = req.user;
const baseKey = type === 1 ? 'model3Count' : type === 2 ? 'model4Count' : type === 3 ? 'drawMjCount' : null;
const baseKey = type === 1
? 'model3Count'
: type === 2
? 'model4Count'
: type === 3
? 'drawMjCount'
: null;
const now = new Date();
const log = await this.fingerprintLogEntity.findOne({ where: { fingerprint: id } });
const { visitorModel3Num, visitorModel4Num, visitorMJNum } = await this.globalConfigService.getConfigs(['visitorModel3Num', 'visitorModel4Num', 'visitorMJNum']);
const log = await this.fingerprintLogEntity.findOne({
where: { fingerprint: id },
});
const { visitorModel3Num, visitorModel4Num, visitorMJNum } = await this.globalConfigService.getConfigs([
'visitorModel3Num',
'visitorModel4Num',
'visitorMJNum',
]);
const settings = {
model3Count: visitorModel3Num ? Number(visitorModel3Num) : 0,
model4Count: visitorModel4Num ? Number(visitorModel4Num) : 0,
drawMjCount: visitorMJNum ? Number(visitorMJNum) : 0
drawMjCount: visitorMJNum ? Number(visitorMJNum) : 0,
};
if (!log) {
let data = {
fingerprint: id,
model3Count: 0,
model4Count: 0,
drawMjCount: 0
drawMjCount: 0,
};
data[baseKey] = data[baseKey] + amount;
if (data[baseKey] > settings[baseKey]) {
@@ -182,7 +223,7 @@ let UserBalanceService = class UserBalanceService {
let data = {
model3Count,
model4Count,
drawMjCount
drawMjCount,
};
const date = Number(new Date(log.updatedAt));
const isUpdateLastDay = this.isUpdatedToday(date);
@@ -193,7 +234,7 @@ let UserBalanceService = class UserBalanceService {
data = {
model3Count: 0,
model4Count: 0,
drawMjCount: 0
drawMjCount: 0,
};
data[baseKey] = data[baseKey] + amount;
}
@@ -216,37 +257,40 @@ let UserBalanceService = class UserBalanceService {
if (!b) {
throw new common_1.HttpException('缺失当前用户账户记录!', common_1.HttpStatus.BAD_REQUEST);
}
const memberKey = deductionType === 1
? 'memberModel3Count'
: deductionType === 2
? 'memberModel4Count'
: deductionType === 3
? 'memberDrawMjCount'
: null;
const baseKey = deductionType === 1 ? 'model3Count' : deductionType === 2 ? 'model4Count' : deductionType === 3 ? 'drawMjCount' : null;
const updateKey = b.packageId && b[memberKey] < amount ? baseKey : b.packageId ? memberKey : baseKey;
let useKey = null;
if (updateKey.includes('odel3')) {
useKey = 'useModel3Token';
}
if (updateKey.includes('odel4')) {
useKey = 'useModel4Token';
}
if (updateKey.includes('MjCount')) {
useKey = 'useDrawMjToken';
const keys = {
1: {
member: 'memberModel3Count',
nonMember: 'model3Count',
token: 'useModel3Token',
},
2: {
member: 'memberModel4Count',
nonMember: 'model4Count',
token: 'useModel4Token',
},
3: {
member: 'memberDrawMjCount',
nonMember: 'drawMjCount',
token: 'useDrawMjToken',
},
};
const { member, nonMember, token } = keys[deductionType];
let remainingAmount = amount;
let newMemberBalance = Math.max(b[member] - remainingAmount, 0);
remainingAmount -= b[member] - newMemberBalance;
let newNonMemberBalance = b[nonMember];
if (remainingAmount > 0) {
newNonMemberBalance = Math.max(b[nonMember] - remainingAmount, 0);
remainingAmount -= b[nonMember] - newNonMemberBalance;
}
const updateBalance = {
[updateKey]: b[updateKey] - amount < 0 ? 0 : b[updateKey] - amount,
[useKey]: b[useKey] + UseAmount,
[member]: newMemberBalance,
[nonMember]: newNonMemberBalance,
[token]: (b[token] || 0) + UseAmount,
};
if (useKey === 'useModel3Token') {
updateBalance['useModel3Count'] = b['useModel3Count'] + amount;
}
if (useKey === 'useModel4Token') {
updateBalance['useModel4Count'] = b['useModel4Count'] + amount;
}
if (useKey === 'useDrawMjToken') {
updateBalance['useDrawMjToken'] = b['useDrawMjToken'] + amount;
if (token === 'useModel3Token' || token === 'useModel4Token') {
updateBalance[token.replace('Token', 'Count')] =
(b[token.replace('Token', 'Count')] || 0) + amount;
}
const result = await this.userBalanceEntity.update({ userId }, updateBalance);
if (result.affected === 0) {
@@ -282,10 +326,18 @@ let UserBalanceService = class UserBalanceService {
throw new common_1.HttpException('查询当前用户余额失败!', common_1.HttpStatus.BAD_REQUEST);
}
}
res.sumModel3Count = res.packageId ? res.model3Count + res.memberModel3Count : res.model3Count;
res.sumModel4Count = res.packageId ? res.model4Count + res.memberModel4Count : res.model4Count;
res.sumDrawMjCount = res.packageId ? res.drawMjCount + res.memberDrawMjCount : res.drawMjCount;
res.expirationTime = res.expirationTime ? (0, date_1.formatDate)(res.expirationTime, 'YYYY-MM-DD') : null;
res.sumModel3Count = res.packageId
? res.model3Count + res.memberModel3Count
: res.model3Count;
res.sumModel4Count = res.packageId
? res.model4Count + res.memberModel4Count
: res.model4Count;
res.sumDrawMjCount = res.packageId
? res.drawMjCount + res.memberDrawMjCount
: res.drawMjCount;
res.expirationTime = res.expirationTime
? (0, date_1.formatDate)(res.expirationTime, 'YYYY-MM-DD')
: null;
return res;
}
catch (error) {
@@ -293,35 +345,53 @@ let UserBalanceService = class UserBalanceService {
}
}
async saveRecordRechargeLog(logInfo) {
const { userId, rechargeType, model3Count, model4Count, drawMjCount, days = -1, pkgName = '', extent = '' } = logInfo;
const { userId, rechargeType, model3Count, model4Count, drawMjCount, days = -1, pkgName = '', extent = '', } = logInfo;
if (!userId) {
throw new common_1.HttpException('当前用户不存在,记录充值日志异常', common_1.HttpStatus.BAD_REQUEST);
}
const uid = (0, utils_1.createRandomUid)();
return await this.accountLogEntity.save({ userId, rechargeType, model3Count, model4Count, drawMjCount, days, extent, uid, pkgName });
return await this.accountLogEntity.save({
userId,
rechargeType,
model3Count,
model4Count,
drawMjCount,
days,
extent,
uid,
pkgName,
});
}
async createBaseUserBalance(userId, userBalanceInfo = {}) {
const { model3Count = 0, model4Count = 0, drawMjCount = 0 } = userBalanceInfo;
const { model3Count = 0, model4Count = 0, drawMjCount = 0, } = userBalanceInfo;
const balance = await this.userBalanceEntity.findOne({ where: { userId } });
if (balance) {
throw new common_1.HttpException('当前用户无需创建账户信息!', common_1.HttpStatus.BAD_REQUEST);
}
return await this.userBalanceEntity.save({ userId, model3Count, model4Count, drawMjCount });
return await this.userBalanceEntity.save({
userId,
model3Count,
model4Count,
drawMjCount,
});
}
async addBalanceToUser(userId, balance, days = -1) {
try {
const userBalanceInfo = (await this.userBalanceEntity.findOne({ where: { userId } })) || (await this.createBaseUserBalance(userId));
const userBalanceInfo = (await this.userBalanceEntity.findOne({ where: { userId } })) ||
(await this.createBaseUserBalance(userId));
if (!userBalanceInfo) {
throw new common_1.HttpException('查询用户账户信息失败!', common_1.HttpStatus.BAD_REQUEST);
}
const { model3Count, model4Count, drawMjCount, memberModel3Count, memberModel4Count, memberDrawMjCount } = userBalanceInfo;
const { model3Count, model4Count, drawMjCount, memberModel3Count, memberModel4Count, memberDrawMjCount, } = userBalanceInfo;
let params = {};
if (days > 0) {
const { packageId } = balance;
if (!packageId) {
throw new common_1.HttpException('缺失当前套餐ID、充值失败', common_1.HttpStatus.BAD_REQUEST);
}
const pkgInfo = await this.cramiPackageEntity.findOne({ where: { id: packageId } });
const pkgInfo = await this.cramiPackageEntity.findOne({
where: { id: packageId },
});
if (!pkgInfo) {
throw new common_1.HttpException('当前套餐不存在!', common_1.HttpStatus.BAD_REQUEST);
}
@@ -338,7 +408,9 @@ let UserBalanceService = class UserBalanceService {
};
}
else {
const curPackageInfo = await this.cramiPackageEntity.findOne({ where: { id: userBalanceInfo.packageId } });
const curPackageInfo = await this.cramiPackageEntity.findOne({
where: { id: userBalanceInfo.packageId },
});
if (weight >= curPackageInfo.weight) {
params = {
memberModel3Count: memberModel3Count + balance.model3Count,
@@ -380,11 +452,13 @@ let UserBalanceService = class UserBalanceService {
console.log('充值的工单信息:', order);
try {
const { userId, goodsId } = order;
const pkg = await this.cramiPackageEntity.findOne({ where: { id: order.goodsId, status: 1 } });
const pkg = await this.cramiPackageEntity.findOne({
where: { id: order.goodsId, status: 1 },
});
if (!pkg) {
throw new common_1.HttpException('非法操作、当前充值套餐暂不存在!', common_1.HttpStatus.BAD_REQUEST);
}
const { model3Count, model4Count, drawMjCount, days, name: pkgName } = pkg;
const { model3Count, model4Count, drawMjCount, days, name: pkgName, } = pkg;
const money = {
model3Count,
model4Count,
@@ -393,12 +467,24 @@ let UserBalanceService = class UserBalanceService {
packageId: order.goodsId,
};
await this.addBalanceToUser(userId, money, days);
await this.saveRecordRechargeLog({ userId, rechargeType: balance_constant_1.RechargeType.SCAN_PAY, model3Count, model4Count, drawMjCount, pkgName, days });
await this.saveRecordRechargeLog({
userId,
rechargeType: balance_constant_1.RechargeType.SCAN_PAY,
model3Count,
model4Count,
drawMjCount,
pkgName,
days,
});
const userInfo = await this.userEntity.findOne({ where: { id: userId } });
const { invitedBy } = userInfo;
if (invitedBy) {
const inviteUserInfo = await this.userEntity.findOne({ where: { inviteCode: invitedBy } });
const inviteUserSalesInfo = await this.salesUsersEntity.findOne({ where: { userId: inviteUserInfo.id } });
const inviteUserInfo = await this.userEntity.findOne({
where: { inviteCode: invitedBy },
});
const inviteUserSalesInfo = await this.salesUsersEntity.findOne({
where: { userId: inviteUserInfo.id },
});
if (!inviteUserInfo)
return;
const { id: inviterUserId } = inviteUserInfo;
@@ -449,7 +535,9 @@ let UserBalanceService = class UserBalanceService {
take: size,
});
const userIds = rows.map((item) => item.userId);
const userInfo = await this.userEntity.find({ where: { id: (0, typeorm_2.In)(userIds) } });
const userInfo = await this.userEntity.find({
where: { id: (0, typeorm_2.In)(userIds) },
});
rows.forEach((item) => {
const user = userInfo.find((user) => user.id === item.userId);
item.username = user === null || user === void 0 ? void 0 : user.username;
@@ -477,57 +565,6 @@ let UserBalanceService = class UserBalanceService {
async refundMjBalance(userId, amount) {
return await this.deductFromBalance(userId, 'mjDraw', -amount);
}
async upgradeBalance() {
const users = await this.userEntity.find();
if (!users.length)
return;
const upgradeStatus = await this.globalConfigService.getConfigs(['upgradeStatus']);
if (!upgradeStatus) {
await this.globalConfigService.setConfig({ settings: [{ configKey: 'upgradeStatus', configVal: '1' }] });
}
else {
throw new common_1.HttpException('您已经升级过了、请勿重复操作!', common_1.HttpStatus.BAD_REQUEST);
}
users.forEach((user) => {
const { id } = user;
this.balanceEntity.findOne({ where: { userId: id } }).then((res) => {
if (!res)
return;
this.writeOldBalanceToNewTable(id, res);
});
});
}
async writeOldBalanceToNewTable(userId, balanceInfo) {
const { balance = 0, usesLeft = 0, paintCount = 0, useTokens = 0, useChats = 0, usePaints = 0 } = balanceInfo;
const model4Info = await this.whiteListEntity.findOne({ where: { userId } });
const newBalanceInfo = {
userId,
model3Count: Number(usesLeft),
model4Count: (model4Info === null || model4Info === void 0 ? void 0 : model4Info.count) || 0,
drawMjCount: Number(balance),
useModel3Count: Number(useChats),
useModel4Count: (model4Info === null || model4Info === void 0 ? void 0 : model4Info.useCount) || 0,
useDrawMjCount: Number(usePaints),
useModel3Token: Number(useTokens),
useModel4Token: 0,
useDrawMjToken: 0,
};
const userBalanceInfo = await this.userBalanceEntity.findOne({ where: { userId } });
if (userBalanceInfo) {
common_1.Logger.debug(`用户${userId}账户信息已经存在、迁移无效`, 'BalanceService');
}
else {
this.userBalanceEntity
.save(newBalanceInfo)
.then((res) => {
common_1.Logger.debug(`用户${userId}旧账户信息迁移成功`, 'BalanceService');
})
.catch((error) => {
console.log('error: ', error);
common_1.Logger.debug(`用户${userId}旧账户信息迁移失败`, 'BalanceService');
});
}
}
async inheritVisitorData(req) {
const { fingerprint } = req.headers;
const { id: userId } = req.user;
@@ -538,9 +575,15 @@ let UserBalanceService = class UserBalanceService {
}
async getVisitorCount(req) {
const { fingerprint } = req.headers;
const countChat = await this.chatLogEntity.count({ where: { userId: fingerprint } });
const countChatGroup = await this.chatGroupEntity.count({ where: { userId: fingerprint } });
const countMj = await this.midjourneyEntity.count({ where: { userId: fingerprint } });
const countChat = await this.chatLogEntity.count({
where: { userId: fingerprint },
});
const countChatGroup = await this.chatGroupEntity.count({
where: { userId: fingerprint },
});
const countMj = await this.midjourneyEntity.count({
where: { userId: fingerprint },
});
return countChat || countChatGroup || countMj || 0;
}
};
@@ -553,11 +596,10 @@ UserBalanceService = __decorate([
__param(4, (0, typeorm_1.InjectRepository)(config_entity_1.ConfigEntity)),
__param(5, (0, typeorm_1.InjectRepository)(user_entity_1.UserEntity)),
__param(6, (0, typeorm_1.InjectRepository)(salesUsers_entity_1.SalesUsersEntity)),
__param(7, (0, typeorm_1.InjectRepository)(whiteList_entity_1.WhiteListEntity)),
__param(8, (0, typeorm_1.InjectRepository)(fingerprint_entity_1.FingerprintLogEntity)),
__param(9, (0, typeorm_1.InjectRepository)(chatGroup_entity_1.ChatGroupEntity)),
__param(10, (0, typeorm_1.InjectRepository)(chatLog_entity_1.ChatLogEntity)),
__param(11, (0, typeorm_1.InjectRepository)(midjourney_entity_1.MidjourneyEntity)),
__param(7, (0, typeorm_1.InjectRepository)(fingerprint_entity_1.FingerprintLogEntity)),
__param(8, (0, typeorm_1.InjectRepository)(chatGroup_entity_1.ChatGroupEntity)),
__param(9, (0, typeorm_1.InjectRepository)(chatLog_entity_1.ChatLogEntity)),
__param(10, (0, typeorm_1.InjectRepository)(midjourney_entity_1.MidjourneyEntity)),
__metadata("design:paramtypes", [typeorm_2.Repository,
typeorm_2.Repository,
typeorm_2.Repository,
@@ -569,7 +611,6 @@ UserBalanceService = __decorate([
typeorm_2.Repository,
typeorm_2.Repository,
typeorm_2.Repository,
typeorm_2.Repository,
sales_service_1.SalesService,
globalConfig_service_1.GlobalConfigService])
], UserBalanceService);