ЭТО МОЙ ТЕКУЩИЙ НАПИСАННЫЙ КОД
using System.Collections.Generic;
using System.Linq;
using iRIS.InsuranceCore.Entities;
using iRIS.KaskoLink2.Code.Constants;
using iRIS.KaskoLink2.Code.Interfaces.BankSettings;

namespace iRIS.KaskoLink2.Code.Implementations.BankSettings
{
    public class BankSettingsConditionChecker : IBankSettingsConditionChecker
    {
        private readonly IBankSettingsQueryInvoker _bankSettingsQueryInvoker;
        private readonly Dictionary<int, string> _optionsToBankSettings = new Dictionary<int, string>
        {
            { StructIdsCodes.Avarcom2Par, nameof(Entities.BankSettings.Avarkom1) },
            { StructIdsCodes.AvarcomAccident, nameof(Entities.BankSettings.Avarkom2) },
            { StructIdsCodes.AvarcomAny, nameof(Entities.BankSettings.Avarkom3) },
            { StructIdsCodes.AvarcomNote, nameof(Entities.BankSettings.AvarkomSertificates) },
            { StructIdsCodes.Glass1T, nameof(Entities.BankSettings.Mirrors1) },
            { StructIdsCodes.GlassMany, nameof(Entities.BankSettings.Mirrors2) },
            { StructIdsCodes.Percent3, nameof(Entities.BankSettings.Percent3) },
            { StructIdsCodes.Percent5, nameof(Entities.BankSettings.Percent5) },
            { StructIdsCodes.Evacuation, nameof(Entities.BankSettings.Evacuation) },
            { StructIdsCodes.TechHelp, nameof(Entities.BankSettings.EmergencyRepair) },
            { StructIdsCodes.Taxi, nameof(Entities.BankSettings.Taxi) },
            { StructIdsCodes.Rent, nameof(Entities.BankSettings.CarRental) },
            { StructIdsCodes.Damage, nameof(Entities.BankSettings.DmgEnabled) },
            { StructIdsCodes.CarjackingCode, nameof(Entities.BankSettings.HJEnabled) },
            { StructIdsCodes.TotalLoss, nameof(Entities.BankSettings.TotalEnabled) },
            { StructIdsCodes.Gap, nameof(Entities.BankSettings.GAP) },
            { StructIdsCodes.Techcenter, nameof(Entities.BankSettings.RepairStoaWithoutDealer) },
            { StructIdsCodes.DealerTechcenter, nameof(Entities.BankSettings.RepairStoa) },
            { StructIdsCodes.InsurantTechcenter, nameof(Entities.BankSettings.RepairStoaInsurant) }
        };

        private readonly Dictionary<int, string> _franchisesToBankSettings = new Dictionary<int, string>
        {
            { StructIdsCodes.FranchiseCauser, nameof(Entities.BankSettings.FranType) },
            { StructIdsCodes.FranchiseSecondIncEvent, nameof(Entities.BankSettings.FranType) }
        };

        public BankSettingsConditionChecker(IBankSettingsQueryInvoker bankSettingsQueryInvoker)
        {
            _bankSettingsQueryInvoker = bankSettingsQueryInvoker;
        }

        public bool CheckCondition(Deal deal, int structId, bool expectedValue)
        {
            var bank = deal.SuggestedBanks.FirstOrDefault();

            if (bank == null)
            {
                return false;
            }

            var bankSettings = _bankSettingsQueryInvoker.GetBankSettingsAsync(bank.BIK).GetAwaiter().GetResult();
            var setting = bankSettings.FirstOrDefault(x => x.DivisionID == deal.BranchCode) ?? bankSettings.FirstOrDefault(x => x.DivisionID == null);

            if (setting == null)
            {
                return false;
            }

            if (_optionsToBankSettings.TryGetValue(structId, out var optionPropertyName))
            {
                var optionPropertyValue = setting.GetType().GetProperty(optionPropertyName)?.GetValue(setting);

                if (optionPropertyValue is bool boolValue)
                {
                    return boolValue == expectedValue;
                }
            }

            if (_franchisesToBankSettings.TryGetValue(structId, out var franchisePropertyName))
            {
                var franchisePropertyValue = setting.GetType().GetProperty(franchisePropertyName)?.GetValue(setting);

                if (franchisePropertyValue is int intValue)
                {
                    return EvaluateFranchiseCondition(structId, intValue, expectedValue);
                }
            }

            return false;
        }

        private bool EvaluateFranchiseCondition(int structId, int franchiseType, bool expectedValue)
        {
            switch (franchiseType)
            {
                case 0:
                    return !expectedValue; // Франшизы недоступны
                case 2:
                    return structId == StructIdsCodes.FranchiseSecondIncEvent; // Только 2INSEVENT
                case 3:
                    return structId == StructIdsCodes.FranchiseCauser; // Только CAUSER
                default:
                    return expectedValue; // Доступны все франшизы (для случая NULL или 1)
            }
        }
    }
}

