LASAPlatform/OpenAuth.App/ServiceApp/AirportMaintenanceApp.cs

552 lines
22 KiB
C#
Raw Normal View History

2025-06-12 14:33:37 +08:00
using OpenAuth.App.BaseApp.Base;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.Interface;
using SqlSugar;
using Infrastructure;
using OpenAuth.App.ServiceApp.Response;
2025-06-17 13:53:54 +08:00
using DocumentFormat.OpenXml.EMMA;
2025-06-27 15:49:25 +08:00
using NPOI.SS.Formula.Functions;
using Infrastructure.Extensions;
using DocumentFormat.OpenXml.Math;
2025-07-12 15:07:17 +08:00
using Microsoft.Extensions.Configuration;
2025-07-15 09:19:05 +08:00
using Infrastructure.CloudSdk.minio;
2025-07-15 09:49:30 +08:00
using Microsoft.AspNetCore.Http;
2025-06-12 14:33:37 +08:00
namespace OpenAuth.App.ServiceApp
{
public class AirportMaintenanceApp : SqlSugarBaseApp<LasaDronePort, SugarDbContext>
{
2025-07-12 15:07:17 +08:00
private readonly IConfiguration _configuration;
2025-07-15 09:19:05 +08:00
private readonly ISqlSugarClient _client;
private readonly MinioService _minioService;
2025-08-04 11:10:50 +08:00
public AirportMaintenanceApp(MinioService minioService, ISqlSugarClient client,
ISugarUnitOfWork<SugarDbContext> unitWork, IConfiguration configuration,
ISimpleClient<LasaDronePort> repository, IAuth auth) : base(unitWork, repository, auth)
2025-06-12 14:33:37 +08:00
{
2025-07-12 15:07:17 +08:00
_configuration = configuration;
2025-07-15 09:19:05 +08:00
_client = client;
_minioService = minioService;
2025-06-12 14:33:37 +08:00
}
2025-08-04 11:10:50 +08:00
2025-06-12 14:33:37 +08:00
//获取设备绑定码
public async Task<Response<LasaDeviceBindingCode>> GetDeviceBindingCode()
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var info = await db.LasaDeviceBindingCode.AsQueryable().Where(r => r.BindStatus == 0).FirstAsync();
if (info != null)
{
return new Response<LasaDeviceBindingCode>
{
Code = 200,
Message = "获取设备绑定码成功",
Result = info
};
}
else
{
//如果设备绑定码不存在,则创建一个新的设备绑定码
var newBindingCode = new LasaDeviceBindingCode
{
Id = Guid.NewGuid().ToString(),
DeviceBindingCode = Guid.NewGuid().ToString("N").Substring(0, 8), // 生成一个新的绑定码
OrgId = "371300", // 默认组织ID
2025-10-20 14:46:44 +08:00
OrgName = "sdhc", // 默认组织名称
2025-06-12 14:33:37 +08:00
BindStatus = 0 // 未绑定状态
};
await db.LasaDeviceBindingCode.InsertAsync(newBindingCode);
if (db.Commit())
{
return new Response<LasaDeviceBindingCode>
{
Code = 200,
Message = "获取设备绑定码成功",
Result = newBindingCode
};
}
else
{
return new Response<LasaDeviceBindingCode>
{
Code = 500,
Message = "获取设备绑定码失败",
};
}
}
}
}
2025-08-04 11:10:50 +08:00
2025-06-17 13:53:54 +08:00
/// <summary>
/// 修改注册码状态
/// </summary>
/// <param name="DeviceBindingCode"></param>
/// <returns></returns>
public async Task<bool> UpdateCodeStatus(string DeviceBindingCode)
{
using (var db = UnitWork.CreateContext())
{
var flag = await db.LasaDeviceBindingCode.UpdateAsync(it => new LasaDeviceBindingCode()
{
BindStatus = 1,
}, it => it.DeviceBindingCode == DeviceBindingCode);
if (db.Commit())
return true;
else
return false;
}
2025-06-18 15:44:13 +08:00
}
//使用网关
public async Task<Response<LasaGateway>> GetGateway()
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var info = await db.LasaGateway.AsQueryable().Where(r => r.BindStatus == 0).FirstAsync();
if (info != null)
{
return new Response<LasaGateway>
{
Code = 200,
Message = "获取设备绑定码成功",
Result = info
};
}
else
{
2025-07-12 15:07:17 +08:00
var serverIp = _configuration.GetSection("MQTT:Server").Value;
var port = _configuration.GetSection("MQTT:Port").Value;
var username = _configuration.GetSection("MQTT:UserName").Value;
var password = _configuration.GetSection("MQTT:Password").Value;
2025-06-18 15:44:13 +08:00
//如果网关不存在,则创建一个新的
var newGateway = new LasaGateway
{
Id = Guid.NewGuid().ToString(),
CreateTime = DateTime.Now,
2025-07-12 15:07:17 +08:00
GatewayAccount = username,
2025-06-18 15:44:13 +08:00
GatewaySn = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(), // 生成一个新的
2025-07-12 15:07:17 +08:00
MqttGateway = serverIp + ":" + port,
MqttPassword = password,
2025-06-18 15:44:13 +08:00
OrgId = "371300", // 默认组织ID
BindStatus = 0 // 未绑定状态
};
await db.LasaGateway.InsertAsync(newGateway);
if (db.Commit())
{
return new Response<LasaGateway>
{
Code = 200,
Message = "获取设备绑定码成功",
Result = newGateway
};
}
else
{
return new Response<LasaGateway>
{
Code = 500,
Message = "获取设备绑定码失败",
};
}
}
}
}
2025-08-04 11:10:50 +08:00
2025-06-24 10:47:51 +08:00
public async Task<bool> UpdateGateway(string gateway, string did)
2025-06-18 15:44:13 +08:00
{
using (var db = UnitWork.CreateContext())
{
var flag = await db.LasaGateway.UpdateAsync(it => new LasaGateway()
{
BindStatus = 1,
Did = did,
UpdateTime = DateTime.Now,
}, it => it.GatewaySn == gateway);
if (db.Commit())
return true;
else
return false;
}
2025-06-17 13:53:54 +08:00
}
2025-08-05 11:19:30 +08:00
/// <summary>
/// 分页获取所有数据
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public async Task<Response<PageInfo<List<LasaGateway>>>> GetPageList(int page, int limit)
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
2025-09-06 08:53:28 +08:00
var list = await db.LasaGateway.AsQueryable()
2025-08-05 11:19:30 +08:00
.ToPageListAsync(page, limit, totalCount);
return new Response<PageInfo<List<LasaGateway>>>
{
Result = new PageInfo<List<LasaGateway>> { Items = list, Total = totalCount }
};
}
}
2025-06-19 14:47:46 +08:00
//获取网关
public async Task<List<string>> GetGatewaysnList()
{
using (var db = UnitWork.CreateContext())
{
2025-06-19 15:24:03 +08:00
var info = await db.LasaGateway.AsQueryable().Select(r => r.GatewaySn).ToListAsync();
2025-06-19 14:47:46 +08:00
if (info != null)
{
return info;
}
else
{
2025-06-24 10:47:51 +08:00
return new List<string>();
2025-07-04 09:57:44 +08:00
}
}
}
2025-08-04 11:10:50 +08:00
2025-07-04 09:57:44 +08:00
public async Task<List<string>> GetUavSn()
{
using (var db = UnitWork.CreateContext())
{
2025-08-04 11:10:50 +08:00
var uavinfo = await db.LasaUav.AsQueryable().Where(r => r.IsDelete == false).Select(r => r.Sn)
.ToListAsync();
var dockinfo = await db.LasaDronePort.AsQueryable().Where(r => r.IsDelete == false).Select(r => r.Sn)
.ToListAsync();
2025-07-04 14:53:20 +08:00
2025-07-04 09:57:44 +08:00
if (dockinfo != null)
{
return dockinfo.Union(uavinfo).ToList();
}
else
{
return new List<string>();
2025-06-19 14:47:46 +08:00
}
}
}
2025-06-24 10:47:51 +08:00
#region 固件版本管理
2025-08-04 11:10:50 +08:00
2025-06-24 10:47:51 +08:00
public async Task<Response<bool>> AddFirmware(LasaFirmware info)
{
using (var db = UnitWork.CreateContext())
{
2025-07-15 09:49:30 +08:00
info.Id = Guid.NewGuid().ToString();
2025-06-24 10:47:51 +08:00
var flag = await db.LasaFirmware.InsertAsync(info);
if (db.Commit())
return new Response<bool>
{
Result = true,
Message = "添加成功"
};
else
return new Response<bool>
{
Result = false,
Message = "添加失败"
};
}
}
2025-08-04 11:10:50 +08:00
2025-07-15 09:49:30 +08:00
//修改无人机或机场版本
public async Task<Response<bool>> UpdateFirmware(string id, string version, int type)
{
using (var db = UnitWork.CreateContext())
{
if (type == 1)
{
var flag = await db.LasaDronePort.UpdateAsync(it => new LasaDronePort()
{
FirmwareVersion = version,
}, it => it.Id == id);
if (db.Commit())
return new Response<bool> { Result = true, Message = "编辑成功" };
else
return new Response<bool> { Result = false, Message = "编辑失败" };
}
else
{
var flag = await db.LasaUav.UpdateAsync(it => new LasaUav()
{
FirmwareVersion = version,
}, it => it.Id == id);
if (db.Commit())
return new Response<bool> { Result = true, Message = "编辑成功" };
else
return new Response<bool> { Result = false, Message = "编辑失败" };
}
}
}
2025-08-04 11:10:50 +08:00
2025-07-15 09:49:30 +08:00
public Task<string> UploadFile(IFormFile xmlFile)
{
return _minioService.UploadFile(xmlFile, "firmware");
}
2025-08-04 11:10:50 +08:00
2025-06-24 10:47:51 +08:00
#endregion
2025-06-17 13:53:54 +08:00
#region 健康报警
2025-08-04 11:10:50 +08:00
2025-06-18 15:44:13 +08:00
/// <summary>
/// 添加健康报警
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
2025-06-27 15:49:25 +08:00
public bool AddManageDeviceHms(List<LasaManageDeviceHms> newAlarms)
2025-06-17 13:53:54 +08:00
{
using (var db = UnitWork.CreateContext())
{
2025-06-27 15:49:25 +08:00
var now = DateTime.Now;
//获取当前所有未处理的告警信息
var existing = db.LasaManageDeviceHms.AsQueryable()
2025-08-04 11:10:50 +08:00
.Where(x => x.IsResolved == 0).ToList();
2025-06-27 15:49:25 +08:00
var currentKeys = newAlarms.Select(x => x.Code).ToHashSet();
var existingKeys = existing.Select(x => x.Code).ToHashSet();
// 需要解除的(老有,新没有) 起的隆冬强
var toResolve = existing
.Where(x => !currentKeys.Contains(x.Code)).ToList();
foreach (var resolved in toResolve)
{
resolved.IsResolved = 1;
resolved.UpdateTime = now;
}
2025-08-04 11:10:50 +08:00
2025-06-27 15:49:25 +08:00
db.LasaManageDeviceHms.UpdateRange(toResolve);
// 需要新增的(新有,老没有)
var toInsert = newAlarms
.Where(x => !existingKeys.Contains(x.Code))
.ToList();
if (toInsert.Count > 0)
db.LasaManageDeviceHms.InsertRange(toInsert);
//var flag = db.LasaManageDeviceHms.InsertRange(newAlarms);
2025-06-17 13:53:54 +08:00
if (db.Commit())
return true;
else
return false;
}
}
2025-08-04 11:10:50 +08:00
2025-06-27 15:49:25 +08:00
//根据时间获取告警信息列表
2025-08-04 11:10:50 +08:00
public async Task<Response<PageInfo<List<LasaManageDeviceHms>>>> GetManageDeviceHmsList(int level, int model,
DateTime startTime, DateTime endTime, int page, int limit, string message, string getway)
2025-06-27 15:49:25 +08:00
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var list = await db.LasaManageDeviceHms.AsQueryable()
2025-07-22 15:02:15 +08:00
.Where(x => x.CreateTime >= startTime && x.CreateTime <= endTime)
2025-06-27 15:54:31 +08:00
.WhereIF(level != 0, x => x.Level == level)
2025-06-27 15:49:25 +08:00
.WhereIF(model != 0, x => x.Module == model)
2025-06-27 15:54:31 +08:00
.WhereIF(!string.IsNullOrEmpty(message), x => x.MessageZh.Contains(message))
2025-07-22 15:25:05 +08:00
.WhereIF(!string.IsNullOrEmpty(getway), x => x.Sn == getway)
2025-06-27 15:49:25 +08:00
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageListAsync(page, limit, totalCount);
return new Response<PageInfo<List<LasaManageDeviceHms>>>
{
Result = new PageInfo<List<LasaManageDeviceHms>> { Items = list, Total = totalCount }
};
}
}
2025-08-04 11:10:50 +08:00
2025-06-17 13:53:54 +08:00
#endregion
2025-06-18 15:44:13 +08:00
#region 日志
2025-08-04 11:10:50 +08:00
2025-06-18 15:44:13 +08:00
/// <summary>
/// 添加日志
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
2025-09-06 08:53:28 +08:00
public async Task<bool> AddLog(LasaLog info)
2025-06-18 15:44:13 +08:00
{
using (var db = UnitWork.CreateContext())
{
2025-09-06 08:53:28 +08:00
var flag = await db.LasaLog.InsertAsync(info);
2025-06-18 15:44:13 +08:00
if (db.Commit())
return true;
else
return false;
}
}
2025-08-04 11:10:50 +08:00
2025-07-04 14:53:20 +08:00
/// <summary>
/// 获取日志
/// </summary>
/// <param name="sn"></param>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <param name="page"></param>
/// <param name="limit"></param>
/// <returns></returns>
2025-08-04 11:10:50 +08:00
public async Task<Response<PageInfo<List<LasaLog>>>> GetLogList(string sn, DateTime startTime, DateTime endTime,
int page, int limit)
2025-07-04 14:53:20 +08:00
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var topic = $"thing/product/{sn}/osd";
var list = await db.LasaLog.AsQueryable()
.Where(x => x.CreateTime >= startTime && x.CreateTime <= endTime)
.Where(x => x.Topic == topic)
2025-07-12 15:07:17 +08:00
.OrderBy(x => x.CreateTime, OrderByType.Asc)
2025-07-04 14:53:20 +08:00
.ToPageListAsync(page, limit, totalCount);
return new Response<PageInfo<List<LasaLog>>>
{
Result = new PageInfo<List<LasaLog>> { Items = list, Total = totalCount }
};
}
}
2025-11-28 13:39:02 +08:00
/// <summary>
/// 添加无人机操作日志
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public async Task<bool> AddOperationLog(LasaControlOperation info)
{
using (var db = UnitWork.CreateContext())
{
var flag = await db.LasaControlOperation.InsertAsync(info);
if (db.Commit())
return true;
else
return false;
}
}
/// <summary>
/// 获取无人机操作日志
/// </summary>
/// <param name="sn"></param>
/// <param name="page"></param>
/// <param name="limit"></param>
/// <returns></returns>
public async Task<Response<PageInfo<List<LasaControlOperation>>>> GetOperationLogList(string taskid, int page, int limit)
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var list = await db.LasaControlOperation.AsQueryable()
.Where(x => x.TaskId == taskid)
.OrderBy(x => x.CreateTime, OrderByType.Asc)
.ToPageListAsync(page, limit, totalCount);
return new Response<PageInfo<List<LasaControlOperation>>>
{
Result = new PageInfo<List<LasaControlOperation>> { Items = list, Total = totalCount }
};
}
}
2025-06-18 15:44:13 +08:00
#endregion
2025-08-04 11:10:50 +08:00
public async Task<Response<PageInfo<List<LasaMediaFile>>>> GetMediaFile(string flightId,string taskId, string airId, string device,
2025-09-06 08:53:28 +08:00
int? type, string picname, DateTime? startTime, DateTime? endTime, int page, int limit, string parentKey, int? objectKeyExist)
2025-07-10 14:04:47 +08:00
{
RefAsync<int> totalCount = 0;
using (var db = UnitWork.CreateContext())
{
Console.WriteLine(startTime.ToString());
var list = await db.LasaMediaFile.AsQueryable()
2025-08-04 11:10:50 +08:00
.LeftJoin<LasaTask>((x, b) => x.FlightId == b.FlightId)
2025-09-29 09:20:30 +08:00
.WhereIF(!string.IsNullOrEmpty(flightId), (x, b) => x.FlightId == flightId)
.WhereIF(!string.IsNullOrEmpty(device), (x, b) => x.DroneModelKey == device)
.WhereIF(!string.IsNullOrEmpty(picname), (x, b) => x.Name.Contains(picname))
.WhereIF(!string.IsNullOrEmpty(airId), (x, b) => b.AirLineId == airId)
2025-09-06 08:53:28 +08:00
.WhereIF(objectKeyExist is 0, (x, b) => x.ObjectKey == null)
.WhereIF(objectKeyExist is 1, (x, b) => x.ObjectKey != null)
.WhereIF(!"0001/1/1 0:00:00".Equal(startTime.ToString()), (x, b) => x.CreateTime >= startTime)
.WhereIF(!"0001/1/1 0:00:00".Equal(endTime.ToString()), (x, b) => x.CreateTime <= endTime)
.WhereIF(!string.IsNullOrEmpty(parentKey), (x, b) => x.ParentKey == parentKey)
.WhereIF(!string.IsNullOrEmpty(taskId), (x, b) => x.TaskId == taskId)
2025-08-15 13:49:54 +08:00
.WhereIF(type != null, (x, b) => x.Type == type)
.OrderBy((x, b) => x.CreateTime, OrderByType.Desc)
2025-09-06 08:53:28 +08:00
.Select((x, b) => new LasaMediaFile
2025-08-04 11:10:50 +08:00
{
Id = x.Id,
Name = x.Name,
ObjectKey = x.ObjectKey,
Path = x.Path,
CreateTime = x.CreateTime,
TaskId = x.TaskId,
Level = x.Level,
ParentKey = x.ParentKey,
WorkspaceId = x.WorkspaceId,
display = x.display,
ShowOnMap = x.ShowOnMap,
GraffitiJson = x.GraffitiJson,
FileTags = x.FileTags,
Size = x.Size,
Width = x.Width,
Height = x.Height,
2025-08-04 13:45:58 +08:00
Lat = x.Lat,
Lng = x.Lng,
AbsoluteAltitude = x.AbsoluteAltitude,
2025-08-04 11:10:50 +08:00
Tid = x.Tid,
Bid = x.Bid,
FlightType = x.FlightType,
minipic = x.minipic,
CreateUserName = b.CreateUserName,
TaskName = b.TaskName,
AirLineName = b.TaskAirLineName,
2025-08-04 13:56:55 +08:00
GimbalYawDegree = x.GimbalYawDegree,
RelativeAltitude = x.RelativeAltitude,
2025-08-15 13:48:29 +08:00
PayloadModelName = x.PayloadModelName,
Type = x.Type
2025-08-04 11:10:50 +08:00
})
2025-07-10 14:04:47 +08:00
.ToPageListAsync(page, limit, totalCount);
return new Response<PageInfo<List<LasaMediaFile>>>
{
Result = new PageInfo<List<LasaMediaFile>> { Items = list, Total = totalCount }
};
}
}
2025-07-15 09:19:05 +08:00
2025-08-04 11:10:50 +08:00
public async Task<Response<string>> UpdatePicStatus(string id, int showOnMap, int display, string fileTags,
string graffitiJson)
2025-07-15 09:19:05 +08:00
{
2025-08-04 11:10:50 +08:00
string sql = "update lasa_mediafile set \"ShowOnMap\"=" + showOnMap + ",display=" + display +
",\"FileTags\"='" + fileTags + "',\"GraffitiJson\"='" + graffitiJson + "' where \"Id\"='" +
id + "'";
2025-07-15 09:19:05 +08:00
await _client.Ado.ExecuteCommandAsync(sql);
return new Response<string> { Result = "修改成功!" };
}
public async Task<Response<string>> deletepic(string ids)
{
string[] idarray = ids.Split(",");
2025-07-22 15:25:05 +08:00
2025-07-15 09:19:05 +08:00
for (int i = 0; i < idarray.Length; i++)
{
2025-07-22 15:25:05 +08:00
string sqlselect = "select \"ObjectKey\" from lasa_mediafile where \"Id\"='" + idarray[i] + "'";
2025-07-15 09:19:05 +08:00
string objectkey = _client.Ado.SqlQuerySingle<string>(sqlselect);
2025-07-22 15:25:05 +08:00
string sql = "delete from lasa_mediafile where \"Id\"='" + idarray[i] + "'";
2025-07-15 09:19:05 +08:00
await _client.Ado.ExecuteCommandAsync(sql);
await _minioService.DeleteFile(objectkey);
}
2025-08-04 11:10:50 +08:00
2025-07-15 09:19:05 +08:00
return new Response<string> { Result = "删除成功!" };
}
2025-07-16 14:12:22 +08:00
public async Task<Response<string>> UpdatePicName(string id, string name)
{
string sql = "update lasa_mediafile set \"Name\"='" + name + "' where \"Id\"='" + id + "'";
await _client.Ado.ExecuteCommandAsync(sql);
return new Response<string> { Result = "修改成功!" };
}
public async Task<Response<string>> UpdatePicParentKey(string id, string ParentKey)
{
2025-07-18 13:45:32 +08:00
string sql = "update lasa_mediafile set \"ParentKey\"='" + ParentKey + "' where \"Id\"='" + id + "'";
2025-07-16 14:12:22 +08:00
await _client.Ado.ExecuteCommandAsync(sql);
return new Response<string> { Result = "修改成功!" };
}
2025-06-12 14:33:37 +08:00
}
2025-08-04 11:10:50 +08:00
}