private static void FdpChangesetMarkSaved(FdpChangeset changesetToMark, IDbTransaction tran)
        {
            var para = new DynamicParameters();
            para.Add("@FdpChangesetId", changesetToMark.FdpChangesetId, DbType.Int32);

            tran.Connection.Execute("dbo.Fdp_Changeset_MarkSaved", para, tran, commandType: CommandType.StoredProcedure);
        }
        public async Task<FdpChangeset> SaveChangeset(TakeRateFilter filter, FdpChangeset changesetToSave)
        {
            var savedChangeset = await Task.FromResult(_takeRateDataStore.FdpChangesetSave(filter, changesetToSave));
            var savedDataChanges = Enumerable.Empty<DataChange>();

            foreach (var dataChange in changesetToSave.Changes)
            {
                dataChange.FdpChangesetId = savedChangeset.FdpChangesetId;
            }
            savedDataChanges =
                await
                    Task.FromResult(
                        _takeRateDataStore.FdpChangesetDataItemsSave(filter, changesetToSave.Changes));
            
            savedChangeset.Changes = savedDataChanges.ToList();

            return savedChangeset;
        }
        private void FdpChangesetPersist(FdpChangeset changesetToPersist, IDbTransaction tran)
        {
            var para = DynamicParameters.FromCDSId(CurrentCDSID);
            para.Add("@FdpChangesetId", changesetToPersist.FdpChangesetId, DbType.Int32);
            para.Add("@Comment", changesetToPersist.Comment, DbType.String);

            tran.Connection.Execute("dbo.Fdp_Changeset_PersistChanges", para, tran, commandType: CommandType.StoredProcedure);
        }
        public FdpChangeset FdpChangesetUndoAll(TakeRateFilter takeRateFilter, FdpChangeset changesetToUndo)
        {
            FdpChangeset retVal;

            using (var conn = DbHelper.GetDBConnection())
            {
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        var para = new DynamicParameters();
                        para.Add("@FdpChangesetId", changesetToUndo.FdpChangesetId, DbType.Int32);

                        var results = conn.QueryMultiple("dbo.Fdp_Changeset_UndoAll", para, tran, commandType: CommandType.StoredProcedure);

                        var revertedItems = results.Read<DataChange>();
                        var modelMix = results.Read<AllModelMix>().FirstOrDefault();

                        tran.Commit();

                        retVal = FdpChangesetGet(changesetToUndo);
                        var dataChanges = revertedItems as IList<DataChange> ?? revertedItems.ToList();
                        if (dataChanges.Any())
                        {
                            retVal.Reverted = dataChanges.ToList();
                        }

                        retVal.ModelMix = modelMix;
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        throw;
                    }
                }
            }
            return retVal;
        }
        public FdpChangeset FdpChangesetPersist(TakeRateFilter filter, FdpChangeset changesetToPersist)
        {
            FdpChangeset retVal;

            using (var conn = DbHelper.GetDBConnection())
            {
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        FdpChangesetPersist(changesetToPersist, tran);
                        FdpChangesetMarkSaved(changesetToPersist, tran);

                        tran.Commit();

                        retVal = FdpChangesetGet(changesetToPersist);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        throw;
                    }
                }
            }
            return retVal;
        }
        public FdpChangeset FdpChangesetSave(TakeRateFilter filter, FdpChangeset changeSetToSave)
        {
            FdpChangeset retVal = new EmptyFdpChangeset();

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = DynamicParameters.FromCDSId(CurrentCDSID);
                    para.Add("@FdpVolumeHeaderId", filter.TakeRateId, DbType.Int32);
                    para.Add("@MarketId", filter.MarketId, DbType.Int32);
                   
                    var results = conn.Query<FdpChangeset>("dbo.Fdp_Changeset_Save", para, commandType: CommandType.StoredProcedure);
                    var fdpChangesets = results as IList<FdpChangeset> ?? results.ToList();
                    if (fdpChangesets.Any())
                    {
                        retVal = fdpChangesets.First();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public FdpChangeset FdpChangesetGet(FdpChangeset changesetToGet)
        {
            FdpChangeset retVal = new EmptyFdpChangeset();

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@FdpChangesetId", changesetToGet.FdpChangesetId, DbType.Int32);

                    // First resultset is the header information

                    var results = conn.QueryMultiple("dbo.Fdp_Changeset_Get", para, commandType: CommandType.StoredProcedure);
                    var firstResultSet = results.Read<FdpChangeset>();
                    var fdpChangesets = firstResultSet as IList<FdpChangeset> ?? firstResultSet.ToList();
                    if (firstResultSet == null || !fdpChangesets.Any())
                    {
                        return retVal;
                    }
                    retVal = fdpChangesets.First();

                    // Second resultset contains the data changes themselves

                    var secondResultSet = results.Read<DataChange>();
                    if (secondResultSet != null)
                    {
                        retVal.Changes = secondResultSet.ToList();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }