using Furion.DependencyInjection; using Furion.DistributedIDGenerator; using Furion.DynamicApiController; using Microsoft.AspNetCore.Mvc; using Nirvana.Common; using Nirvana.Common.ApiBase; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Waste.Domain; namespace Waste.Application.Device { /// /// 设备管理 /// public class DeviceService : BaseInfoService, IDeviceService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; private readonly IBusinessService _businessService; public DeviceService(ISqlSugarRepository sqlSugarRepository, IBusinessService businessService) { repository = sqlSugarRepository; dbClient = repository.Context; _businessService = businessService; } /// /// 设备批量操作 /// /// /// public async Task BatchSetAsync(DeviceBatchModel deviceBatchModel) { var emptyid = Guid.Empty; //设备所属商户列表 var busslist = await dbClient.Queryable().Where(x => deviceBatchModel.codes.Contains(x.Id) && x.Businessid != emptyid).Select(x => x.Businessid).ToListAsync(); if (deviceBatchModel.type == 1) { busslist.Add(deviceBatchModel.BusinessId); //如果是管理员分配 if (currentUser.AccountType == (int)AccountType.platform) { await dbClient.Updateable().SetColumns(x => new W_Device { Businessid = deviceBatchModel.BusinessId, ActiveTime = DateTime.Now, Status = (int)DeviceStatus.Run }).Where(x => deviceBatchModel.codes.Contains(x.Id)).ExecuteCommandAsync(); await _businessService.InsertOrUpdateRealDataAsync(); } else { await dbClient.Updateable().SetColumns(x => new W_Device { Businessid = deviceBatchModel.BusinessId }).Where(x => deviceBatchModel.codes.Contains(x.Id)).ExecuteCommandAsync(); busslist.Add(currentUser.BusinessId); } await _businessService.InsertOrUpdateRealDataAsync(busslist); return new ResultInfo(ResultState.SUCCESS, "分配成功"); } else { deviceBatchModel.BusinessId = currentUser.AccountType != (int)AccountType.platform ? currentUser.BusinessId : Guid.Empty; //如果是管理员回收 if (currentUser.AccountType == (int)AccountType.platform) { await dbClient.Updateable().SetColumns(x => new W_Device { Businessid = deviceBatchModel.BusinessId, ActiveTime = null, Status = (int)DeviceStatus.WaitActive }).Where(x => deviceBatchModel.codes.Contains(x.Id)).ExecuteCommandAsync(); await _businessService.InsertOrUpdateRealDataAsync(); } else { await dbClient.Updateable().SetColumns(x => new W_Device { Businessid = deviceBatchModel.BusinessId }).Where(x => deviceBatchModel.codes.Contains(x.Id)).ExecuteCommandAsync(); busslist.Add(currentUser.BusinessId); } await _businessService.InsertOrUpdateRealDataAsync(busslist); return new ResultInfo(ResultState.SUCCESS, "回收成功"); } } /// /// 详情 /// /// /// public async Task DetailAsync(Guid id) { var devicedata = await dbClient.Queryable().FirstAsync(x => x.Id == id); var pltdata = await repository.Change().Context.Queryable().FirstAsync(x => x.DeviceId == devicedata.Id); return new DeviceSubmit { DeviceType = devicedata.DeviceType, Secret = pltdata != null ? pltdata.Secret : "", SecretHash = pltdata != null ? pltdata.SecretHash : "", Address = devicedata.Address, Area = devicedata.Area, City = devicedata.City, DevId = pltdata != null ? pltdata.DevId : "", Ecode = devicedata.Ecode, FacEcode = devicedata.FacEcode, Name = devicedata.Name, NetType = devicedata.NetType, Province = devicedata.Province, Tare = devicedata.Tare, Remark = devicedata.Remark, Id = devicedata.Id }; } /// /// 设备列表 /// /// /// public async Task> GetListAsync(QueryParams param) { RefAsync totalnum = 0; var temquery = dbClient.Queryable(); if (param.queryParam != null && param.queryParam.Count > 0) { List conModels = new List(); param.queryParam.ForEach(x => { if (!string.IsNullOrEmpty(x.Value)) { conModels.Add(new ConditionalModel() { FieldName = x.Name, ConditionalType = (ConditionalType)x.Type, FieldValue = x.Value.Trim() }); } }); 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 DeviceList { Id = x.Id, Name = x.Name, Businessid = x.Businessid, Address = x.Address, Ecode = x.Ecode, FacEcode = x.FacEcode, CreateTime = x.CreateTime, DeviceType = x.DeviceType, LastHeartTime = x.LastHeartTime, NetType = x.NetType, Remark = x.Remark, Status = x.Status, InstallTime = x.InstallTime, ActiveTime = x.ActiveTime, Tare = x.Tare }) .Mapper((it, cache) => { var allbuss = cache.Get(list => { var ids = list.Where(e => e.Businessid != Guid.Empty).Select(e => e.Businessid).ToList(); return repository.Change().Context.Queryable().Where(e => ids.Contains(e.Id)).ToList(); }); var alldevicerealdata = cache.Get(list => { var ids = list.Select(e => e.Id).ToList(); return repository.Change().Context.Queryable().Where(e => ids.Contains(e.DeviceId)).ToList(); }); //判断网络是否在线 if (it.LastHeartTime.HasValue && it.LastHeartTime.Value.AddMinutes(16) >= DateTime.Now) { it.NetStatus = (int)DeviceNetStatus.OnLine; } var devicerealdata = alldevicerealdata.FirstOrDefault(e => e.DeviceId == it.Id && e.BusinessId == it.Businessid); if (devicerealdata != null) { it.TodayCount = devicerealdata.TodayCount; it.TodayWeight = devicerealdata.TodayWeigth; it.TotalCount = devicerealdata.TotalCount; it.TotalWeight = devicerealdata.TotalWeight; } var alldevicedata = cache.Get(list => { var ids = list.Select(e => e.Id).ToList(); return repository.Change().Context.Queryable().Where(e => ids.Contains(e.DeviceId)).ToList(); }); var devicedata = alldevicedata.FirstOrDefault(e => e.DeviceId == it.Id); if (devicedata != null) { it.ICCID = devicedata.ICCID; it.IMEI = devicedata.IMEI; it.IMSI = devicedata.IMSI; it.LastBeatTime = devicedata.LastBeatTime; //判断网络是否在线 if (devicedata.LastBeatTime.HasValue && devicedata.LastBeatTime.Value.AddMinutes(16) >= DateTime.Now) { it.NetStatus = (int)DeviceNetStatus.OnLine; } it.sign = (devicedata.Sign.ToDouble() / (6 * 1.0)).ToString("f1"); } it.BusinessName = allbuss.FirstOrDefault(e => e.Id == it.Businessid)?.Name; }) .ToPageListAsync(param.offset, param.limit, totalnum); return new PageParms { page = param.offset, Items = query, totalnum = totalnum, limit = param.limit }; } /// /// 信息提交 /// /// /// public async Task SubmitFormAsync(DeviceSubmit role) { role.Name = role.Name.ToStr(); role.FacEcode = role.FacEcode.ToStr(); role.Ecode = role.Ecode.ToStr(); role.Remark = role.Remark.ToStr(); role.Province = role.Province.ToStr(); role.City = role.City.ToStr(); role.Area = role.Area.ToStr(); role.Address = role.Address.ToStr(); role.Secret = role.Secret.ToStr(); role.SecretHash = role.SecretHash.ToStr(); role.DevId = role.DevId.ToStr(); if (role.Id != Guid.Empty) { //检查序列号是否已存在 if (await dbClient.Queryable().AnyAsync(x => x.FacEcode == role.FacEcode && x.Id != role.Id)) { return new ResultInfo() { code = ResultState.FAIL, message = "此序列号已存在!" }; } //检查机器码是否已存在 if (await dbClient.Queryable().AnyAsync(x => x.Ecode == role.Ecode && x.Id != role.Id)) { return new ResultInfo() { code = ResultState.FAIL, message = "此设备码已存在!" }; } await dbClient.Updateable().SetColumns(x => new W_Device { DeviceType = role.DeviceType, Address = role.Address, Area = role.Area, City = role.City, Ecode = role.Ecode, FacEcode = role.FacEcode, Name = role.Name, Remark = role.Remark, NetType = role.NetType, Province = role.Province, Tare = role.Tare }).Where(x => x.Id == role.Id).ExecuteCommandAsync(); //更新平台信息 var tdbclient = repository.Change().Context; if (!await tdbclient.Queryable().AnyAsync(x => x.DeviceId == role.Id)) { await tdbclient.Insertable(new W_SZDevice { DeviceId = role.Id, Secret = role.Secret, SecretHash = role.SecretHash, DevId = role.DevId }).ExecuteCommandAsync(); } else { await tdbclient.Updateable().SetColumns(x => new W_SZDevice { Secret = role.Secret, SecretHash = role.SecretHash, DevId = role.DevId }).Where(x => x.DeviceId == role.Id).ExecuteCommandAsync(); } return new ResultInfo() { code = ResultState.SUCCESS, message = "修改成功!" }; } else { //检查序列号是否已存在 if (await dbClient.Queryable().AnyAsync(x => x.FacEcode == role.FacEcode)) { return new ResultInfo() { code = ResultState.FAIL, message = "此序列号已存在!" }; } //检查机器码是否已存在 if (await dbClient.Queryable().AnyAsync(x => x.Ecode == role.Ecode)) { return new ResultInfo() { code = ResultState.FAIL, message = "此设备码已存在!" }; } role.CreateTime = DateTime.Now; role.Status = (int)DeviceStatus.WaitActive; role.Id = IDGen.NextID(); await dbClient.Insertable(new W_Device { Id = role.Id, Status = role.Status, ActiveTime = role.ActiveTime, Address = role.Address, Area = role.Area, Businessid = role.Businessid, City = role.City, CreateTime = role.CreateTime, DeviceType = role.DeviceType, Ecode = role.Ecode, FacEcode = role.FacEcode, Remark = role.Remark, InstallTime = role.InstallTime, LastHeartTime = role.LastHeartTime, Name = role.Name, NetType = role.NetType, Province = role.Province, Tare = role.Tare }).ExecuteCommandAsync(); //更新平台信息 var tdbclient = repository.Change().Context; await tdbclient.Insertable(new W_SZDevice { DeviceId = role.Id, Secret = role.Secret, SecretHash = role.SecretHash, DevId = role.DevId }).ExecuteCommandAsync(); await _businessService.InsertOrUpdateRealDataAsync(); return new ResultInfo() { code = ResultState.SUCCESS, message = "添加成功!" }; } } } }