Exemplo n.º 1
0
        public Response <Customer> FindAll(Guid queryID, int index, int count, IEnumerable <Sort> sorts)
        {
            Model.Customers.Query cQuery = _queryRepository.FindBy(queryID);

            // Sorting
            string orderByClause = string.Empty;

            if (sorts != null)
            {
                orderByClause = " Order By ";
                bool firstFlag = true;
                foreach (var sort in sorts)
                {
                    orderByClause += String.Format("{0} {1} {2} ", firstFlag ? "" : ",", sort.SortColumn, sort.Asc ? "" : "Desc");
                    firstFlag      = false;
                }
            }
            cQuery.QueryText = cQuery.QueryText.Replace("OrderByClause", orderByClause);
            cQuery.QueryText = cQuery.QueryText.Replace("WhereClause", string.Empty);

            IQuery query = SessionFactory.GetCurrentSession().CreateQuery(cQuery.QueryText);

            if (cQuery.PrmDefinition != null && cQuery.PrmValues != null)
            {
                string[] PrmDefinition = cQuery.PrmDefinition.Split(new char[] { ';' });
                string[] PrmValues     = cQuery.PrmValues.Split(new char[] { ';' });

                int i = 0;
                foreach (string prmDef in PrmDefinition)
                {
                    query.SetParameter(prmDef, PrmValues[i]);
                    i++;
                }
            }

            ICriteria criteriaQuery = SessionFactory.GetCurrentSession().CreateCriteria(typeof(Customer));
            //criteriaQuery.SetFetchMode("Center", FetchMode.Lazy).
            //    SetFetchMode("Agency", FetchMode.Lazy).
            //    SetFetchMode("Network", FetchMode.Lazy).
            //    SetFetchMode("SuctionMode", FetchMode.Lazy).
            //    SetFetchMode("SuctionModeDetail", FetchMode.Lazy).
            //    SetFetchMode("DocumentStatus", FetchMode.Lazy).
            //    SetFetchMode("Employee", FetchMode.Lazy).
            //    SetFetchMode("BuyPossibility", FetchMode.Lazy).
            //    SetFetchMode("FollowStatus", FetchMode.Lazy);
            //int resultCount = criteriaQuery.SetProjection(Projections.Count("ID")).UniqueResult<int>();
            var count1 = SessionFactory.GetCurrentSession().QueryOver <Customer>().RowCount();
            //IList<Customer> list=new List<Customer>();
            //list = query.List<Customer>();
            //int total = list.Count();
            //IEnumerable<Customer> customerList = list.Skip(index).Take(count);
            IList <Customer> customerList = query.SetFirstResult(index).SetMaxResults(count).List <Customer>();


            //int resultCount = customerList.Count();
            int finalCount = 0;

            if (cQuery.Counting)
            {
                finalCount = cQuery.CustomerCount;
            }
            if (!cQuery.Counting && cQuery.AllCustomer)
            {
                finalCount = SessionFactory.GetCurrentSession().QueryOver <Customer>().RowCount();
            }
            if (!cQuery.Counting && !cQuery.AllCustomer)
            {
                finalCount = query.List <Customer>().Count();
            }
            return(new Response <Customer>(customerList, finalCount));
        }
