Пример #1
0
        public UpdateOperationResponse UpdateOperation(UpdateOperationRequest request)
        {
            bool isKpiExisted =
                DataContext.KeyOperationConfigs.Include(x => x.Kpi).FirstOrDefault(x => x.Kpi.Id == request.KpiId) != null;

            if (request.Id == 0 && !isKpiExisted)
            {
                var operationConfig = new KeyOperationConfig();
                operationConfig.IsActive = request.IsActive.HasValue && request.IsActive.Value;
                operationConfig.Order    = request.Order.HasValue ? request.Order.Value : 0;
                operationConfig.Kpi      = DataContext.Kpis.Single(x => x.Id == request.KpiId);
                if (request.KeyOperationGroupId > 0)
                {
                    operationConfig.KeyOperationGroup =
                        DataContext.KeyOperationGroups.Single(x => x.Id == request.KeyOperationGroupId);
                }

                DataContext.KeyOperationConfigs.Add(operationConfig);
                DataContext.SaveChanges();
                return(new UpdateOperationResponse
                {
                    IsSuccess = true,
                    Message = "Operation Config has been saved succesfully",
                    Id = operationConfig.Id
                });
            }
            else
            {
                var operationConfig = DataContext.KeyOperationConfigs
                                      .Include(x => x.KeyOperationGroup)
                                      .Single(x => x.Id == request.Id);
                if (request.IsActive.HasValue)
                {
                    operationConfig.IsActive = request.IsActive.Value;
                }

                if (request.Order.HasValue)
                {
                    operationConfig.Order = request.Order.Value;
                }

                if (request.KeyOperationGroupId != 0)
                {
                    var group = new KeyOperationGroup {
                        Id = request.KeyOperationGroupId
                    };
                    DataContext.KeyOperationGroups.Attach(group);
                    operationConfig.KeyOperationGroup = group;
                }

                DataContext.SaveChanges();
                return(new UpdateOperationResponse
                {
                    IsSuccess = true,
                    Message = "Operation Config has been saved succesfully",
                    Id = operationConfig.Id
                });
            }
        }
Пример #2
0
        public GetOperationDataConfigurationResponse GetOperationDataConfigurationForAllGroup(GetOperationDataConfigurationRequest request)
        {
            var response = new GetOperationDataConfigurationResponse();

            response.RoleGroupId = request.RoleGroupId;
            response.ScenarioId  = request.ScenarioId;

            var periodeType = request.PeriodeType;

            List <KeyOperationConfig> keyOperationConfigs = DataContext.KeyOperationConfigs
                                                            .Include(x => x.Kpi)
                                                            .Include(x => x.Kpi.Measurement)
                                                            .Include(x => x.KeyOperationGroup)
                                                            .Where(x => x.IsActive && x.KeyOperationGroup != null).ToList();

            switch (periodeType)
            {
            case PeriodeType.Yearly:
                var operationDataYearly =
                    DataContext.KeyOperationDatas
                    .Include(x => x.Kpi)
                    .Include(x => x.Scenario)
                    .Include(x => x.KeyOperationConfig)
                    .Where(x => x.PeriodeType == PeriodeType.Yearly && x.Scenario.Id == request.ScenarioId).ToList();

                foreach (var keyOperationConfig in keyOperationConfigs)
                {
                    var kpiDto = keyOperationConfig.Kpi.MapTo <GetOperationDataConfigurationResponse.Kpi>();
                    kpiDto.GroupName = keyOperationConfig.KeyOperationGroup.Name;
                    foreach (var number in YearlyNumbersForOperationData)
                    {
                        var operation = operationDataYearly.FirstOrDefault(x => x.Kpi != null && x.Kpi.Id == keyOperationConfig.Kpi.Id && x.Periode.Year == number);

                        if (operation != null)
                        {
                            var operationtDataDto =
                                operation.MapTo <GetOperationDataConfigurationResponse.OperationData>();
                            operationtDataDto.ScenarioId           = request.ScenarioId;
                            operationtDataDto.KeyOperationConfigId = keyOperationConfig.Id;
                            kpiDto.OperationData.Add(operationtDataDto);
                        }
                        else
                        {
                            var operationtDataDto = new GetOperationDataConfigurationResponse.OperationData();
                            operationtDataDto.Periode = new DateTime(number, 1, 1);
                            operationtDataDto.KeyOperationConfigId = keyOperationConfig.Id;
                            kpiDto.OperationData.Add(operationtDataDto);
                        }
                    }

                    response.Kpis.Add(kpiDto);
                }
                break;

            case PeriodeType.Monthly:
                var operationDataMonthly = DataContext.KeyOperationDatas
                                           .Include(x => x.Kpi)
                                           .Include(x => x.Scenario)
                                           .Include(x => x.KeyOperationConfig)
                                           .Where(x => x.PeriodeType == PeriodeType.Monthly && x.Periode.Year == request.Year && x.Scenario.Id == request.ScenarioId).ToList();

                foreach (var keyOperationConfig in keyOperationConfigs)
                {
                    var kpiDto = keyOperationConfig.Kpi.MapTo <GetOperationDataConfigurationResponse.Kpi>();
                    kpiDto.GroupName = keyOperationConfig.KeyOperationGroup.Name;
                    KeyOperationConfig config = keyOperationConfig;
                    var operationDatas        = operationDataMonthly.Where(x => x.Kpi.Id == config.Kpi.Id).ToList();
                    for (int i = 1; i <= 12; i++)
                    {
                        var operationData = operationDatas.FirstOrDefault(x => x.Periode.Month == i);
                        if (operationData != null)
                        {
                            var operationDataDto =
                                operationData.MapTo <GetOperationDataConfigurationResponse.OperationData>();
                            operationDataDto.ScenarioId           = request.ScenarioId;
                            operationDataDto.KeyOperationConfigId = keyOperationConfig.Id;
                            kpiDto.OperationData.Add(operationDataDto);
                        }
                        else
                        {
                            var operationDataDto = new GetOperationDataConfigurationResponse.OperationData();
                            operationDataDto.Periode              = new DateTime(request.Year, i, 1);
                            operationDataDto.ScenarioId           = request.ScenarioId;
                            operationDataDto.KeyOperationConfigId = keyOperationConfig.Id;
                            kpiDto.OperationData.Add(operationDataDto);
                        }
                    }
                    response.Kpis.Add(kpiDto);
                }
                break;
            }

            response.IsSuccess = true;


            return(response);
        }