平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 lines
6.0 KiB

  1. using MoYu.RemoteRequest.Extensions;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using SafeCampus.Application.Manager.DeepelephManager;
  5. using System.Collections.Generic;
  6. namespace SafeCampus.Application.Services.Business.CameraInfoService;
  7. public class CameraInfoService:DbRepository<CameraInfo>, ICameraInfoService
  8. {
  9. private readonly IDeepelephManager _deepelephManager;
  10. private readonly ISimpleCacheService _simpleCacheService;
  11. public CameraInfoService(IDeepelephManager deepelephManager, ISimpleCacheService simpleCacheService)
  12. {
  13. _deepelephManager = deepelephManager;
  14. _simpleCacheService = simpleCacheService;
  15. }
  16. public async Task<bool> DataSync()
  17. {
  18. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  19. var list = await Context.Queryable<CameraInfo>().ToListAsync();
  20. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/page_query/brief"
  21. .SetBody(new
  22. {
  23. token = _deepelephManager.GetToken(),
  24. tenantCode = appSettings.TenantCode,
  25. poiId = appSettings.PoiId,
  26. pageNo = 1,
  27. pageSize = 1000,
  28. })
  29. .SetContentType("application/json")
  30. .PostAsAsync<string>();
  31. var model = JsonConvert.DeserializeObject<JObject>(str);
  32. if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
  33. if (model["data"] != null)
  34. {
  35. foreach (var item in model["data"]["items"])
  36. {
  37. var old_model = list.FirstOrDefault(x => x.SensorId == item["sensorId"].ToString());
  38. if (old_model != null)
  39. {
  40. old_model.DeviceStatus = item["deviceStatus"].ToString() == "online";
  41. old_model.DirectUrlIp = item["directUrlIp"].ToString();
  42. old_model.FieldId = item["fieldId"].ToString();
  43. old_model.FieldName = item["fieldName"].ToString();
  44. old_model.LastTime = DateTime.Now;
  45. old_model.ResHeight = (int)item["resHeight"];
  46. old_model.ResWidth = (int)item["resWidth"];
  47. old_model.SensorName = item["sensorName"].ToString();
  48. old_model.SnapshotUrl = item["snapshotUrl"].ToString();
  49. await UpdateAsync(old_model);
  50. }
  51. else
  52. {
  53. await InsertAsync(new CameraInfo
  54. {
  55. DeviceStatus = item["deviceStatus"].ToString()== "online",
  56. DirectUrlIp = item["directUrlIp"].ToString(),
  57. FieldId = item["fieldId"].ToString(),
  58. FieldName = item["fieldName"].ToString(),
  59. LastTime = DateTime.Now,
  60. ResHeight = (int)item["resHeight"],
  61. ResWidth = (int)item["resWidth"],
  62. SensorId = item["sensorId"].ToString(),
  63. SensorName = item["sensorName"].ToString(),
  64. SnapshotUrl = item["snapshotUrl"].ToString(),
  65. });
  66. }
  67. }
  68. return true;
  69. }
  70. return false;
  71. }
  72. public async Task<SqlSugarPagedList<CameraInfoDto>> GetPageList(CameraSearch search)
  73. {
  74. var query = Context.Queryable<CameraInfo>()
  75. .Includes(x=>x.SysUserItem)
  76. .Includes(x=>x.CameraGroupItem)
  77. .WhereIF(search.DeviceStatus.HasValue, x => x.DeviceStatus == search.DeviceStatus)
  78. .WhereIF(!string.IsNullOrEmpty(search.SensorId), x => x.SensorId == search.SensorId)
  79. .WhereIF(!string.IsNullOrEmpty(search.SensorName), x => x.SensorName == search.SensorName)
  80. .WhereIF(search.GroupId.HasValue, x => (search.GroupId.Value==-1?x.GroupId==null:x.GroupId==search.GroupId.Value));
  81. var list = await query.OrderBy(x => x.LastTime)
  82. .ToPagedListAsyncMapster<CameraInfo, CameraInfoDto>(search.PageNum, search.PageSize);
  83. return list;
  84. }
  85. public async Task<bool> BatchSetGroup(SetGroupInput input)
  86. {
  87. if (input.Id==-1)
  88. {
  89. var result = await Context.Updateable<CameraInfo>()
  90. .SetColumns(x => x.GroupId == null)
  91. .Where(x => input.Ids.Contains(x.Id))
  92. .ExecuteCommandAsync();
  93. return result == input.Ids.Count;
  94. }
  95. else
  96. {
  97. var result = await Context.Updateable<CameraInfo>()
  98. .SetColumns(x => x.GroupId == input.Id)
  99. .Where(x => input.Ids.Contains(x.Id))
  100. .ExecuteCommandAsync();
  101. return result == input.Ids.Count;
  102. }
  103. }
  104. public async Task<bool> BatchSetPushPersonByGroup(SetPushPersonGroupInput input)
  105. {
  106. var result = await Context.Updateable<CameraInfo>()
  107. .SetColumns(x => x.PushUserId == input.UserId)
  108. .Where(x =>x.GroupId==input.GroupId)
  109. .ExecuteCommandAsync();
  110. return result >0;
  111. }
  112. public async Task<bool> BatchSetPushPerson(SetPushPersonInput input)
  113. {
  114. var result = await Context.Updateable<CameraInfo>()
  115. .SetColumns(x => x.PushUserId == input.UserId)
  116. .Where(x => input.Ids.Contains(x.Id))
  117. .ExecuteCommandAsync();
  118. return result == input.Ids.Count;
  119. }
  120. public async Task<bool> BatchSetPushPersonByWarn(SetPushPersonWarnInput input)
  121. {
  122. var warn = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  123. var warnGroupInfo = warn.FirstOrDefault(x => x.Code == input.WarnCode);
  124. if (warnGroupInfo != null)
  125. {
  126. var ids = warnGroupInfo.CameraId;
  127. var result = await Context.Updateable<CameraInfo>()
  128. .SetColumns(x => x.PushUserId == input.UserId)
  129. .Where(x => ids.Contains(x.SensorId))
  130. .ExecuteCommandAsync();
  131. return result >0;
  132. }
  133. throw Oops.Oh("分组不存在");
  134. }
  135. }