Exemplo n.º 2
0
        public GeneralResponse PrepareToAddCustomerLevel(AddCustomerLevelRequest request)
        {
            GeneralResponse response = new GeneralResponse();

            try
            {
                CustomerLevel customerLevel = new CustomerLevel();
                customerLevel.ID             = Guid.NewGuid();
                customerLevel.CreateDate     = PersianDateTime.Now;
                customerLevel.CreateEmployee = _employeeRepository.FindBy(request.CreateEmployeeID);
                customerLevel.Customer       = this._customerRepository.FindBy(request.CustomerID);
                customerLevel.Level          = this._levelRepository.FindBy(request.NewLevelID);
                customerLevel.Note           = request.Note;


                customerLevel.RowVersion = 1;

                #region Validation

                if (customerLevel.GetBrokenRules().Count() > 0)
                {
                    foreach (BusinessRule businessRule in customerLevel.GetBrokenRules())
                    {
                        response.ErrorMessages.Add(businessRule.Rule);
                    }

                    return(response);
                }

                #endregion

                #region Check Conditions

                if (customerLevel.Customer.Center == null)
                {
                    response.ErrorMessages.Add("هیچ گونه مرکز مخابراتی برای مشتری مورد نظر تعریف نشده است. لطفاً با مراجعه به تنظیمات، مرکز مخابراتی مربوط به پیش شماره مشتری را تعریف کنید.");

                    return(response);
                }

                CheckConditionResponse cres = CheckLevelCondition(customerLevel.Level, customerLevel.Customer);
                if (!cres.CanEnter)
                {
                    foreach (string error in cres.ErrorMessages)
                    {
                        response.ErrorMessages.Add(error);
                    }

                    return(response);
                }

                #endregion

                #region Change Customer Query Count

                #endregion

                #region CreateSupport

                if (customerLevel.Level.CreateSupportOnEnter)
                {
                    Support support = new Support();
                    support.ID             = Guid.NewGuid();
                    support.CreateDate     = PersianDateTime.Now;
                    support.CreateEmployee = customerLevel.CreateEmployee;
                    support.SupportTitle   = "پشتیبانی ";
                    support.SupportComment = "پشتیبانی ایجاد شده خودکار توسط سیستم";
                    support.Customer       = customerLevel.Customer;
                    support.CreateBy       = Support.Creator.BySystem;
                    support.SupportStatus  =
                        _supportStatusRepository.FindAll().Where(x => x.Key == "NoStatus").FirstOrDefault();
                    _supportRepository.Add(support);
                }

                #endregion

                _customerLevelRepository.Add(customerLevel);

                #region Query Customer Count

                //اگر مشتری جدید بود فقط به مرحله جدید یک واحد اضافه کن
                if (request.NewCustomer == true)
                {
                    Infrastructure.Querying.Query newQuery = new Infrastructure.Querying.Query();
                    Criterion crt1 = new Criterion("Level.ID", request.NewLevelID, CriteriaOperator.Equal);
                    newQuery.Add(crt1);
                    Model.Customers.Query OldQuery = _queryRepository.FindBy(newQuery).FirstOrDefault();
                    if (OldQuery != null)
                    {
                        OldQuery.CustomerCount += 1;
                        _queryRepository.Save(OldQuery);
                    }
                }
                // اگر مشتری قبلی بود از مرحله قبل یکی کم و به مرحله جدید یکی اضافه کنذ
                else
                {
                    Infrastructure.Querying.Query oldQuery = new Infrastructure.Querying.Query();
                    Criterion crt1 = new Criterion("Level.ID", customerLevel.Customer.Level.ID, CriteriaOperator.Equal);
                    oldQuery.Add(crt1);
                    Model.Customers.Query OldQuery = _queryRepository.FindBy(oldQuery).FirstOrDefault();
                    OldQuery.CustomerCount -= 1;
                    _queryRepository.Save(OldQuery);

                    Infrastructure.Querying.Query newQuery = new Infrastructure.Querying.Query();
                    Criterion crt2 = new Criterion("Level.ID", request.NewLevelID, CriteriaOperator.Equal);
                    newQuery.Add(crt2);
                    Model.Customers.Query NewQuery = _queryRepository.FindBy(newQuery).FirstOrDefault();
                    NewQuery.CustomerCount += 1;
                    _queryRepository.Save(NewQuery);
                }

                #endregion

                #region Change Customer Level In Customer Table

                Customer customer = _customerRepository.FindBy(request.CustomerID);
                customer.Level          = _levelRepository.FindBy(request.NewLevelID);
                customer.LevelEntryDate = PersianDateTime.Now;
                _customerRepository.Save(customer);


                #endregion

                _uow.Commit();

                #region Sending Email

                string displayName = "ماهان نت";
                string subject;
                //List<string> recipients = new List<string>();
                GeneralResponse sendResponse = new GeneralResponse();


                // if OnEnterSendEmail is true
                if (customerLevel.Level.OnEnterSendEmail)
                {
                    email.ID             = Guid.NewGuid();
                    email.CreateDate     = PersianDateTime.Now;
                    email.CreateEmployee = customerLevel.CreateEmployee;
                    email.Customer       = customerLevel.Customer;
                    subject          = customer.Name;
                    email.Subject    = subject;
                    email.RowVersion = 1;

                    #region Validation

                    if (email.GetBrokenRules().Count() > 0)
                    {
                        foreach (BusinessRule businessRule in email.GetBrokenRules())
                        {
                            response.ErrorMessages.Add(businessRule.Rule);
                        }

                        return(response);
                    }

                    #endregion

                    #region Send Email

                    // Replacing:
                    string emailBody = ReplaceTemplate(customerLevel.Level.EmailText, customerLevel.Customer.ConvertToCustomerView());

                    email.Body = emailBody;

                    string recipient = email.Customer.Email;
                    if (recipient == null || recipient == string.Empty)
                    {
                        response.ErrorMessages.Add("برای مشتری مورد نظر هیچ ایمیلی در سیستم تعریف نشده است.");

                        return(response);
                    }

                    //===============  Threading:
                    EmailData emailData = new EmailData()
                    {
                        displayName = displayName,
                        body        = emailBody,
                        subject     = subject,
                        recipient   = recipient
                    };

                    Thread oThread = new Thread(SendEmailVoid);
                    oThread.Start(emailData);

                    #endregion

                    _emailRepository.Add(email);
                }

                #endregion

                #region Sending Sms

                if (customerLevel.Level.OnEnterSendSMS)
                {
                    Sms sms = new Sms();
                    sms.ID             = Guid.NewGuid();
                    sms.CreateDate     = PersianDateTime.Now;
                    sms.CreateEmployee = customerLevel.CreateEmployee;
                    //sms.Body = customerLevel.Level.SMSText;
                    sms.Customer   = customerLevel.Customer;
                    sms.RowVersion = 1;

                    #region Validation

                    if (sms.GetBrokenRules().Count() > 0)
                    {
                        foreach (BusinessRule businessRule in sms.GetBrokenRules())
                        {
                            response.ErrorMessages.Add(businessRule.Rule);
                        }

                        return(response);
                    }

                    #endregion

                    string smsBody = ReplaceTemplate(customerLevel.Level.SMSText, customerLevel.Customer.ConvertToCustomerView());

                    // Threading
                    SmsData smsData = new SmsData()
                    {
                        body = smsBody, phoneNumber = customerLevel.Customer.Mobile1
                    };
                    Thread oThread = new Thread(SendSmsVoid);
                    oThread.Start(smsData);

                    _smsRepository.Add(sms);
                }

                #endregion
            }
            catch (Exception ex)
            {
                response.ErrorMessages.Add(ex.Message);
            }

            return(response);
        }