<?php
namespace model;

use model\database;


class records {
    
    public static function get_records_by_uid(int $uid) {
        $records = ((new database())->pdo_prepare(
            'select r.rid, b.bid, b.cover'.
            ' from records r left join books b on r.bid = b.bid where r.uid = :uid')->execute([':uid' => $uid])) ?? false;
        return isset($records[0]['rid']) ? $records : [$records];
    }
    
    public static function get_in_records_by_uid(int $uid) {
        $records = ((new database())->pdo_prepare(
            'select r.rid, b.bid, b.cover'.
            ' from records r left join books b on r.bid = b.bid where r.uid = :uid and r.state = 0')->execute([':uid' => $uid])) ?? false;
        return isset($records[0]['rid']) ? $records : [$records];
    }
    
    public static function add_record($bid, $uid, $sid, $borrow_time) {
        return (new database())->table('records')->insert([
            'bid' => $bid,
            'uid' => $uid,
            'sid' => $sid,
            'borrow_time' => $borrow_time,
            'state' => 0
        ]);
    }
    
    public static function update_record($rid, $return_time) {
        return (new database())->table('records')->where(['rid' => $rid])->update(['return_time' => $return_time, 'state' => 1]);
    }
    
    public static function list_hot_records(int $page = 1) {
        $records = ((new database())->pdo_query(
            'select b.bid, b.cid, b.name, b.author, b.publish, b.isbn, b.cover, b.lang, any_value(r.borrow_time) as borrow_time'.
            ' from records r left join books b on r.bid = b.bid group by b.bid order by borrow_time desc limit '.(($page-1)*20).', 20')) ?? false;
        return isset($records[0]['bid']) ? $records : [$records];
    }
    
    public static function list_records_by_uid(int $uid, int $page = 1) {
        $records = ((new database())->pdo_prepare(
            'select r.rid, r.sid, b.name, b.author, b.publish, b.isbn, r.borrow_time, r.return_time, r.state'.
            ' from records r left join books b on r.bid = b.bid where r.uid = :uid limit '.(($page-1)*20).', 20')->execute([':uid' => $uid])) ?? false;
        return isset($records[0]['rid']) ? $records : [$records];
    }
    
    public static function count_records() {
        return (new database())->table('records')->count();
    }
    
    public static function count_hot_records() {
        return (new database())->pdo_query('select count(*) from (select `bid` as `count` from `records` group by `bid`) as c')['count(*)'] ?? 0;
    }
    
    public static function count_records_by_uid(int $uid) {
        return (new database())->table('records')->where(['uid' => $uid])->count();
    }
    
    public static function count_in_records() {
        return (new database())->table('records')->where(['state' => 0])->count();
    }
    
    public static function count_in_records_by_uid(int $uid) {
        return (new database())->table('records')->where(['uid' => $uid, 'state' => 0])->count();
    }
    
}
