using OpenAuth.App.BaseApp.Base; using OpenAuth.Repository.Domain; using OpenAuth.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OpenAuth.App.Interface; using SqlSugar; using Infrastructure; using OpenAuth.App.ServiceApp.Response; using DocumentFormat.OpenXml.EMMA; namespace OpenAuth.App.ServiceApp { public class AirportMaintenanceApp : SqlSugarBaseApp { public AirportMaintenanceApp(ISugarUnitOfWork unitWork, ISimpleClient repository, IAuth auth) : base(unitWork, repository, auth) { } //获取设备绑定码 public async Task> GetDeviceBindingCode() { RefAsync totalCount = 0; using (var db = UnitWork.CreateContext()) { var info = await db.LasaDeviceBindingCode.AsQueryable().Where(r => r.BindStatus == 0).FirstAsync(); if (info != null) { return new Response { Code = 200, Message = "获取设备绑定码成功", Result = info }; } else { //如果设备绑定码不存在,则创建一个新的设备绑定码 var newBindingCode = new LasaDeviceBindingCode { Id = Guid.NewGuid().ToString(), DeviceBindingCode = Guid.NewGuid().ToString("N").Substring(0, 8), // 生成一个新的绑定码 OrgId = "371300", // 默认组织ID OrgName = "临沂市", // 默认组织名称 BindStatus = 0 // 未绑定状态 }; await db.LasaDeviceBindingCode.InsertAsync(newBindingCode); if (db.Commit()) { return new Response { Code = 200, Message = "获取设备绑定码成功", Result = newBindingCode }; } else { return new Response { Code = 500, Message = "获取设备绑定码失败", }; } } } } /// /// 修改注册码状态 /// /// /// public async Task UpdateCodeStatus(string DeviceBindingCode) { using (var db = UnitWork.CreateContext()) { var flag = await db.LasaDeviceBindingCode.UpdateAsync(it => new LasaDeviceBindingCode() { BindStatus = 1, }, it => it.DeviceBindingCode == DeviceBindingCode); if (db.Commit()) return true; else return false; } } //使用网关 public async Task> GetGateway() { RefAsync totalCount = 0; using (var db = UnitWork.CreateContext()) { var info = await db.LasaGateway.AsQueryable().Where(r => r.BindStatus == 0).FirstAsync(); if (info != null) { return new Response { Code = 200, Message = "获取设备绑定码成功", Result = info }; } else { //如果网关不存在,则创建一个新的 var newGateway = new LasaGateway { Id = Guid.NewGuid().ToString(), CreateTime = DateTime.Now, GatewayAccount="sdhc", GatewaySn = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(), // 生成一个新的 MqttGateway= "175.27.168.120:6011", MqttPassword = "", OrgId = "371300", // 默认组织ID BindStatus = 0 // 未绑定状态 }; await db.LasaGateway.InsertAsync(newGateway); if (db.Commit()) { return new Response { Code = 200, Message = "获取设备绑定码成功", Result = newGateway }; } else { return new Response { Code = 500, Message = "获取设备绑定码失败", }; } } } } public async Task UpdateGateway(string gateway,string did) { using (var db = UnitWork.CreateContext()) { var flag = await db.LasaGateway.UpdateAsync(it => new LasaGateway() { BindStatus = 1, Did = did, UpdateTime = DateTime.Now, }, it => it.GatewaySn == gateway); if (db.Commit()) return true; else return false; } } //获取网关 public async Task> GetGatewaysnList() { using (var db = UnitWork.CreateContext()) { var info = await db.LasaGateway.AsQueryable().Select(r => r.GatewaySn).ToListAsync(); if (info != null) { return info; } else { return new List(); } } } #region 健康报警 /// /// 添加健康报警 /// /// /// public bool AddManageDeviceHms(List info) { using (var db = UnitWork.CreateContext()) { var flag = db.LasaManageDeviceHms.InsertRange(info); if (db.Commit()) return true; else return false; } } #endregion #region 日志 /// /// 添加日志 /// /// /// public bool AddLog(LasaLog info) { using (var db = UnitWork.CreateContext()) { var flag = db.LasaLog.Insert(info); if (db.Commit()) return true; else return false; } } #endregion } }