123 lines
5.9 KiB
JavaScript
123 lines
5.9 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.WithdrawService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
const typeorm_2 = require("typeorm");
|
|
const withdraw_model_1 = require("../../../model/withdraw.model");
|
|
const wallet_model_1 = require("../../../model/wallet.model");
|
|
const user_bank_model_1 = require("../../../model/user_bank.model");
|
|
const wallet_bill_model_1 = require("../../../model/wallet_bill.model");
|
|
const api_1 = require("../../common/api");
|
|
const user_id_context_1 = require("../../common/context/user_id.context");
|
|
const data_source_context_1 = require("../../common/context/data_source.context");
|
|
const balance_log_help_1 = require("../../common/util/balance_log_help");
|
|
const system_config_service_1 = require("../../common/service/system_config.service");
|
|
let WithdrawService = class WithdrawService {
|
|
withdrawModel;
|
|
walletModel;
|
|
userBankModel;
|
|
walletBillModel;
|
|
systemConfigService;
|
|
constructor(withdrawModel, walletModel, userBankModel, walletBillModel, systemConfigService) {
|
|
this.withdrawModel = withdrawModel;
|
|
this.walletModel = walletModel;
|
|
this.userBankModel = userBankModel;
|
|
this.walletBillModel = walletBillModel;
|
|
this.systemConfigService = systemConfigService;
|
|
}
|
|
async applyWithdraw(amount, userBankId) {
|
|
const userId = (0, user_id_context_1.getUserIdContext)();
|
|
const amountNum = parseFloat(amount);
|
|
if (isNaN(amountNum) || amountNum <= 0) {
|
|
return api_1.Api.error('无效的提现金额');
|
|
}
|
|
const systemConfig = await this.systemConfigService.readConfig();
|
|
const minWithdrawAmount = systemConfig.minWithdrawAmount;
|
|
if (parseFloat(amount) < parseFloat(minWithdrawAmount)) {
|
|
return api_1.Api.error(`最低提现金额为 ${Number(minWithdrawAmount).toLocaleString()} 元`);
|
|
}
|
|
const userBank = await this.userBankModel.findOne({
|
|
where: { id: userBankId, userId },
|
|
});
|
|
if (!userBank) {
|
|
return api_1.Api.error('银行卡不存在');
|
|
}
|
|
const wallet = await this.walletModel.findOne({
|
|
where: { userId },
|
|
});
|
|
if (!wallet) {
|
|
return api_1.Api.error('用户钱包不存在');
|
|
}
|
|
await data_source_context_1.DataSourceContext.startTransaction(async (transaction) => {
|
|
const walletRepo = transaction.getRepository(wallet_model_1.WalletModel);
|
|
const walletBillRepo = transaction.getRepository(wallet_bill_model_1.WalletBillModel);
|
|
const withdrawRepo = transaction.getRepository(withdraw_model_1.WithdrawModel);
|
|
const updateResult = await walletRepo
|
|
.createQueryBuilder('wallet')
|
|
.update(wallet_model_1.WalletModel)
|
|
.set({
|
|
balance: () => `balance - ${amount}`,
|
|
frozenBalance: () => `frozenBalance + ${amount}`,
|
|
})
|
|
.where('id = :id', { id: wallet.id })
|
|
.andWhere('balance >= :amount', { amount })
|
|
.execute();
|
|
if (updateResult.affected === 0) {
|
|
throw new common_1.HttpException('余额不足', common_1.HttpStatus.BAD_REQUEST);
|
|
}
|
|
const updatedWallet = await walletRepo.findOne({
|
|
where: { id: wallet.id },
|
|
});
|
|
if (!updatedWallet) {
|
|
throw new Error('钱包不存在');
|
|
}
|
|
await balance_log_help_1.BalanceLogHelp.record(walletBillRepo, {
|
|
userId,
|
|
type: wallet_bill_model_1.WalletBillType.Withdrawal,
|
|
before: wallet.balance,
|
|
after: updatedWallet.balance,
|
|
amount,
|
|
remark: `提现申请,金额 ${amount}`,
|
|
});
|
|
const withdraw = withdrawRepo.create({
|
|
userId,
|
|
amount,
|
|
actualAmount: amount,
|
|
userBankId,
|
|
status: withdraw_model_1.WithdrawStatus.Pending,
|
|
cardNo: userBank.cardNo,
|
|
bankName: userBank.bankName,
|
|
phone: userBank.phone,
|
|
});
|
|
await withdrawRepo.save(withdraw);
|
|
});
|
|
return api_1.Api.success();
|
|
}
|
|
};
|
|
exports.WithdrawService = WithdrawService;
|
|
exports.WithdrawService = WithdrawService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__param(0, (0, typeorm_1.InjectRepository)(withdraw_model_1.WithdrawModel)),
|
|
__param(1, (0, typeorm_1.InjectRepository)(wallet_model_1.WalletModel)),
|
|
__param(2, (0, typeorm_1.InjectRepository)(user_bank_model_1.UserBankModel)),
|
|
__param(3, (0, typeorm_1.InjectRepository)(wallet_bill_model_1.WalletBillModel)),
|
|
__metadata("design:paramtypes", [typeorm_2.Repository,
|
|
typeorm_2.Repository,
|
|
typeorm_2.Repository,
|
|
typeorm_2.Repository,
|
|
system_config_service_1.SystemConfigService])
|
|
], WithdrawService);
|
|
//# sourceMappingURL=withdraw.service.js.map
|