Exemplo n.º 1
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;
        }