public void Insert(T entity)
 {
     if (entity == null)
     {
         throw new ArgumentNullException("entity");
     }
     entities.Add(entity);
     context.SaveChanges();
 }
示例#2
0
        public MappingDataDTO SaveMappingData(MappingDataDTO model)
        {
            log.MethodStart();

            var            currentDateTime = DateTime.Now;
            MappingDataDTO objReturn       = new MappingDataDTO();

            using (var trans = _db.Database.BeginTransaction())
            {
                try
                {
                    var objModel = new TRMapping();

                    if (model.mp_status == 'N')
                    {
                        objModel = _mapper.Map <TRMapping>(model);

                        objModel.mp_createdate = currentDateTime;
                        objModel.mp_updatedate = currentDateTime;

                        _db.TRMappings.Add(objModel);
                    }
                    else if (model.mp_status == 'E')
                    {
                        objModel               = _db.TRMappings.FirstOrDefault(x => x.mp_id == model.mp_id);
                        objModel.mp_status     = model.mp_status;
                        objModel.mp_version    = model.mp_version;
                        objModel.mp_startdate  = model.mp_startdate;
                        objModel.mp_enddate    = model.mp_enddate;
                        objModel.mp_program    = model.mp_program;
                        objModel.mp_filetype   = model.mp_filetype;
                        objModel.mp_dateformat = model.mp_dateformat;
                        objModel.mp_AntibioticIsolateOneRec = model.mp_AntibioticIsolateOneRec;
                        objModel.mp_firstlineisheader       = model.mp_firstlineisheader;
                        objModel.mp_updateuser = model.mp_updateuser;
                        objModel.mp_updatedate = currentDateTime;
                    }
                    else if (model.mp_status == 'A')
                    {
                        objModel                 = _db.TRMappings.FirstOrDefault(x => x.mp_id == model.mp_id);
                        objModel.mp_status       = model.mp_status;
                        objModel.mp_version      = objModel.mp_version + 0.01M;
                        objModel.mp_approveduser = model.mp_approveduser;
                        objModel.mp_approveddate = currentDateTime;
                        objModel.mp_updateuser   = model.mp_updateuser;
                        objModel.mp_updatedate   = currentDateTime;

                        var oldVersion = _db.TRMappings.OrderByDescending(x => x.mp_version).FirstOrDefault(x => x.mp_lab == model.mp_lab &&
                                                                                                            x.mp_hos_code == model.mp_hos_code &&
                                                                                                            x.mp_status == 'A' &&
                                                                                                            x.mp_program == model.mp_program &&
                                                                                                            x.mp_filetype == model.mp_filetype &&
                                                                                                            x.mp_startdate < model.mp_startdate &&
                                                                                                            x.mp_enddate == null &&
                                                                                                            x.mp_flagdelete == false);
                        if (oldVersion != null)
                        {
                            oldVersion.mp_enddate = model.mp_startdate.Value.AddDays(-1);
                        }
                    }


                    #region Save Log Process ...
                    _db.LogProcesss.Add(new LogProcess()
                    {
                        log_usr_id     = (objModel.mp_updateuser ?? objModel.mp_createuser),
                        log_mnu_id     = "",
                        log_mnu_name   = "Mapping",
                        log_tran_id    = objModel.mp_id.ToString(),
                        log_action     = (objModel.mp_status == 'N' ? "New" : "Update"),
                        log_desc       = "Update Mapping ",
                        log_createuser = "******",
                        log_createdate = currentDateTime
                    });
                    #endregion

                    _db.SaveChanges();

                    trans.Commit();

                    objReturn = _mapper.Map <MappingDataDTO>(objModel);
                }
                catch (Exception ex)
                {
                    // TODO: Handle failure
                    trans.Rollback();
                }
                finally
                {
                    trans.Dispose();
                }
            }

            log.MethodFinish();

            return(objReturn);
        }