group EventSqlDao;

fields(prefix) ::= <<
    <prefix>id,
    <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
>>

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

insertEvent() ::= <<
    insert into subscription_events (<fields()>)
    values (
    :id
    , :eventType
    , :userType
    , :requestedDate
    , :effectiveDate
    , :subscriptionId
    , :planName
    , :phaseName
    , :priceListName
    , :userToken
    , :currentVersion
    , :isActive
    , :userName
    , :createdDate
    , :userName
    , :updatedDate
    );   
>>

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

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

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


getFutureActiveEventForSubscription() ::= <<
    select record_id, <fields()>
    from subscription_events
    where
      subscription_id = :subscriptionId
      and is_active = 1
      and effective_date > :now
    order by
      effective_date asc
      , record_id asc
    ;
>> 

getEventsForSubscription() ::= <<
    select record_id, <fields()>
    from subscription_events
    where
      subscription_id = :subscriptionId
    order by
      effective_date asc
      , record_id asc
    ;      
>>

getRecordId() ::= <<
    SELECT record_id
    FROM subscription_events
    WHERE id = :id;
>>

auditFields(prefix) ::= <<
    <prefix>table_name,
    <prefix>record_id,
    <prefix>change_type,
    <prefix>change_date,
    <prefix>changed_by,
    <prefix>reason_code,
    <prefix>comments,
    <prefix>user_token
>>

insertAuditFromTransaction() ::= <<
    INSERT INTO audit_log(<auditFields()>)
    VALUES(:tableName, :recordId, :changeType, :createdDate, :userName, :reasonCode, :comment, :userToken);
>>


