Exemplo n.º 1
0
        public BaseResponse BatchUpdateKpiTargetss(BatchUpdateTargetRequest request)
        {
            var response = new BaseResponse();
            try
            {
                int i = 0;
                foreach (var item in request.BatchUpdateKpiTargetItemRequest)
                {
                    var kpiTarget = item.MapTo<KpiTarget>();
                    var exist = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark);
                    //skip no change value
                    if (exist != null)
                    {
                        continue;
                    }
                    var attachedEntity = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode);
                    if (attachedEntity != null)
                    {
                        kpiTarget.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 (kpiTarget.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.KpiTargets.Attach(kpiTarget);
                        DataContext.Entry(kpiTarget).State = EntityState.Modified;
                    }
                    else
                    {
                        kpiTarget.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId);
                        DataContext.KpiTargets.Add(kpiTarget);
                    }
                    i++;
                }
                DataContext.SaveChanges();
                response.IsSuccess = true;
                if (i > 0)
                {
                    response.Message = string.Format("{0}  KPI Target 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;
        }
Exemplo n.º 2
0
        /*public BaseResponse BatchUpdateKpiTargetss(BatchUpdateTargetRequest request)
        {
            var response = new BaseResponse();
            try
            {
                int i = 0;
                foreach (var item in request.BatchUpdateKpiTargetItemRequest)
                {
                    var kpiTarget = item.MapTo<KpiTarget>();
                    var exist = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark);
                    //skip no change value
                    if (exist != null)
                    {
                        continue;
                    }
                    var attachedEntity = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode);
                    if (attachedEntity != null)
                    {
                        kpiTarget.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 (kpiTarget.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.KpiTargets.Attach(kpiTarget);
                        DataContext.Entry(kpiTarget).State = EntityState.Modified;
                    }
                    else
                    {
                        kpiTarget.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId);
                        DataContext.KpiTargets.Add(kpiTarget);
                    }
                    i++;
                }
                DataContext.SaveChanges();
                response.IsSuccess = true;
                if (i > 0)
                {
                    response.Message = string.Format("{0}  KPI Target 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;
        }*/
        public BaseResponse BatchUpdateKpiTargetss(BatchUpdateTargetRequest request)
        {
            var response = new BaseResponse();
            try
            {
                int deletedCounter = 0;
                int updatedCounter = 0;
                int addedCounter = 0;
                int skippedCounter = 0;
                foreach (var item in request.BatchUpdateKpiTargetItemRequest)
                {
                    if (!string.IsNullOrEmpty(item.Value))
                    {
                        var existedKpiTarget =
                            DataContext.KpiTargets.FirstOrDefault(
                                x =>
                                x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode);

                        if (existedKpiTarget != null)
                        {
                            if (item.Value.Equals("-") || item.Value.ToLowerInvariant().Equals("null"))
                            {
                                DataContext.KpiTargets.Remove(existedKpiTarget);
                                deletedCounter++;
                            }
                            else
                            {
                                if (existedKpiTarget.Value.Equals(item.RealValue))
                                {
                                    skippedCounter++;
                                }
                                else
                                {
                                    existedKpiTarget.Value = item.RealValue;
                                    DataContext.Entry(existedKpiTarget).State = EntityState.Modified;
                                    updatedCounter++;
                                }
                            }
                        }
                        else
                        {
                            var kpiTarget = item.MapTo<KpiTarget>();
                            if (kpiTarget.Value.HasValue)
                            {
                                kpiTarget.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId);
                                DataContext.KpiTargets.Add(kpiTarget);
                                addedCounter++;
                            }
                            else
                            {
                                skippedCounter++;
                            }
                        }
                    }
                }
                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;
        }
Exemplo n.º 3
0
        private BaseResponse UpdateKpiTarget(IEnumerable<ConfigurationViewModel.Item> data)
        {
            var response = new BaseResponse { IsSuccess = false, Message = "Data Not Valid" };
            if (data != null)
            {
                var batch = new BatchUpdateTargetRequest();
                foreach (var datum in data)
                {
                    var prepare = new SaveKpiTargetRequest() { Value = datum.Value, KpiId = datum.KpiId, Periode = datum.Periode, PeriodeType = datum.PeriodeType, Remark = datum.Remark };
                    batch.BatchUpdateKpiTargetItemRequest.Add(prepare);
                }
                response = _kpiTargetService.BatchUpdateKpiTargetss(batch);

            }
            return response;
        }
Exemplo n.º 4
0
        private BaseResponse _UpdateKpiTarget(List<ConfigurationViewModel.Item> datas)
        {
            var response = new BaseResponse();
            if (datas != null)
            {
                var batch = new BatchUpdateTargetRequest();
                foreach (var data in datas)
                {
                    var prepare = new SaveKpiTargetRequest() { Value = data.Value, KpiId = data.KpiId, Periode = data.Periode, PeriodeType = data.PeriodeType, Remark = data.Remark };
                    batch.BatchUpdateKpiTargetItemRequest.Add(prepare);
                }
                response = _kpiTargetService.BatchUpdateKpiTargetss(batch);

            }
            return response;
        }