import { Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { MchChannelEntity } from '../entity/mchChannel';

@Provide()
export class MchChannelService extends BaseService {
  @InjectEntityModel(MchChannelEntity)
  mchChannelEntity: Repository<MchChannelEntity>;

  async page(query) {
    let { mchId = '', payType = '', code = '', status = '' } = query;
    const sql = `SELECT * FROM dj_mch_channel a WHERE 1=1
      ${this.setSql(mchId, 'and a.mchId like ?', [`%${mchId}%`])}
      ${this.setSql(payType, 'and a.payType like ?', [`%${payType}%`])}
      ${this.setSql(code, 'and a.code like ?', [`%${code}%`])}
      ${this.setSql(status, 'and a.status like ?', [`%${status}%`])}
    `;
    return this.sqlRenderPage(sql, query);
  }

  async getByMchIdAndType(mchId, payType, currency) {
    return await this.mchChannelEntity.findBy({
      mchId,
      payType,
      currency,
      status: 1
    })
  }
}
