-- Function: send_system_mail()-- DROP FUNCTION send_system_mail();CREATE OR REPLACE FUNCTION send_system_mail_to_all(mail_max_count integer, sender_param character varying, title_param character varying, content_param character varying, create_time_param bigint) RETURNS integer AS$BODY$DECLAREcharac_cur refcursor;mail_cur refcursor;charac_id_c bigint;mail_count integer;mail_id_c integer;ret integer;beginopen charac_cur for select charac_id from account order by charac_id;loop fetch charac_cur into charac_id_c; if not found then exit; end if; --检查每个角色系统邮件数量是否超限 select count(id) from mail where (type&(1<<1)>>1) = 1 and charac_id = charac_id_c into mail_count; if mail_count > mail_max_count then open mail_cur for (select id from mail where (type&(1<<1)>>1) = 1 and charac_id = charac_id_c order by create_time desc limit mail_count - mail_max_count +1); loop fetch mail_cur into mail_id_c; if not found then exit; end if; --删除多余的邮件 delete from mail where id = mail_id_c; --RAISE NOTICE 'value % % %',charac_id_c, mail_count, mail_id_c; end loop; close mail_cur; end if; INSERT INTO mail(charac_id, type, create_time, sender, title, content) VALUES (charac_id_c, 2, create_time_param, sender_param, title_param, content_param);end loop;close charac_cur;return 1; end$BODY$ LANGUAGE plpgsql VOLATILE COST 100;ALTER FUNCTION send_system_mail() OWNER TO postgres;COMMENT ON FUNCTION send_system_mail() IS '发送系统邮件';