private LMS_TamScanLogLMS ConvertToRecord(LMS_TamScanLogLMS tam, List<Hre_ProfileEntity> listProfile, List<Hre_CardHistoryEntity> listCardHistory, DateTime from, DateTime to) { if (tam.Code == null && tam.CodeEmp == null) return null; string CardCode = tam.Code.GetString().Trim(); string CodeEmp = tam.CodeEmp.GetString().Trim(); if (!string.IsNullOrWhiteSpace(CodeEmp)) { var profile = listProfile.Where(d => d.CodeEmp == CodeEmp).FirstOrDefault(); if (profile != null) { tam.ProfileID = profile.ID; return tam; } } else { DateTime dateMin = DateTime.MinValue; Boolean check = false; foreach (var profile in listProfile) { if (!string.IsNullOrWhiteSpace(CardCode)) { var lstCardHistoryByProfile = GetCardCodeByProfile(listCardHistory, profile, from, to); lstCardHistoryByProfile = lstCardHistoryByProfile.OrderBy(card => card.DateEffect).ToList(); foreach (var item in lstCardHistoryByProfile) { //ma cham cong History truoc do cua nhan vien khac. Quet the chi lay toi ngay hieu luc cua nhan vien do thoi var cardHistoryByCard = listCardHistory.Where(card => card.DateEffect > item.DateEffect && card.CardCode != null && card.CardCode.ToString().Trim() == item.CardCode).OrderBy(card => card.DateEffect).FirstOrDefault(); //Kiem tra khong duoc lay qua ngay hieu luc cua nhan vien khac cung su dung ma the do. if (item.DateEffect.HasValue && item.DateEffect >= dateMin && item.DateEffect <= tam.TimeLog && CardCode == item.CardCode.ToString().Trim() && (cardHistoryByCard == null || tam.TimeLog < cardHistoryByCard.DateEffect)) { dateMin = item.DateEffect.Value; tam.ProfileID = profile.ID; check = true; break; } } } if (check) { break; } } if (check) { return tam; } } return null; }
public List<LMS_TamScanLogLMS> SaveTAMLog(DataTable table, DateTime dateFrom, DateTime dateTo, Guid userID, List<Guid> listProfileID, bool filterByCodeEmp) { using (var context = new VnrHrmDataContext()) { IUnitOfWork unitOfWork = new UnitOfWork(context); if (table.Columns.Count == 2) { table.Columns.Add(); table.Columns.Add(); } else if (table.Columns.Count == 3) { table.Columns.Add(); } DataRow[] rows = new DataRow[table.Rows.Count]; table.Rows.CopyTo(rows, 0); string waitStatus = ProfileStatusSyn.E_WAITING.ToString(); List<LMS_TamScanLogLMS> listTAMScanLog = new List<LMS_TamScanLogLMS>(); var profileQueryable = unitOfWork.CreateQueryable<Hre_Profile>(userID, pro => (pro.StatusSyn == null || pro.StatusSyn != waitStatus) && (pro.DateQuit == null || pro.DateQuit.Value > dateFrom)); var cardHistoryQueryable = unitOfWork.CreateQueryable<Hre_CardHistory>(userID); if (listProfileID != null && listProfileID.Count() > 0) { profileQueryable = profileQueryable.Where(pro => listProfileID.Contains(pro.ID)); cardHistoryQueryable = cardHistoryQueryable.Where(pro => pro.ProfileID.HasValue && listProfileID.Contains(pro.ProfileID.Value)); } else { cardHistoryQueryable = cardHistoryQueryable.Where(cr => cr.Hre_Profile != null && (!cr.Hre_Profile.IsDelete.HasValue || cr.Hre_Profile.IsDelete == false)); } var listAllProfile = profileQueryable.Select(d => new Hre_ProfileEntity { ID = d.ID, CodeEmp = d.CodeEmp }).ToList<Hre_ProfileEntity>(); var listAllCardHistory = cardHistoryQueryable.Select(d => new Hre_CardHistoryEntity { ID = d.ID, ProfileID = d.ProfileID, CardCode = d.CardCode, DateEffect = d.DateEffect }).ToList<Hre_CardHistoryEntity>(); for (int i = 0; i < rows.Length; i++) { DataRow row = rows[i]; string cardCode = row[0].ToString(); cardCode = cardCode.ToUpper().Trim(); LMS_TamScanLogLMS tam = new LMS_TamScanLogLMS(); if (filterByCodeEmp) { tam.CodeEmp = cardCode; tam.Comment = "Hệ thống cũ"; } else { tam.Code = cardCode; } tam.TimeLog = Convert.ToDateTime(row[1].ToString().Trim()); tam.Type = row[2].ToString().Trim(); tam.MachineCode = row[3].ToString().Trim(); tam.Status = TAMScanStatus.E_LOADED.ToString(); List<Hre_ProfileEntity> listProfileByCode = null; if (!string.IsNullOrWhiteSpace(tam.Code)) { var listCardHistoryByCode = listAllCardHistory.Where(ch => ch.CardCode != null && ch.CardCode.ToString().ToUpper().Trim() == cardCode).ToList(); if (listCardHistoryByCode.Count() <= 0) { listProfileByCode = listAllProfile.Where(d => d.CodeAttendance != null && d.CodeAttendance.ToString().ToUpper().Trim() == cardCode).ToList(); } else { var listProfileIDByCode = listCardHistoryByCode.Select(d => d.ProfileID).ToList(); listProfileByCode = listAllProfile.Where(d => listProfileIDByCode.Contains(d.ID)).ToList(); } } else if (!string.IsNullOrWhiteSpace(tam.CodeEmp)) { listProfileByCode = listAllProfile.Where(d => d.CodeEmp != null && d.CodeEmp.ToString().ToUpper().Trim() == cardCode).ToList(); } if (listProfileByCode != null && listProfileByCode.Count > 0) { var dateStart = tam.TimeLog.HasValue ? tam.TimeLog.Value.Date : dateFrom; var dateEnd = tam.TimeLog.HasValue ? tam.TimeLog.Value.Date.AddDays(1) : dateTo; var rec = ConvertToRecord(tam, listProfileByCode, listAllCardHistory, dateStart, dateEnd); if (rec != null) { listTAMScanLog.Add(rec); } } } DeleteTAMLog(unitOfWork, dateFrom, dateTo, listTAMScanLog, userID); unitOfWork.AddObject(listTAMScanLog.ToArray()); unitOfWork.SaveChanges(userID); return listTAMScanLog; } }