Exemple #1
0
        // Author: DucBM
        public ResponseObjectDto UpdateAppointment(int appointmentId, List <SampleGettingDto> sampleGettingDtos)
        {
            var responseObject = new ResponseObjectDto();

            responseObject.Success = true;
            responseObject.Message = "Chỉnh sửa lịch thành công";

            var appRepo = this.RepositoryHelper.GetRepository <IAppointmentRepository>(this.UnitOfWork);
            // get existing appointment by AppointmentCode

            var appointment = appRepo.GetAppointmentByIdInclude(appointmentId);

            // delete old records
            // TEMPORARY !! -> too waist memory !!
            foreach (var sg in appointment.SampleGettings)
            {
                sg.IsDeleted = true;
                foreach (var lt in sg.LabTestings)
                {
                    lt.IsDeleted = true;
                }
            }

            // modify SampleGettings property
            appointment.SampleGettings = new List <SampleGetting>();
            foreach (var sgDto in sampleGettingDtos)
            {
                var sg = Mapper.Map <SampleGettingDto, SampleGetting>(sgDto);
                sg.LabTestings = new List <LabTesting>();
                sg.Status      = "NEW";
                sg.TableId     = 1;
                sg.IsDeleted   = false;
                foreach (var id in sgDto.LabTestIds)
                {
                    var lt = new LabTesting();
                    lt.LabTestId = id;
                    sg.LabTestings.Add(lt);
                }
                appointment.SampleGettings.Add(sg);
            }
            try
            {
                // update entity
                appRepo.Update(appointment);
                // save to DB
                var result = this.UnitOfWork.SaveChanges();
                if (result.Any())
                {
                    responseObject.Success = false;
                    responseObject.Message = "Có lỗi xảy ra";
                    responseObject.Data    = result;
                }
            }
            catch (Exception ex)
            {
                responseObject.Success = false;
                responseObject.Message = "Có lỗi xảy ra";
                responseObject.Data    = new
                {
                    ErrorMessage = ex.Message,
                    StackTrace   = ex.StackTrace
                };
            }

            return(responseObject);
        }
