Waste/Waste.Application/ResultInfos/ResultService.cs

221 lines
9.8 KiB
C#
Raw Normal View History

2021-05-27 16:58:40 +08:00
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Waste.Domain;
namespace Waste.Application
{
/// <summary>
/// 投放记录管理
/// </summary>
public class ResultService : BaseInfoService, IResultService, ITransient
{
private readonly ISqlSugarRepository<W_Result> repository;
private readonly SqlSugarClient dbClient;
private readonly ILoggerService _loggerService;
private readonly ISuZhouService _suZhouService;
public ResultService(ISqlSugarRepository<W_Result> sqlSugarRepository, ILoggerService loggerService, ISuZhouService suZhouService)
{
repository = sqlSugarRepository;
dbClient = repository.Context;
_loggerService = loggerService;
_suZhouService = suZhouService;
}
/// <summary>
/// 获取投放记录
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public async Task<PageParms<ResultList>> GetListAsync(QueryParams param)
{
RefAsync<int> totalnum = 0;
var temquery = dbClient.Queryable<W_Result>();
if (param.queryParam != null && param.queryParam.Count > 0)
{
List<IConditionalModel> conModels = new List<IConditionalModel>();
param.queryParam.ForEach(x =>
{
var val = x.Value.ToStr();
if (!string.IsNullOrEmpty(val))
{
if (x.Name.ToStr().ToLower() == "facecode")
{
temquery = temquery.Where(t => SqlFunc.Subqueryable<W_Device>().Where(e => e.Ecode == val && t.DeviceId == e.Id).Any());
}
else if (x.Name.ToStr().ToLower() == "name")
{
temquery = temquery.Where(t => SqlFunc.Subqueryable<W_Device>().Where(e => e.Name == val && t.DeviceId == e.Id).Any());
}
else
{
conModels.Add(new ConditionalModel()
{
FieldName = x.Name,
ConditionalType = (ConditionalType)x.Type,
FieldValue = val
});
}
}
});
if (conModels.Count > 0)
{
temquery = temquery.Where(conModels);
}
}
if (currentUser.AccountType != (int)AccountType.platform)
{
temquery = temquery.Where(x => x.BusinessId == currentUser.BusinessId);
}
string sorts = string.Format("{0} {1}", param.sort, param.order);
var query = await temquery.OrderBy(sorts)
.Select(x => new ResultList
{
Id = x.Id,
BusinessId = x.BusinessId,
DeviceId = x.DeviceId,
WasteType = x.WasteType,
Tare = x.Tare,
GrossWeight = x.GrossWeight,
NetWeight = x.NetWeight,
Registration = x.Registration,
CreateTime = x.CreateTime
})
.Mapper((it, cache) =>
{
var allbus = cache.Get(list =>
{
var ids = list.Where(e => e.BusinessId != Guid.Empty).Select(e => e.BusinessId).ToList();
return repository.Change<W_Business>().Context.Queryable<W_Business>().Where(e => ids.Contains(e.Id)).ToList();
});
it.BusinessName = allbus.FirstOrDefault(e => e.Id == it.BusinessId)?.Name;
var alldev = cache.Get(list =>
{
var ids = list.Where(e => e.DeviceId != Guid.Empty).Select(e => e.DeviceId).ToList();
return repository.Change<W_Device>().Context.Queryable<W_Device>().Where(e => ids.Contains(e.Id)).ToList();
});
var dev = alldev.FirstOrDefault(e => e.Id == it.DeviceId);
if (dev != null)
{
it.DeviceName = dev.Name;
it.DeviceFacEcode = dev.FacEcode;
it.DeviceEcode = dev.Ecode;
it.DeviceAddress = dev.Address;
}
})
.ToPageListAsync(param.offset, param.limit, totalnum);
return new PageParms<ResultList>
{
page = param.offset,
Items = query,
totalnum = totalnum,
limit = param.limit
};
}
/// <summary>
/// 增加测量记录
/// </summary>
/// <param name="myPackage"></param>
/// <returns></returns>
public async Task<ResultInfo> InsertResultAsync(MyPackage myPackage)
{
//查找设备
var device = await repository.Change<W_Device>().Context.Queryable<W_Device>().FirstAsync(x => myPackage.IMEI == x.Ecode);
_loggerService.AddLogger($"接收到的数据,参数:{myPackage.ToJson()}", 2);
if (device == null)
{
//记录日志
_loggerService.AddLogger($"设备未找到,参数:{myPackage.ToJson()}", 3);
return new ResultInfo(ResultState.FAIL, "设备未找到");
}
var devicedata = await repository.Change<W_DeviceData>().Context.Queryable<W_DeviceData>().FirstAsync(x => x.DeviceId == device.Id);
var resultid = IDGen.NextID();
DateTime time = DateTime.Now;
if (myPackage.IsHeart)
{
if (string.IsNullOrEmpty(myPackage.Longitude) || myPackage.Longitude.ToDecimal() == 0) myPackage.Longitude = devicedata != null ? devicedata.Longitude : "0";
if (string.IsNullOrEmpty(myPackage.Latitude) || myPackage.Latitude.ToDecimal() == 0) myPackage.Latitude = devicedata != null ? devicedata.Latitude : "0";
}
else
{
if (string.IsNullOrEmpty(myPackage.IMSI)) myPackage.IMSI = devicedata != null ? devicedata.IMSI : "";
if (string.IsNullOrEmpty(myPackage.IMEI)) myPackage.IMEI = devicedata != null ? devicedata.IMEI : "";
if (string.IsNullOrEmpty(myPackage.ICCID)) myPackage.ICCID = devicedata != null ? devicedata.ICCID : "";
if (!string.IsNullOrEmpty(myPackage.Time) && myPackage.Time.Length == 14)
{
time = $"{myPackage.Time.Substring(0, 4)}-{myPackage.Time.Substring(4, 2)}-{myPackage.Time.Substring(6, 2)} {myPackage.Time.Substring(8, 2)}:{myPackage.Time.Substring(10, 2)}:{myPackage.Time.Substring(12, 2)}".ToDate();
}
}
//检查设备是否为今天第一次上报
bool isfrist = false;
if (device.LastHeartTime.HasValue && device.LastHeartTime.Value.Date != DateTime.Now.Date)
{
isfrist = true;
}
//记录数据
await dbClient.Ado.UseStoredProcedure().ExecuteCommandAsync("Proc_InsertResult", new
{
deviceid = device.Id,
businessid = device.Businessid,
resultid = resultid,
imei = myPackage.IMEI,
iccid = myPackage.ICCID,
imsi = myPackage.IMSI,
time = time,
latitude = myPackage.Latitude,
longitude = myPackage.Longitude,
sign = myPackage.GSLQ,
city = myPackage.City,
area = myPackage.Area,
wastetype = myPackage.WasteType,
weigth = myPackage.Weight,
isheart = myPackage.IsHeart,
tare = device.Tare,
isfrist = isfrist
});
//上传垃圾数据
if (myPackage.IsWeight && myPackage.Weight.ToDecimal() >0)
{
var devicesecret = await repository.Change<W_SZDevice>().Context.Queryable<W_SZDevice>().FirstAsync(x => x.DeviceId == device.Id);
if (devicesecret != null)
{
int timestamp = GetTimestamp(time);
await _suZhouService.PostGarbagesAsync(new GarbagePltC2SDto
{
Weight = myPackage.Weight.ToDouble(),
secret = devicesecret.Secret,
secrethash = devicesecret.SecretHash,
ScanningTime = timestamp,
DStatus = 0,
deviceid = devicesecret.DevId,
Trash = "202101",
Type = TrashType(myPackage.WasteType)
});
}
}
return new ResultInfo(ResultState.SUCCESS, "success");
}
private static int GetTimestamp(DateTime time)
{
DateTime dateTimeStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1, 8, 0, 0));
int timestamp = Convert.ToInt32((time - dateTimeStart).TotalSeconds);
return timestamp;
}
private int TrashType(string type)
{
if (type == "厨余垃圾") return 1;
else if (type == "可回收物") return 2;
else if (type == "有害垃圾") return 3;
else if (type == "其他垃圾") return 4;
else return 0;
}
}
}