postgresql日程排程函数的编写实例
此函数用于《云杉树成长管理器软件》。
一、函数功能
此函数是根据任务(成长任务,课程)、指定期间(学期)、任务主体(学生)、每周排程字符串信息(周几第几节等)信息,将日程排至每一天的具体时段。同时要考虑节假日及补休处理。
二、参数说明
学员名称。 期间名称。 任务名称。 每周日程。
函数执行代码:
select e9_lcrwpc('小明','22-23第一学期','语文','week1:上午2,week5:上午2,week3:上午3,week2:上午4,week4:上午4,week1:下午2')
三、使用到的表
(1)成长任务表(课程表,包括课程内容和要求等):
e9czrw字段列表: id int4 32 编号 textedit oid varchar 30 任务编号 textedit rwmc varchar 100 任务名称 textedit rwnr varchar 1000 任务内容 buttontextedit rwyq varchar 1000 任务要求 buttontextedit jhwj varchar 300 任务文件 fileedit rwsc numeric 12 计划时数 textedit 0.00 xmmc varchar 60 项目计划 buttonedit czys varchar 20 成长要素 textread tsxx varchar 100 提示信息 textedit xzss bool 0 限制时数 checkedit rclb varchar 20 日程类别 comboedit sfsh bool 0 是否审核 checkedit shry varchar 20 审核人员 textedit sfql bool 0 是否清理 checkedit bhjr bool 0 假日排程 checkedit
(2)学员期间设置表(包含期间的开始结束时间等信息)
e9zrqjfw字段列表: id int4 32 编号 textedit czzt varchar 30 成长学员 listedit edit qjxh int4 32 期间序号 textedit qjmc varchar 100 期间名称 textedit ksrq date 0 开始日期 dateedit yyyy-MM-dd jsrq date 0 截止日期 dateedit yyyy-MM-dd xjks varchar 4 夏季开始(月日) textedit 0000 xjjs varchar 4 夏季结束(月日) textedit 0000 qjsm varchar 60 期间说明 textedit bqzs int4 32 本期周数 textread bqts int4 32 本期天数 textread fzds varchar 40 textedit sfsh bool 0 是否审核 checkedit shry varchar 40 审核人员 textedit sdfl varchar 20 时间表版本 listedit
(3)年度假日表
e9mljr字段列表: id int4 32 编号 textedit nd bpchar 4 年度 textedit jrxh int4 32 假日序号 textedit ## jrmc varchar 20 假日名称 textedit xxrq date 0 休息日期 dateedit yyyy-MM-dd xxrqxq varchar 20 休息日期星期几 textread sfbx bool 0 是否补休 checkedit bxrq date 0 补休日期 dateedit yyyy-MM-dd bxrqxq varchar 20 补休日期星期几 textread sfsh bool 0 是否审核 checkedit shry varchar 40 审核人员 textread
(4)每日时段表(课时表,第几节从几点到几点…)
e9mrsd字段列表: id int4 32 编号 textedit sdfl varchar 100 时段版本 textedit sdxh int4 32 时段序号 textedit sdmc varchar 60 时段名称 textedit kssj varchar 5 冬季开始时间 textedit jssj varchar 5 冬季结束时间 textedit sdsc varchar 5 冬季时长 textread 0:00 kssj1 varchar 5 夏季开始时间 textedit jssj1 varchar 5 夏季结束时间 textedit sdsc1 varchar 5 夏季时长 textread 0:00 sdsm varchar 200 时段说明 textedit sfsh bool 0 是否审核 checkedit shry varchar 20 审核人员 textedit fkmc varchar 10 时块名称 comboedit edit
(5)日程表(排程结果表,包含每天的日程)
e9rcb字段列表: id int4 32 编号 textedit qjmc varchar 100 期间名称 textedit rq date 0 日期 textedit yyyy-MM-dd week int4 32 星期 textedit xmmc varchar 60 项目计划 buttonedit czys varchar 20 成长要素 textread sdxh int4 32 序号 buttonedit sdmc varchar 60 时段 textedit kssj bpchar 5 开始 textedit 00:00 jssj bpchar 5 结束 textedit 00:00 rwsc bpchar 5 任务时长 textedit hh:mm rwss int4 32 任务时数 textedit rwbh varchar 20 任务编号 buttonedit edit rwmc varchar 100 任务名称 textedit rclb varchar 40 日程类别 comboboxedit edit rczq varchar 60 日期周期 textedit sftx bool 0 是否提醒 checkedit tsxx varchar 100 提示信息 textedit rcyh varchar 60 日程用户 checkedcomboboxedit fzmc varchar 60 分组名称 textedit pcyh varchar 400 排除用户 textread wcbl int4 32 完成比率 spinedit 0至10 rczz varchar 100 日程排定 textedit wcqk varchar 100 完成情况 textedit sfsh bool 0 是否审核 checkedit shry varchar 20 审核人员 textedit sfqx bool 0 已经取消 checkread sfql bool 0 是否清理 checkedit
四、函数代码
CREATE OR REPLACE FUNCTION public.e9_lcrwpc( sszt character varying, ssqj character varying, ssrwmc character varying, ssmzrc character varying) RETURNS character varying LANGUAGE 'plpgsql' COST 100 VOLATILE PARALLEL UNSAFE AS $BODY$ DECLARE ssxjks varchar(4);ssxjjs varchar(4); -- 夏季开始,夏季结束; ssrclb varchar(20);sssdmc varchar(20);ssmzxq integer; ssid int2;ssrwmc varchar(50);ssxmmc varchar(30);ssczys varchar(10); ssrwsc numeric(12,2);sspcsc numeric(12,2);sssxsc numeric(12,2); sssxzq varchar(10);sssdsc varchar(5); ssmyrq int;ssdayweek int;sstsxx varchar(50); ssdqdate date;ssksdate date;ssjzdate date;ssxxrq date;ssbxrq date; -- 休息日期、补休日期 ssrczz varchar(50);ssxzss bool;ssbhjr bool; sssdxh int;sskssj char(5);ssjssj char(5); ssrcid int;ssrc varchar(50); ssrwss numeric(12,2); sssdfl varchar(20); BEGIN ssrclb:=''; SELECT rwmc,xmmc,czys,tsxx,xzss,bhjr,rwsc * 60::numeric(12,2) INTO ssrwmc,ssxmmc,ssczys,sstsxx,ssxzss,ssbhjr,ssrwsc FROM e9czrw WHERE rwmc = $3; SELECT ksrq,jsrq,xjks,xjjs,sdfl INTO ssksdate,ssjzdate,ssxjks,ssxjjs,sssdfl FROM e9zrqjfw WHERE czzt = $1 AND qjmc = $2 and sfsh=true; sspcsc := 0.00; DROP TABLE If Exists sstbrc; CREATE TEMP TABLE sstbrc(id int,xq int,sd varchar(20),sfpd bool); INSERT INTO sstbrc (id,sd,sfpd) SELECT row_number() over() AS id,unnest AS sd,FALSE AS sfpd FROM unnest(string_to_array($4, ',')) ; UPDATE sstbrc SET xq = (replace(split_part(sd,':',1),'week',''))::int2,sd = split_part(sd,':',2); UPDATE sstbrc set xq = 0 where xq= 7 ; ssdqdate:=ssksdate; LOOP EXIT WHEN ssjzdate < ssdqdate; ssmzxq:=extract(dow FROM ssdqdate); IF exists(SELECT xq FROM sstbrc WHERE xq = ssmzxq) THEN LOOP EXIT WHEN NOT exists(SELECT sd FROM sstbrc WHERE xq = ssmzxq AND sfpd = FALSE); SELECT id,sd INTO ssrcid,sssdmc FROM sstbrc WHERE xq = ssmzxq AND sfpd = FALSE ORDER BY id LIMIT 1; ssxxrq:=NULL; SELECT xxrq,bxrq INTO ssxxrq,ssbxrq FROM e9mljr WHERE xxrq = ssdqdate; --不是休息日 IF (ssxxrq IS NULL) THEN IF NOT exists(SELECT sdmc FROM e9rcb WHERE qjmc = $2 AND sdmc = sssdmc AND rcyh = $1 AND rq = ssdqdate) THEN IF to_char(ssdqdate,'mmdd') BETWEEN ssxjks AND ssxjjs THEN SELECT sdxh,kssj1,jssj1,sdsc1 INTO sssdxh,sskssj,ssjssj,sssdsc FROM e9mrsd WHERE sdfl = sssdfl AND sdmc = sssdmc; ELSE SELECT sdxh,kssj,jssj,sdsc INTO sssdxh,sskssj,ssjssj,sssdsc FROM e9mrsd WHERE sdfl = sssdfl AND sdmc = sssdmc; END IF; sssxsc:=e9_scss(sssdsc); IF ssjssj > sskssj AND (ssxzss = false OR ssrwsc > (sspcsc + sssxsc-15)) THEN INSERT INTO e9rcb (rq,week,qjmc,xmmc,czys,sdxh,sdmc,kssj,jssj, rwsc,rwss,rwmc,rclb,tsxx,rcyh,sdfl) VALUES (ssdqdate,ssmzxq,$2,ssxmmc,ssczys,sssdxh,sssdmc,sskssj,ssjssj, sssdsc,sssxsc,ssrwmc,ssrclb,sstsxx,$1,sssdfl); sspcsc:=sspcsc + sssxsc; END IF; END IF; ELSE --休息日 IF not(ssbxrq IS NULL) AND (ssbxrq BETWEEN ssksdate AND ssjzdate) THEN IF NOT exists(SELECT sdmc FROM e9rcb WHERE qjmc = $2 AND sdmc = sssdmc AND rcyh = $1 AND rq = ssbxrq) THEN IF to_char(ssbxrq,'mmdd') BETWEEN ssxjks AND ssxjjs THEN SELECT sdxh,kssj1,jssj1,sdsc1 INTO sssdxh,sskssj,ssjssj,sssdsc FROM e9mrsd WHERE sdfl = sssdfl AND sdmc = sssdmc; ELSE SELECT sdxh,kssj,jssj,sdsc INTO sssdxh,sskssj,ssjssj,sssdsc FROM e9mrsd WHERE sdfl = sssdfl AND sdmc = sssdmc; END IF; sssxsc:=e9_scss(sssdsc); IF ssjssj > sskssj AND (ssxzss = false OR ssrwsc > (sspcsc + sssxsc-15)) THEN INSERT INTO e9rcb (rq,week,qjmc,xmmc,czys,sdxh,sdmc,kssj,jssj, rwsc,rwss,rwmc,rclb,tsxx,rcyh,sdfl) VALUES (ssbxrq,extract(dow FROM ssbxrq), $2,ssxmmc,ssczys,sssdxh,sssdmc,sskssj,ssjssj, sssdsc,sssxsc,ssrwmc,ssrclb,sstsxx,$1,sssdfl); sspcsc:=sspcsc + sssxsc; END IF; END IF; END IF; END IF; UPDATE sstbrc SET sfpd = TRUE WHERE id = ssrcid; END LOOP; END IF; ssdqdate :=ssdqdate+1 ; UPDATE sstbrc SET sfpd = FALSE; END LOOP; DROP TABLE If Exists sstbrc; RETURN ''; END; $BODY$; ALTER FUNCTION public.e9_lcrwpc(character varying, character varying, character varying, character varying) OWNER TO postgres;
五、总结
本例为日程排程的一种尝试,可以结合《云杉树成长管理器》软件进行了解,后台代码完成开放,谨供参考。