消防站文档解析
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.

145 lines
5.6 KiB

using Domain.Models.FireMen;
using Domain.Models.UnitEquipments;
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;
namespace BackGround.Hangfire.Jobs.UMEs
{
public class ImportUMEsJob : ITransientDependency
{
private readonly FireStationDbContext _context;
private readonly UMETableResolver _resolver;
private readonly IConverter _converter;
private readonly IWordParser _parser;
public ImportUMEsJob(FireStationDbContext context, IConverter converter, IWordParser parser, UMETableResolver resolver)
{
_context = context;
_converter = converter;
_parser = parser;
_resolver = resolver;
}
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;
var fileName = file.Path;
var errorMessages = new List<string>();
var waterQuery = _context.Water.AsQueryable();
var word = _parser.ReadDocument(fileName);
var parseResult = _parser.Parse(word, _resolver);
word.Close();
foreach (var dic in parseResult)
{
var fireMenInfos = (List<Dictionary<string, object>>)dic["队员情况"];
await SaveFireMen(fireMenInfos, unitId,uniacid);
var eqInfos = (List<Dictionary<string, object>>)dic["消防装备器材配备"];
await SaveEquipments(eqInfos, unitId, uniacid);
}
}
private async Task SaveFireMen(List<Dictionary<string, object>> fireMenInfos,int unitId, int uniacid)
{
foreach (var fireMenInfo in fireMenInfos)
{
var fireMen = new FireMen
{
Uniacid=uniacid,
Name = fireMenInfo["姓名"].ToString(),
Gender = fireMenInfo["性别"].ToString().Trim().Contains("男") ? Domain.Shared.GenderType.Male : Domain.Shared.GenderType.Female,
BirthDay = fireMenInfo["出生年月"].ToString(),
Job = fireMenInfo["站内职务"].ToString(),
PhoneNumber = fireMenInfo["联系电话"].ToString(),
UnitId=unitId,
UpdateTime=DateTime.Now
};
var existFireMen = await _context.FireMen.Where(f => f.Name == fireMen.Name && f.Gender == fireMen.Gender && f.PhoneNumber == fireMen.PhoneNumber).FirstOrDefaultAsync();
if (existFireMen == null)
{
await _context.FireMen.AddAsync(fireMen);
}
else
{
if (existFireMen.Job != fireMen.Job)
{ existFireMen.Job = fireMen.Job;
_context.FireMen.Update(existFireMen);
}
}
}
await _context.SaveChangesAsync();
}
private async Task SaveEquipments(List<Dictionary<string, object>> equipmentInfos,int unitId, int uniacid)
{
foreach(var equipmentInfo in equipmentInfos)
{
foreach(var dict in equipmentInfo)
{
if (!string.IsNullOrEmpty(dict.Key) && !string.IsNullOrWhiteSpace(dict.Key))
{
var unitEquipment = new UnitEquipment
{
Uniacid=uniacid,
Name = dict.Key,
Count = GetCount(dict.Value.ToString()),
UpdateTime=DateTime.Now,
UnitId=unitId
};
var existEquipment = await _context.UnitEquipments.Where(e => e.Name == unitEquipment.Name).FirstOrDefaultAsync();
if (existEquipment == null)
{
await _context.UnitEquipments.AddAsync(unitEquipment);
}
else
{
if (existEquipment.Count != unitEquipment.Count)
{
existEquipment.Count = unitEquipment.Count;
_context.UnitEquipments.Update(existEquipment);
}
}
}
}
await _context.SaveChangesAsync();
}
}
private int GetCount(string countStr)
{
var pattern = "\\d+";
var matchResult = Regex.Match(countStr, pattern);
if (matchResult.Success)
{
return int.Parse(matchResult.Value);
}
else
{
return 0;
}
}
}
}