#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package Report::DB::Item::ReportNotify;
use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::Assert;
use base 'Common::DB::Item';

use constant kTable => 'report_notify';
use constant kDB    => Common::DB::Item::kClientDB();

sub GetAllByReportID {
    my $class    = shift;
    my $reportID = shift;

    assert($reportID);

    my $sql = "SELECT * FROM " . kTable . " WHERE report_id = " . $class->quote($reportID);

    return $class->SUPER::GetAll($sql);
}

sub AddUserID {
    my $class    = shift;
    my %args     = @_;
    my $userID   = $args{user_id};
    my $reportID = $args{report_id};

    assert($userID);
    assert($reportID);

    my $sql =
        "INSERT INTO "
      . kTable
      . " (report_id, active_user_id, date_created, date_modified) "
      . " VALUES ( "
      . $class->quote($reportID) . ', '
      . $class->quote($userID)
      . ', NOW(), NOW() )'
      . " ON DUPLICATE KEY UPDATE active_user_id = "
      . $class->quote($userID);

    my $dbo = Common::RSApp::GetClientDB();
    Log->debug("QUERY: $sql");

    $dbo->DoCmd($sql);
}

sub IsNotify {
    my $class = shift;
    my %args  = @_;

    my $userID   = $args{user_id};
    my $reportID = $args{report_id};

    assert($userID);
    assert($reportID);

    my $sql =
      "SELECT * FROM " . kTable . " WHERE report_id = " . $class->quote($reportID) . " AND active_user_id = " . $class->quote($userID);

    Log->debug("QUERY: $sql");
    my $collection = $class->SUPER::GetAll($sql);

    return $collection->hasNext() ? 1 : 0;
}

1;
