#!/usr/bin/perl 

use lib "/www/odev/module";;
use Opals::Context('/etc/opals/conf/opl_ha');
use strict;
use Opals::UrlRegistry qw(
    ureg_getUrlRegistry
    ureg_getMenu
);
use Time::HiRes qw( time );
use CGI;
use DBI;
use Text::CSV_XS;
use Opals::Date qw(
    date_parse
    date_getDeadLineDate
);
use Class::CSV;
use JSON;
use Opals::Pathfinder qw(
    pf_add
    pf_save
    pf_delete
    pf_addBookRs
    pf_addWebRs
    pf_getRecById
    pf_getRecByIdList
    pf_getPfList
    pf_getWebRsList
    pf_getBookRsList
    pf_getSubjectAreaList
    pf_parsePfXml
    pf_getPfListByGrp
    pf_getPfListByField
    pf_getPathfinderFromRecordId
);

use Opals::Template qw(
    tmpl_preference
);

use Opals::BackgroundJobs qw(
    bgjob_create
);
use Digest::SHA qw(
    sha1_hex
);
  my $cgi    = CGI->new;

my $dbh = Opals::Context->dbh();
my @RS_TBL_FIELD_MAP=(
    {rsType=>'library',table=>"pf_bookRs", fields=>[qw (pfId groupId rid title author pubName pubPlace pubDate callNumber isbn)]},
    {rsType=>'web',    table=>"pf_webRs",  fields=>[qw (pfId groupId title author description url)]},
    {rsType=>'file',   table=>"pf_file",   fields=>[qw (pfId groupId title fName fRealName description url)]}
);

my $pfList =pf_getPfList($dbh,"2,3,a1,4");
foreach my $pf (@$pfList){
    print $pf->{'title'},"\n";
    foreach my $sbj(@{$pf->{'subject'}}){
        print "\t - $sbj\n";
    }
}
exit 0;
my $pfRecJson =to_json(pf_getById($dbh,2),{pretty=>1});
print $pfRecJson;
exit 1;

#pf_save($dbh,$pfRec);

#####################################################
#


sub pf_save{
    my ($dbh,$pfRec) = @_;
    my $pfId=$pfRec->{'pfId'};
    if($pfRec->{'title'} eq ''){
        return 0;
    }
    my @recData=();
    my @recField=();
    foreach my $f(qw(title author authPosition introduction audience expiryDate iconUrl)){
        push @recData,$pfRec->{$f};
        push @recField,"$f=?";
    }
    if(!$pfId ||$pfId eq ''){
        $dbh->do("insert into pf_record set created=now()," . join(",",@recField),undef,@recData) ;
        $pfId=$dbh->{'mysql_insertid'};
        $pfRec->{'pfId'} = $pfId;
    }
    else{
        $dbh->do("update pf_record set " . join(",",@recField) . " where pfId=$pfId",undef,@recData);
    }
    #update resources
    my $sth_grp=$dbh->prepare("insert into pf_group set rsType=?, pfId=?,groupId=?, header=?,description=?");
    my $sth_bookRs=$dbh->prepare("insert into pf_bookRs set pfId=?, rid=?, title=?,author=?,pubName=?,pubPlace=?,pubDate=?,callNumber=?,isbn=?");
    my $groupId=1;

    $dbh->do("delete from pf_group where pfId=$pfId");
    $dbh->do("delete from pf_bookRs where pfId=$pfId");
    $dbh->do("delete from pf_webRs where pfId=$pfId");
    $dbh->do("delete from pf_file where pfId=$pfId");
    $dbh->do("delete from pf_subject where pfId=$pfId");
    foreach my $sbj(@{$pfRec->{'subject'}}){
        $dbh->do("insert into pf_subject set pfId=?,subject=?",undef,$pfId,$sbj);
    }
    foreach my $rsDef(@RS_TBL_FIELD_MAP){
        $groupId=1;
        my $rsType=$rsDef->{'rsType'};
        foreach my $rs(@{$pfRec->{$rsType ."RsList"}}){
            $sth_grp->execute($rsType, $pfId,$groupId,$rs->{'header'},$rs->{'description'});            
            my @rsField=();
            foreach my $f(@{$rsDef->{'fields'}}){
                    push @rsField,"$f=?";
            }
            my $sth_rs=$dbh->prepare("insert into $rsDef->{'table'} set " . join(",",@rsField));
            foreach my $item(@{$rs->{'rsList'}}){
                $item->{'pfId'}=$pfId;
                $item->{'groupId'}=$groupId;
                my @rsData=();
                foreach my $f(@{$rsDef->{'fields'}}){
                    push @rsData,$item->{$f};
                }
                $sth_rs->execute(@rsData);
            }
            $groupId++;

        }
    }
}
#==================================================================================
sub pf_getById{
    my ($dbh,$pfId) = @_;
   my $rec =$dbh->selectrow_hashref("select title, author, authPosition, introduction, audience, expiryDate, iconUrl from pf_record where pfId=?",undef,$pfId);
   my $sth_grp=$dbh->prepare("select groupId,header,description from pf_group where rsType=? && pfId=? order by groupId");
   my $sth_sbj=$dbh->prepare("select subject from pf_subject where pfId=?");
   if($rec){
       $sth_sbj->execute($pfId);
       $rec->{'subject'}=[];
       while(my ($sbj)=$sth_sbj->fetchrow_array){
           push @{$rec->{'subject'}},$sbj;
       }
       foreach my $rsDef(@RS_TBL_FIELD_MAP){
           my $rsType=$rsDef->{'rsType'};
           $rec->{$rsType ."RsList"}=[];
           $sth_grp->execute($rsType,$pfId);
           my $sth_rs=$dbh->prepare("select * from " . $rsDef->{'table'} . " where pfId=? && groupId=? order by id");
           while(my $rs=$sth_grp->fetchrow_hashref){
               $sth_rs->execute($pfId,$rs->{"groupId"});
               $rs->{'rsList'}=[];
               while(my $rsItem=$sth_rs->fetchrow_hashref){
                    push @{$rs->{'rsList'}},$rsItem;
               }
               push @{$rec->{$rsType ."RsList"}},$rs;
           }
       }
   }
   return $rec;
}


#==================================================================================
sub pf_delete{
    my ($dbh,$pfId) = @_;
    $dbh->do("delete from pf_record where pfId=?",undef,$pfId);
    $dbh->do("delete from pf_group where pfId=?",undef,$pfId);
    $dbh->do("delete from pf_bookRs where pfId=?",undef,$pfId);
    $dbh->do("delete from pf_webRs where pfId=?",undef,$pfId);
    $dbh->do("delete from pf_file where pfId=?",undef,$pfId);
}


