public DataChange(DataChange parentChange)
 {
     MarketId = parentChange.MarketId;
     ModelIdentifier = parentChange.ModelIdentifier;
     FeatureIdentifier = parentChange.FeatureIdentifier;
     Comment = parentChange.Comment;
 }
 public async Task<DataChange> RecalculateChangesetDataChange(DataChange changeToRecalculate) 
 {
     return await Task.FromResult(_takeRateDataStore.FdpChangesetDataItemRecalculate(changeToRecalculate));
 }
 public async Task<IEnumerable<TakeRateDataItem>> CalculateTakeRateAndVolumeByMarket(TakeRateFilter filter, DataChange forChange)
 {
     IEnumerable<TakeRateDataItem> retVal = Enumerable.Empty<TakeRateDataItem>();
     if (filter.Mode == Model.Enumerations.TakeRateResultMode.PercentageTakeRate)
     {
         retVal = await Task.FromResult(_takeRateDataStore.FdpTakeRateByMarketGetMany(filter, forChange.PercentageTakeRate));
     }
     else
     {
         retVal = await Task.FromResult(_takeRateDataStore.FdpVolumeByMarketGetMany(filter, forChange.Volume));
     }
     return retVal;
 }
        public DataChange FdpChangesetDataItemRecalculate(DataChange changeToRecalculate)
        {
            DataChange retVal = new EmptyDataChange();

            using (var conn = DbHelper.GetDBConnection())
            {
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        var para = new DynamicParameters();
                        para.Add("@FdpChangesetDataItemId", changeToRecalculate.FdpChangesetDataItemId.GetValueOrDefault(), DbType.Int32);
                        var results = conn.Query<DataChange>("dbo.Fdp_ChangesetDataItem_Recalculate", para, tran, commandType: CommandType.StoredProcedure);
                        var dataChanges = results as IList<DataChange> ?? results.ToList();
                        if (dataChanges.Any())
                        {
                            retVal = dataChanges.First();
                        }

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        throw;
                    }
                }
            }
            return retVal;
        }
        private DataChange FdpChangesetDataItemSave(TakeRateFilter filter, DataChange dataItemToSave, IDbTransaction tran)
        {
            DataChange retVal = new EmptyDataChange();

            
                    try
                    {
                        var para = DynamicParameters.FromCDSId(CurrentCDSID);
                        para.Add("@FdpChangesetId", dataItemToSave.FdpChangesetId.GetValueOrDefault(), DbType.Int32);
                        para.Add("@ParentFdpChangesetDataItemId", dataItemToSave.ParentFdpChangesetDataItemId, DbType.Int32);
                        para.Add("@MarketId", dataItemToSave.MarketId, DbType.Int32);
                        para.Add(!dataItemToSave.IsFdpModel ? "@ModelId" : "@FdpModelId", dataItemToSave.GetModelId(),
                            DbType.Int32);

                        if (dataItemToSave.IsFdpFeature)
                        {
                            para.Add("@FdpFeatureId", dataItemToSave.GetFeatureId(), DbType.Int32);
                        }
                        else if (dataItemToSave.IsFeaturePack)
                        {
                            para.Add("@FeaturePackId", dataItemToSave.GetFeatureId(), DbType.Int32);
                        }
                        else if (dataItemToSave.IsPowertrainChange)
                        {
                            para.Add("@DerivativeCode", dataItemToSave.DerivativeCode, DbType.String);
                        }
                        else
                        {
                            para.Add("@FeatureId", dataItemToSave.GetFeatureId(), DbType.Int32);
                        }
                        

                        if (dataItemToSave.Volume.HasValue)
                        {
                            para.Add("@TotalVolume", dataItemToSave.Volume, DbType.Int32);
                        }
                        if (dataItemToSave.PercentageTakeRateAsFraction.HasValue)
                        {
                            para.Add("@PercentageTakeRate", dataItemToSave.PercentageTakeRateAsFraction, DbType.Decimal);
                        }
                        para.Add("@OriginalPercentageTakeRate", dataItemToSave.OriginalPercentageTakeRate, DbType.Decimal);
                        para.Add("@OriginalVolume", dataItemToSave.OriginalVolume, DbType.Int32);
                        para.Add("@FdpVolumeDataItemId", dataItemToSave.FdpVolumeDataItemId, DbType.Int32);
                        para.Add("@FdpTakeRateSummaryId", dataItemToSave.FdpTakeRateSummaryId, DbType.Int32);
                        para.Add("@FdpTakeRateFeatureMixId", dataItemToSave.FdpTakeRateFeatureMixId, DbType.Int32);
                        para.Add("@FdpPowertrainDataItemId", dataItemToSave.FdpPowertrainDataItemId, DbType.Int32);

                        para.Add("@IsVolumeUpdate", dataItemToSave.Mode == TakeRateResultMode.Raw, DbType.Boolean);
                        para.Add("@IsPercentageUpdate", dataItemToSave.Mode == TakeRateResultMode.PercentageTakeRate, DbType.Boolean);

                        var results = tran.Connection.Query<DataChange>("dbo.Fdp_ChangesetDataItem_Save", para, tran, commandType: CommandType.StoredProcedure);
                        var dataChanges = results as IList<DataChange> ?? results.ToList();
                        if (dataChanges.Any())
                        {
                            retVal = dataChanges.First();
                        }
                        
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        throw;
                    }
                
            return retVal;
        }