use bigwork;
DELIMITER ##
drop trigger if exists `trig_create_user_stat`;
create trigger `trig_create_user_stat` 
    after insert on user
    for each row begin
    insert into user_stat (`user_id`, `user_limit`) values(NEW.user_id, NEW.user_limit);
end ##
DELIMITER ;

DELIMITER ##
drop trigger if exists `trig_delete_user_stat`;
create trigger `trig_delete_user_stat` 
    after delete on user
    for each row begin
    delete from user_stat where user_id=OLD.user_id;
end ##
DELIMITER ;

drop trigger if exists `trig_update_stat_book_ins`;
DELIMITER ##
create trigger `trig_update_stat_book_ins` 
    after insert on book 
    for each row begin
    update user_stat set user_bookcount=user_bookcount+1 where user_stat.user_id=NEW.user_id;
end ##
DELIMITER ;

drop trigger if exists `trig_update_stat_book_del`;
DELIMITER ##
create trigger `trig_update_stat_book_del` 
    after delete on book 
    for each row begin
    update user_stat set user_bookcount=user_bookcount-1 where user_stat.user_id=OLD.user_id;
end ##
DELIMITER ;

drop trigger if exists `trig_update_stat_doc_ins`;
DELIMITER ##
create trigger `trig_update_stat_doc_ins` 
    after insert on document 
    for each row begin
    update user_stat set user_doccount=user_doccount+1 where user_stat.user_id=NEW.user_id;
    update user_stat set user_usedspace=user_usedspace+NEW.doc_size where user_stat.user_id=NEW.user_id;
end ##
DELIMITER ;

drop trigger if exists `trig_update_stat_doc_del`;
DELIMITER ##
create trigger `trig_update_stat_doc_del` 
    after delete on document 
    for each row begin
    update user_stat set user_doccount=user_doccount-1 where user_stat.user_id=OLD.user_id;
    update user_stat set user_usedspace=user_usedspace-OLD.doc_size where user_stat.user_id=OLD.user_id;
end ##
DELIMITER ;

drop trigger if exists `trig_update_stat_note_ins`;
DELIMITER ##
create trigger `trig_update_stat_note_ins` 
    after insert on note
    for each row begin
    update user_stat set user_notecount=user_notecount+1 where user_stat.user_id=NEW.user_id;
end ##
DELIMITER ;

drop trigger if exists `trig_update_stat_note_del`;
DELIMITER ##
create trigger `trig_update_stat_note_del` 
    after delete on note
    for each row begin
    update user_stat set user_notecount=user_notecount-1 where user_stat.user_id=OLD.user_id;
end ##
DELIMITER ;

drop procedure if exists `clean_up_type_author`;
DELIMITER ##

create procedure `clean_up_type_author`(in uid_in int) begin
declare done boolean default false;
declare v_bookid int;
declare cur cursor for select distinct book_id from book where book.user_id = uid_in;
declare continue handler for not found set done=true;

    open cur;
mainloop: loop 
        fetch cur into v_bookid;
        if done then
            leave mainloop;
        end if;
        delete from typetable where type_id in (select type_id from book_type where book_id=v_bookid);
        delete from book_type where book_id = v_bookid;
    end loop;
    close cur;
end ##

DELIMITER ;

drop procedure if exists `add_new_book_type`;
DELIMITER ##
create procedure `add_new_book_type`(in bookid_in int, in typename_in varchar(200)) begin
declare isnewtype int default 0;
declare isduplicated int default 0;
declare v_typeid int;
    select count(type_name) into isnewtype from typetable where type_name=typename_in;
    if isnewtype=0 then
        insert into typetable (`type_name`) values (typename_in);
    end if;
    select type_id into v_typeid from typetable where type_name = typename_in;
    select count(book_id) into isduplicated from book_type where type_id=v_typeid and book_id=bookid_in;
    if isduplicated=0 then
        insert into book_type (`type_id`,`book_id`) values (v_typeid, bookid_in);
    end if;
end ##
DELIMITER ;
