Ejemplo n.º 1
0
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                Run();
            }
            catch (Exception ex)
            {
                Global.SchedulerLogger.Error(nameof(Run), ex);
            }

            void Run()
            {
                var biohis = new com.biohis.Medicine {
                    header = szgjToken,
                };

                var hospitalNumbers = mongo.PatientCollection.AsQueryable().Where(p => !p.IsDisabled).Select(p => p.Hospitalization.HospitalNumber).Where(p => !string.IsNullOrEmpty(p)).ToList();

                Global.SchedulerLogger.Info($"{hospitalNumbers.Count} patients will be resynced");

                var cvtr = new SZGJConverter();

                foreach (var number in hospitalNumbers)
                {
                    try
                    {
                        var xml = biohis.getInpatient(number);
                        var cvt = cvtr.SuZhouPatients2Patients(xml);
                        if (cvt.Code == 1)
                        {
                            mongo.PatientCollection.UpdateOne(p => p.Hospitalization.HospitalNumber == number, Builders <Patient> .Update.Set(p => p.IsDisabled, true));
                        }
                        else
                        {
                            var patObj = cvt?.Data.FirstOrDefault();
                            if (patObj != null)
                            {
                                mongo.PatientCollection.FindOneAndReplace <Patient>(x => x.UniqueId == patObj.UniqueId, patObj, new FindOneAndReplaceOptions <Patient, Patient> {
                                    IsUpsert = true
                                });
                            }
                            else
                            {
                                Global.SchedulerLogger.Info($"'{number}' not found.");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Global.SchedulerLogger.Error($"HospitalNumber => '{number}'", ex);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public bool reciveData(string xml)
        {
            try
            {
                var    code = 0;
                string msg  = null;

                var cvtr          = new SZGJConverter();
                var prescriptions = cvtr.SuZhouApply2Prescriptions(xml).Data;
                if (prescriptions.All(p => p.IsAddition))
                {
                    foreach (var p in prescriptions)
                    {
                        var medication = mongo.MedicationCollection.AsQueryable().First(m => !m.IsDisabled && m.Mode == ExchangeMode.Medication && m.DoctorId == p.DoctorId && m.PatientId == p.PatientId && m.GoodsId == p.GoodsId && m.QtyActual == p.Qty && m.PrescriptionId == null);
                        mongo.MedicationCollection.UpdateOne(x => x.UniqueId == medication.UniqueId, Builders <Medication> .Update.Set(o => o.PrescriptionId, p.UniqueId).Set(o => o.IsSynchronized, true));

                        p.QtyActual = medication.QtyActual;
                        p.Plans     = medication.Plans;
                        mongo.PrescriptionCollection.FindOneAndReplace <Prescription>(x => x.UniqueId == p.UniqueId, p, new FindOneAndReplaceOptions <Prescription, Prescription> {
                            IsUpsert = true
                        });
                    }
                    code = 0;
                    msg  = "先预支后补充的医嘱";
                }
                else
                {
                    if (prescriptions.Any(p => p.Qty != Math.Ceiling(p.Qty)))
                    {
                        // 2017-12-17 取药量为浮点数时医嘱发送到中心药房
                        code = 2;
                        msg  = "非整数医嘱";
                    }
                    else
                    {
                        // 临时医嘱,查询接收部门是否库存足够
                        var dst  = prescriptions.Select(p => p.DepartmentDestinationId).Where(p => !string.IsNullOrEmpty(p)).Distinct().FirstOrDefault();
                        var data = mongo.CustomerCollection.AsQueryable().Where(c => !c.IsDisabled).Select(cs => new
                        {
                            Cabinets      = cs.Cabinets.Where(c => c.DepartmentId == dst),
                            OutOfCabinets = cs.OutOfCabinets.Where(v => v.DepartmentId == dst),
                        }).ToList();
                        var fills  = data.SelectMany(d => d.Cabinets).Concat(data.SelectMany(d => d.OutOfCabinets)).SelectMany(c => c.Drawers).SelectMany(d => d.Boxes).SelectMany(b => b.Fills).ToList();
                        var unExes = mongo.PrescriptionCollection.AsQueryable().Where(p => !p.IsDisabled && p.DepartmentDestinationId == dst && p.FinishTime == null)
                                     .Select(p => new { p.GoodsId, p.BatchNumber, p.ExpiredDate, p.Mode, p.Qty, p.QtyActual, }).ToList();
                        if (prescriptions.All(p =>
                        {
                            var qty = fills.Where(f => f.GoodsId == p.GoodsId && f.ExpiredDate > DateTime.Now && (string.IsNullOrEmpty(p.BatchNumber) || f.BatchNumber == p.BatchNumber)).Sum(f => f.QtyExisted);
                            qty -= unExes.Where(e => e.GoodsId == p.GoodsId && (p.BatchNumber ?? string.Empty) == e.BatchNumber && e.Mode == ExchangeMode.CheckOut).Sum(e => e.Qty - e.QtyActual); // 取药
                            qty += unExes.Where(e => e.GoodsId == p.GoodsId && (p.BatchNumber ?? string.Empty) == e.BatchNumber && e.Mode == ExchangeMode.CheckIn).Sum(e => e.Qty - e.QtyActual);  // 退药
                            return(qty >= p.Qty);
                        }))
                        {
                            foreach (var p in prescriptions)
                            {
                                mongo.PrescriptionCollection.FindOneAndReplace <Prescription>(x => x.UniqueId == p.UniqueId, p, new FindOneAndReplaceOptions <Prescription, Prescription> {
                                    IsUpsert = true
                                });
                                mongo.PatientCollection.FindOneAndReplace <Patient>(x => x.UniqueId == p.Patient.UniqueId, p.Patient, new FindOneAndReplaceOptions <Patient, Patient> {
                                    IsUpsert = true
                                });
                            }

                            code = 0;
                            msg  = "临时医嘱库存充足";
                        }
                        else
                        {
                            code = 1;
                            msg  = "本地库存不足";
                        }
                    }
                }
                BaseConverter.WriteSyncLog(nameof(Prescription), $"{SfraObject.GenerateId()}_{msg}", xml);
                return(code == 0);
            }
            catch (Exception ex)
            {
                BaseConverter.WriteSyncLog(nameof(Prescription), SfraObject.GenerateId(), xml, ex);
                return(false);
            }
        }