group EventSqlDao;

CHECK_TENANT() ::= "tenant_record_id = :tenantRecordId"
AND_CHECK_TENANT() ::= "AND <CHECK_TENANT()>"

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,
    <prefix>account_record_id,
    <prefix>tenant_record_id
>>

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

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

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
    ;      
>>

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

getById() ::= <<
    SELECT record_id, <fields()>
    FROM subscription_events
    WHERE id = :id <AND_CHECK_TENANT()>
    ;
>>

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,
    <prefix>account_record_id,
    <prefix>tenant_record_id
>>

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


