You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.8 KiB
156 lines
5.8 KiB
using EntityFramework; |
|
using Infrastructure.Abstructions; |
|
using Infrastructure.Converts; |
|
using Infrastructure.Excel; |
|
using Microsoft.EntityFrameworkCore; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Text.RegularExpressions; |
|
using System.Threading.Tasks; |
|
|
|
namespace BackGround.Hangfire.Jobs.Waters |
|
{ |
|
public class ImportWaterJob : ITransientDependency, IImportWaterJob |
|
{ |
|
private readonly IParser _excelParser; |
|
private readonly FireStationDbContext _context; |
|
private readonly IConverter _converter; |
|
public ImportWaterJob(IParser parser, FireStationDbContext context, IConverter converter) |
|
{ |
|
_excelParser = parser; |
|
_context = context; |
|
_converter = converter; |
|
} |
|
public async Task Execute(Guid fileId,int categoryId) |
|
{ |
|
var file = await _context.UploadedFiles.Where(u => u.Id == fileId).FirstOrDefaultAsync(); |
|
if (file == null) |
|
{ |
|
throw new Exception($"没有找到id为{fileId}的文件"); |
|
} |
|
var fileUnit = await _context.FileUnits.FirstOrDefaultAsync(f => f.FileId == fileId); |
|
if (fileUnit == null) |
|
{ |
|
throw new Exception($"没有找到文件{fileId}的单位关联记录"); |
|
} |
|
var unitId = fileUnit.UnitId; |
|
int uniacid = fileUnit.Uniacid; |
|
int groupId = fileUnit.GroupId; |
|
var fileName = file.Path; |
|
var waterQuery = _context.Water.AsEnumerable(); |
|
var workBook = _excelParser.ReadExcel(fileName); |
|
var parseResult = _excelParser.ParseExcel<Application.Shared.Dtos.Waters.Imports.Water>(workBook); |
|
if (parseResult.Any()) |
|
{ |
|
var currentArea = string.Empty; |
|
foreach (var result in parseResult) |
|
{ |
|
try |
|
{ |
|
if (!string.IsNullOrEmpty(result.Area)) |
|
{ |
|
currentArea = result.Area; |
|
} |
|
var existWater = waterQuery.FirstOrDefault(w => w.LocationName == currentArea + result.Street + result.Position); |
|
if (existWater == null) |
|
{ |
|
|
|
var waterEntity = BuildWater(result, currentArea, uniacid, groupId,categoryId); |
|
await _context.Water.AddAsync(waterEntity); |
|
} |
|
else |
|
{ |
|
var water = BuildWater(result, currentArea, uniacid, groupId,categoryId); |
|
existWater.Longitude = water.Longitude; |
|
existWater.Latitude = water.Latitude; |
|
existWater.UpdateTime = water.UpdateTime; |
|
existWater.InstallDate = water.InstallDate; |
|
existWater.Status = water.Status; |
|
_context.Water.Update(existWater); |
|
} |
|
await _context.SaveChangesAsync(); |
|
} |
|
catch(Exception e) |
|
{ |
|
throw e; |
|
} |
|
|
|
} |
|
} |
|
} |
|
|
|
private Domain.Models.Waters.Water BuildWater(Application.Shared.Dtos.Waters.Imports.Water waterDto, string area, int uniacid, int groupId,int categoryId) |
|
{ |
|
var water = new Domain.Models.Waters.Water |
|
{ |
|
Uniacid=uniacid, |
|
GroupId=groupId, |
|
LocationName = area + waterDto.Street + waterDto.Position, |
|
CategoryId=categoryId, |
|
UpdateTime = DateTime.Now |
|
}; |
|
if (waterDto.Point != null) |
|
{ |
|
var pattern = "\\d+\\.?\\d*"; |
|
if (float.TryParse(waterDto.Point.EastLongitude,out var longitudevalue)) |
|
{ |
|
water.Longitude = longitudevalue; |
|
} |
|
else |
|
{ |
|
var match= Regex.Match(waterDto.Point.EastLongitude, pattern); |
|
if (match.Success) |
|
{ |
|
water.Longitude = float.Parse(match.Value); |
|
} |
|
else |
|
throw new Exception($"经度值{waterDto.Point?.EastLongitude}有误"); |
|
} |
|
|
|
if (float.TryParse(waterDto.Point.NorthLatitude, out var latitudeValue)) |
|
{ |
|
water.Latitude = latitudeValue; |
|
} |
|
else |
|
{ |
|
var match = Regex.Match(waterDto.Point.NorthLatitude, pattern); |
|
if (match.Success) |
|
{ |
|
water.Longitude = float.Parse(match.Value); |
|
} |
|
else |
|
|
|
throw new Exception($"纬度值{waterDto.Point?.EastLongitude}有误"); |
|
} |
|
} |
|
|
|
if (!string.IsNullOrEmpty(waterDto.ConstructionDate)) |
|
{ |
|
water.InstallDate = waterDto.ConstructionDate; |
|
|
|
} |
|
if (!string.IsNullOrEmpty(waterDto.WaterStatus)) |
|
{ |
|
if (waterDto.WaterStatus.Contains("不好用")) |
|
{ |
|
water.Status = 2; |
|
} |
|
else if (waterDto.WaterStatus.Contains("好用")) |
|
{ |
|
water.Status = 1; |
|
} |
|
else if (waterDto.WaterStatus.Contains("坏")) |
|
{ |
|
water.Status = 0; |
|
} |
|
else |
|
{ |
|
water.Status = 2; |
|
} |
|
} |
|
return water; |
|
} |
|
} |
|
}
|
|
|