Exemple #2
0
        public ResponseObjectDto Create(AppointmentDto appointmentDto)
        {
            var responseObject = new ResponseObjectDto();

            responseObject.Success = true;
            responseObject.Message = "Đặt lịch thành công";

            try
            {
                var appointmentRepo    = this.RepositoryHelper.GetRepository <IAppointmentRepository>(this.UnitOfWork);
                var sgRepo             = this.RepositoryHelper.GetRepository <ISampleGettingRepository>(this.UnitOfWork);
                var sampleRepo         = this.RepositoryHelper.GetRepository <ISampleRepository>(this.UnitOfWork);
                var tableRepo          = this.RepositoryHelper.GetRepository <ITableRepository>(this.UnitOfWork);
                var patientRepo        = this.RepositoryHelper.GetRepository <IPatientRepository>(this.UnitOfWork);
                var patientAccountRepo = this.RepositoryHelper.GetRepository <IPatientAccountRepository>(this.UnitOfWork);
                var accRepo            = this.RepositoryHelper.GetRepository <IAccountRepository>(this.UnitOfWork);

                Patient patient = null;
                if (appointmentDto.PatientDto != null) // Make Ap. without Login
                {
                    //var idcNumber = appointmentDto.PatientDto.IdentityCardNumber;
                    //patient = patientRepo.GetByIDCNumber(idcNumber);
                    var phone   = appointmentDto.PatientDto.PhoneNumber;
                    var account = accRepo.GetByPhoneNumber(phone);

                    if (account == null)
                    {
                        account             = new Account();
                        account.FullName    = appointmentDto.PatientDto.FullName;
                        account.PhoneNumber = phone;
                        account.Password    = ConstantManager.DEFAULT_PASSWORD;
                        account.RoleId      = (int)RoleEnum.Patient;
                        account.IsDeleted   = false;
                        accRepo.Create(account);
                        try
                        {
                            var result = this.UnitOfWork.SaveChanges();
                            if (result.Any())
                            {
                                responseObject.Success = false;
                                responseObject.Message = "Có lỗi xảy ra";
                                responseObject.Data    = result;
                                return(responseObject);
                            }
                        }
                        catch (Exception ex)
                        {
                            responseObject.Success = false;
                            responseObject.Message = "Có lỗi xảy ra";
                            responseObject.Data    = ex;
                            return(responseObject);
                        }
                        responseObject.Data = new
                        {
                            appointmentDto.PatientDto.PhoneNumber,
                            DefaultPassword = account.Password
                        };
                    }
                    int accountId = account.AccountId;

                    var dateOfBirth = DateTime.Parse(appointmentDto.PatientDto.DateOfBirth);
                    patient = patientRepo.GetBy(accountId, appointmentDto.PatientDto.FullName, dateOfBirth);
                    if (patient == null)
                    {
                        // Create a new Patient
                        patient                    = new Patient();
                        patient.AccountId          = accountId;
                        patient.FullName           = appointmentDto.PatientDto.FullName;
                        patient.IdentityCardNumber = appointmentDto.PatientDto.IdentityCardNumber;
                        patient.PhoneNumber        = appointmentDto.PatientDto.PhoneNumber;
                        patient.DateOfBirth        = dateOfBirth;
                        patient.Gender             = appointmentDto.PatientDto.Gender;
                        patient.HomeAddress        = appointmentDto.PatientDto.HomeAddress;
                        patient.IsDeleted          = false;

                        patientRepo.Create(patient);
                        try
                        {
                            var result = this.UnitOfWork.SaveChanges();
                            if (result.Any())
                            {
                                responseObject.Success = false;
                                responseObject.Message = "Có lỗi xảy ra";
                                responseObject.Data    = result;
                                return(responseObject);
                            }
                        }
                        catch (Exception ex)
                        {
                            responseObject.Success = false;
                            responseObject.Message = "Có lỗi xảy ra";
                            responseObject.Data    = ex;
                            return(responseObject);
                        }
                        patient.PatientCode = "BN" + patient.PatientId;
                    }

                    if (account != null && patient != null)
                    {
                        var patientAccount = new PatientAccount();
                        patientAccount.AccountId = account.AccountId;
                        patientAccount.PatientId = patient.PatientId;
                        patientAccount.IsDeleted = false;
                        patientAccountRepo.Create(patientAccount);
                        var result = this.UnitOfWork.SaveChanges();
                        if (result.Any())
                        {
                            responseObject.Success = false;
                            responseObject.Message = "Có lỗi xảy ra";
                            responseObject.Data    = result;
                            return(responseObject);
                        }
                    }

                    appointmentDto.PatientId = patient.PatientId;
                }
                else if (appointmentDto.PatientId != null) // Make Ap. with Login
                {
                    patient = patientRepo.GetById((int)appointmentDto.PatientId);
                    if (patient == null)
                    {
                        responseObject.Success = false;
                        responseObject.Message = "Có lỗi xảy ra";
                        responseObject.Data    = new
                        {
                            MessageForDev = "Không tồn tại PatientId này"
                        };
                        return(responseObject);
                    }
                }
                else
                {
                    responseObject.Success = false;
                    responseObject.Message = "Có lỗi xảy ra";
                    responseObject.Data    = new
                    {
                        MessageForDev = "PatientId truyền vào là Null"
                    };
                    return(responseObject);
                }

                var appointment = new Appointment();
                // Convert AppointmentDto to Appointment
                var now   = DateTime.Now;
                var sDate = now.ToString("yyyy-MM-dd");

                // Generate code
                var lastcode = appointmentRepo.GetLastCode(sDate);
                var count    = 0;
                if (lastcode != null)
                {
                    count = int.Parse(lastcode.Substring("yyyy-MM-dd-".Length));
                }
                var code = sDate + "-" + (count + 1);


                appointment.PatientId       = patient.PatientId;
                appointment.AppointmentCode = code;
                appointment.Status          = "NEW";
                appointment.PatientId       = appointmentDto.PatientId;
                appointment.EnterTime       = now;
                appointment.IsOnline        = true;
                appointment.IsDeleted       = false;

                appointment.SampleGettings = new List <SampleGetting>();

                var sampleGettingDtos = appointmentDto.SampleGettingDtos;
                foreach (var sgDto in appointmentDto.SampleGettingDtos)
                {
                    var duplicatedSG = sgRepo.GetFirst(sgDto.SampleId, sgDto.GettingDate, (int)appointmentDto.PatientId);
                    if (duplicatedSG != null)
                    {
                        var sampleName = sampleRepo.GetById(sgDto.SampleId).SampleName;
                        var date       = DateTime.Parse(sgDto.GettingDate).ToString("dd-MM-yyyy");
                        responseObject.Success = false;
                        responseObject.Message = string.Format("Bạn đã từng đăng ký lấy mẫu {0} vào ngày {1}.\n Bạn không thể lấy mẫu {0} 2 lần 1 ngày.", sampleName, date);
                        return(responseObject);
                    }

                    var sg        = Mapper.Map <SampleGettingDto, SampleGetting>(sgDto);
                    var avaiTable = tableRepo.GetFirstAvailableTable((int)sg.SlotId, (DateTime)sg.GettingDate);
                    if (avaiTable == null)
                    {
                        responseObject.Success = false;
                        responseObject.Message = "Có ca xét nghiệm đã hết chỗ";
                        return(responseObject);
                    }
                    sg.TableId     = avaiTable.TableId;
                    sg.Status      = "NEW";
                    sg.LabTestings = new List <LabTesting>();
                    sg.IsGot       = false;
                    sg.IsPaid      = false;
                    sg.IsDeleted   = false;
                    foreach (var id in sgDto.LabTestIds)
                    {
                        var labTesting = new LabTesting();
                        labTesting.LabTestId       = id;
                        labTesting.SampleGettingId = sg.SampleGettingId;
                        labTesting.Status          = "NEW";
                        labTesting.IsDeleted       = false;
                        sg.LabTestings.Add(labTesting);
                    }
                    appointment.SampleGettings.Add(sg);
                }
                // Create
                appointmentRepo.Create(appointment);
                try
                {
                    var result = this.UnitOfWork.SaveChanges();
                    if (result.Any())
                    {
                        responseObject.Success = false;
                        responseObject.Message = "Có lỗi xảy ra";
                        responseObject.Data    = result;
                        return(responseObject);
                    }
                }
                catch (Exception ex)
                {
                    responseObject.Success = false;
                    responseObject.Message = "Có lỗi xảy ra";
                    responseObject.Data    = ex;
                    return(responseObject);
                }
            }
            catch (Exception ex)
            {
                responseObject.Success = false;
                responseObject.Message = "Có lỗi xảy ra";
                responseObject.Data    = ex;
                return(responseObject);
            }

            return(responseObject);
        }
        //ten ten
        public bool Add(AppointmentAddDto data, List <LabTestDto> labTests)
        {
            var rs       = false;
            var appRepo  = RepositoryHelper.GetRepository <IAppointmentRepository>(UnitOfWork);
            var accRepo  = RepositoryHelper.GetRepository <IAccountRepository>(UnitOfWork);
            var paRepo   = RepositoryHelper.GetRepository <IPatientRepository>(UnitOfWork);
            var sgRepo   = RepositoryHelper.GetRepository <ISampleGettingRepository>(UnitOfWork);
            var slotRepo = RepositoryHelper.GetRepository <ISlotRepository>(UnitOfWork);

            try
            {
                //tao account
                var account = accRepo.GetAll().FirstOrDefault(p => p.PhoneNumber == data.Phone);
                if (account == null)
                {
                    accRepo.Create(new Account
                    {
                        PhoneNumber = data.Phone,
                        FullName    = data.Name,
                        IsDeleted   = false,
                    });
                }
                UnitOfWork.SaveChanges();
                //tao patient
                var     accId   = accRepo.GetAll().FirstOrDefault(p => p.PhoneNumber == data.Phone).AccountId;
                Patient patient = null;
                patient = new Patient();

                patient.AccountId = accId;
                patient.Gender    = data.Gender;
                //IdentityCardNumber = data.IdentityCardNumber,
                patient.DateOfBirth = data.DateOfBirth;
                patient.HomeAddress = data.Address;
                patient.FullName    = data.Name;
                patient.PhoneNumber = data.Phone;
                patient.IsDeleted   = false;
                paRepo.Create(patient);

                UnitOfWork.SaveChanges();
                patient.PatientCode = "BN" + patient.PatientId;

                //tao cuoc hen
                //var paId = paRepo.GetFirst(p => p.PhoneNumber == data.Phone).PatientId;//lấy Id bệnh nhân  dựa vào phone
                var appCode = CreateAppReturnCode(new Appointment //tạo app
                {
                    PatientId = patient.PatientId,
                    Status    = "NEW",
                    IsOnline  = false,
                    //Date = DateTime.Now.Date,
                    IsDeleted = false
                });
                UnitOfWork.SaveChanges();

                //xep lich hen cho tung loai xet nghiem
                var appId = appRepo.GetFirst(p => p.AppointmentCode == appCode).AppointmentId;//
                if (data.Mau == true)
                {
                    //tim slot va ban trong
                    var slotAndTable = GetEmptyTableAndSlot(1);
                    //neu con ban va slot trong thi moi tao lich hen
                    if (slotAndTable != null)
                    {
                        var sg = new SampleGetting //xong tạo sg ở đây
                        {
                            GettingDate   = DateTime.Now.Date,
                            AppointmentId = appId,
                            SampleId      = 1,
                            SlotId        = slotAndTable.slotId,
                            TableId       = slotAndTable.tableId,
                            IsDeleted     = false
                        };
                        sg.LabTestings = new List <LabTesting>();
                        foreach (var lt in labTests)
                        {
                            if (lt.SampleId == 1) // 1: Mau
                            {
                                var labTesting = new LabTesting();
                                labTesting.LabTestId = lt.LabTestId;
                                sg.LabTestings.Add(labTesting);
                            }
                        }
                        //tao lich hen loai xet nghiem mau
                        sgRepo.Create(sg);
                        //var ID = sgRepo.GetFirst(p => p.SampleGettingId == sgId).SampleGettingId;
                        rs = true;
                    }
                }
                if (data.NuocTieu == true)
                {
                    //tim slot va ban trong
                    var slotAndTable = GetEmptyTableAndSlot(1);
                    //neu con ban va slot trong thi moi tao lich hen
                    if (slotAndTable != null)
                    {
                        var sg = new SampleGetting //xong tạo sg ở đây
                        {
                            GettingDate   = DateTime.Now.Date,
                            AppointmentId = appId,
                            SampleId      = 2,
                            SlotId        = slotAndTable.slotId,
                            TableId       = slotAndTable.tableId,
                            IsDeleted     = false
                        };
                        sg.LabTestings = new List <LabTesting>();
                        foreach (var lt in labTests)
                        {
                            if (lt.SampleId == 2)
                            {
                                var labTesting = new LabTesting();
                                labTesting.LabTestId = lt.LabTestId;
                                sg.LabTestings.Add(labTesting);
                            }
                        }
                        //tao lich hen loai xet nghiem mau
                        sgRepo.Create(sg);
                        //var ID = sgRepo.GetFirst(p => p.SampleGettingId == sgId).SampleGettingId;
                        rs = true;
                    }
                }
                if (data.Phan == true)
                {
                    var slotAndTable = GetEmptyTableAndSlot(2);
                    if (slotAndTable != null)
                    {
                        var sg = new SampleGetting //xong tạo sg ở đây
                        {
                            GettingDate   = DateTime.Now.Date,
                            AppointmentId = appId,
                            SampleId      = 4,
                            SlotId        = slotAndTable.slotId,
                            TableId       = slotAndTable.tableId,
                            IsDeleted     = false
                        };
                        sg.LabTestings = new List <LabTesting>();
                        foreach (var lt in labTests)
                        {
                            if (lt.SampleId == 4)
                            {
                                var labTesting = new LabTesting();
                                labTesting.LabTestId = lt.LabTestId;
                                sg.LabTestings.Add(labTesting);
                            }
                        }
                        //tao lich hen loai xet nghiem phan
                        sgRepo.Create(sg);
                        rs = true;
                    }
                }
                if (data.TeBaoHoc == true)
                {
                    //tim slot va ban trong
                    var slotAndTable = GetEmptyTableAndSlot(3);
                    //neu con ban va slot trong thi moi tao lich hen
                    if (slotAndTable != null)
                    {
                        var sg = new SampleGetting //xong tạo sg ở đây
                        {
                            GettingDate   = DateTime.Now.Date,
                            AppointmentId = appId,
                            SampleId      = 3,
                            SlotId        = slotAndTable.slotId,
                            TableId       = slotAndTable.tableId,
                            IsDeleted     = false
                        };
                        sg.LabTestings = new List <LabTesting>();
                        foreach (var lt in labTests)
                        {
                            if (lt.SampleId == 3) // 1: Mau
                            {
                                var labTesting = new LabTesting();
                                labTesting.LabTestId = lt.LabTestId;
                                sg.LabTestings.Add(labTesting);
                            }
                        }
                        //tao lich hen loai xet nghiem mau
                        sgRepo.Create(sg);
                        //var ID = sgRepo.GetFirst(p => p.SampleGettingId == sgId).SampleGettingId;
                        rs = true;
                    }
                }
                if (data.Dich == true)
                {
                    var slotAndTable = GetEmptyTableAndSlot(4);
                    if (slotAndTable != null)
                    {
                        var sg = new SampleGetting //xong tạo sg ở đây
                        {
                            GettingDate   = DateTime.Now.Date,
                            AppointmentId = appId,
                            SampleId      = 5,
                            SlotId        = slotAndTable.slotId,
                            TableId       = slotAndTable.tableId,
                            IsDeleted     = false
                        };
                        sg.LabTestings = new List <LabTesting>();
                        foreach (var lt in labTests)
                        {
                            if (lt.SampleId == 5)
                            {
                                var labTesting = new LabTesting();
                                labTesting.LabTestId = lt.LabTestId;
                                sg.LabTestings.Add(labTesting);
                            }
                        }
                        //tao lich hen loai xet nghiem dich
                        sgRepo.Create(sg);
                        rs = true;
                    }
                }

                UnitOfWork.SaveChanges();
            }
            catch (Exception ex) { return(false); }
            return(rs);
        }