group DirectPaymentSqlDao: EntitySqlDao;

tableName() ::= "direct_payments"

historyTableName() ::= "direct_payment_history"

extraTableFieldsWithComma(prefix) ::= <<
, <prefix>record_id as payment_number
>>

defaultOrderBy(prefix) ::= <<
order by <prefix>created_date ASC, <recordIdField(prefix)> ASC
>>

tableFields(prefix) ::= <<
  <prefix>account_id
, <prefix>payment_method_id
, <prefix>external_key
, <prefix>current_state_name
, <prefix>created_by
, <prefix>created_date
, <prefix>updated_by
, <prefix>updated_date
>>

tableValues() ::= <<
  :accountId
, :paymentMethodId
, :externalKey
, :currentStateName
, :createdBy
, :createdDate
, :updatedBy
, :updatedDate
>>

updateDirectPaymentForNewTransaction() ::= <<
update <tableName()>
set updated_by = :updatedBy
, updated_date = :createdDate
where id = :id
<AND_CHECK_TENANT()>
;
>>

updateCurrentPaymentStateName() ::= <<
update <tableName()>
set current_state_name = :currentStateName
, updated_by = :updatedBy
, updated_date = :createdDate
where id = :id
<AND_CHECK_TENANT()>
;
>>

getDirectPaymentByExternalKey() ::= <<
select
<allTableFields("")>
from <tableName()>
where external_key = :externalKey
;
>>


getByPluginName() ::= <<
select
<allTableFields("t.")>
from <tableName()> t
join payment_methods pm on pm.id = t.payment_method_id
where pm.plugin_name = :pluginName
order by record_id
limit :offset, :rowCount
;
>>

getCountByPluginName() ::= <<
select
  count(1) as count
from <tableName()> t
join payment_methods pm on pm.id = t.payment_method_id
where pm.plugin_name = :pluginName
;
>>
