Рефакторинг импорта РТК бурение

This commit is contained in:
Степанов Дмитрий 2023-10-12 15:29:02 +05:00
parent 558f70cf61
commit 2f2599f9ab
2 changed files with 42 additions and 38 deletions

View File

@ -20,7 +20,7 @@ namespace AsbCloudInfrastructure.Services.ProcessMaps.WellDrillingProcessMap;
*/
public class WellDrillingProcessMapImportService : IProcessMapImportService
{
private readonly IWellDrillingProcessMapRepository wellDrillingProcessMapRepository;
private readonly IProcessMapPlanRepository<ProcessMapPlanWellDrillingDto> processMapPlanWellDrillingRepository;
private readonly ICrudRepository<WellSectionTypeDto> wellSectionTypeRepository;
private readonly IWellService wellService;
@ -45,14 +45,15 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
private const int columnRopPlan = 15;
private const int columnUsageSaub = 16;
private const int columnUsageSpin = 17;
private const int columnComment = 18;
private WellSectionTypeDto[] sections = null!;
public WellDrillingProcessMapImportService(IWellDrillingProcessMapRepository wellDrillingProcessMapRepository,
public WellDrillingProcessMapImportService(IProcessMapPlanRepository<ProcessMapPlanWellDrillingDto> processMapPlanWellDrillingRepository,
ICrudRepository<WellSectionTypeDto> wellSectionTypeRepository,
IWellService wellService)
{
this.wellDrillingProcessMapRepository = wellDrillingProcessMapRepository;
this.processMapPlanWellDrillingRepository = processMapPlanWellDrillingRepository;
this.wellSectionTypeRepository = wellSectionTypeRepository;
this.wellService = wellService;
}
@ -67,7 +68,7 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
var wellDrillingProcessMaps = ParseWorkBook(workBook);
if (deleteProcessMapPlansBeforeImport)
await wellDrillingProcessMapRepository.RemoveByWellAsync(idWell, cancellationToken);
await processMapPlanWellDrillingRepository.RemoveByWellAsync(idWell, cancellationToken);
foreach (var wellDrillingProcessMap in wellDrillingProcessMaps)
{
@ -75,7 +76,7 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
wellDrillingProcessMap.IdUser = idUser;
}
await wellDrillingProcessMapRepository.InsertRangeAsync(wellDrillingProcessMaps, cancellationToken);
await processMapPlanWellDrillingRepository.InsertRangeAsync(wellDrillingProcessMaps, cancellationToken);
}
public async Task<(string Name, Stream File)> ExportAsync(int idWell, CancellationToken cancellationToken)
@ -85,7 +86,7 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
sections = (await wellSectionTypeRepository.GetAllAsync(cancellationToken)).ToArray();
var processMapPlans = (await wellDrillingProcessMapRepository.GetByIdWellAsync(idWell,
var processMapPlans = (await processMapPlanWellDrillingRepository.GetByIdWellAsync(idWell,
cancellationToken)).ToArray();
var file = await GenerateExcelFileStreamAsync(processMapPlans, cancellationToken);
@ -113,48 +114,49 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
return (name, memoryStream);
}
private void AddToWorkbook(XLWorkbook workbook, IEnumerable<WellDrillingProcessMapDto> wellDrillingProcessMaps)
private void AddToWorkbook(XLWorkbook workbook, IEnumerable<ProcessMapPlanWellDrillingDto> processMapPlanWellDrillings)
{
var sheet = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNamePlan)
?? throw new FileFormatException($"Книга excel не содержит листа {sheetNamePlan}.");
AddToSheet(sheet, wellDrillingProcessMaps.ToArray());
AddToSheet(sheet, processMapPlanWellDrillings.ToArray());
}
private void AddToSheet(IXLWorksheet sheet, IList<WellDrillingProcessMapDto> wellDrillingProcessMaps)
private void AddToSheet(IXLWorksheet sheet, IList<ProcessMapPlanWellDrillingDto> processMapPlanWellDrillings)
{
if (!wellDrillingProcessMaps.Any())
if (!processMapPlanWellDrillings.Any())
return;
for (int i = 0; i < wellDrillingProcessMaps.Count; i++)
for (int i = 0; i < processMapPlanWellDrillings.Count; i++)
{
var row = sheet.Row(1 + i + headerRowsCount);
AddToRow(row, wellDrillingProcessMaps[i]);
AddToRow(row, processMapPlanWellDrillings[i]);
}
}
private void AddToRow(IXLRow row, WellDrillingProcessMapDto wellDrillingProcessMap)
private void AddToRow(IXLRow row, ProcessMapPlanWellDrillingDto processMapPlanWellDrillings)
{
row.Cell(columnWellSectionType).Value = sections.First(x => x.Id == wellDrillingProcessMap.IdWellSectionType).Caption;
row.Cell(columnMode).Value = GetModeCaption(wellDrillingProcessMap.IdMode);
row.Cell(columnDepthStart).Value = wellDrillingProcessMap.DepthStart;
row.Cell(columnDepthEnd).Value = wellDrillingProcessMap.DepthEnd;
row.Cell(columnPressurePlan).Value = wellDrillingProcessMap.Pressure.Plan;
row.Cell(columnPressureLimitMax).Value = wellDrillingProcessMap.Pressure.LimitMax;
row.Cell(columnAxialLoadPlan).Value = wellDrillingProcessMap.AxialLoad.Plan;
row.Cell(columnAxialLoadLimitMax).Value = wellDrillingProcessMap.AxialLoad.LimitMax;
row.Cell(columnTopDriveTorquePlan).Value = wellDrillingProcessMap.TopDriveTorque.Plan;
row.Cell(columnTopDriveTorqueLimitMax).Value = wellDrillingProcessMap.TopDriveTorque.LimitMax;
row.Cell(columnTopDriveSpeedPlan).Value = wellDrillingProcessMap.TopDriveSpeed.Plan;
row.Cell(columnTopDriveSpeedLimitMax).Value = wellDrillingProcessMap.TopDriveSpeed.LimitMax;
row.Cell(columnFlowPlan).Value = wellDrillingProcessMap.Flow.Plan;
row.Cell(columnFlowLimitMax).Value = wellDrillingProcessMap.Flow.LimitMax;
row.Cell(columnRopPlan).Value = wellDrillingProcessMap.RopPlan;
row.Cell(columnUsageSaub).Value = wellDrillingProcessMap.UsageSaub;
row.Cell(columnUsageSpin).Value = wellDrillingProcessMap.UsageSpin;
row.Cell(columnWellSectionType).Value = sections.First(x => x.Id == processMapPlanWellDrillings.IdWellSectionType).Caption;
row.Cell(columnMode).Value = GetModeCaption(processMapPlanWellDrillings.IdMode);
row.Cell(columnDepthStart).Value = processMapPlanWellDrillings.DepthStart;
row.Cell(columnDepthEnd).Value = processMapPlanWellDrillings.DepthEnd;
row.Cell(columnPressurePlan).Value = processMapPlanWellDrillings.Pressure.Plan;
row.Cell(columnPressureLimitMax).Value = processMapPlanWellDrillings.Pressure.LimitMax;
row.Cell(columnAxialLoadPlan).Value = processMapPlanWellDrillings.AxialLoad.Plan;
row.Cell(columnAxialLoadLimitMax).Value = processMapPlanWellDrillings.AxialLoad.LimitMax;
row.Cell(columnTopDriveTorquePlan).Value = processMapPlanWellDrillings.TopDriveTorque.Plan;
row.Cell(columnTopDriveTorqueLimitMax).Value = processMapPlanWellDrillings.TopDriveTorque.LimitMax;
row.Cell(columnTopDriveSpeedPlan).Value = processMapPlanWellDrillings.TopDriveSpeed.Plan;
row.Cell(columnTopDriveSpeedLimitMax).Value = processMapPlanWellDrillings.TopDriveSpeed.LimitMax;
row.Cell(columnFlowPlan).Value = processMapPlanWellDrillings.Flow.Plan;
row.Cell(columnFlowLimitMax).Value = processMapPlanWellDrillings.Flow.LimitMax;
row.Cell(columnRopPlan).Value = processMapPlanWellDrillings.RopPlan;
row.Cell(columnUsageSaub).Value = processMapPlanWellDrillings.UsageSaub;
row.Cell(columnUsageSpin).Value = processMapPlanWellDrillings.UsageSpin;
row.Cell(columnComment).Value = processMapPlanWellDrillings.Comment;
}
private IEnumerable<WellDrillingProcessMapDto> ParseWorkBook(IXLWorkbook workbook)
private IEnumerable<ProcessMapPlanWellDrillingDto> ParseWorkBook(IXLWorkbook workbook)
{
var sheet = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNamePlan)
?? throw new FileFormatException($"Книга excel не содержит листа {sheetNamePlan}.");
@ -162,7 +164,7 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
return ParseSheet(sheet);
}
private IEnumerable<WellDrillingProcessMapDto> ParseSheet(IXLWorksheet sheet)
private IEnumerable<ProcessMapPlanWellDrillingDto> ParseSheet(IXLWorksheet sheet)
{
const int columnsCount = 17;
@ -172,9 +174,9 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
var rowsCount = sheet.RowsUsed().Count() - headerRowsCount;
if (rowsCount <= 0)
return Array.Empty<WellDrillingProcessMapDto>();
return Array.Empty<ProcessMapPlanWellDrillingDto>();
var processMapPlans = new WellDrillingProcessMapDto[rowsCount];
var processMapPlans = new ProcessMapPlanWellDrillingDto[rowsCount];
var parseErrors = new List<string>();
@ -198,7 +200,7 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
return processMapPlans;
}
private WellDrillingProcessMapDto ParseRow(IXLRow row)
private ProcessMapPlanWellDrillingDto ParseRow(IXLRow row)
{
var wellSectionTypeCaption = row.Cell(columnWellSectionType).GetCellValue<string>()?.Trim().ToLower();
var modeName = row.Cell(columnMode).GetCellValue<string>()?.Trim().ToLower();
@ -217,6 +219,7 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
var ropPlan = row.Cell(columnRopPlan).GetCellValue<double>();
var usageSaub = row.Cell(columnUsageSaub).GetCellValue<double>();
var usageSpin = row.Cell(columnUsageSpin).GetCellValue<double>();
var comment = row.Cell(columnComment).GetCellValue<string?>();
var wellSection = sections.FirstOrDefault(s => s.Caption.Trim().ToLower() == wellSectionTypeCaption)
?? throw new FileFormatException(
@ -320,18 +323,19 @@ public class WellDrillingProcessMapImportService : IProcessMapImportService
},
RopPlan = ropPlan,
UsageSaub = usageSaub,
UsageSpin = usageSpin
UsageSpin = usageSpin,
Comment = comment
};
}
private async Task<Stream> GenerateExcelFileStreamAsync(WellDrillingProcessMapDto[] wellDrillingProcessMaps,
private async Task<Stream> GenerateExcelFileStreamAsync(ProcessMapPlanWellDrillingDto[] processMapPlanWellDrillings,
CancellationToken cancellationToken)
{
using var excelTemplateStream = (await GetExcelTemplateStreamAsync(cancellationToken)).File;
using var workbook = new XLWorkbook(excelTemplateStream, XLEventTracking.Disabled);
AddToWorkbook(workbook, wellDrillingProcessMaps);
AddToWorkbook(workbook, processMapPlanWellDrillings);
MemoryStream memoryStream = new();
workbook.SaveAs(memoryStream, new SaveOptions { });