#!/usr/bin/perl -w

#SITE=ztest; INFILE=/tmp/textbook.mrc; \cp -fp /www/opals/script/util/tbk_marc_import /tmp/ttt; perl -pi -e "s/_SITECODE_/$SITE/" /tmp/ttt; /tmp/ttt $INFILE
#
#SITE=ztest
#INFILE=/tmp/textbook.mrc
#\cp -fp script/util/tbk_marc_import /tmp/ttt
#perl -pi -e "s/_SITECODE_/$SITE/" /tmp/ttt
#/tmp/ttt $INFILE

use lib "/www/opals/module";
use Opals::Context("/etc/opals/conf/_SITECODE_");
use Time::localtime;

use strict;
use MARC::Field;
use MARC::File::USMARC;


my $dbh = Opals::Context->dbh();;
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

my $tm = localtime;
my $dateToday = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $tm->year+1900, ($tm->mon)+1, $tm->mday, $tm->hour, $tm->min, $tm->sec);

$| = 1;
# Codes start...

print $ARGV[0], "\n";
my $marcfile = MARC::File::USMARC->in($ARGV[0]);
my $i = 0;

#$dbh->do("truncate opl_idGen");
#$dbh->do("truncate tb_records_CISD");
#$dbh->do("truncate tb_items_CISD");

while (my $record = $marcfile->next()) {
    my $rid = tb_record_idGen($dbh);
    print "record id  ...:\t$rid\n";
    tb_record_add($dbh, $rid, '005', $dateToday);
    tb_record_add($dbh, $rid, '010_a', $record->subfield('010',"a"));
    foreach my $f ($record->field('020')) {
        tb_record_add($dbh, $rid, '020_a', $record->subfield('020',"a"));
    }
    tb_record_add($dbh, $rid, '022_a', $record->subfield('022',"a"));
    foreach my $f ($record->field('100')) {
        tb_record_add($dbh, $rid, '100_a', $record->subfield('100',"a"));
    }
    foreach my $f ($record->field('110')) {
        tb_record_add($dbh, $rid, '110_a', $record->subfield('110',"a"));
    }
    tb_record_add($dbh, $rid, '245_a', $record->subfield('245',"a"));
    tb_record_add($dbh, $rid, '245_b', $record->subfield('245',"b"));
    tb_record_add($dbh, $rid, '250_a', $record->subfield('250',"a"));
    tb_record_add($dbh, $rid, '260_a', $record->subfield('260',"a"));
    tb_record_add($dbh, $rid, '260_b', $record->subfield('260',"b"));
    tb_record_add($dbh, $rid, '260_c', $record->subfield('260',"c"));
    tb_record_add($dbh, $rid, '300_a', $record->subfield('300',"a"));
    tb_record_add($dbh, $rid, '490_a', $record->subfield('490',"a"));
    tb_record_add($dbh, $rid, '490_v', $record->subfield('490',"v"));
    tb_record_add($dbh, $rid, '500_a', $record->subfield('500',"a"));
    tb_record_add($dbh, $rid, '505_g', $record->subfield('505',"g"));
    tb_record_add($dbh, $rid, '520_a', $record->subfield('520',"a"));
    tb_record_add($dbh, $rid, '521_a', $record->subfield('521',"a"));
    foreach my $f ($record->field('650')) {
        tb_record_add($dbh, $rid, '650_a', $record->subfield('650',"a"));
    }
    foreach my $f ($record->field('856')) {
        tb_record_add($dbh, $rid, '856_u', $record->subfield('856',"u"));
    }
    my $emptyStr = "";
    my ($classno, $data);
    foreach my $f ($record->field('852')) {
        if (! $f->subfield('p')){
            next;
        }
        $classno = '';
        foreach my $code ('k', 'h', 'i', 'm') {
            $data = $f->subfield($code);
            $classno .= $data . ' ' if $data;
        }
        $classno =~ s/ +/ /g;
        $classno =~ s/(^ | $)//g;

        my $params = {
            rid         => $rid,
            barcode     => $f->subfield('p'),
            typeId      => 'TBK',
            lCode       => $f->subfield('b') ? $f->subfield('b') : "",
            price       => '',
            classno     => $classno,
            acqDate     => $emptyStr,
            PONo        => $emptyStr,
            distributor => $emptyStr,
            regionCode  => $emptyStr,
            districtCode=> $emptyStr,
            buildingCode=> $emptyStr,
            importDate  => $dateToday,
        };
        tb_item_add($dbh,$params);
    }

} #end while
$marcfile->close();
# Codes end.

exit 0;

#add record 
sub tb_record_add{
    my ($dbh, $rid, $fId, $fVal) = @_;
    return -1 if ($rid eq '');
    my($sql, $sth, $id);
    $sql = "insert into tb_records_MHML set rid=?, fId=?, fVal=?";    
    $sth = $dbh->prepare($sql);
    my $result = $sth->execute($rid,$fId,$fVal);
    $id = $dbh->{'mysql_insertid'};
    $sth->finish;
    return $id;

}
sub tb_item_add{

    my ($dbh,$params) = @_;
    return -1 if ($params->{'rid'} eq '');
    my ($sql, $sth, $id);
        $sql = <<_SQL_;
            insert into tb_items_MHML set 
                rid=?, 
                barcode=?, 
                available=1,
                typeId=?, 
                locationCode=?, 
                price=?,
                classNumber=?,
                acquisitionDate=?,
                PONumber = ?,
                distributor = ?,
                regionCode = ?,
                districtCode = ?,
                buildingCode = ?,
                importDate=?
_SQL_

        $sth = $dbh->prepare($sql);
        my @param = (
                    $params->{'rid'}, 
                    $params->{'barcode'}, 
                    $params->{'typeId'},
                    $params->{'lCode'},
                    $params->{'price'},
                    $params->{'classno'},
                    $params->{'acqDate'},
                    $params->{'PONo'},
                    $params->{'distributor'},
                    $params->{'regionCode'},
                    $params->{'districtCode'},
                    $params->{'buildingCode'}, 
                    $params->{'importDate'}
        );
        $sth->execute(@param) || die $params->{'rid'}, "\n";
        $id = $dbh->{'mysql_insertid'};
    $sth->finish;
    return $id;
}

################################################################################
sub tb_record_idGen {
    
    my ($dbh) = @_;
    #my ($sec,$min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
    #my $checkSum = sha1_base64($sec,$min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
    my $checkSum = `date +\%Y\%m\%d\%H\%M\%S\%N`;
    chomp $checkSum;

    my $sql = "insert into opl_idGen_MHML set type = 'textbook', checkSum=? ";
    my $sth = $dbh->prepare($sql);
    $sth->execute($checkSum);
    $sql = "select id from opl_idGen_MHML where type = ? and checkSum=? ";
    $sth = $dbh->prepare($sql);
    $sth->execute('textbook',$checkSum);
    my ($id) = $sth->fetchrow_array();
    $sth->finish;
    return $id;
}

=item
sub blahblahblah {
}
=cut
############################################################
