Exemplo n.º 1
0
        public bool SendSmsAppointment(string apptId, SmsCase smsCase, out string messageCode)
        {
            bool result = false;
            messageCode = MessageCode.GeneralCode.SystemError;
            try
            {
                SingletonLogger.Instance.Debug(String.Format("SendSmsAppointment-------ApptId: {0}", apptId));

                #region Validate
                var appt = DataRepository.AppointmentProvider.GetById(apptId);
                if (appt == null || appt.IsDisabled)
                {
                    messageCode = MessageCode.SmsCode.ApptNotExists;
                    return false;
                }

                var patient = DataRepository.VcsPatientProvider.GetByPatientCode(appt.PatientCode).FirstOrDefault();
                if (patient == null || string.IsNullOrEmpty(patient.MobilePhone))
                {
                    messageCode = MessageCode.SmsCode.NoMobile;
                    return false;
                }
                #endregion

                #region Lấy thông tin
                // Lấy đầy đủ thông tin
                var doctor = DataRepository.UsersProvider.GetByUsername(appt.Username);
                var room = DataRepository.RoomProvider.GetById(appt.RoomId.HasValue ? appt.RoomId.Value : 0);
                var service =
                    DataRepository.ServicesProvider.GetById(appt.ServicesId.HasValue ? appt.ServicesId.Value : 0);
                string doctorName = doctor == null ? string.Empty : doctor.Firstname,
                       roomName = room == null ? string.Empty : room.Title,
                       serviceName = service == null ? string.Empty : service.Title;
                #endregion

                #region Lưu Sms, Receiver, Log
                var sms = DataRepository.SmsProvider.GetByAppointmentId(apptId).FirstOrDefault();
                var receivers = new TList<SmsReceiver>();
                if (sms == null)
                {
                    sms = new Sms
                              {
                                  EntityState = EntityState.Added
                              };
                }
                else
                {
                    sms.EntityState = EntityState.Changed;
                    receivers = DataRepository.SmsReceiverProvider.GetBySmsId(sms.Id);
                }

                switch (smsCase)
                {
                    case SmsCase.WebApp:
                        sms.Message = String.Format(ServiceFacade.SettingsHelper.ApptSmsTemplate, patient.FirstName,
                                                    appt.StartTime, doctorName, roomName, serviceName);
                        break;
                    case SmsCase.Auto:
                        sms.Message = String.Format(ServiceFacade.SettingsHelper.ApptAutoSmsTemplate, patient.FirstName,
                                                    appt.StartTime, doctorName, roomName, serviceName);
                        break;
                }
                sms.SmsType = SmsType.Appointment;
                sms.SendTime = DateTime.Now;
                sms.IsSendNow = true;
                sms.IsSent = false;
                sms.SendingTimes += 1;
                sms.AppointmentId = appt.Id;
                sms = DataRepository.SmsProvider.Save(sms);
                if (sms == null)
                {
                    return false;
                }

                var receiver = receivers.FirstOrDefault(x => x.Mobile.Equals(patient.MobilePhone));
                if (receiver == null)
                {
                    receiver = new SmsReceiver
                                   {
                                       Mobile = patient.MobilePhone,
                                       FirstName = patient.FirstName,
                                       LastName = patient.LastName,
                                       UserType = SmsUserType.Patient,
                                       SmsId = sms.Id,
                                       IsSent = false,
                                       SendingTimes = 0,
                                       CreateDate = DateTime.Now
                                   };
                    DataRepository.SmsReceiverProvider.Insert(receiver);
                }

                var smsLog = new SmsLog
                                 {
                                     Id = Guid.NewGuid(),
                                     SmsId = sms.Id,
                                     Message = sms.Message,
                                     Mobile = receiver.Mobile,
                                     SendTime = sms.SendTime,
                                     RealSendTime = DateTime.Now,
                                     IsSent = false
                                 };
                DataRepository.SmsLogProvider.Insert(smsLog);
                #endregion

                #region Gửi sms và update thông tin

                var status = SendSms(smsLog.Mobile, smsLog.Title, smsLog.Message);
                if (status)
                {
                    sms.IsSent = receiver.IsSent = smsLog.IsSent = true;
                    result = true;
                    messageCode = MessageCode.SmsCode.Success;
                }

                DataRepository.SmsLogProvider.Update(smsLog);
                DataRepository.SmsProvider.Update(sms);
                DataRepository.SmsReceiverProvider.Update(receiver);

                #endregion
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error(String.Format("SendSmsAppointment"), ex);
            }
            return result;
        }
Exemplo n.º 2
0
        public bool AddSms(Sms sms, string phones, out string messageCode)
        {
            bool result = false;
            messageCode = MessageCode.GeneralCode.SystemError;
            try
            {
                DataRepository.SmsProvider.Insert(sms);

                // Parse phone list
                var lstPhone = phones.Split(new[] { GeneralConstants.Seperate }, StringSplitOptions.None).Distinct().ToList();
                var receivers = new TList<SmsReceiver>();
                var smsLogs = new TList<SmsLog>();
                foreach (var p in lstPhone)
                {
                    var phone = p.Trim();
                    if (string.IsNullOrEmpty(phone))
                    {
                        continue;
                    }

                    receivers.Add(new SmsReceiver
                                      {
                                          Mobile = phone,
                                          UserType = SmsUserType.Patient,
                                          SmsId = sms.Id,
                                          IsSent = sms.IsSendNow, // Set true if send now
                                          SendingTimes = 1,
                                          CreateUser = sms.CreateUser,
                                          CreateDate = DateTime.Now
                                      });

                    if (sms.IsSendNow)
                    {
                        sms.IsSent = true;

                        smsLogs.Add(new SmsLog
                                        {
                                            Id = Guid.NewGuid(),
                                            SmsId = sms.Id,
                                            Message = sms.Message,
                                            Mobile = phone,
                                            SendTime = DateTime.Now,
                                            RealSendTime = DateTime.Now,
                                            IsSent = true,
                                            CreateDate = DateTime.Now,
                                            CreateUser = sms.CreateUser
                                        });
                        SendSms(phone, string.Empty, sms.Message);
                    }
                }

                DataRepository.SmsReceiverProvider.Insert(receivers);
                if (smsLogs.Any())
                {
                    DataRepository.SmsLogProvider.Insert(smsLogs);
                }

                DataRepository.SmsProvider.Update(sms);

                messageCode = MessageCode.SmsCode.Success;
                result = true;
            }
            catch (Exception ex)
            {
                SingletonLogger.Instance.Error(String.Format("AddSms"), ex);
            }
            return result;
        }