group EventSqlDao: EntitySqlDao;


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> user_token
, <prefix> current_version
, <prefix> is_active
, <prefix> created_by
, <prefix> created_date
, <prefix> updated_by
, <prefix> updated_date
>>

tableValues() ::= <<
  :eventType
, :userType
, :requested_date
, :effective_date
, :subscription_id
, :plan_name
, :phase_name
, :price_list_name
, :user_token
, :current_version
, :is_active
, :created_by
, :created_date
, :updated_by
, :updated_date
>>

getEventById() ::= <<
  select record_id, <fields()>
  from subscription_events
  where
      id = :id
  <AND_CHECK_TENANT()>
  ;
>>


updateVersion() ::= <<
    update subscription_events
    set
      current_version = :currentVersion
    where
      id = :id
    <AND_CHECK_TENANT()>
    ;
>>

unactiveEvent() ::= <<
    update subscription_events
    set
      is_active = 0
      , updated_by = :userName
      , updated_date = :updatedDate
    where
      id = :id
    <AND_CHECK_TENANT()>
    ;
>>

reactiveEvent() ::= <<
    update subscription_events
    set
      is_active = 1
      , updated_by = :userName
      , updated_date = :updatedDate
    where
      event_id = :eventId
    <AND_CHECK_TENANT()>
    ;
>>


getFutureActiveEventForSubscription() ::= <<
    select record_id, <fields()>
    from subscription_events
    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 record_id, <fields()>
    from subscription_events
    where
      subscription_id = :subscriptionId
    <AND_CHECK_TENANT()>
    order by
      effective_date asc
      , record_id asc
    ;      
>>
