#!/usr/bin/perl -w

#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
#
#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/odev/module";
use Opals::Context("/etc/opals/conf/opl_khanh");
use Time::localtime;
use JSON;
use Text::CSV_XS;
use strict;

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 "Testing....", $dateToday;
my $rid=172;

my $eqDef = eq_def_getList_brief($dbh, "item");
print "no:", scalar(@$eqDef), "\n";
print "eqDef:", to_json($eqDef,{pretty=>1}), "\n";
foreach my $f (@$eqDef){
  print "f=>",$f->{'id'} ,"\n";
}
my $iids = getIid_MissingFieldsByRid($dbh,$rid);
print "iids:", to_json($iids);

sub eq_def_getList_brief{
    my ($dbh, $defType) = @_;
    my $ret;
    my $sql = "select id, name from eq_def ";
    if (defined $defType && $defType =~ m/record|item/){
        $sql .= " where hidden=0 && defType = '$defType' ";
    }
    $sql .= " order by id";
    $ret = $dbh->selectall_arrayref($sql,{Slice=>{}});
    return $ret;
}

sub getIid_MissingFieldsByRid{
    my($dbh,$rid)=@_;
    return undef if $rid<=0 || $rid eq undef;
    my $sql="select iid from eq_items where rid=172 and iid not in (select iid from eq_itemFields where rid=? group by iid) order by iid";
    my $ret = $dbh->selectall_arrayref($sql,{Slice=>{}},$rid);
    return $ret;
}
exit 0;

