Exemplo n.º 1
0
        /// <summary>
        /// send email for quote
        /// </summary>
        /// <param name="id"></param>
        /// <param name="rootUrl"></param>
        /// <returns></returns>
        public async Task <Operate> SendEmailById(int id, string rootUrl, string isTripleEmail = null)
        {
            var result = new Operate();

            try
            {
                var order = await _orderRepositroy.GetOrderOrQuoteById(id);

                if (order == null)
                {
                    result.Status  = -1;
                    result.Message = "Order or quote Id does not exist";
                    return(result);
                }
                var orderManager = await _managerRepository.GetById(order.ManagerId ?? 0);

                if (orderManager == null)
                {
                    result.Status  = -1;
                    result.Message = "Manager Id does not exist";
                    return(result);
                }
                var managers = await _managerRepository.GetAllAdmin();

                var to = new List <string>();
                if (!string.IsNullOrEmpty(isTripleEmail))
                {
                    to = (from manager in managers
                          select manager.Email).ToList();
                    to.Add(orderManager.Email);
                    to.Add(order.ContactEmail);
                }
                else
                {
                    to = (from manager in managers
                          select manager.Email).ToList();
                }

                if (string.IsNullOrWhiteSpace(order.PDFPath))
                {
                    result.Status  = -1;
                    result.Message = "PDF file can't be found";
                    return(result);
                }

                var emailTemplate = _settingService.GetEmailTemplates();
                //var emailTemplate = new MulitViewResult<Model.SettingModel.EmailTemplateModel>();
                if (emailTemplate.Datas.Count <= 0)
                {
                    result.Status  = -1;
                    result.Message = "Email template cannot be found";
                    return(result);
                }
                var subject = emailTemplate.Datas[0]?.Subject;
                var body    = emailTemplate.Datas[0]?.Body;
                body = body.Replace("\n", "<br/>");
                var fileList = new List <string> {
                    order.PDFPath
                };
                new MailService().SendMail(to, new List <string>(), subject, body, fileList);
            }
            catch (Exception ex)
            {
                result.Status  = -1;
                result.Message = ex.Message;
                Logger.WriteErrorLog("OrderService", "SendEmailById", ex);
                throw ex;
            }
            return(result);
        }