lg_backend/langguanApi/Service/GpsHistoryService.cs

56 lines
2.0 KiB
C#
Raw Permalink Normal View History

2024-09-01 10:11:10 +00:00
using langguanApi.Common.Redis;
using langguanApi.Common.WebSocket;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model.Entity;
using Mapster;
using StackExchange.Redis;
namespace langguanApi.Service
{
[ServiceInjection(InjectionType.Transient)]
public class GpsHistoryService : BaseService<GpsHistory>
{
private readonly PushService _pushService;
private IDatabase _redis;
private IConfiguration _config;
public GpsHistoryService(IConfiguration config, PushService pushService,
RedisHelper redisHelper) : base(config, nameof(Model.Entity.GpsHistory))
{
_pushService = pushService;
_redis = redisHelper._database;
_config = config;
}
/// <summary>
/// 新加gps历史记录
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task AddGpsHistory(GpsHistoryDTO input)
{
var entity = input.Adapt<GpsHistory>();
var redisKey = RedisKeylist.GetBatchid(entity.DeviceId);
///如果redis中不存在该设备的批次号则生成一个批次号
if (!await _redis.KeyExistsAsync(redisKey))
{
int time = _config.GetValue<int>("BatchId");
long utc = DateTime.UtcNow.Ticks;
await _redis.StringSetAsync(redisKey, utc, TimeSpan.FromMinutes(time));
}
else
{
//如果存在了就把批次号延期30分钟
await _redis.KeyExpireAsync(redisKey, TimeSpan.FromMinutes(30));
}
entity.BatchId = (long)await _redis.StringGetAsync(redisKey);
await base.CreateAsync(entity);
await _pushService.SendMessageToAll(new
{
entity.Lat,
entity.DeviceId,
entity.Lon,
entity.BatchId
});
}
}
}