getTherapyProgram (dbFactory: unit -> CoreDb) programId =
async {
use db = dbFactory()
let! programRow =
query {
for p in db.TherapyPrograms do
where (p.Id = programId)
groupJoin pr in db.ReceptionProtocols
on (p.Id =? pr.TherapyProgramId) into res
for pr in res.DefaultIfEmpty() do
select {|
ProgramId = p.Id
PatientId = p.PatientId
ProtocolId = Nullable pr.Id
State = p.State
DiagnosisId = p.DiagnosisId
DiagnosisKind = p.DiagnosisKind
AttachedToId = p.AttachedDoctorId
SignedDate = p.SigningTime
|}
} |> Query.tryHeadAsync
let! itemsRows = db.ProgramItemDetails programId
|> Query.toListAsync
let items =
itemsRows
|> List.groupBy (fun p -> {| ItemId = p.ItemId; ServiceId = p.ServiceId; Count = p.Count; Provided = p.Provided; HasSatellites = p.HasSatellites; |})
|> List.map (fun (k, det) ->
let details =
det
|> List.map (fun p ->
let protocol: Program.Protocol option =
if p.ProtocolId.HasValue && p.Signed.HasValue && p.Signed.Value then
let pr: Program.Protocol = {
ProtocolId = p.ProtocolId.Value
IsSigned = p.Signed.Value
SignDate = p.SignDate |> Option.ofNullable
}
Some (pr)
else
None
{
//ProtocolId = p.ProtocolId |> Option.ofNullable
Protocol = protocol
IsProvided = p.IsProvided
AtDate = p.AtDate |> Option.ofNullable
ByDoctor = p.ByDoctor |> Option.ofNullable
HasAttachments = p.HasFiles
})
|> List.filter (fun p -> p.AtDate.IsSome)
|> List.sortByDescending (fun p -> p.AtDate)
let head = {
ItemId = k.ItemId
ServiceId = k.ServiceId
Count = k.Count
Provided = k.Provided
//ProtocolId = details.Head.ProtocolId
Protocol = details |> List.map (fun p -> p.Protocol) |> List.choose id |> List.tryHead
выглядит как несколько запросов) но вообще норм)
Обсуждают сегодня