Различия
Здесь показаны различия между двумя версиями данной страницы.
— |
администрирование:скрипты:фбд:push.-.повторная.явка [2019/07/04 08:18] (текущий) |
||
---|---|---|---|
Строка 1: | Строка 1: | ||
+ | ====== Push - повторная явка ====== | ||
+ | <code sql> | ||
+ | execute block | ||
+ | as | ||
+ | /* | ||
+ | * Скрипт рассылки пуш-уведомлений для повторного визита пациентов | ||
+ | * | ||
+ | * Автор: Коробка А.А. | ||
+ | */ | ||
+ | -- константы | ||
+ | declare ttl int; | ||
+ | declare day_delay int; | ||
+ | declare datemsg date; | ||
+ | declare cnt int; | ||
+ | declare uid type of column doctor.dcode; | ||
+ | -- данные протокола | ||
+ | declare date_interv int; | ||
+ | declare p1 int; | ||
+ | declare txt type of column paramsinfo.valuetext; | ||
+ | declare tmp type of column paramsinfo.valuetext; | ||
+ | declare date_litera ttext16; | ||
+ | -- данные пациента | ||
+ | declare pcode type of column clients.pcode; | ||
+ | declare notify type of column clphones.notificationid; | ||
+ | -- данные приема | ||
+ | declare treatcode type of column treat.treatcode; | ||
+ | declare trdate type of column treat.treatdate; | ||
+ | declare fjid type of column filials.jid; | ||
+ | declare filial type of column filials.filid; | ||
+ | declare fname type of column filials.shortname; | ||
+ | declare depnum type of column departments.depnum; | ||
+ | declare depname type of column departments.depname; | ||
+ | declare dcode type of column doctor.dcode; | ||
+ | declare dname type of column doctor.dname; | ||
+ | declare dcode_send type of column doctor.dcode; | ||
+ | -- данные по услуге | ||
+ | declare spec int; | ||
+ | declare schid type of column wschema.schid; | ||
+ | declare worktime int; | ||
+ | declare structid type of column wschema.structid; | ||
+ | -- вставка сообщения | ||
+ | declare logid type of column sms_log.logid; | ||
+ | declare smsid type of column sms_logdet.smsid; | ||
+ | declare smstypeid type of column sms_log.smstypeid; | ||
+ | declare smsrefid type of column sms_log.smsrefid; | ||
+ | declare smsprofileid type of column sms_log.smsprofileid; | ||
+ | declare pushtext type of column sms_log.smstext; | ||
+ | declare planbtime type of column sms_log.planbtime; | ||
+ | declare planftime type of column sms_log.planftime; | ||
+ | begin | ||
+ | day_delay = 1; -- приглашать за 1 день минимум | ||
+ | ttl = day_delay*24*60*60; -- напоминание держится (расчет на отправку сразу) | ||
+ | uid = 0; | ||
+ | -- филиал выполнения | ||
+ | select first 1 f.filid, f.shortname, f.jid | ||
+ | from repl$getgrpid g | ||
+ | left join filials f on f.grpid=g.repl$grpid | ||
+ | into filial, fname, fjid; | ||
+ | -- поиск структуры | ||
+ | select first 1 structid from pricestructure | ||
+ | where bdate<= current_date+1+ :day_delay order by bdate desc | ||
+ | into structid; | ||
+ | for select p.pcode, p.treatdate, dcode, treatcode, lower(trim(valuetext)), cp.notificationid | ||
+ | from paramsinfo p | ||
+ | left join clphones cp on cp.pcode=p.pcode | ||
+ | where p.codeparams in (990001860/* повторная явка */,990002043/*гастро*/, | ||
+ | 990001689/*аллерг-1*/,990001823/*аллерг-2*/, | ||
+ | 990001748/*невро*/,640000067/*берем*/, | ||
+ | 990002216/*офтальм*/) | ||
+ | and p.ver_no = 0 | ||
+ | and p.treatdate between current_date-370 and current_date | ||
+ | and trim(valuetext)!='' | ||
+ | and valuetext not like '%за результатами%' | ||
+ | and coalesce(cp.isdeleted,0)=0 | ||
+ | and cp.phonetype=21 | ||
+ | and cp.notificationid>'' | ||
+ | into pcode, trdate, dcode, treatcode, txt, notify do | ||
+ | begin | ||
+ | datemsg = null; | ||
+ | date_interv = 0; | ||
+ | -- | ||
+ | while (position(' ', txt)>0) do | ||
+ | begin | ||
+ | txt = replace(txt, ' ', ' '); | ||
+ | end | ||
+ | |||
+ | -- первым указано `через N период` | ||
+ | if (position('через ', txt)=1) then | ||
+ | begin | ||
+ | -- отсекаем 1е слово | ||
+ | tmp = trim(substring(txt from 6)); | ||
+ | -- иищем начало 2го слова | ||
+ | p1 = position(' ', tmp); | ||
+ | if (p1>0) then | ||
+ | begin | ||
+ | begin | ||
+ | date_interv = trim(substring(tmp from 1 for p1)); | ||
+ | date_litera = trim(substring(tmp from p1 for 6)); | ||
+ | end | ||
+ | when any do begin end | ||
+ | end | ||
+ | -- | ||
+ | if (date_interv>0) then | ||
+ | begin | ||
+ | datemsg = case when date_litera like 'год%' then dateadd(date_interv year to trdate) | ||
+ | when date_litera like 'мес%' then dateadd(date_interv month to trdate) | ||
+ | when date_litera like 'нед%' then dateadd(date_interv week to trdate) | ||
+ | when date_litera like 'дн%' then dateadd(date_interv day to trdate) | ||
+ | when date_litera like 'ден%' then dateadd(date_interv day to trdate) | ||
+ | else null end; | ||
+ | end | ||
+ | end | ||
+ | |||
+ | -- первой указана дата | ||
+ | else | ||
+ | begin | ||
+ | tmp = substring(txt from 1 for 10); | ||
+ | begin | ||
+ | begin | ||
+ | datemsg = cast(tmp as date); | ||
+ | end | ||
+ | when any do begin end | ||
+ | end | ||
+ | |||
+ | tmp = substring(txt from 1 for 8); | ||
+ | if (datemsg is null) then | ||
+ | begin | ||
+ | begin | ||
+ | datemsg = cast(tmp as date); | ||
+ | end | ||
+ | when any do begin end | ||
+ | end | ||
+ | end | ||
+ | |||
+ | if ( datemsg between current_date+ :day_delay and current_date+1+ :day_delay ) then | ||
+ | begin | ||
+ | -- беру специализацию | ||
+ | select first 1 ws.speccode from treat t | ||
+ | join orderdet od on od.orderno=t.orderno | ||
+ | join wschema ws on ws.schid=od.schcode | ||
+ | where t.treatcode= :treatcode | ||
+ | and ws.consult=1 | ||
+ | into spec; | ||
+ | -- смотрю наличие записей к доктору | ||
+ | select count(*) | ||
+ | from doctor d | ||
+ | left join USER$WEB__DOCTOR_WSCHEMA(d.dcode, null, :spec) u on 1=1 | ||
+ | left join schedule s on s.workdate= :datemsg and s.dcode=d.dcode | ||
+ | where d.filial= :filial | ||
+ | and coalesce(d.locked,0)=0 | ||
+ | and u.avalue=1 | ||
+ | and s.pcode= :pcode | ||
+ | into cnt; | ||
+ | -- готовлю сообщение | ||
+ | if (cnt=0) then | ||
+ | begin | ||
+ | pushtext = null; | ||
+ | schid = null; | ||
+ | dcode_send = null; | ||
+ | select first 1 schid, dcode, worktime | ||
+ | from (select ws.schid, d.dcode, coalesce(ws.worktime, dic.rekvint3) worktime, | ||
+ | (ds.endhour*60+ds.endmin) - (ds.beghour*60+ds.begmin) alltime, | ||
+ | (select sum(case when pcode>0 or nazndis=1 then (s.fhour*60+s.fmin)-(s.bhour*60+s.bmin) else 0 end ) | ||
+ | from schedule s left join shedmarks m on m.mrkid=s.tmstatus where s.schedident=ds.schedident) denytime | ||
+ | from wschema ws | ||
+ | left join get_pricebyid(:datemsg, 1, :filial, :fjid, 0, ws.schid) s on 1=1 | ||
+ | left join doctspec dsp on dsp.speccode= ws.speccode | ||
+ | join doctor d on d.dcode=dsp.dcode and d.filial= :filial | ||
+ | left join USER$WEB__DOCTOR_WSCHEMA(d.dcode, ws.schid) u on 1=1 | ||
+ | join doctshedule ds on ds.dcode=d.dcode and ds.wdate= :datemsg | ||
+ | left join dicinfo dic on dic.refid=-10010011 and dic.dcode=d.dcode and dic.rekvint1=ws.schid | ||
+ | where ws.structid= :structid and ws.speccode= :spec and ws.consult=1 | ||
+ | and s.sprice>0 | ||
+ | and u.avalue=1 | ||
+ | order by coalesce(repconsult,0) desc /* повторная выше */, case when d.dcode= :dcode then 1 else 0 end desc /* тот же доктор выше */) | ||
+ | where alltime>denytime /* есть время для записи */ | ||
+ | and alltime-denytime > worktime | ||
+ | into schid, dcode_send, worktime; | ||
+ | -- возможность формирования | ||
+ | if (schid is not null and dcode_send is not null) then | ||
+ | begin | ||
+ | -- данные | ||
+ | select d.dname, dep.depnum, dep.depname | ||
+ | from doctor d | ||
+ | left join departments dep on dep.depnum=d.depnum | ||
+ | where d.dcode= :dcode_send | ||
+ | into dname, depnum, depname; | ||
+ | -- формирование текста | ||
+ | pushtext = '{"to":"'||notify||'","time_to_leave":'||ttl||',"notification":{'; | ||
+ | pushtext = pushtext||'"title":"Приглашение","body":"Приглашаем Вас на повторный прием на '||(select datestr from formatdate(:datemsg))||' к доктору '||dname||'.",'; | ||
+ | pushtext = pushtext||'"sound":"true","tag":"record"},'; | ||
+ | pushtext = pushtext||'"data":{"action":"record",'; | ||
+ | pushtext = pushtext||'"DCode":'||dcode_send||',"DName":"'||dname||'",'; | ||
+ | pushtext = pushtext||'"DepNum":'||depnum||',"DepName":"'||depname||'",'; | ||
+ | pushtext = pushtext||'"filial":'||filial||',"FName":"'||fname||'",'; | ||
+ | pushtext = pushtext||'"WorkDate":"'||(select datestr from formatdate(:datemsg))||'",'; | ||
+ | pushtext = pushtext||'"SchList":"'||schid||'","wtime":"'||worktime||'",'; | ||
+ | pushtext = pushtext||'}}'; | ||
+ | -- заполняем данные | ||
+ | logid = gen_id(SMS_LOG_GEN, 1); | ||
+ | smsid = gen_id(SMS_GEN, 1); | ||
+ | smstypeid = 0; | ||
+ | smsprofileid = 1; | ||
+ | smsrefid = 70005; | ||
+ | planbtime = dateadd(9 hour to cast(current_date as timestamp)); | ||
+ | planftime = dateadd(18 hour to cast(datemsg as timestamp)); | ||
+ | -- вставляем запись | ||
+ | insert into sms_log(logid,smstypeid,smsrefid,smsprofileid,filial, | ||
+ | createdate,smstext,pcode,uid,internalid,planbtime,planftime,state) | ||
+ | values(:logid, :smstypeid, :smsrefid, :smsprofileid, :filial, | ||
+ | current_timestamp, :pushtext, :pcode, :uid, :treatcode, :planbtime, :planftime, 0); | ||
+ | insert into sms_logdet(smsid,logid,phonetype,extsmsid,phprefix,phone,state,notificationid) | ||
+ | values(:smsid, :logid, 21, null, null, null, 0, :notify); | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | </code> |