#DiKaskoConstructorExternalFunctionsInvoker
        public bool IsConditionAllowedByBank(Deal deal, int structId)
        {
            return CheckConditionByBank(deal, structId, false);
        }

        public bool IsConditionRequiredByBank(Deal deal, int structId)
        {
            return CheckConditionByBank(deal, structId, true);
        }

        private bool CheckConditionByBank(Deal deal, int structId, bool expectedValue)
        {
            var conditionChecker = _serviceProvider.GetRequiredService<IBankSettingsConditionChecker>();
            var checkResult = conditionChecker.CheckCondition(deal, structId, expectedValue);

            return checkResult;
        }

CREATE PROCEDURE tmp_AddRule (@Struct_ID int)
AS
BEGIN
    DECLARE @Product_ID int;
    DECLARE @Rule_Id int;
    DECLARE @Rule_Condition varchar(2000);
    DECLARE @isAvailable bit = 0;

    -- Получаем ID продукта
    SELECT @Product_ID = TOP 1 Id 
    FROM Product WITH(NOLOCK) 
    WHERE ProductDictCode = 'KASKO' AND State = 2 AND IsDeleted = 0;

    -- Формируем условие правила в зависимости от переданного параметра @Struct_ID
    SET @Rule_Condition = CONCAT(
        'DiKaskoConstructorExternalFunctionsInvoker.IsConditionAllowedByBank(Deal, ', 
        @Struct_ID, 
        ')'
    );

    -- Проверяем и добавляем правило, если его еще не существует
    IF NOT EXISTS 
    (
        SELECT 1 
        FROM [Rule] WITH(NOLOCK) 
        WHERE Condition = @Rule_Condition AND Product_Id = @Product_ID
    ) 
    BEGIN 
        INSERT INTO [Rule] 
        (
            Condition, EntityId, AreaId, CreateDate, State, DictionaryDate, 
            IsDeleted, Product_Id, InitializationType
        )
        VALUES 
        (
            @Rule_Condition, NEXT VALUE FOR SQ_Rule, 1, GETDATE(), 2, 
            GETDATE(), 0, @Product_ID, 'FirstInitialization'
        );

        SELECT @Rule_Id = SCOPE_IDENTITY();
    END
    ELSE
    BEGIN
        SELECT @Rule_Id = Id 
        FROM [Rule] WITH(NOLOCK) 
        WHERE Condition = @Rule_Condition AND Product_Id = @Product_ID;
    END

    -- Вставляем в ConditionSetting
    IF NOT EXISTS 
    (
        SELECT 1 
        FROM ConditionSetting WITH(NOLOCK) 
        WHERE structId = @Struct_ID AND Rule_Id = @Rule_Id
    )
    BEGIN
        INSERT INTO ConditionSetting 
        (
            StructId, IsDefault, IsRequired, IsAvailable, [Check], Uncheck,
            EntityId, AreaId, CreateDate, State, DictionaryDate, IsDeleted, Rule_Id
        )
        VALUES 
        (
            @Struct_ID, NULL, NULL, @isAvailable, NULL, NULL,
            NEXT VALUE FOR SQ_ConditionSetting, 1, GETDATE(), 2, GETDATE(), 0, @Rule_Id
        );
    END

    -- Вставляем в StructIdForRule
    IF NOT EXISTS 
    (
        SELECT 1 
        FROM StructIdForRule WITH(NOLOCK) 
        WHERE structId = @Struct_ID AND Rule_Id = @Rule_Id
    )
    BEGIN
        INSERT INTO StructIdForRule 
        (
            StructId, EntityId, AreaId, CreateDate, State, DictionaryDate, IsDeleted, Rule_Id
        )
        VALUES 
        (
            @Struct_ID, NEXT VALUE FOR SQ_StructIdForRule, 1, GETDATE(), 2, GETDATE(), 0, @Rule_Id
        );
    END
END
GO

-- Causer
EXEC tmp_AddRule 5027;
-- SecondEvent
EXEC tmp_AddRule 5028;

DROP PROCEDURE tmp_AddRule;
GO

Помоги мне реализовать функционал по ограничению суммы франшиз для определенных банков, требования к задаче такие:
Доступные суммы франшиз в оффере выводятся  в массиве franchises.availableValues[]. В таблице BankSettings в поле FranSum хранится максимальная сумма франшизы для конкретного банка.

Если значение этого поля = NULL, ограничений по сумме франшизы нет. Если же указано конкретное значение - в массиве сумм франшиз должны быть только те, которые не больше этого значения.

Есть похожий скрипт, который может помочь:
-- Сужаем значения сумм у франшизы виновника
If not exists 
(select 1 from SetSumSetting with(nolock) where StructId = @Struct_ID and Rule_Id = @Rule_Id)
begin
	Insert into SetSumSetting (StructId
      ,RangeStart ,RangeEnd
      ,IsInclude
      ,DefaultSum
      ,EntityId ,AreaId
      ,CreateDate,State,DictionaryDate,IsDeleted
      ,Rule_Id)
	Values (@Struct_ID,
			10000, 30000,
			1,
			10000,
			next value for SQ_SetSumSetting, 1,
			GETDATE(), 2, GETDATE(), 0,
			@Rule_Id)
end