import { PaymentService } from './payment'; import { CustomerEntity } from './../entity/customer'; import { Init, Inject, Provide } from '@midwayjs/decorator'; import { BaseService, CoolCommException } from '@cool-midway/core'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { Repository } from 'typeorm'; import { PayeeEntity } from '../entity/payee'; import { MerchantEntity } from '../entity/merchant'; /** * 描述 */ @Provide() export class PayeeService extends BaseService { @InjectEntityModel(PayeeEntity) payeeEntity: Repository; @InjectEntityModel(CustomerEntity) customerEntity: Repository; @InjectEntityModel(MerchantEntity) merchantEntity: Repository; @Inject() paymentService: PaymentService; @Inject() ctx; /** * 添加收款人 * @param params * @returns */ async add(params: any) { let customer = await this.getCustomer(params); const merchant = await this.getMerchant(customer.merchantId); if (!customer) { throw new CoolCommException('客户不存在'); } const custom = { ...params, customer_id: customer.customer_id, beneficiary_type: customer.type ? 'COMPANY' : 'INDIVIDUAL', }; const res = await this.paymentService .setChannel(customer.channel) .addPayee(custom); params.beneficiary_id = res.data.id; params.customerId = customer.id; params.merchantId = merchant.mchId; params.channel = customer.channel; params.type = customer.type ? 'COMPANY' : 'INDIVIDUAL'; const payee = await super.add(params); return payee; } async update(params: any) { let customer = await this.getCustomer(params); const payee = await this.payeeEntity.findOneBy({ id: params.id, }); const custom = { ...params, customer_id: customer.customer_id, beneficiary_type: customer.type ? 'COMPANY' : 'INDIVIDUAL', id: payee.beneficiary_id, }; const res = await this.paymentService .setChannel(customer.channel) .updatePayee(custom); await super.update(params); return res; } async delete(params: any) { const res = await this.paymentService .setChannel(params.channel) .deletePayee(params); await super.delete(params); return res; } private _isOpenApi: boolean = false setIsOpenApi(payload = false) { this._isOpenApi = payload } async getCustomer(params) { let customer: CustomerEntity; if (this._isOpenApi) { customer = await this.customerEntity.findOneBy({ customer_id: params.customer_id, }); } else { const { merchant } = this.ctx.admin; customer = await this.customerEntity.findOneBy({ merchantId: merchant.id, }); } return customer } async getMerchant(id) { let merchant: MerchantEntity; if (this._isOpenApi) { merchant = await this.merchantEntity.findOneBy({ id: id, }); } else { merchant = this.ctx.admin.merchant } return merchant } async page(params: any) { const merchantId = await this.getMerchantId(params); const query = `SELECT * FROM payee WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`; return this.sqlRenderPage(query, params, false); } 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 // }) merchantId = params?.mch_id || params?.mchId } else { const { merchant } = this.ctx.admin; merchantId = merchant.mchId; } return merchantId } }