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 { 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; } /// /// 新加gps历史记录 /// /// /// public async Task AddGpsHistory(GpsHistoryDTO input) { var entity = input.Adapt(); var redisKey = RedisKeylist.GetBatchid(entity.DeviceId); ///如果redis中不存在该设备的批次号,则生成一个批次号 if (!await _redis.KeyExistsAsync(redisKey)) { int time = _config.GetValue("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 }); } } }