private bool CheckExists(H_LoaiNhanVien UserType, HMSEntities db) { H_LoaiNhanVien obj = null; if (!string.IsNullOrEmpty(UserType.Code)) { obj = db.H_LoaiNhanVien.FirstOrDefault(x => !x.IsDeleted && x.Id != UserType.Id && x.Code.Trim().ToUpper().Equals(UserType.Code.Trim().ToUpper())); } return(obj != null ? true : false); }
public ResponseModel InsertOrUpdate(string connectString, H_LoaiNhanVien UserType) { var result = new ResponseModel(); result.IsSuccess = true; using (var db = new HMSEntities(connectString)) { if (!CheckExists(UserType, db)) { if (UserType.Id == 0) { db.H_LoaiNhanVien.Add(UserType); } else { var found = db.H_LoaiNhanVien.FirstOrDefault(x => !x.IsDeleted && x.Id == UserType.Id); if (found != null) { found.Code = UserType.Code; found.Note = UserType.Note; } else { result.IsSuccess = false; result.sms = "Loại nhân viên đã 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); } }
public JsonResult Save(H_LoaiNhanVien model) { var rs = BLLUserType.Instance.InsertOrUpdate(App_Global.AppGlobal.Connectionstring, model); return(Json(rs)); }
public int InsertFromExcel(Stream fileStream, string path, string connectionString) { try { using (var excel = new ExcelPackage(fileStream)) { var ws = excel.Workbook.Worksheets.First(); using (var db = new HMSEntities(connectionString)) { var dsLoais = (from x in db.H_LoaiNhanVien where !x.IsDeleted select new { Id = x.Id, Code = x.Code, Name = x.Note }).ToList(); var dsNVs = (from x in db.H_NhanVien where !x.IsDeleted select new { Id = x.Id, Code = x.Code, Name = x.Name }).ToList(); H_NhanVien nhanVien = null; H_LoaiNhanVien loaiNhanVien = null; H_Account account = null; string _loainv = "", _code = "", _name = "", _dthoai = "", _dChi = "", _ghiChu = "", _tk = "", _mk = ""; for (int ii = 2; ii <= ws.Dimension.End.Row; ii++) { _loainv = ws.Cells[ii, 1].Text.ToString(); _code = ws.Cells[ii, 2].Text.ToString(); _name = ws.Cells[ii, 3].Text.ToString(); _dthoai = ws.Cells[ii, 4].Text.ToString(); _dChi = ws.Cells[ii, 5].Text.ToString(); _ghiChu = ws.Cells[ii, 6].Text.ToString(); _tk = ws.Cells[ii, 7].Text.ToString(); _mk = ws.Cells[ii, 8].Text.ToString(); if (dsNVs.FirstOrDefault(x => x.Code.Trim().ToUpper() == _code.Trim().ToUpper() || x.Name.Trim().ToUpper() == _name.Trim().ToUpper()) == null) { var found = dsLoais.FirstOrDefault(x => x.Name.Trim().ToUpper() == _loainv.Trim().ToUpper()); if (found == null) { loaiNhanVien = new H_LoaiNhanVien() { Code = _loainv, Note = _loainv }; loaiNhanVien.H_NhanVien = new List <H_NhanVien>(); loaiNhanVien.H_NhanVien.Add(nhanVien); nhanVien = new H_NhanVien() { Code = _code, Name = _name, Phone = _dthoai, Address = _dChi, Note = _ghiChu, H_LoaiNhanVien = loaiNhanVien }; if (!string.IsNullOrEmpty(_tk) && !string.IsNullOrEmpty(_mk)) { account = new H_Account() { AccName = _tk, Password = GlobalFunction.EncryptMD5(_mk), IsAdmin = false, H_NhanVien = nhanVien, Role = 1 }; nhanVien.H_Account = new List <H_Account>(); nhanVien.H_Account.Add(account); } db.H_LoaiNhanVien.Add(loaiNhanVien); } else { nhanVien = new H_NhanVien() { Code = _code, Name = _name, Phone = _dthoai, Address = _dChi, Note = _ghiChu, LoaiNVId = found.Id }; if (!string.IsNullOrEmpty(_tk) && !string.IsNullOrEmpty(_mk)) { account = new H_Account() { AccName = _tk, Password = GlobalFunction.EncryptMD5(_mk), IsAdmin = false, H_NhanVien = nhanVien, Role = 1 }; nhanVien.H_Account = new List <H_Account>(); nhanVien.H_Account.Add(account); } db.H_NhanVien.Add(nhanVien); } } } db.SaveChanges(); return(1); } } } catch (Exception ex) { return(0); } }