using System; using System.Collections.Generic; using System.Linq; using Ultimus.Utilitarios; using ULA.Cathay.CreditoModel.Credito; using System.Data.Entity.Validation; using Ultimus.Interfaces; namespace ULA.Cathay.CreditoModel.DAL.Credito { public class DiferenciaDatosSAPBPMDAL { private UltimusLogs logger = new UltimusLogs("DiferenciaDatosSAPBPMDAL"); public BaseJson Insertar(List model) { DiferenciaDatosSAPBPM actual = new DiferenciaDatosSAPBPM(); BaseJson respuesta = new BaseJson(); try { using (CreditoContex Conn = new CreditoContex()) { foreach (var nuevo in model) { actual = Conn.DiferenciaDatosSAPBPM.Where(x => x.IdCliente == nuevo.IdCliente && x.IdSolicitud == nuevo.IdSolicitud && x.CodigoBP == nuevo.CodigoBP && x.Incidente == nuevo.Incidente && x.IdPropiedad == nuevo.IdPropiedad).FirstOrDefault(); if (actual == null) { int? Next_id = Conn.DiferenciaDatosSAPBPM.Max(t => (int?)t.IdDiferencia); nuevo.IdDiferencia = (Next_id == null) ? 1 : Next_id.Value + 1; Conn.DiferenciaDatosSAPBPM.Add(nuevo); } else { nuevo.IdDiferencia = actual.IdDiferencia; Conn.Entry(actual).State = System.Data.Entity.EntityState.Detached; Conn.DiferenciaDatosSAPBPM.Attach(nuevo); Conn.Entry(nuevo).State = System.Data.Entity.EntityState.Modified; } Conn.SaveChanges(); } respuesta.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString(); } } catch (DbEntityValidationException exs) { logger.Error(exs); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); } catch (Exception ex) { logger.Error(ex); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); } finally { actual = null; } return respuesta; } } }