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.
201 lines
8.0 KiB
201 lines
8.0 KiB
using BackGround.Hangfire.Jobs.Units; |
|
using Domain.Models.UnitEquipments; |
|
using Domain.Models.Waters; |
|
using EntityFramework; |
|
using Infrastructure.Abstructions; |
|
using Infrastructure.Converts; |
|
using Infrastructure.Word; |
|
using Microsoft.EntityFrameworkCore; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Text.RegularExpressions; |
|
using System.Threading.Tasks; |
|
using static Infrastructure.Converts.Converter; |
|
|
|
namespace BackGround.Hangfire.Jobs.FireHydrant |
|
{ |
|
public class ImportFireHydrantsJob:ITransientDependency, IImportFireHydrantsJob |
|
{ |
|
private readonly FireHydrantTableResolver _fireHydrantTableResolver; |
|
private readonly FireStationDbContext _context; |
|
private readonly IConverter _converter; |
|
private readonly IWordParser _parser; |
|
public ImportFireHydrantsJob(FireHydrantTableResolver fireHydrantTableResolver, FireStationDbContext context, IConverter converter, IWordParser parser) |
|
{ |
|
_fireHydrantTableResolver = fireHydrantTableResolver; |
|
_context = context; |
|
_converter = converter; |
|
_parser = parser; |
|
} |
|
public async Task Execute(Guid fileId) |
|
{ |
|
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 errorMessages = new List<string>(); |
|
var waterQuery = _context.Water.AsQueryable(); |
|
var word = _parser.ReadDocument(fileName); |
|
var parseResult= _parser.Parse(word, _fireHydrantTableResolver); |
|
word.Close(); |
|
foreach (var dict in parseResult) |
|
{ |
|
try |
|
{ |
|
var water = BuildWater(dict,uniacid,groupId); |
|
var existWater = waterQuery.FirstOrDefault(w => w.LocationName == water.LocationName); |
|
if (existWater == null) |
|
{ |
|
await _context.Water.AddAsync(water); |
|
} |
|
else |
|
{ |
|
existWater.Type = water.Type; |
|
existWater.Code = water.Code; |
|
existWater.GuanwangDiam = water.GuanwangDiam; |
|
existWater.GuanwangPressure = water.GuanwangPressure; |
|
existWater.GuanangType = water.GuanangType; |
|
existWater.SupplyCompany = water.SupplyCompany; |
|
existWater.PrincipalName = water.SupplyCompany; |
|
existWater.PrincipalPhoneNum = water.PrincipalPhoneNum; |
|
existWater.InstallDate = water.InstallDate; |
|
existWater.UpdateTime = water.UpdateTime; |
|
existWater.CancelDate = water.CancelDate; |
|
existWater.UpdateTime = DateTime.Now; |
|
} |
|
await _context.SaveChangesAsync(); |
|
} |
|
catch(Exception e) |
|
{ |
|
if (!errorMessages.Contains(e.Message)) |
|
errorMessages.Add(e.Message); |
|
} |
|
|
|
} |
|
if (errorMessages.Any()) |
|
{ |
|
var message = string.Join(';', errorMessages); |
|
throw new Exception(message); |
|
} |
|
} |
|
|
|
private Water BuildWater(Dictionary<string,object> dictionary, int uniacid, int groupId) |
|
{ |
|
var water = new Water |
|
{ |
|
Uniacid=uniacid, |
|
GroupId=groupId, |
|
LocationName = dictionary["位置"].ToString(), |
|
CategoryId = 1, |
|
Type = (short)(dictionary["形式(地上、地下)"].ToString().Trim() == "地上" ? 0 : 1), |
|
Code = dictionary["消火栓编号"].ToString(), |
|
Status = 1, |
|
Latitude = 0, |
|
Longitude = 0, |
|
GuanangType = (short)(dictionary["管网形式(环状、枝状)"].ToString().Contains("环") ? 0 : 1), |
|
SupplyCompany = dictionary["供水单位"].ToString(), |
|
PrincipalName = dictionary["负责人"].ToString(), |
|
PrincipalPhoneNum = dictionary["电话"].ToString(), |
|
InstallDate = dictionary["安装日期"].ToString(), |
|
ChangeDate = dictionary["变更日期"].ToString(), |
|
CancelDate = dictionary["注销日期"].ToString(), |
|
GuanwangDiam = (float)ConvertLength(dictionary["管网直径"].ToString()), |
|
GuanwangPressure = (float)ConvertPresure(dictionary["管网压力"].ToString()), |
|
UpdateTime=DateTime.Now, |
|
|
|
}; |
|
return water; |
|
|
|
} |
|
|
|
private double ConvertLength(string length) |
|
{ |
|
var pattern = "\\d+\\.?\\d*"; |
|
var match= Regex.Match(length, pattern); |
|
if (match.Success) |
|
{ |
|
var value = double.Parse(match.Value); |
|
var unitStr = length.Replace(value.ToString(), "").ToLower(); |
|
LengthUnits unit; |
|
if (unitStr == LengthUnits.mm.ToString()) |
|
{ |
|
unit = LengthUnits.mm; |
|
} |
|
else if (unitStr == LengthUnits.cm.ToString()) |
|
{ |
|
unit = LengthUnits.cm; |
|
} |
|
else if (unitStr == LengthUnits.dm.ToString()) |
|
{ |
|
unit = LengthUnits.dm; |
|
} |
|
else if (unitStr == LengthUnits.m.ToString()) |
|
{ |
|
unit = LengthUnits.m; |
|
} |
|
else if (unitStr == LengthUnits.km.ToString()) |
|
{ |
|
unit = LengthUnits.km; |
|
} |
|
else |
|
{ |
|
throw new Exception($"错误的长度值{length}"); |
|
} |
|
return _converter.ConvertLength(value, unit, LengthUnits.mm); |
|
} |
|
else |
|
{ |
|
throw new Exception($"错误的长度值{length}"); |
|
} |
|
} |
|
private double ConvertPresure(string presure) |
|
{ |
|
var pattern = "\\d+\\.?\\d*"; |
|
var match = Regex.Match(presure, pattern); |
|
if (match.Success) |
|
{ |
|
var value = double.Parse(match.Value); |
|
var unit = presure.Replace(value.ToString(), "").ToLower(); |
|
PresureUnits presureUnit ; |
|
if (unit == PresureUnits.pa.ToString()) |
|
{ |
|
presureUnit = PresureUnits.pa; |
|
} |
|
else if(unit == PresureUnits.hpa.ToString()) |
|
{ |
|
presureUnit = PresureUnits.hpa; |
|
} |
|
else if (unit == PresureUnits.kpa.ToString()) |
|
{ |
|
presureUnit = PresureUnits.kpa; |
|
} |
|
else if (unit == PresureUnits.mpa.ToString()) |
|
{ |
|
presureUnit = PresureUnits.mpa; |
|
} |
|
else |
|
{ |
|
throw new Exception($"压力值非法{presure}"); |
|
} |
|
var presureValue = _converter.ConvertPresure(value, presureUnit, PresureUnits.pa); |
|
return presureValue; |
|
} |
|
else |
|
{ |
|
throw new Exception($"压力值非法{presure}"); |
|
} |
|
} |
|
} |
|
}
|
|
|