|
@@ -5,6 +5,8 @@ import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
import { Repository } from 'typeorm';
|
|
|
import { PaymentService } from './payment';
|
|
|
import {MerchantEntity} from "../entity/merchant";
|
|
|
+import {CustomerEntity} from "../entity/customer";
|
|
|
+import {Utils} from "../../../comm/utils";
|
|
|
@Provide()
|
|
|
export class BusinessService extends BaseService {
|
|
|
@InjectEntityModel(BusinessEntity)
|
|
@@ -13,12 +15,17 @@ export class BusinessService extends BaseService {
|
|
|
@InjectEntityModel(MerchantEntity)
|
|
|
merchantEntity: Repository<MerchantEntity>;
|
|
|
|
|
|
+ @InjectEntityModel(CustomerEntity)
|
|
|
+ customerEntity: Repository<CustomerEntity>;
|
|
|
+
|
|
|
@Inject()
|
|
|
paymentService: PaymentService;
|
|
|
@Inject()
|
|
|
ctx;
|
|
|
@Config(ALL)
|
|
|
config;
|
|
|
+ @Inject()
|
|
|
+ utils: Utils;
|
|
|
@Init()
|
|
|
async init() {
|
|
|
await super.init();
|
|
@@ -29,32 +36,38 @@ export class BusinessService extends BaseService {
|
|
|
|
|
|
/**
|
|
|
* 添加或者更新当前商户的 business 客户信息
|
|
|
- * @param param
|
|
|
+ * @param params
|
|
|
* @returns
|
|
|
*/
|
|
|
- async add(param) {
|
|
|
- let merchantId = await this.getMerchantId(param);
|
|
|
+ async add(params) {
|
|
|
+ const { merchant } = this.ctx.admin;
|
|
|
+ const merchantId = merchant.mchId;
|
|
|
+ if (!params?.registration_date) {
|
|
|
+ params.registration_date = this.utils.getNewTime()
|
|
|
+ }
|
|
|
const custom = {
|
|
|
- ...param,
|
|
|
+ ...params,
|
|
|
out_user_id: merchantId,
|
|
|
- webhook_url: this.getWebhook_url(param),
|
|
|
+ webhook_url: this.getWebhook_url(params),
|
|
|
customer_type: 'COMPANY',
|
|
|
+ openApi: this.ctx.admin?.openApi || false
|
|
|
};
|
|
|
let res = await this.paymentService
|
|
|
.setChannel('SUNPAY')
|
|
|
.setCustomerInfo(custom);
|
|
|
if (merchantId) {
|
|
|
- param.merchantId = param?.mch_id || param?.mchId || merchantId
|
|
|
+ params.merchantId = params?.mch_id || params?.mchId || merchantId
|
|
|
}
|
|
|
await super.add({
|
|
|
- ...param,
|
|
|
- ...param.company,
|
|
|
+ ...params,
|
|
|
+ ...params.company,
|
|
|
customer_id: res.data.customer_id,
|
|
|
});
|
|
|
return res;
|
|
|
}
|
|
|
async update(param) {
|
|
|
- let merchantId = this.getMerchantId(param);
|
|
|
+ const { merchant } = this.ctx.admin;
|
|
|
+ const merchantId = merchant.mchId;
|
|
|
const custom = {
|
|
|
...param,
|
|
|
out_user_id: merchantId,
|
|
@@ -64,10 +77,7 @@ export class BusinessService extends BaseService {
|
|
|
const res = await this.paymentService
|
|
|
.setChannel('SUNPAY')
|
|
|
.setCustomerInfo(custom);
|
|
|
- await super.update({
|
|
|
- ...param,
|
|
|
- ...param.company,
|
|
|
- });
|
|
|
+ await super.update(param);
|
|
|
return res;
|
|
|
}
|
|
|
|
|
@@ -75,23 +85,6 @@ export class BusinessService extends BaseService {
|
|
|
this._isOpenApi = payload
|
|
|
}
|
|
|
|
|
|
- async getMerchantId(params) {
|
|
|
- let merchantId;
|
|
|
- if (this._isOpenApi) {
|
|
|
- merchantId = params.out_user_id;
|
|
|
- } else if (params?.mch_id || params?.mchId) {
|
|
|
- // const merchantInfo = await this.merchantEntity.findOneBy({
|
|
|
- // mchId: params?.mch_id || params?.mchId
|
|
|
- // })
|
|
|
- merchantId = params?.mch_id || params?.mchId
|
|
|
- } else {
|
|
|
- const { merchant } = this.ctx.admin;
|
|
|
- merchantId = merchant.mchId;
|
|
|
- }
|
|
|
-
|
|
|
- return merchantId
|
|
|
- }
|
|
|
-
|
|
|
getWebhook_url(params) {
|
|
|
if (this._isOpenApi) {
|
|
|
return params.webhook_url
|
|
@@ -99,10 +92,35 @@ export class BusinessService extends BaseService {
|
|
|
return this.config.callback.sunpay
|
|
|
}
|
|
|
|
|
|
-
|
|
|
async page(params: any) {
|
|
|
- const merchantId = await this.getMerchantId(params);
|
|
|
- const query = `SELECT * FROM business WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`;
|
|
|
+ const { merchant, roleIds } = this.ctx.admin;
|
|
|
+ const merchantId = merchant.mchId;
|
|
|
+ // 初始化查询
|
|
|
+ let query = '';
|
|
|
+ const queryParams: any[] = [];
|
|
|
+ if (roleIds.filter(id => [1, 3].includes(id)).length) {
|
|
|
+ // 角色为 1 或 3 时,仅查询 business 表
|
|
|
+ query = `
|
|
|
+ SELECT *
|
|
|
+ FROM business
|
|
|
+ WHERE business.merchantId = '${merchantId}'
|
|
|
+ ORDER BY business.merchantId ASC
|
|
|
+ `;
|
|
|
+ } else {
|
|
|
+ // 其他角色,关联查询 business 和 customer 表
|
|
|
+ const out_user_idIsNull = this.ctx.admin?.openApi ? `AND customer.out_user_id = '${merchantId}'` : `AND customer.out_user_id is null`;
|
|
|
+ query = `
|
|
|
+ SELECT business.*
|
|
|
+ FROM business
|
|
|
+ JOIN customer ON business.customer_id = customer.customer_id
|
|
|
+ WHERE business.merchantId = '${merchantId}'
|
|
|
+ ${out_user_idIsNull}
|
|
|
+ ORDER BY business.merchantId ASC
|
|
|
+ `;
|
|
|
+ }
|
|
|
+ // 打印调试信息
|
|
|
+ console.log('SQL Query:', query, 'Params:', queryParams, 'RoleIds:', roleIds);
|
|
|
+ // 执行分页查询
|
|
|
return this.sqlRenderPage(query, params, false);
|
|
|
}
|
|
|
}
|