Exemplo n.º 1
0
 public IActionResult Upload(FileStoreDTO upModel)
 {
     if (Request.Form != null && Request.Form.Files.Count > 0)
     {
         foreach (var formFile in Request.Form.Files)
         {
             if (formFile.Length > 100000000)
             {
                 return(StatusCode(500));//TODO вывести нормальную ошибку
             }
             //Костыль с кастомным FileStore. Когда будет новый core, нужно переписать
             var coreModel = new Core.Data.DTO.Common.FileStoreDTO();
             _objectMapper.Map(upModel, coreModel);
             var dto = FileStoreHelper.SaveFile(Configuration, formFile, coreModel);
             if (dto != null)
             {
                 try
                 {
                     _objectMapper.Map(dto, upModel);
                     Service.SaveAsync(upModel).Wait();
                 }
                 catch
                 {
                     FileStoreHelper.DeleteFileIfExist(dto.FilePath);
                     throw;
                 }
             }
         }
         return(Ok(new { count = Request.Form.Files.Count }));
     }
     else
     {
         return(NotFound());
     }
 }
Exemplo n.º 2
0
        public IActionResult Upload(FileStoreDTO upModel)
        {
            if (Request.Form != null && Request.Form.Files.Count > 0)
            {
                foreach (var formFile in Request.Form.Files)
                {
                    if (formFile.Length > 1e+8)
                    {
                        return(Json(new { success = false, fileSize = formFile.Length, fileName = formFile.FileName }));
                    }
                    //Костыль с кастомным FileStore. Когда будет новый core, нужно переписать
                    var coreModel = new Core.Data.DTO.Common.FileStoreDTO();
                    _objectMapper.Map(upModel, coreModel);
                    var dto = FileStoreHelper.SaveFile(Configuration, formFile, coreModel);
                    if (dto == null)
                    {
                        continue;
                    }

                    try
                    {
                        _objectMapper.Map(dto, upModel);
                        Service.SaveAsync(upModel).Wait();
                    }
                    catch
                    {
                        FileStoreHelper.DeleteFileIfExist(dto.FilePath);
                        throw;
                    }
                }
                return(Ok(new { count = Request.Form.Files.Count }));
            }
            else
            {
                return(NotFound());
            }
        }
Exemplo n.º 3
0
        public async Task <List <string> > UploadMedicine(Guid?appId, IFormFile file)
        {
            var countOfErrors = 0;
            var cellErrors    = new List <string>();
            var medicines     = new List <ImlMedicine>();

            using (var excelPackage = new ExcelPackage(file.OpenReadStream()))
            {
                var workbook    = excelPackage.Workbook;
                var worksheet   = workbook.Worksheets.First();
                var row         = 9;
                var checkString = "";
                var limsRps     = await DataService.GetDtoAsync <LimsRPMinDTO>(x => x.EndDate > DateTime.Now && x.OffOrderDate == null, extraParameters : new object[] { "" });

                do
                {
                    var    imlMedicine  = new ImlMedicine();
                    var    checkOnError = false;
                    string cellNum      = "";
                    for (var col = 1; col < 14; col++)
                    {
                        var cell     = worksheet.Cells[row, col].First();
                        var valueObj = cell.Value;
                        if (valueObj != null)
                        {
                            var value = valueObj.ToString();
                            switch (col)
                            {
                            case 1:
                                cellNum = value;
                                break;

                            case 2:
                                imlMedicine.MedicineName = value;
                                checkString = value;
                                break;

                            case 3:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.FormName = value;
                                break;

                            case 4:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.DoseInUnit = value;
                                break;

                            case 5:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.NumberOfUnits = value;
                                break;

                            case 6:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.MedicineNameEng = value;
                                break;

                            case 7:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                var rplz = limsRps.FirstOrDefault(x => x.RegNum == value);
                                if (rplz == null)
                                {
                                    cellErrors.Add(cellNum);
                                    checkOnError = true;
                                    break;
                                }
                                else
                                {
                                    imlMedicine.LimsRpId = rplz.Id;
                                }

                                imlMedicine.RegisterNumber = value;
                                break;

                            case 8:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.AtcCode = value;
                                break;

                            case 9:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.ProducerName = value;
                                break;

                            case 10:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.ProducerCountry = value;
                                break;

                            case 11:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.SupplierName = value;
                                break;

                            case 12:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.SupplierCountry = value;
                                break;

                            case 13:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.SupplierAddress = value;
                                break;

                            case 14:
                                if (string.IsNullOrEmpty(value))
                                {
                                    checkOnError = true;
                                }
                                imlMedicine.Notes = value;
                                break;
                            }
                        }
                        else
                        {
                            if (col == 2)
                            {
                                checkString  = string.Empty;
                                checkOnError = true;
                                break;
                            }
                        }

                        //if (checkOnError)
                        //    break;
                    }

                    imlMedicine.ApplicationId = appId.Value;
                    imlMedicine.CreatedByJson = _userInfo.PersonId;

                    if (!checkOnError)
                    {
                        medicines.Add(imlMedicine);
                    }
                    //else
                    //    countOfErrors++;

                    row++;
                } while (!string.IsNullOrEmpty(checkString));

                await SaveMedicines(medicines);
            }
            var fileStore = new FileStoreDTO
            {
                FileType     = FileType.Unknown,
                OrigFileName = file.Name + ".xlsx",
                FileSize     = file.Length,
                Ock          = false,
                EntityId     = appId.Value,
                EntityName   = nameof(ImlApplication),
                ContentType  = ".xlsx",
                Description  = "Medicines"
            };
            var dto = FileStoreHelper.SaveFile(_configuration, file, fileStore);

            DataService.Add <FileStore>(dto);
            await DataService.SaveChangesAsync();

            return(cellErrors);
        }