"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.WalletService = void 0; const common_1 = require("@nestjs/common"); const typeorm_1 = require("@nestjs/typeorm"); const typeorm_2 = require("typeorm"); const wallet_model_1 = require("../../../model/wallet.model"); const api_1 = require("../../common/api"); const system_config_service_1 = require("../../common/service/system_config.service"); const user_id_context_1 = require("../../common/context/user_id.context"); const data_source_context_1 = require("../../common/context/data_source.context"); const wallet_bill_model_1 = require("../../../model/wallet_bill.model"); const repository_help_1 = require("../../common/util/repository_help"); const balance_log_help_1 = require("../../common/util/balance_log_help"); let WalletService = class WalletService { walletModel; systemConfigService; constructor(walletModel, systemConfigService) { this.walletModel = walletModel; this.systemConfigService = systemConfigService; } async getWallet(userId) { const wallet = await this.walletModel.findOne({ where: { userId }, }); if (!wallet) { return api_1.Api.error('钱包不存在'); } return api_1.Api.success(wallet); } async transfer() { const userId = (0, user_id_context_1.getUserIdContext)(); const wallet = await this.walletModel.findOneByOrFail({ userId }); const config = await this.systemConfigService.readConfig(); const publicityFund = wallet.publicityFund; const minTransferPublicityAmount = config.minTransferPublicityAmount; if (Number(publicityFund) < Number(minTransferPublicityAmount)) { return api_1.Api.error(`宣传金满${Number(minTransferPublicityAmount).toLocaleString()}才能划转到余额`); } return data_source_context_1.DataSourceContext.startTransaction(async (ctx) => { const walletRepo = ctx.getRepository(wallet_model_1.WalletModel); const walletBillRepo = ctx.getRepository(wallet_bill_model_1.WalletBillModel); const count = await repository_help_1.RepositoryHelp.update(walletRepo, { userId, publicityFund: (0, typeorm_2.MoreThanOrEqual)(wallet.publicityFund), }, { publicityFund: '0', balance: () => `balance + ${wallet.publicityFund}`, }); if (!count) { return api_1.Api.error('宣传进不足'); } const updatedWallet = await walletRepo.findOneByOrFail({ id: wallet.id }); await balance_log_help_1.BalanceLogHelp.record(walletBillRepo, { userId, type: wallet_bill_model_1.WalletBillType.Transfer, before: wallet.balance, after: updatedWallet.balance, amount: wallet.publicityFund, remark: `宣传金划转,金额 ${wallet.publicityFund}`, }); return api_1.Api.success(); }); } }; exports.WalletService = WalletService; exports.WalletService = WalletService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, typeorm_1.InjectRepository)(wallet_model_1.WalletModel)), __metadata("design:paramtypes", [typeorm_2.Repository, system_config_service_1.SystemConfigService]) ], WalletService); //# sourceMappingURL=wallet.service.js.map