payee.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { PaymentService } from './payment';
  2. import { CustomerEntity } from './../entity/customer';
  3. import { Init, Inject, Provide } from '@midwayjs/decorator';
  4. import { BaseService, CoolCommException } from '@cool-midway/core';
  5. import { InjectEntityModel } from '@midwayjs/typeorm';
  6. import { Repository } from 'typeorm';
  7. import { PayeeEntity } from '../entity/payee';
  8. import { MerchantEntity } from '../entity/merchant';
  9. /**
  10. * 描述
  11. */
  12. @Provide()
  13. export class PayeeService extends BaseService {
  14. @InjectEntityModel(PayeeEntity)
  15. payeeEntity: Repository<PayeeEntity>;
  16. @InjectEntityModel(CustomerEntity)
  17. customerEntity: Repository<CustomerEntity>;
  18. @InjectEntityModel(MerchantEntity)
  19. merchantEntity: Repository<MerchantEntity>;
  20. @Inject()
  21. paymentService: PaymentService;
  22. @Inject()
  23. ctx;
  24. /**
  25. * 添加收款人
  26. * @param params
  27. * @returns
  28. */
  29. async add(params: any) {
  30. let customer = await this.getCustomer(params);
  31. const merchant = await this.getMerchant(customer.merchantId);
  32. if (!customer) {
  33. throw new CoolCommException('客户不存在');
  34. }
  35. const custom = {
  36. ...params,
  37. customer_id: customer.customer_id,
  38. beneficiary_type: customer.type ? 'COMPANY' : 'INDIVIDUAL',
  39. };
  40. const res = await this.paymentService
  41. .setChannel(customer.channel)
  42. .addPayee(custom);
  43. params.beneficiary_id = res.data.id;
  44. params.customerId = customer.id;
  45. params.merchantId = merchant.mchId;
  46. params.channel = customer.channel;
  47. params.type = customer.type ? 'COMPANY' : 'INDIVIDUAL';
  48. const payee = await super.add(params);
  49. return payee;
  50. }
  51. async update(params: any) {
  52. let customer = await this.getCustomer(params);
  53. const payee = await this.payeeEntity.findOneBy({
  54. id: params.id,
  55. });
  56. const custom = {
  57. ...params,
  58. customer_id: customer.customer_id,
  59. beneficiary_type: customer.type ? 'COMPANY' : 'INDIVIDUAL',
  60. id: payee.beneficiary_id,
  61. };
  62. const res = await this.paymentService
  63. .setChannel(customer.channel)
  64. .updatePayee(custom);
  65. await super.update(params);
  66. return res;
  67. }
  68. async delete(params: any) {
  69. const res = await this.paymentService
  70. .setChannel(params.channel)
  71. .deletePayee(params);
  72. await super.delete(params);
  73. return res;
  74. }
  75. private _isOpenApi: boolean = false
  76. setIsOpenApi(payload = false) {
  77. this._isOpenApi = payload
  78. }
  79. async getCustomer(params) {
  80. let customer: CustomerEntity;
  81. if (this._isOpenApi) {
  82. customer = await this.customerEntity.findOneBy({
  83. customer_id: params.customer_id,
  84. });
  85. } else {
  86. const { merchant } = this.ctx.admin;
  87. customer = await this.customerEntity.findOneBy({
  88. merchantId: merchant.id,
  89. });
  90. }
  91. return customer
  92. }
  93. async getMerchant(id) {
  94. let merchant: MerchantEntity;
  95. if (this._isOpenApi) {
  96. merchant = await this.merchantEntity.findOneBy({
  97. id: id,
  98. });
  99. } else {
  100. merchant = this.ctx.admin.merchant
  101. }
  102. return merchant
  103. }
  104. async page(params: any) {
  105. const merchantId = await this.getMerchantId(params);
  106. const query = `SELECT * FROM payee WHERE merchantId = '${merchantId}' ORDER BY merchantId ASC`;
  107. return this.sqlRenderPage(query, params, false);
  108. }
  109. async getMerchantId(params) {
  110. let merchantId;
  111. if (this._isOpenApi) {
  112. merchantId = params.out_user_id;
  113. } else if (params?.mch_id || params?.mchId) {
  114. // const merchantInfo = await this.merchantEntity.findOneBy({
  115. // mchId: params?.mch_id
  116. // })
  117. merchantId = params?.mch_id || params?.mchId
  118. } else {
  119. const { merchant } = this.ctx.admin;
  120. merchantId = merchant.mchId;
  121. }
  122. return merchantId
  123. }
  124. }