package Opals::Ebook;

require Exporter;
@ISA       = qw(Exporter);
# Symbols to be exported by default
#@EXPORT    = qw(
#    opl_
#);
# Symbols to be exported on request
@EXPORT_OK = qw(

    eb_deleteEbook

    eb_getMarcXmlRid
    eb_getBidByMarcXmlRid
    eb_setMarcXmlRid


    eb_bookShelves_add
    eb_bookShelves_delById
    eb_bookShelves_delByUidBid
    eb_bookShelves_delByUidRid
    eb_bookShelves_get

    eb_bookmark_save
    eb_bookmark_get
    eb_bookmark_del

    eb_saveLicense
    eb_getLicense
    eb_deleteLicense
    eb_getLicenseList
    eb_getLicenseLog
);


# Version number
$VERSION   = 0.01;

use Time::localtime;

use Opals::Date qw(
 date_text
 );
use Opals::Template qw(
 tmpl_rangedPageList
);

#use utf8;
use strict;
use Date::Calc::Object qw(
    :all
);
########################################################################################
sub eb_deleteEbook{
    my ($dbh,$bid)=@_;

    # eb_licMgnt, opl_ridBid
   return if(!$bid);
   
    #eb_licMgnt
   $dbh->do("update eb_licMgnt set deleted =1 where bid=$bid");
   
    # eb_ridBid      
    $dbh->do("delete from  opl_ridBid where  bid =$bid");
    

}########################################################################################
sub  _LIC_add{
    my ($dbh,$uid,$licInfo)=@_;

    my $sth=$dbh->prepare("insert into eb_licMgnt set uid=?,bid=?,licenseType=?,noLicense=?,
                                                      datePurchase=?,poNum=?,vendor=?,
                                                      startDate=?,endDate=?,note=?");

        my $bid             =  ($licInfo->{'bid'})          || 0;
        my $licenseType     =  ($licInfo->{'licenseType'})  || "paid";
        my $noLicense       =  ($licInfo->{'noLicense'})    || 0;
        my $datePurchase    =  ($licInfo->{'datePurchase'}) || "";
        my $poNum           =  ($licInfo->{'poNum'})        || "";
        my $vendor          =  ($licInfo->{'vendor'})       || "";
        my $startDate       =  ($licInfo->{'startDate'})    || "";
        my $endDate         =  ($licInfo->{'endDate'})      || "";
        my $note            =  ($licInfo->{'note'})         || "";


    $sth->execute($uid,$bid,$licenseType,$noLicense,$datePurchase,$poNum,$vendor,$startDate,$endDate,$note);
    my $licId= $dbh->{'mysql_insertid'};
    $sth->finish;
    return $licId;
}
########################################################################################
sub  _LIC_update{
    my ($dbh,$uid,$licInfo)=@_;
    my $licId     =  $licInfo->{'licId'};

    my $sth=$dbh->prepare("update  eb_licMgnt set uid=?,licenseType=?,noLicense=?,
                                                   datePurchase=?,poNum=?,vendor=?,
                                                   startDate=?,endDate=?,note=?
                                         where   licId=?                       ");


        my $licenseType     =  ($licInfo->{'licenseType'})  || "paid";
        my $noLicense       =  ($licInfo->{'noLicense'})    || 0;
        my $datePurchase    =  ($licInfo->{'datePurchase'}) || "";
        my $poNum           =  ($licInfo->{'poNum'})        || "";
        my $vendor          =  ($licInfo->{'vendor'})       || "";
        my $startDate       =  ($licInfo->{'startDate'})    || "";
        my $endDate         =  ($licInfo->{'endDate'})      || "";
        my $note            =  ($licInfo->{'note'})         || "";

    $sth->execute($uid,$licenseType,$noLicense,$datePurchase,$poNum,$vendor,$startDate,$endDate,$note,$licId );
    $sth->finish;
}



########################################################################################
sub  eb_saveLicense{
    my ($dbh,$uid,$licInfo)=@_;
    my $licId     =  $licInfo->{'licId'};
    if($licId && $licId>0){
        _LIC_update($dbh,$uid,$licInfo);
    }
    else{
        $licId =_LIC_add($dbh,$uid,$licInfo);
    }
    #log license history
    my $sth_log = $dbh->prepare("insert into 
                                 eb_licMgntLog(licId,uid,bid,licenseType,noLicense,
                                               datePurchase,modDate,poNum,vendor,
                                               startDate,endDate,note) 
                                 select licId,uid,bid,licenseType,noLicense,datePurchase,
                                        modDate,poNum,vendor,startDate,endDate,note 
                                 from   eb_licMgnt where licId=?");
    $sth_log->execute($licId);
    $sth_log->finish;
    return $licId;

}

########################################################################################
sub  eb_getLicense{
    my ($dbh,$bid)=@_;
    my $retVal=undef;
    my $sth=$dbh->prepare("select   licenseType,sum(noLicense) totalLicense 
                           from     eb_licMgnt where  deleted =0 && bid=? ");
    $sth->execute($bid);
    if(my $rec=$sth->fetchrow_hashref){
        $retVal=$rec;
    }
    $sth->finish;
    return $retVal;
}

########################################################################################
sub eb_deleteLicense {
    my ($dbh,$licId)=@_;
    my $sth=$dbh->prepare("update eb_licMgnt set deleted =1 where licId=?");
    my $result= $sth->execute($licId);
    $sth->finish;
    return $result eq '0E0'?0:1;
}


########################################################################################
sub  eb_getLicenseList{
   my ($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select * from eb_licMgnt where  bid=? && deleted =0 order by licId");
    my @licList;
    $sth->execute($bid);
    while(my $lic = $sth->fetchrow_hashref){
        if($lic->{'datePurchase'} eq '0000-00-00 00:00:00'){
            $lic->{'datePurchase'} ="";
        }
        push @licList, $lic;
    }
    $sth->finish;
    return \@licList;
}
########################################################################################
sub  eb_getLicenseLog{
    my ($dbh,$bid,$licId)=@_;
    my $sql  = "select * from eb_licMgntLog where bid=? ";
    
    my @eData=($bid);
    if($licId){
        $sql .= " &licId =?";
        push @eData,$licId;
    }
    $sql .= "  order by id";
    my $sth=$dbh->prepare($sql);
     
    $sth->execute(@eData);
    my @licList;
    while(my $lic = $sth->fetchrow_hashref){
        push @licList, $lic;
    }
    $sth->finish;
    return \@licList;
}


########################################################################################
sub eb_getMarcXmlRid{
    my($dbh,$bid)=@_;
    my $sth=$dbh->prepare("select rid from opl_ridBid where bid=?");
    $sth->execute($bid);
    my ($rid) = $sth->fetchrow_array;

    $sth->finish;
    return $rid eq '0E0'? 0:$rid;;
}

########################################################################################
sub eb_getBidByMarcXmlRid{
    my($dbh,$rid)=@_;
    my $sth=$dbh->prepare("select bid from opl_ridBid where rid=?");
    $sth->execute($rid);
    my ($bid) = $sth->fetchrow_array;
    $sth->finish;
    return $bid eq '0E0'? 0:$bid;
}

########################################################################################
sub eb_setMarcXmlRid{
    my($dbh,$rid,$bid)=@_;
    my $sth=$dbh->prepare("replace into opl_ridBid set rid=?,bid=?");
    $sth->execute($rid,$bid);
    $sth->finish;
}


########################################################################################
sub eb_bookShelves_add{
    my ($dbh,$uid,$rid)=@_;
    my $sth=$dbh->prepare("replace into eb_bookshelves set uid=?,rid=?");
    $sth->execute($uid,$rid);
    $sth->finish;
}
########################################################################################
sub eb_bookShelves_delById{
    my ($dbh,$bsId)=@_;
    my $sth=$dbh->prepare("delete  from eb_bookshelves where bsId=?");
    $sth->execute($bsId);

}
########################################################################################
sub eb_bookShelves_delByUidBid{
    my ($dbh,$uid,$bid)=@_;

    my $sql = "delete  from eb_bookshelves ";
    my @eData=(); my $condStr = "";
    if(defined $bid && $bid >0){
        $condStr .= "  bid=?";
        push @eData,$bid;
    }

    if(defined $uid && $uid >0){
        $condStr .= ($condStr eq "")? " uid=?" :" && uid=?";
        push @eData,$uid;
    }
    $sql .= " where $condStr" if($condStr ne ""); 
    my $sth=$dbh->prepare($sql);
    $sth->execute(@eData);
    $sth->finish;
}
########################################################################################
sub eb_bookShelves_delByUidRid{
    my ($dbh,$uid,$rid)=@_;

    my $sql = "delete  from eb_bookshelves ";
    my @eData=(); my $condStr = "";
    if(defined $rid && $rid >0){
        $condStr .= "  rid=?";
        push @eData,$rid;
    }

    if(defined $uid && $uid >0){
        $condStr .= ($condStr eq "")? " uid=?" :" && uid=?";
        push @eData,$uid;
    }
    $sql .= " where $condStr" if($condStr ne ""); 
    my $sth=$dbh->prepare($sql);
    $sth->execute(@eData);
    $sth->finish;
}

########################################################################################
sub eb_bookShelves_get{
    my ($dbh,$uid)=@_;
    my $sth=$dbh->prepare("select * from eb_bookshelves where uid=? order by bsId");
    $sth->execute($uid);
    my @bsList;
    while(my $rec = $sth->fetchrow_hashref){
        push @bsList, $rec;
    }
    $sth->finish;
    return \@bsList; 
}
########################################################################################
sub eb_bookmark_get{
    my ($dbh,$uid,$bid)=@_;
    my $sth=$dbh->prepare("select * from eb_bookmark   where m.uid=? && m.bid =? order by bmId");
    $sth->execute($uid,$bid);
    my @bmList;
    while(my $rec = $sth->fetchrow_hashref){
       # $rec->{'bmDate'} = date_text($rec->{'bmDate'},0);
        push @bmList, $rec;
    }
    $sth->finish;
    return \@bmList; 
}
########################################################################################
sub eb_bookmark_save{
    my ($dbh,$uid,$bid,$name,$note,$fid,$percFr,$percTo,$bmDate)=@_;
    my $sth=$dbh->prepare("replace into eb_bookmark set uid=?,bid=?,name=?, note=?,fid=?, percFr =?, percTo =?,bmDate=?");
    $sth->execute($uid,$bid,$name,$note,$fid,$percFr,$percTo,$bmDate);
    my $bmId= $dbh->{'mysql_insertid'};

    $sth->finish;
    return $bmId;
}
########################################################################################
sub eb_bookmark_del{
    my ($dbh,$uid,$bid,$fid,$percFr,$percTo)=@_;
   
    my $sth=$dbh->prepare("delete from eb_bookmark where uid=? && bid=? && fid=? && percFr =? && percTo =?");
    $sth->execute($uid,$bid,$fid,$percFr,$percTo);
    $sth->finish;
         
  }

########################################################################################
1;
