|
@@ -1,5 +1,5 @@
|
|
|
import { BalanceEntity } from '../entity/balance';
|
|
|
-import { Inject, Provide } from '@midwayjs/decorator';
|
|
|
+import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
|
|
|
import { BaseService } from '@cool-midway/core';
|
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
import { Repository } from 'typeorm';
|
|
@@ -7,15 +7,14 @@ import * as _ from 'lodash';
|
|
|
import { BalanceQueue } from '../queue/balanceQueue';
|
|
|
import { Utils } from '../../../comm/utils';
|
|
|
import { MerchantEntity } from '../entity/merchant';
|
|
|
+import { WalletEntity } from '../entity/wallet';
|
|
|
|
|
|
@Provide()
|
|
|
+@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
|
|
export class BalanceService extends BaseService {
|
|
|
@InjectEntityModel(BalanceEntity)
|
|
|
balanceEntity: Repository<BalanceEntity>;
|
|
|
|
|
|
- @Inject()
|
|
|
- balanceQueue: BalanceQueue;
|
|
|
-
|
|
|
@Inject()
|
|
|
utils: Utils;
|
|
|
|
|
@@ -25,88 +24,32 @@ export class BalanceService extends BaseService {
|
|
|
@InjectEntityModel(MerchantEntity)
|
|
|
merchantEntity: Repository<MerchantEntity>;
|
|
|
|
|
|
- async queryBalance(query) {
|
|
|
- let { mchId = '' } = query;
|
|
|
- const { roleIds, userId } = this.ctx.admin;
|
|
|
- if (this.utils.isMerchant(roleIds)) {
|
|
|
- const merchant = await this.merchantEntity.findOneBy({ userId });
|
|
|
- if (merchant) {
|
|
|
- mchId = merchant.mchId;
|
|
|
- } else {
|
|
|
- mchId = '-1';
|
|
|
- }
|
|
|
- }
|
|
|
- const sql = `SELECT a.* FROM dj_mch_balance a
|
|
|
- WHERE 1 = 1
|
|
|
- ${
|
|
|
- this.utils.isMerchant(roleIds)
|
|
|
- ? this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])
|
|
|
- : this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])
|
|
|
- }
|
|
|
- `;
|
|
|
- return this.nativeQuery(sql);
|
|
|
- }
|
|
|
+ @InjectEntityModel(WalletEntity)
|
|
|
+ walletEntity: Repository<WalletEntity>;
|
|
|
|
|
|
- async add(param) {
|
|
|
- param.orderNo =
|
|
|
- param.orderNo || this.utils.createOrderNo(this.getPrefix(param.type));
|
|
|
- return new Promise(resolve => {
|
|
|
- this.balanceQueue.add(param);
|
|
|
- resolve(param);
|
|
|
+ async addBalance(param) {
|
|
|
+ const data = await this.walletEntity.findOneBy({
|
|
|
+ mchId: param.mchId,
|
|
|
+ currency: param.currency
|
|
|
});
|
|
|
- }
|
|
|
-
|
|
|
- async summary(query) {
|
|
|
- let { orderNo = '', mchId = '', type = '', createTime = [] } = query;
|
|
|
- const { roleIds, userId } = this.ctx.admin;
|
|
|
- if (this.utils.isMerchant(roleIds)) {
|
|
|
- const merchant = await this.merchantEntity.findOneBy({ userId });
|
|
|
- if (merchant) {
|
|
|
- mchId = merchant.mchId;
|
|
|
- } else {
|
|
|
- mchId = '-1';
|
|
|
- }
|
|
|
+ if (data) {
|
|
|
+ await this.balanceEntity.insert(param);
|
|
|
+ data.balance = +data.balance + +param.amount;
|
|
|
+ await this.walletEntity.save(data);
|
|
|
+ } else {
|
|
|
+ await this.balanceEntity.insert(param);
|
|
|
+ await this.walletEntity.save({
|
|
|
+ mchId: param.mchId,
|
|
|
+ currency: param.currency,
|
|
|
+ balance: +param.amount,
|
|
|
+ freeze: 0
|
|
|
+ })
|
|
|
}
|
|
|
- const sql = `SELECT
|
|
|
- SUM(IF(a.type=1, a.amount, 0)) as num1,
|
|
|
- SUM(IF(a.type=2, a.amount, 0)) as num2,
|
|
|
- SUM(IF(a.type=3, a.amount, 0)) as num3,
|
|
|
- SUM(IF(a.type=4, a.amount, 0)) as num4
|
|
|
- FROM dj_balance a WHERE 1=1
|
|
|
- ${this.setSql(orderNo, 'and a.orderNo like ?', [`%${orderNo}%`])}
|
|
|
- ${
|
|
|
- this.utils.isMerchant(roleIds)
|
|
|
- ? this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])
|
|
|
- : this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])
|
|
|
- }
|
|
|
- ${this.setSql(type, 'and a.type = ?', [type])}
|
|
|
- ${this.setSql(
|
|
|
- createTime && createTime.length === 2,
|
|
|
- 'and (a.createTime between ? and ?)',
|
|
|
- createTime
|
|
|
- )}
|
|
|
- `;
|
|
|
- const data = (await this.nativeQuery(sql))[0] || {
|
|
|
- num1: 0,
|
|
|
- num2: 0,
|
|
|
- num3: 0,
|
|
|
- num4: 0,
|
|
|
- num5: 0,
|
|
|
- };
|
|
|
- const bsql = `SELECT
|
|
|
- SUM(a.balance) as balance
|
|
|
- FROM pay_mch_balance a WHERE 1=1
|
|
|
- ${this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])}
|
|
|
- `;
|
|
|
- const data2 = (await this.nativeQuery(bsql))[0] || { balance: 0 };
|
|
|
- data.num5 = data2.balance;
|
|
|
- return data;
|
|
|
}
|
|
|
|
|
|
async page(query) {
|
|
|
- let { orderNo = '', mchId = '', type = '', createTime = [] } = query;
|
|
|
+ let { orderNo = '', mchId = '', type = '', currency = '', createTime = [] } = query;
|
|
|
const { roleIds, userId } = this.ctx.admin;
|
|
|
- console.log(userId, roleIds);
|
|
|
if (this.utils.isMerchant(roleIds)) {
|
|
|
const merchant = await this.merchantEntity.findOneBy({ userId });
|
|
|
if (merchant) {
|
|
@@ -117,12 +60,12 @@ export class BalanceService extends BaseService {
|
|
|
}
|
|
|
const sql = `SELECT * FROM dj_balance a WHERE 1=1
|
|
|
${this.setSql(orderNo, 'and a.orderNo like ?', [`%${orderNo}%`])}
|
|
|
- ${
|
|
|
- this.utils.isMerchant(roleIds)
|
|
|
- ? this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])
|
|
|
- : this.setSql(mchId, 'and a.mchId like ?', [`%${mchId}%`])
|
|
|
+ ${this.utils.isMerchant(roleIds)
|
|
|
+ ? this.setSql(mchId, 'and a.mchId = ?', [`${mchId}`])
|
|
|
+ : this.setSql(mchId, 'and a.mchId like ?', [`%${mchId}%`])
|
|
|
}
|
|
|
${this.setSql(type, 'and a.type = ?', [type])}
|
|
|
+ ${this.setSql(currency, 'and a.currency like ?', [`%${currency}%`])}
|
|
|
${this.setSql(
|
|
|
createTime && createTime.length === 2,
|
|
|
'and (a.createTime between ? and ?)',
|
|
@@ -131,16 +74,4 @@ export class BalanceService extends BaseService {
|
|
|
`;
|
|
|
return this.sqlRenderPage(sql, query);
|
|
|
}
|
|
|
-
|
|
|
- getPrefix(type) {
|
|
|
- if (+type === 2) {
|
|
|
- return 'XF';
|
|
|
- } else if (+type === 3) {
|
|
|
- return 'CZ';
|
|
|
- } else if (+type === 4) {
|
|
|
- return 'NC';
|
|
|
- } else {
|
|
|
- return 'DS';
|
|
|
- }
|
|
|
- }
|
|
|
}
|