group PaymentSqlDao: EntitySqlDao;

tableName() ::= "payments"

historyTableName() ::= "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>state_name
, <prefix>created_by
, <prefix>created_date
, <prefix>updated_by
, <prefix>updated_date
>>

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

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

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

getPaymentByExternalKey() ::= <<
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 t.record_id asc
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
;
>>
