public async Task <IActionResult> Edit(Guid id, [Bind("DevicePlaceId,Code,Description,DeviceTypeId")] DevicePlace devicePlace)
        {
            if (id != devicePlace.DevicePlaceId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(devicePlace);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DevicePlaceExists(devicePlace.DevicePlaceId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DeviceTypeId"] = new SelectList(_context.DeviceTypes, "DeviceTypeId", "Name", devicePlace.DeviceTypeId);
            return(View(devicePlace));
        }
Example #2
0
        private string GenerateReportCode(Guid DevicePlaceId, Guid DeviceStatusId, int ReporterId, Guid WagonId, DateTime date)
        {
            DevicePlace  dp  = _context.DevicePlaces.Find(DevicePlaceId);
            DeviceStatus ds  = _context.DeviceStatus.Find(DeviceStatusId);
            Reporter     rep = _context.Reporters.Find(ReporterId);
            Wagon        w   = _context.Wagons.Find(WagonId);

            return(w.Number + dp.Code.ToUpper() + ds.Code.ToUpper() + rep.UserName.ToUpper() + date.ToString("yyMMdd"));
        }
        public async Task <IActionResult> Create([Bind("DevicePlaceId,Code,Description,DeviceTypeId")] DevicePlace devicePlace)
        {
            if (ModelState.IsValid)
            {
                devicePlace.DevicePlaceId = Guid.NewGuid();
                _context.Add(devicePlace);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DeviceTypeId"] = new SelectList(_context.DeviceTypes, "DeviceTypeId", "Name", devicePlace.DeviceTypeId);
            return(View(devicePlace));
        }
Example #4
0
        //[ValidateAntiForgeryToken]
        //[RequestSizeLimit(5000000)]
        public async Task <IActionResult> Upload(IFormFile File)
        {
            IFormFile file = File;

            if (file == null || file.Length == 0)
            {
                return(RedirectToAction(nameof(Index)));
            }
            List <Report> Reports = new List <Report>();
            Dictionary <string, Report> ChildReports = new Dictionary <string, Report>();
            List <int> LostReports = new List <int>();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream).ConfigureAwait(false);

                using (ExcelPackage package = new ExcelPackage(memoryStream))
                {
                    ExcelWorksheet worksheet   = package.Workbook.Worksheets[1]; // Tip: To access the first worksheet, try index 1, not 0
                    int            totalRows   = worksheet.Dimension.Rows;
                    int[]          ReportCells = { 0, 2, 3, 4, 6 };              //بر اساس فایل گزارشهای شرکت ریل پرداز
                    int[]          RepairCells = { 5, 7, 11 };                   //بر اساس فایل گزارشهای شرکت ریل پرداز
                    for (int i = 2; i < totalRows; i++)
                    {
                        string[] ReportCellValues = new string[ReportCells.Length];
                        bool     SkipRow          = false;
                        for (int c = 0; c < ReportCells.Length; c++)
                        {
                            object CellValue = ((object[, ])(worksheet.Cells.Value))[i, ReportCells[c]];
                            if (CellValue == null)
                            {
                                SkipRow = true;
                                continue;
                            }
                            ReportCellValues[c] = CellValue.ToString();
                        }
                        if (SkipRow)
                        {
                            LostReports.Add(i + 1);
                            continue;
                        }
                        DateTime date;
                        try
                        {
                            date = FromOldExcelDateTime(ReportCellValues[0]);
                        }
                        catch
                        {
                            LostReports.Add(i + 1);
                            continue;
                        }
                        Wagon        wagon = _context.Wagons.Where(w => w.Number == Convert.ToInt32(ReportCellValues[1])).FirstOrDefault();
                        DevicePlace  devp  = _context.DevicePlaces.Where(dp => dp.Code == ReportCellValues[2]).FirstOrDefault();
                        Reporter     rep   = _context.Reporters.Where(r => r.UserName == ReportCellValues[3]).FirstOrDefault();
                        DeviceStatus devS  = _context.DeviceStatus.Where(ds => ds.Code == ReportCellValues[4]).FirstOrDefault();
                        string       Code;
                        try
                        {
                            Code = GenerateReportCode(devp.DevicePlaceId, devS.StatusId, rep.ReporterId, wagon.WagonId, date);
                        }
                        catch
                        {
                            LostReports.Add(i + 1);
                            continue;
                        }
                        if (!_context.Reports.Any(r => r.Code == Code) &&
                            !Reports.Any(r => r.Code == Code))
                        {
                            Reports.Add(new Report
                            {
                                DateTimeCreated = date,
                                WagonId         = wagon.WagonId,
                                DevicePlaceId   = devp.DevicePlaceId,
                                DeviceStatusId  = devS.StatusId,
                                ReporterId      = rep.ReporterId,
                                Code            = Code
                            });
                        }
                        //else
                        //    LostReports.Add(new ReportValues
                        //    {
                        //        Date = date,
                        //        WagonNum = ReportCellValues[1],
                        //        DevicePlaceCode = ReportCellValues[2],
                        //        UserName = ReportCellValues[3],
                        //        DeviceStatusCode = ReportCellValues[4]
                        //    });

                        string[] RepairCellValues = new string[RepairCells.Length];
                        bool     NoChild          = false;
                        for (int c = 0; c < RepairCells.Length; c++)
                        {
                            object CellValue = ((object[, ])(worksheet.Cells.Value))[i, RepairCells[c]];
                            if (CellValue == null || CellValue.ToString().Length < 2)
                            {
                                NoChild = true;
                                break;
                            }
                            RepairCellValues[c] = CellValue.ToString();
                        }
                        if (NoChild)
                        {
                            continue;
                        }

                        Reporter     repairer = _context.Reporters.Where(r => r.UserName == RepairCellValues[0]).FirstOrDefault();
                        DeviceStatus dsRep    = _context.DeviceStatus.Where(ds => ds.Code == RepairCellValues[1]).FirstOrDefault();
                        DateTime     RepDate  = FromOldExcelDateTime(RepairCellValues[2]);
                        string       RepCode;
                        try
                        {
                            RepCode = GenerateReportCode(devp.DevicePlaceId, dsRep.StatusId, repairer.ReporterId, wagon.WagonId, RepDate);
                        }
                        catch
                        {
                            LostReports.Add(i + 1);
                            continue;
                        }
                        ChildReports.Add(Code, new Report
                        {
                            DateTimeCreated = RepDate,
                            WagonId         = wagon.WagonId,
                            DevicePlaceId   = devp.DevicePlaceId,
                            DeviceStatusId  = dsRep.StatusId,
                            ReporterId      = repairer.ReporterId,
                            Code            = RepCode
                        });
                    }
                }
            }
            _context.Reports.AddRange(Reports);
            await _context.SaveChangesAsync();

            string fileName = _env.WebRootPath + @"\Excel\LostReports.xlsx";

            FileInfo LostReportsFile = new FileInfo(fileName);

            if (LostReportsFile.Exists)
            {
                LostReportsFile.Delete();
            }

            using (ExcelPackage ExcelPackage = new ExcelPackage(LostReportsFile))
            {
                ExcelWorksheet worksheet = ExcelPackage.Workbook.Worksheets.Add(Expressions.LostReports);
                worksheet.Cells[1, 1].Value = "شماره ردیف";
                int row = 2;
                foreach (int r in LostReports)
                {
                    worksheet.Cells[row++, 1].Value = r.ToString();
                }
                ExcelPackage.Save();
            }

            List <Report> ChildReports2 = new List <Report>();

            foreach (KeyValuePair <string, Report> item in ChildReports)
            {
                Report parentReport = _context.Reports.Where(r => r.Code == item.Key).FirstOrDefault();
                if (parentReport == null)
                {
                    continue;//ParentReportIsLost
                }

                item.Value.ParentReportId = parentReport.ReportId;

                if (!_context.Reports.Any(r => r.Code == item.Value.Code))
                {
                    ChildReports2.Add(item.Value);
                }
                else
                {
                    continue;
                }
                _context.Reports.Update(parentReport);
            }
            _context.Reports.AddRange(ChildReports2);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }