Exemplo n.º 1
0
        public IActionResult AddPlanAndRateByExcel(int InsuranceCompanyId, int PolicyYear, [FromForm(Name = "file")] IFormFile formFile)
        {
            try
            {
                if (formFile == null || formFile.Length <= 0)
                {
                    return(BadRequest(new { error = "file is empty" }));
                }


                if (!Path.GetExtension(formFile.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                {
                    if (!Path.GetExtension(formFile.FileName).Equals(".xls", StringComparison.OrdinalIgnoreCase))
                    {
                        return(BadRequest(new { error = "Not Support file extension" }));
                    }
                }


                using (var stream = new MemoryStream())
                {
                    formFile.CopyTo(stream);
                    //UploadRatesByAge(int id, int PolicyYear, MemoryStream stream, int userId)
                    var data = _itemService.UpLoadRate(InsuranceCompanyId, PolicyYear, stream);
                    //using (var package = new ExcelPackage(stream))
                    //{
                    //    ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
                    //    var rowCount = worksheet.Dimension.Rows;

                    //    for (int row = 2; row <= rowCount; row++)
                    //    {
                    //        list.Add(new CoversBenefit
                    //        {
                    //            InsuranceBenefitTypeId = int.Parse(worksheet.Cells[row, 1].Value.ToString().Trim()),
                    //            CoverId = CoverId,
                    //            Value = worksheet.Cells[row, 4].Value == null ? "" : worksheet.Cells[row, 4].Value.ToString().Trim(),
                    //            CreatedAt = DateTime.Now,
                    //            FCreateUserId = GetNameClaim()

                    //        });
                    //    }
                    //}
                    return(Ok(data));
                }

                // add list to db ..
                // here just read and return
            }

            catch (AppException ex)
            {
                return(DefaultError(ex.Message));
            }
        }