diff --git a/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs b/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs index bb346fe..9a831e5 100644 --- a/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs +++ b/DataVAPI.ServerDB/MongoDB/MongodbHelper.cs @@ -99,7 +99,7 @@ namespace DataVAPI.ServerDB.MongoDB var oldValue = old.GetType().GetProperty(prop.Name).GetValue(old); if (newValue != null) { - if (!newValue.ToString().Equals(oldValue.ToString())) + if (oldValue==null || !newValue.ToString().Equals(oldValue.ToString())) { old.GetType().GetProperty(prop.Name).SetValue(old, newValue); //if (Type == typeof(DateTime).Name) @@ -130,6 +130,25 @@ namespace DataVAPI.ServerDB.MongoDB return collection.Find(a => a.Id == id)?.ToList().FirstOrDefault(); } + public List QueryAllNew(string clientId, string deviceId) + { + if (string.IsNullOrEmpty(clientId)) + { + return collection.Find(par=>par.Id!=null)?.ToList().OrderByDescending(a => a.CreateTime).ToList(); + } + else + { + if (string.IsNullOrEmpty(deviceId)) + { + return collection.Find(a => a.ClientId == clientId)?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); + } + else + { + return collection.Find(a => a.ClientId == clientId && a.DeviceId.Contains(deviceId))?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); + } + } + } + public List QueryAllTime(string clientId, string deviceId, DateTime datesta, DateTime datastop) { if (string.IsNullOrEmpty(clientId)) @@ -164,7 +183,7 @@ namespace DataVAPI.ServerDB.MongoDB } else { - return collection.Find(a => a.ClientId == clientId && a.DeviceId == deviceId)?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); + return collection.Find(a => a.ClientId == clientId && a.DeviceId.Contains(deviceId))?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); } } } @@ -185,12 +204,23 @@ namespace DataVAPI.ServerDB.MongoDB return collection.Find(a => a.ClientId == clientId && a.CreateTime >= DateTime.Now.Date)?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); } + public List QueryClientIdName(string clientId,string name) + { + return collection.Find(a => a.ClientId == clientId && a.devicename==name)?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); + } + public T QueryClientIdMax(string clientId) { List clo = collection.Find(a => a.ClientId == clientId && a.CreateTime >= DateTime.Now.Date && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); return clo.Count>0?clo[0]:null; } + public T QueryClientIdMaxName(string clientId,string name) + { + List clo = collection.Find(a => a.ClientId == clientId && a.devicename==name && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); + return clo.Count > 0 ? clo[0] : null; + } + public List QueryDeviceId(string clientId, string deviceId) { return collection.Find(a => a.DeviceId == deviceId && a.ClientId == clientId && a.CreateTime >= DateTime.Now.Date && a.State == "y")?.ToList().OrderByDescending(a => a.CreateTime)?.ToList(); diff --git a/DataVAPI/Controllers/DeviceController.cs b/DataVAPI/Controllers/DeviceController.cs index f457189..1a1b058 100644 --- a/DataVAPI/Controllers/DeviceController.cs +++ b/DataVAPI/Controllers/DeviceController.cs @@ -128,6 +128,25 @@ namespace DataVAPI.Controllers } } /// + /// 根据客户端ID 设备id 模糊查询 + /// + /// + /// + /// + [HttpGet] + public JsonMsg> QueryNew(string clientId, string deviceId) + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + return JsonMsg>.OK(mg.QueryAllNew(clientId, deviceId), st); + } + catch (Exception ex) + { + return JsonMsg>.Error(null, st, ex.Message); + } + } + /// /// 分页查询 /// /// 客户端ID diff --git a/DataVAPI/Controllers/DeviceStatsController.cs b/DataVAPI/Controllers/DeviceStatsController.cs index eeac4c3..226d8fe 100644 --- a/DataVAPI/Controllers/DeviceStatsController.cs +++ b/DataVAPI/Controllers/DeviceStatsController.cs @@ -22,9 +22,10 @@ namespace DataVAPI.Controllers /// 根据客户端ID查询最近上下线与告警时间 /// /// + /// /// [HttpGet] - public JsonMsg StatsOnOff(string clientId) + public JsonMsg StatsOnOff(string clientId,string name) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try @@ -33,8 +34,19 @@ namespace DataVAPI.Controllers string sx_time = "未知"; string xx_time = "未知"; - AlarmTable alarmTable = mggj.QueryClientIdMax(clientId); - List log = mglog.QueryClientId(clientId); + AlarmTable alarmTable = new AlarmTable(); + List log = new List(); + if (!string.IsNullOrEmpty(name)) + { + alarmTable = mggj.QueryClientIdMaxName(clientId,name); + log = mglog.QueryClientIdName(clientId, name); + } + else + { + alarmTable = mggj.QueryClientIdMax(clientId); + log = mglog.QueryClientId(clientId); + } + if (alarmTable != null) gj_time = alarmTable.AlarmTime; LogTable logTable1 = log.Find(par => par.LogMessage.Contains("设备在线了")); LogTable logTable2 = log.Find(par => par.LogMessage.Contains("设备离线了")); diff --git a/DataVAPI/Controllers/OrderController.cs b/DataVAPI/Controllers/OrderController.cs index 40a0a38..db4e211 100644 --- a/DataVAPI/Controllers/OrderController.cs +++ b/DataVAPI/Controllers/OrderController.cs @@ -1,7 +1,12 @@ using DataVApi.Order; +using DataVApi.Order.Dto; using DataVApi.Order.RequestModel; using DataVAPI.Model; +using DataVAPI.ModelDataBus; +using DataVAPI.ServerDB.MongoDB; using Microsoft.AspNetCore.Mvc; +using NPOI.Util; +using System.Collections.Generic; namespace DataVAPI.Controllers { @@ -12,6 +17,7 @@ namespace DataVAPI.Controllers { string st = System.Reflection.MethodBase.GetCurrentMethod().Name; OrderProvider orderProvider = new OrderProvider(); + MongoDbHelper mg = new MongoDbHelper(DataBus.connStr, DataBus.dbName); /// /// 全部数据 /// @@ -35,7 +41,7 @@ namespace DataVAPI.Controllers /// /// [HttpGet] - public JsonMsg GetAllOrderDataState(int? OrgId,int? DeviceId) + public JsonMsg GetAllOrderDataState(int? OrgId, int? DeviceId) { st = System.Reflection.MethodBase.GetCurrentMethod().Name; try @@ -79,7 +85,7 @@ namespace DataVAPI.Controllers st = System.Reflection.MethodBase.GetCurrentMethod().Name; try { - FullScreenBasic fullScreen=new FullScreenBasic(); + FullScreenBasic fullScreen = new FullScreenBasic(); fullScreen.Count = Count; object retdata = orderProvider.OrderLine(fullScreen); return JsonMsg.OK(retdata, st); @@ -146,5 +152,161 @@ namespace DataVAPI.Controllers return JsonMsg.Error(null, st, ex.Message); } } + + /// + /// 获取店铺集合 + /// + /// + [HttpGet] + public JsonMsg StoreList() + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + object retdata = orderProvider.StoreList(); + return JsonMsg.OK(retdata, st); + } + catch (System.Exception ex) + { + return JsonMsg.Error(null, st, ex.Message); + } + } + + /// + /// 查询组织好的店铺信息 + /// + /// + [HttpGet] + public JsonMsg StoreListIOT() + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + + List _dev = mg.QueryAllTime(null, null, System.DateTime.MinValue, System.DateTime.MinValue); + Dictionary> retdata = orderProvider.StoreList(); + List list = new List(); + + foreach (var item in retdata) + { + int i = 0; + string namesy = string.Empty; + string devid = string.Empty; + int ClientId = item.Key; + List aly_data = new List(); + if (retdata.ContainsKey(ClientId))//查询店铺下面所有设备集合 + { + List devices = _dev?.FindAll(par => par.ClientId == ClientId.ToString()); + item.Value?.ForEach(p => + { + #region 查询设备阿里云连接 + DeviceTable dev = devices?.Find(x => + { + List retudata = new List(); + if (x.DeviceId != null) + { + + string[] vs; + if (x.DeviceId.Contains(" ")) vs = x.DeviceId.Split(" "); + else if (x.DeviceId.Contains(",")) vs = x.DeviceId.Split(","); + else { vs = new string[1]; vs[0] = x.DeviceId; } + retudata = Arrays.AsList(vs); + } + if (retudata == null || retudata.Count <= 0 || !retudata.Contains(p.DeviceID.ToString())) + return false; + else + return true; + }); + #endregion + if (i == 0) { + devid = p.DeviceID.ToString(); + namesy = dev != null ? dev.devicename : ""; + i++; + } + aly_data.Add( new + { + ClientId = p.ClientId, + ClientName = p.ClientName, + DeviceID = p.DeviceID, + DeviceName = p.DeviceName, + DeviceTypeKey = p.DeviceTypeKey, + AlyName = dev != null ? dev.devicename : "", + }); + }); + } + object _data = new + { + Name = item.Value?[0]?.ClientName, + ClientId = item.Value?[0]?.ClientId, + DeviceList= aly_data, + FirstName = namesy, + FirstDeviceID= devid + }; + list.Add(_data); + } + return JsonMsg.OK(list, st); + } + catch (System.Exception ex) + { + return JsonMsg.Error(null, st, ex.Message); + } + } + + /// + /// 根据客户端ID查询设备信息 + /// + /// + [HttpGet] + public JsonMsg> StoreListClientId(int ClientId) + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + List _dev = mg.QueryAllTime(null, null, System.DateTime.MinValue, System.DateTime.MinValue); + Dictionary> retdata = orderProvider.StoreList(); + + List list = new List(); + if (retdata.ContainsKey(ClientId)) + { + List devices= _dev?.FindAll(par => par.ClientId == ClientId.ToString()); + List listdto = retdata[ClientId]; + listdto?.ForEach(p => + { + DeviceTable dev= devices?.Find(x => + { + List retudata = new List(); + if (x.DeviceId != null) + { + + string[] vs; + if (x.DeviceId.Contains(" ")) vs = x.DeviceId.Split(" "); + else if (x.DeviceId.Contains(",")) vs = x.DeviceId.Split(","); + else { vs = new string[1];vs[0]= x.DeviceId ; } + retudata =Arrays.AsList(vs); + } + if (retudata == null || retudata.Count <= 0 || !retudata.Contains(p.DeviceID.ToString())) + return false; + else + return true; + }); + object obj = new + { + ClientId= p.ClientId, + ClientName= p.ClientName, + DeviceID= p.DeviceID, + DeviceName= p.DeviceName, + DeviceTypeKey = p.DeviceTypeKey, + AlyName= dev!=null?dev.devicename: "", + }; + list.Add(obj); + }); + } + return JsonMsg>.OK(list, st); + } + catch (System.Exception ex) + { + return JsonMsg>.Error(null, st, ex.Message); + } + } } } diff --git a/DataVApi.Order/Dto/StoreListDto.cs b/DataVApi.Order/Dto/StoreListDto.cs new file mode 100644 index 0000000..b80ef9b --- /dev/null +++ b/DataVApi.Order/Dto/StoreListDto.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataVApi.Order.Dto +{ + public class StoreListDto + { + public int ClientId { get; set; } + public string ClientName { get; set; } + public int DeviceID { get; set; } + public string DeviceName { get; set; } + public string DeviceTypeKey { get; set; } + } +} diff --git a/DataVApi.Order/OrderProvider.cs b/DataVApi.Order/OrderProvider.cs index 8d8a4db..3c5a276 100644 --- a/DataVApi.Order/OrderProvider.cs +++ b/DataVApi.Order/OrderProvider.cs @@ -208,5 +208,28 @@ WHERE B_Day = ((Day + L_Day) == 0) ? 0 : (((Day - L_Day) * 100) / (Day + L_Day)) }; } + /// + /// 获取店铺集合 + /// + /// + public Dictionary> StoreList() + { + var db = DbContext(); + + StringBuilder sb = new StringBuilder(); + sb.Append(@"SELECT a.AutoKey as ClientId,a.Name as ClientName, b.AutoKey as DeviceID,b.DeviceName,b.DeviceTypeKey FROM BPA_Organize as a JOIN BPA_DeviceInfo as b ON a.Id= b.OrgId"); + + var sql = sb.ToString(); + var data = db.SqlQueryable(sql).ToList(); + Dictionary> dic = new Dictionary>(); + + IEnumerable> re = data.GroupBy(s => s.ClientId); + foreach (IGrouping item in re) + { + dic[item.Key] = item?.ToList(); + } + return dic; + } + } }