public ResponseModel InsertOrUpdate(string connectString, DichVuModel model) { var result = new ResponseModel(); result.IsSuccess = true; using (var db = new HMSEntities(connectString)) { if (!CheckExists(model, db)) { H_DichVu DichVu = null; if (model.Id == 0) { DichVu = new H_DichVu() { WorkId = model.CVId, WorkTypeId = model.LoaiCVId, TimeProcess = model.TGXL, Price_Out = model.GiaBan, Price_In = model.GiaMua, Note = model.Note }; db.H_DichVu.Add(DichVu); } else { var found = db.H_DichVu.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id); if (found != null) { found.WorkId = model.CVId; found.WorkTypeId = model.LoaiCVId; found.TimeProcess = model.TGXL; found.Price_In = model.GiaMua; found.Price_Out = model.GiaBan; found.Note = model.Note; } else { result.IsSuccess = false; result.sms = "Công việc đã bị xóa hoặc không tồn tại trong hệ thống.!"; } } if (result.IsSuccess) { db.SaveChanges(); } } else { result.IsSuccess = false; result.sms = "Mã này đã tồn tại trong hệ thống. Vui lòng chọn lại mã khác."; } return(result); } }
private bool CheckExists(DichVuModel DichVu, HMSEntities db) { H_DichVu obj = db.H_DichVu.FirstOrDefault(x => !x.IsDeleted && x.Id != DichVu.Id && x.WorkId == DichVu.CVId && x.WorkTypeId == DichVu.LoaiCVId); return(obj != null ? true : false); }