コード例 #1
0
 public async Task <IActionResult> Post([FromForm] SimCardImportDto model)
 {
     return(await CheckAndExecuteModel(async() =>
     {
         if (model.File.Length < 1)
         {
             throw new NullReferenceException();
         }
         else if (!model.File.FileName.EndsWith(".xlsx"))
         {
             throw new InvalidCastException("Invalid File Type.");
         }
         var response = await _service.InsertFromFileAsync(model);
         if (response.StatusCode == 200)
         {
             return Ok("Simcard Insertion Succesful.");
         }
         return StatusCode(response.StatusCode, response.Message);
     }));
 }
コード例 #2
0
        public async Task <ResponseResult> InsertFromFileAsync(SimCardImportDto dto)
        {
            using (var stream = new MemoryStream())
            {
                await dto.File.CopyToAsync(stream);

                using (SpreadsheetDocument document = SpreadsheetDocument.Open(stream, true))
                {
                    WorkbookPart          workbookPart = document.WorkbookPart;
                    WorksheetPart         worksheetPart = workbookPart.WorksheetParts.First();
                    SheetData             sheetData = worksheetPart.Worksheet.Elements <SheetData>().First();
                    SharedStringTablePart shared = document.WorkbookPart.GetPartsOfType <SharedStringTablePart>().First();
                    SharedStringItem[]    items = shared.SharedStringTable.Elements <SharedStringItem>().ToArray();
                    string key, text = "";
                    var    list = new List <SimCard>();
                    var    wrongInputs = new List <WrongInput>();
                    long   r;
                    var    rows = sheetData.Elements <Row>().ToList();
                    for (int i = 0; i < rows.Count; i++)
                    {
                        if (rows[i].RowIndex.Value > 1)
                        {
                            var sim = new SimCard
                            {
                                OperatorId        = dto.OperatorId,
                                OperatorPackageId = dto.OperatorPackageId,
                                UserName          = dto.UserName,
                                UserId            = dto.UserId,
                                UpdateTime        = DateTime.Now,
                                UpdateBy          = dto.UserId
                            };
                            var    cells = rows[i].Elements <Cell>().ToList();
                            string mobileNo = "", simNo = "";
                            if (cells.Count == 2)
                            {
                                bool isMobileCorrect = false, isSimCorrect = false;
                                if (cells[0].DataType != null && cells[0].DataType.Value == CellValues.SharedString)
                                {
                                    mobileNo = items[int.Parse(cells[0].CellValue.Text)].InnerText;
                                    if (mobileNo.Length == 11)
                                    {
                                        long.TryParse(mobileNo, out r);
                                        if (r > 1000000000)
                                        {
                                            sim.MobileNumber = mobileNo;
                                            isMobileCorrect  = true;
                                        }
                                        else
                                        {
                                            text = $"MobileNo have wrong character.";
                                        }
                                    }
                                    else
                                    {
                                        text = $"MobileNo {mobileNo.Length} digit.";
                                    }
                                }
                                else
                                {
                                    text = "MobileNo DataType Not Text.";
                                }
                                if (isMobileCorrect)
                                {
                                    if (cells[1].DataType != null && cells[1].DataType.Value == CellValues.SharedString)
                                    {
                                        simNo = items[int.Parse(cells[1].CellValue.Text)].InnerText;
                                        if (simNo.Length > 11 && simNo.Length < 50)
                                        {
                                            sim.SimNumber = simNo;
                                            isSimCorrect  = true;
                                        }
                                        else
                                        {
                                            text = "SimNo not should between 12 and 50";
                                        }
                                    }
                                    else
                                    {
                                        text = "SimNo DataType Not Text.";
                                    }
                                }
                                if (!isSimCorrect)
                                {
                                    key = !string.IsNullOrEmpty(mobileNo) ? mobileNo : !string.IsNullOrEmpty(simNo) ? simNo : "";
                                    if (string.IsNullOrEmpty(key))
                                    {
                                        try
                                        {
                                            key = items[int.Parse(cells[0].CellValue.Text)].InnerText;
                                        }
                                        catch (Exception)
                                        {
                                            key = cells[0].InnerText;
                                        }
                                    }
                                    if (!string.IsNullOrEmpty(key))
                                    {
                                        wrongInputs.Add(new WrongInput {
                                            Key = key, Cause = text
                                        });
                                    }
                                }
                                else
                                {
                                    list.Add(sim);
                                }
                            }
                        }
                    }
                    var response = _repository.BulkInsert(list);
                    wrongInputs.AddRange(response);
                    var result = new ResponseResult
                    {
                        StatusCode = response.Count > 0 ? 409 : wrongInputs.Count > 0 ? 411: 200,
                        Message    = wrongInputs
                    };
                    return(await Task.FromResult(result));
                }
            }
        }