public bool ImportTimeSheet(out string message, DateTime dateJobExcute) { message = "error"; DateTime yesterday = dateJobExcute.AddDays(-1).Date; TMSDbContext DbContext = new TMSDbContext(); try { var listTimeSheet = DbContext.CHECKINOUT.Where(x => x.CHECKTIME >= yesterday && x.CHECKTIME < dateJobExcute).ToList(); if (listTimeSheet.Count > 0) { _tmpTimeSheetRepository.RemoveAllData(); List <FingerTimeSheetTmp> listTmp = new List <FingerTimeSheetTmp>(); foreach (var item in listTimeSheet) { FingerTimeSheetTmp tmp = new FingerTimeSheetTmp(); var user = DbContext.USERINFO.FirstOrDefault(x => x.USERID == item.USERID); if (user == null) { continue; } tmp.UserNo = user.Badgenumber; tmp.Date = item.CHECKTIME; tmp.AccName = DbContext.USERINFO.Where(x => x.USERID == item.USERID).Select(x => x.Name).FirstOrDefault(); _tmpTimeSheetRepository.Add(tmp); listTmp.Add(tmp); } _unitOfWork.Commit(); //import to table finger timesheet int countSuccess = 0; List <FingerTimeSheetTmpErrorModel> listModel = new List <FingerTimeSheetTmpErrorModel>(); if (_fingerTSService.ImportTimeSheet(out countSuccess, DbContext, out listModel)) { message = "Success"; log.Info("Job Time Sheet Run Success :" + DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss")); return(true); } else { message = ""; StringBuilder sb = new StringBuilder(); foreach (var item in listModel) { sb.AppendLine(item.Error); } message = sb.ToString(); return(false); } } else { message = "FingerTimeSheetTmp table has no data !"; return(true); } } catch (Exception ex) { message = ex.Message; log.Error(ex.Message); if (ex.StackTrace != null) { log.Info(ex.StackTrace); } return(false); } }
public async Task <HttpResponseMessage> Import() { if (!Request.Content.IsMimeMultipartContent()) { Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, MessageSystem.FormatIsNotSupport); } var root = HttpContext.Current.Server.MapPath(CommonConstants.PathUploadTimeSheet); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } var provider = new MultipartFormDataStreamProvider(root); var result = await Request.Content.ReadAsMultipartAsync(provider); //Upload files int addedCount = 0; foreach (MultipartFileData fileData in result.FileData) { IList <string> AllowedFileExtensions = new List <string> { CommonConstants.FileTimeSheetSupport }; var ext = fileData.Headers.ContentDisposition.FileName.Substring(fileData.Headers.ContentDisposition.FileName.LastIndexOf('.')); var extension = ext.Remove(ext.Length - 1, 1).ToLower(); if (!AllowedFileExtensions.Contains(extension)) { return(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, MessageSystem.FormatIsNotSupport)); } if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName)) { return(Request.CreateResponse(HttpStatusCode.NotAcceptable, MessageSystem.FormatIsNotSupport)); } string fileName = fileData.Headers.ContentDisposition.FileName; if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } var fullPath = Path.Combine(root, fileName); File.Copy(fileData.LocalFileName, fullPath, true); //insert to DB List <FingerTimeSheetTmp> listFingerTimeSheet = new List <FingerTimeSheetTmp>(); List <FingerTimeSheetTmpErrorModel> listFingerTimeSheetError = new List <FingerTimeSheetTmpErrorModel>(); this.ReadTimeSheetFromTextFile(fullPath, out listFingerTimeSheet, out listFingerTimeSheetError); if (listFingerTimeSheet.Count > 0 && listFingerTimeSheetError.Count == 0) { listFingerTimeSheet.Where(x => x.Date < DateTime.Today.Date); _fingerTimeSheetTmpService.ClearAllData(); foreach (var timeSheet in listFingerTimeSheet) { _fingerTimeSheetTmpService.Add(timeSheet); } _fingerTimeSheetTmpService.Save(); var context = HttpContext.Current.GetOwinContext().Get <TMSDbContext>(); var isSuccess = _fingerTimeSheetService.ImportTimeSheet(out addedCount, context, out listFingerTimeSheetError); WriteErrorToFile(listFingerTimeSheetError); if (listFingerTimeSheetError.Count != 0 || !isSuccess) { return(Request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ErrorImportFile)); } } else { try { WriteErrorToFile(listFingerTimeSheetError); return(Request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ErrorImportFile)); } catch (IOException ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message.ToString())); } } } return(Request.CreateResponse(HttpStatusCode.OK, "Import " + addedCount + " time sheet successfully.")); }