Beispiel #1
0
 public static int SaveWorkReportProperty(WorkReportProperty property)
 {
     using (MyDbContext db = new MyDbContext())
     {
         db.WorkReportProperties.Add(property);
         return(db.SaveChanges());
     }
 }
Beispiel #2
0
        public ActionResult UploadWorkFile(UploadWorkFileViewModel model)
        {
            if (ModelState.IsValid)
            {
                string localFileFullName = Path.GetTempFileName();
                try
                {
                    string[] arr           = model.Month.Split("/".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                    int      selectedYear  = Convert.ToInt32(arr[1]);
                    int      selectedMonth = Convert.ToInt32(arr[0]);

                    if (model.UploadedFile.ContentLength == 0)
                    {
                        ModelState.AddModelError("", "The uploaded file is empty");
                    }
                    else
                    {
                        string uploadFileName = model.UploadedFile.FileName;
                        localFileFullName = Path.ChangeExtension(localFileFullName, Path.GetExtension(uploadFileName));
                        model.UploadedFile.SaveAs(localFileFullName);

                        if (model.WorkFileType == EWorkFileType.WorkLog)
                        {
                            WorkLogUploadService service = new WorkLogUploadService(uploadFileName, localFileFullName, selectedYear, selectedMonth, LoginName);
                            ReturnResult         rst     = service.ReadFile();
                            if (rst.Succeeded)
                            {
                                WorkLogService.ClearWorkLogs(selectedYear, selectedMonth);
                                List <WorkLog>  workLogs        = service.GetWorkLogs();
                                WorkLogProperty workLogProperty = service.GetWorkLogProperty();
                                WorkLogService.SaveWorkLogs(workLogs);
                                WorkLogPropertyService.SaveWorkLogProperty(workLogProperty);

                                var dic = new RouteValueDictionary();
                                dic.Add("month", selectedMonth + "/" + selectedYear);
                                return(RedirectToAction("WorkLog", dic));
                            }
                            else
                            {
                                ViewBag.ErrorMsg = rst.ErrorMsg;
                            }
                        }
                        else if (model.WorkFileType == EWorkFileType.WorkReport)
                        {
                            WorkReportUploadService service = new WorkReportUploadService(uploadFileName, localFileFullName, selectedYear, selectedMonth, LoginName);
                            ReturnResult            rst     = service.ReadFile();
                            if (rst.Succeeded)
                            {
                                WorkReportService.ClearWorkReports(selectedYear, selectedMonth);
                                List <WorkReport> workReports = service.GetWorkReports();
                                WorkReportService.SaveWorkReports(workReports);
                                WorkReportProperty workReportProperty = service.GetWorkReportProperty();
                                WorkReportPropertyService.SaveWorkReportProperty(workReportProperty);

                                var dic = new RouteValueDictionary();
                                dic.Add("month", selectedMonth + "/" + selectedYear);
                                return(RedirectToAction("WorkReport", dic));
                            }
                            else
                            {
                                ViewBag.ErrorMsg = rst.ErrorMsg;
                            }
                        }
                        else
                        {
                            throw new Exception("No implemented service for " + model.WorkFileType.GetDescription());
                        }
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.ErrorMsg = ex.Message;
                    if (ex.InnerException != null && ex.InnerException.InnerException != null)
                    {
                        ViewBag.ErrorMsg = ex.Message + " Inner Exception:" + ex.InnerException.InnerException.Message;
                    }
                }
                finally
                {
                    if (System.IO.File.Exists(localFileFullName))
                    {
                        System.IO.File.Delete(localFileFullName);
                    }
                }
            }
            var fileTypeSelectItems = new List <SelectListItem>();

            foreach (EWorkFileType val in Enum.GetValues(typeof(EWorkFileType)))
            {
                fileTypeSelectItems.Add(new SelectListItem()
                {
                    Text  = val.GetDescription(),
                    Value = val.ToString()
                });
            }
            ViewBag.FileTypeSelectItems = fileTypeSelectItems;
            return(View(model));
        }