lg_backend/langguanApi/Service/GpsHistoryService.cs

56 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
});
}
}
}