group EventSqlDao: EntitySqlDao;

tableName() ::= "subscription_events"

andCheckSoftDeletionWithComma(prefix) ::= "and <prefix>is_active"

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

tableFields(prefix) ::= <<
  <prefix> event_type
, <prefix> user_type
, <prefix> requested_date
, <prefix> effective_date
, <prefix> subscription_id
, <prefix> plan_name
, <prefix> phase_name
, <prefix> price_list_name
, <prefix> current_version
, <prefix> is_active
, <prefix> created_by
, <prefix> created_date
, <prefix> updated_by
, <prefix> updated_date
>>

tableValues() ::= <<
  :eventType
, :userType
, :requestedDate
, :effectiveDate
, :subscriptionId
, :planName
, :phaseName
, :priceListName
, :currentVersion
, :isActive
, :createdBy
, :createdDate
, :updatedBy
, :updatedDate
>>


updateVersion() ::= <<
update <tableName()>
set
current_version = :currentVersion
, updated_by = :createdBy
, updated_date = :createdDate
where
id = :id
<AND_CHECK_TENANT()>
;
>>

unactiveEvent() ::= <<
update <tableName()>
set
is_active = 0
, updated_by = :createdBy
, updated_date = :createdDate
where
id = :id
<AND_CHECK_TENANT()>
;
>>

reactiveEvent() ::= <<
update <tableName()>
set
is_active = 1
, updated_by = :createdBy
, updated_date = :createdDate
where
event_id = :eventId
<AND_CHECK_TENANT()>
;
>>



getFutureActiveEventForSubscription() ::= <<
select <allTableFields()>
, record_id as total_ordering
from <tableName()>
where
subscription_id = :subscriptionId
and is_active = 1
and effective_date > :now
<AND_CHECK_TENANT()>
order by
effective_date asc
, record_id asc
;
>> 

getEventsForSubscription() ::= <<
select <allTableFields()>
, record_id as total_ordering
from <tableName()>
where
subscription_id = :subscriptionId
<AND_CHECK_TENANT()>
order by
effective_date asc
, record_id asc
;
>>

