using Infrastructure;
using Infrastructure.Cache;
using Infrastructure.CloudSdk.minio;
using Infrastructure.Helpers;
using Microsoft.DotNet.InternalAbstractions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.BasicQueryService;
using OpenAuth.App.Interface;
using OpenAuth.App.ServiceApp.Algo;
using OpenAuth.App.ServiceApp.Algo.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.ServiceApp
{
///
/// 消息推送模块
///
public class LasaPlatformPushApp : SqlSugarBaseApp
{
private readonly ILogger _logger;
public LasaPlatformPushApp(ILogger logger,ISugarUnitOfWork unitWork, ISimpleClient repository, IAuth auth): base(unitWork, repository, auth)
{
_logger = logger;
}
///
/// 获取推送平台
///
///
///
///
///
public async Task>>> GetPlatformList(int page, int limit, string key)
{
RefAsync totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var list = await db.LasaPlatform.AsQueryable()
.Where((a) => a.IsDelete == false)
.WhereIF(!string.IsNullOrEmpty(key), (a) => a.PlatformName.Contains(key))
.ToPageListAsync(page, limit, totalCount);
return new Response>>
{
Result = new PageInfo> { Items = list, Total = totalCount }
};
}
}
///
/// 添加平台信息
///
///
///
public async Task> AddPlatform(LasaPlatform info)
{
using (var db = UnitWork.CreateContext())
{
info.Id = Guid.NewGuid().ToString();
info.IsDelete = false;
await db.LasaPlatform.InsertAsync(info);
if (db.Commit())
return new Response
{
Result = true,
Message = "添加成功"
};
else
return new Response
{
Result = false,
Message = "添加失败"
};
}
}
//修改平台信息
public async Task> UpdatePlatform(LasaPlatform info)
{
using (var db = UnitWork.CreateContext())
{
// 编辑
await db.LasaPlatform.UpdateAsync(u => new LasaPlatform
{
Url = info.Url,
PlatformName = info.PlatformName,
RoutingKey = info.RoutingKey,
Exchange = info.Exchange
}, u => u.Id == info.Id);
// 提交事务
if (db.Commit())
{
return new Response
{
Result = true,
Message = "修改成功"
};
}
else
{
return new Response
{
Result = false,
Message = "修改失败"
};
}
}
}
//删除平台信息
public async Task> DeletePlatform(string id)
{
using (var db = UnitWork.CreateContext())
{
await db.LasaPlatform.DeleteByIdAsync(id);
if (db.Commit())
return new Response
{
Result = true,
Message = "删除成功"
};
else
return new Response
{
Result = false,
Message = "删除失败"
};
}
}
///
/// 添加ai算法任务信息
///
///
///
public async Task> AddTaskAi(LasaTaskAi info)
{
using (var db = UnitWork.CreateContext())
{
if (string.IsNullOrEmpty(info.Id))
info.Id = Guid.NewGuid().ToString();
await db.LasaTaskAi.InsertAsync(info);
if (db.Commit())
return new Response
{
Result = true,
Message = "添加成功"
};
else
return new Response
{
Result = false,
Message = "添加失败"
};
}
}
///
/// 添加ai算法推送log
///
///
///
public async Task> AddTaskAi(LasaTaskAiLog info)
{
using (var db = UnitWork.CreateContext())
{
if (string.IsNullOrEmpty(info.Id))
info.Id = Guid.NewGuid().ToString();
await db.LasaTaskAiLog.InsertAsync(info);
if (db.Commit())
return new Response
{
Result = true,
Message = "添加成功"
};
else
return new Response
{
Result = false,
Message = "添加失败"
};
}
}
///
/// 获取推送记录
///
///
///
///
///
public async Task>>> GetAiLogList(int page, int limit, string key)
{
RefAsync totalCount = 0;
using (var db = UnitWork.CreateContext())
{
var list = await db.LasaTaskAiLog.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(key), (a) => a.Data.Contains(key))
.ToPageListAsync(page, limit, totalCount);
return new Response>>
{
Result = new PageInfo> { Items = list, Total = totalCount }
};
}
}
public async Task> AddImgTopic(string payload)
{
using var db = UnitWork.CreateContext();
var aiinfo = JsonConvert.DeserializeObject(payload);
if (aiinfo == null)
{
return new Response
{
Result = true,
Message = "添加成功"
};
}
_logger.LogInformation("标签信息:{tag}", JsonConvert.SerializeObject(aiinfo.tag));
_logger.LogInformation("aiid:{aiid}", aiinfo.aiid);
var config = ConfigHelper.GetConfigRoot();
var imageBaseUrl = $"http://{config["Minio:Endpoint"]}/{config["Minio:BucketName"]}/";
var (lat, lng) = GetDroneLocation(aiinfo);
// 一次性加载模型标签
var modelDict = (await db.LasaModelLabel.AsQueryable().Where(r => r.PId == aiinfo.aiid).ToListAsync())
.ToDictionary(x => x.EnumValue, x => x.Name);
foreach (var tag in aiinfo.tag)
{
if (tag.confidence < 0.3)
continue;
var tagKey = tag.class_id.ToString();
var achievement = await db.LasaAiAchievement
.GetFirstAsync(r => r.TaskId == aiinfo.taskid && r.Tag == tagKey);
if (achievement == null)
{
await db.LasaAiAchievement.InsertAsync(new LasaAiAchievement
{
Id = Guid.NewGuid().ToString(),
CreateTime = DateTime.Now,
TaskId = aiinfo.taskid,
AlgoId = aiinfo.aiid,
AiModel = "yolo12x",
Tag = tag.class_id.ToString(),
Title = modelDict.TryGetValue(tag.class_id, out var name) ? name : string.Empty,
ConfidenceLevel = (float)Math.Round(tag.confidence, 2) * 100,
Cover = imageBaseUrl + aiinfo.path,
Lat = (float)lat,
Lng = (float)lng
});
}
await db.LasaAiAchievementDetail.InsertAsync(new LasaAiAchievementDetail
{
Id = Guid.NewGuid().ToString(),
AiAchievementId = achievement.Id,
Image = imageBaseUrl + aiinfo.path,
Lat = lat,
Lng = lng
});
}
if (db.Commit())
return new Response
{
Result = true,
Message = "添加成功"
};
else
return new Response
{
Result = false,
Message = "添加失败"
};
}
private static (double lat, double lng) GetDroneLocation(AiImgReq aiinfo)
{
if (aiinfo.drone_info?.data == null)
return (0.0, 0.0);
return (
aiinfo.drone_info.data.latitude ?? 0.0,
aiinfo.drone_info.data.longitude ?? 0.0
);
}
}
}