private BaseResponse _UpdateEconomic(List<OperationDataConfigurationViewModel.Item> datas) { var response = new BaseResponse(); if (datas != null) { var batch = new BatchUpdateOperationDataRequest(); foreach (var data in datas) { //var prepare = new UpdateOperationDataRequest() { Id = data.Id, KpiId = data.KpiId, Periode = data.Periode, Value = data.Value, PeriodeType = data.PeriodeType, Remark = data.Remark, KeyOperationConfigId = data.OperationId, ScenarioId = data.ScenarioId };// data.MapTo<UpdateKpiAchievementItemRequest>(); //batch.BatchUpdateOperationDataItemRequest.Add(prepare); } response = _operationDataService.BatchUpdateOperationDatas(batch); } return response; }
public BaseResponse BatchUpdateOperationDatas(BatchUpdateOperationDataRequest request) { //var response = new BaseResponse(); //try //{ // int i = 0; // foreach (var item in request.BatchUpdateOperationDataItemRequest) // { // var operationData = item.MapTo<KeyOperationData>(); // operationData.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); // operationData.Scenario = DataContext.Scenarios.FirstOrDefault(x => x.Id == item.ScenarioId); // operationData.KeyOperationConfig = DataContext.KeyOperationConfigs.FirstOrDefault(x => x.Id == item.KeyOperationConfigId); // var exist = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark && x.Scenario.Id == item.ScenarioId && x.KeyOperationConfig.Id == item.KeyOperationConfigId); // //skip no change value // if (exist != null) // { // continue; // } // var attachedEntity = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Scenario.Id == item.ScenarioId && x.KeyOperationConfig.Id == item.KeyOperationConfigId); // if (attachedEntity != null) // { // operationData.Id = attachedEntity.Id; // } // //jika tidak ada perubahan di skip aja // //if (existing.Value.Equals(item.Value) && existing.Periode.Equals(item.Periode) && existing.Kpi.Id.Equals(item.KpiId) && existing.PeriodeType.Equals(item.PeriodeType)) { // // break; // //} // if (operationData.Id != 0) // { // //var attachedEntity = DataContext.KpiAchievements.Find(item.Id); // if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached) // { // DataContext.Entry(attachedEntity).State = EntityState.Detached; // } // DataContext.KeyOperationDatas.Attach(operationData); // DataContext.Entry(operationData).State = EntityState.Modified; // } // else // { // operationData.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); // DataContext.KeyOperationDatas.Add(operationData); // } // i++; // } // DataContext.SaveChanges(); // response.IsSuccess = true; // if (i > 0) // { // response.Message = string.Format("{0} Operation Data items has been updated successfully", i.ToString()); // } // else // { // response.Message = "File Successfully Parsed, but no data changed!"; // } //} //catch (InvalidOperationException invalidOperationException) //{ // response.Message = invalidOperationException.Message; //} //catch (ArgumentNullException argumentNullException) //{ // response.Message = argumentNullException.Message; //} //return response; var response = new BaseResponse(); try { int deletedCounter = 0; int updatedCounter = 0; int addedCounter = 0; int skippedCounter = 0; foreach (var item in request.BatchUpdateOperationDataItemRequest) { if (!string.IsNullOrEmpty(item.Value)) { var existedOperationDatum = DataContext.KeyOperationDatas .Include(x => x.KeyOperationConfig) .Include(x => x.Scenario) .FirstOrDefault(x =>x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Scenario.Id == item.ScenarioId); if (existedOperationDatum != null) { if (item.Value.Equals("-") || item.Value.ToLowerInvariant().Equals("null")) { DataContext.KeyOperationDatas.Remove(existedOperationDatum); deletedCounter++; } else { string oldValue = !existedOperationDatum.Value.HasValue ? string.Empty : existedOperationDatum.Value.Value.ToString(CultureInfo.InvariantCulture); string newValue = !item.RealValue.HasValue ? string.Empty : item.RealValue.Value.ToString(CultureInfo.InvariantCulture); if (oldValue.Equals(newValue)) { skippedCounter++; } else { existedOperationDatum.Value = item.RealValue; DataContext.Entry(existedOperationDatum).State = EntityState.Modified; updatedCounter++; } } } else { var operationDatum = item.MapTo<KeyOperationData>(); if (operationDatum.Value.HasValue) { operationDatum.Kpi = DataContext.Kpis.Single(x => x.Id == item.KpiId); operationDatum.KeyOperationConfig = DataContext.KeyOperationConfigs.Single(x => x.Id == item.KeyOperationConfigId); operationDatum.Scenario = DataContext.Scenarios.Single(x => x.Id == item.ScenarioId); DataContext.KeyOperationDatas.Add(operationDatum); addedCounter++; } } } } DataContext.SaveChanges(); response.IsSuccess = true; response.Message = string.Format("{0} data has been added, {1} data has been updated, {2} data has been removed, {3} data didn't change", addedCounter.ToString() , updatedCounter.ToString(), deletedCounter.ToString(), skippedCounter.ToString()); } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; } return response; }
private BaseResponse UpdateOperationData(IEnumerable<ConfigurationViewModel.Item> datas) { var response = new BaseResponse { IsSuccess = false, Message = "Data Not Valid" }; if (datas != null) { var batch = new BatchUpdateOperationDataRequest(); foreach (var data in datas) { var prepare = new UpdateOperationDataRequest() { Id = data.Id, KpiId = data.KpiId, Periode = data.Periode, Value = data.Value, PeriodeType = data.PeriodeType, Remark = data.Remark, KeyOperationConfigId = data.OperationId, ScenarioId = data.ScenarioId };// data.MapTo<UpdateKpiAchievementItemRequest>(); batch.BatchUpdateOperationDataItemRequest.Add(prepare); } response = _operationDataService.BatchUpdateOperationDatas(batch); } return response; }
public BaseResponse BatchUpdateOperationDatas(BatchUpdateOperationDataRequest request) { var response = new BaseResponse(); try { int i = 0; foreach (var item in request.BatchUpdateOperationDataItemRequest) { var operationData = item.MapTo<KeyOperationData>(); operationData.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); operationData.Scenario = DataContext.Scenarios.FirstOrDefault(x => x.Id == item.ScenarioId); operationData.KeyOperationConfig = DataContext.KeyOperationConfigs.FirstOrDefault(x => x.Id == item.KeyOperationConfigId); var exist = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark && x.Scenario.Id == item.ScenarioId && x.KeyOperationConfig.Id == item.KeyOperationConfigId); //skip no change value if (exist != null) { continue; } var attachedEntity = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Scenario.Id == item.ScenarioId && x.KeyOperationConfig.Id == item.KeyOperationConfigId); if (attachedEntity != null) { operationData.Id = attachedEntity.Id; } //jika tidak ada perubahan di skip aja //if (existing.Value.Equals(item.Value) && existing.Periode.Equals(item.Periode) && existing.Kpi.Id.Equals(item.KpiId) && existing.PeriodeType.Equals(item.PeriodeType)) { // break; //} if (operationData.Id != 0) { //var attachedEntity = DataContext.KpiAchievements.Find(item.Id); if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached) { DataContext.Entry(attachedEntity).State = EntityState.Detached; } DataContext.KeyOperationDatas.Attach(operationData); DataContext.Entry(operationData).State = EntityState.Modified; } else { operationData.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); DataContext.KeyOperationDatas.Add(operationData); } i++; } DataContext.SaveChanges(); response.IsSuccess = true; if (i > 0) { response.Message = string.Format("{0} Operation Data items has been updated successfully", i.ToString()); } else { response.Message = "File Successfully Parsed, but no data changed!"; } } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; } return response; }