private List <ScPriceTypeEntity> GetRecords(UserConnection userConnection, ScPriceTypeEntity us) { var records = new List <ScPriceTypeEntity>(); // SQL-запрос. var select = (Select) new Select(userConnection) .Column("Id") .Column("AccountId") // контрагент .Column("StartDate") // дата начала .Column("EndDate") // дата окончания .Column("vsDeliveryLocationId") // место досатвки .Column("vsLogisticTypeId") // тип логистики .From("ScPriceType") // таблица "Коммерческие условия" .Where() .OpenBlock("AccountId").IsEqual(Column.Parameter(us.AccountId)) // контрагент .And("Id").IsNotEqual(Column.Parameter(us.Id)) // исключить саму запись .CloseBlock(); // место доставки var deliveryCondition = new QueryCondition() { ConditionType = us.DeliveryLocationId == Guid.Empty ? QueryConditionType.IsNull : QueryConditionType.Equal, LeftExpression = new QueryColumnExpression(Column.SourceColumn("vsDeliveryLocationId")), RightExpressions = { Column.Parameter(us.DeliveryLocationId) } }; select.AddCondition(deliveryCondition, LogicalOperation.And); // тип логистики var logisticTypeCondition = new QueryCondition() { ConditionType = us.LogisticTypeId == Guid.Empty ? QueryConditionType.IsNull : QueryConditionType.Equal, LeftExpression = new QueryColumnExpression(Column.SourceColumn("vsLogisticTypeId")), RightExpressions = { Column.Parameter(us.LogisticTypeId) } }; select.AddCondition(logisticTypeCondition, LogicalOperation.And); // Использование экземпляра DBExecutor в основном потоке. using (DBExecutor dbExecutor = userConnection.EnsureDBConnection()) { using (IDataReader dataReader = select.ExecuteReader(dbExecutor)) { while (dataReader.Read()) { records.Add(new ScPriceTypeEntity( dataReader.GetColumnValue <Guid>("Id"), dataReader.GetColumnValue <Guid>("AccountId"), dataReader.GetColumnValue <DateTime>("StartDate"), dataReader.GetColumnValue <DateTime>("EndDate"), dataReader.GetColumnValue <Guid>("vsDeliveryLocationId"), dataReader.GetColumnValue <Guid>("vsLogisticTypeId"))); } } } return(records); }
private bool CheckRulesDbExecutor(UserConnection userConnection, Entity entity) { // Значения полей. var us = new ScPriceTypeEntity(entity); // Список записей таблицы "Коммерческие условия". var records = GetRecords(userConnection, us); // Найдены записи. if (records.Count > 0) { var caption = "Контроль коммерческих условий." + Environment.NewLine; // 1. Совпадение дат. string msg; if (records.Any(rec => rec.StartDate == us.StartDate && rec.EndDate == us.EndDate)) { msg = "Совпадение дат (код 1)!"; throw new Exception(caption + msg); } // 2. Нет дат завершения. if (records.Any(rec => us.EndDate.IsNullOrEmpty() && rec.EndDate.IsNullOrEmpty())) { msg = "Нет дат завершения (код 2)!"; throw new Exception(caption + msg); } // 3. Дата начала пользователя меньше даты завершения записей и нет даты завершения пользователя. if (records.Any(rec => us.StartDate <= rec.EndDate && us.EndDate.IsNullOrEmpty())) { msg = "Пересечение дат (код 3)!"; throw new Exception(caption + msg); } // 4. Дата завершения пользователя меньше даты начала записей и нет даты завершения записей. if (records.Any(rec => us.EndDate >= rec.StartDate && rec.EndDate.IsNullOrEmpty())) { msg = "Пересечение дат (код 4)!"; throw new Exception(caption + msg); } // 5.1. Вхождение дат. if (records.Any(rec => (!rec.StartDate.IsNullOrEmpty() && !rec.EndDate.IsNullOrEmpty() && !us.StartDate.IsNullOrEmpty() && !us.EndDate.IsNullOrEmpty()) && us.StartDate >= rec.StartDate && us.StartDate <= rec.EndDate && us.EndDate >= rec.StartDate && us.EndDate <= rec.EndDate)) { msg = "Пересечение дат (код 5.1)!"; throw new Exception(caption + msg); } // 5.2. Вхождение дат. if (records.Any(rec => (!rec.StartDate.IsNullOrEmpty() && !rec.EndDate.IsNullOrEmpty() && !us.StartDate.IsNullOrEmpty() && !us.EndDate.IsNullOrEmpty()) && us.StartDate <= rec.StartDate && us.EndDate >= rec.StartDate && us.EndDate <= rec.EndDate)) { msg = "Пересечение дат (код 5.2)!"; throw new Exception(caption + msg); } // 5.3. Вхождение дат. if (records.Any(rec => (!rec.StartDate.IsNullOrEmpty() && !rec.EndDate.IsNullOrEmpty() && !us.StartDate.IsNullOrEmpty() && !us.EndDate.IsNullOrEmpty()) && us.StartDate >= rec.StartDate && us.StartDate <= rec.EndDate && us.EndDate >= rec.EndDate)) { msg = "Пересечение дат (код 5.3)!"; throw new Exception(caption + msg); } // 5.4. Вхождение дат. if (records.Any(rec => (!rec.StartDate.IsNullOrEmpty() && !rec.EndDate.IsNullOrEmpty() && !us.StartDate.IsNullOrEmpty() && !us.EndDate.IsNullOrEmpty()) && us.StartDate >= rec.StartDate && us.StartDate <= rec.EndDate && us.EndDate >= rec.StartDate && us.EndDate <= rec.EndDate)) { msg = "Пересечение дат (код 5.4)!"; throw new Exception(caption + msg); } } return(true); }