平安校园
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.
 
 
 
 
 
 

147 lines
5.0 KiB

  1. using Mapster;
  2. using MoYu.DependencyInjection;
  3. using MoYu.FriendlyException;
  4. using SafeCampus.Application.Services.Business;
  5. using SafeCampus.Application.Services.Business.Warn.Dto;
  6. using SafeCampus.Application.Services.Business.Warn.Service;
  7. namespace SafeCampus.Web.Core.Controllers.Application.Business.Warn.Service;
  8. public class WarnInfoService:DbRepository<WarnInfo>, IWarnInfoService,ITransient
  9. {
  10. private readonly ISimpleCacheService _simpleCacheService;
  11. public WarnInfoService(ISimpleCacheService simpleCacheService)
  12. {
  13. _simpleCacheService = simpleCacheService;
  14. }
  15. public async Task<bool> Add(WarnInfoDto vm)
  16. {
  17. var model = vm.Adapt<WarnInfo>();
  18. await InsertAsync(model);
  19. return true;
  20. }
  21. public async Task<bool> Delete(long Id)
  22. {
  23. var model = await GetFirstAsync(x=>x.Id==Id);
  24. if (model == null)
  25. {
  26. throw Oops.Oh("信息不存在");
  27. }
  28. await DeleteAsync(model);
  29. return true;
  30. }
  31. public async Task<bool> Update(WarnInfoDto vm)
  32. {
  33. if (!vm.ID.HasValue)
  34. {
  35. throw Oops.Oh("请填写ID");
  36. }
  37. var model = await GetFirstAsync(p => p.Id == vm.ID);
  38. if (model == null)
  39. {
  40. throw Oops.Oh("信息不存在");
  41. }
  42. var res = vm.Adapt(model);
  43. await UpdateAsync(res);
  44. return true;
  45. }
  46. public async Task<WarnInfoDto> GetInfo(long Id)
  47. {
  48. var model = await GetFirstAsync(p => p.Id == Id);
  49. if (model == null)
  50. {
  51. throw Oops.Oh("信息不存在!");
  52. }
  53. return model.Adapt<WarnInfoDto>();
  54. }
  55. public async Task<SqlSugarPagedList<WarnInfoList>> GetList(WarnInfoSearch search)
  56. {
  57. var config = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  58. var guolvIds = new List<long>();
  59. if (config!=null)
  60. {
  61. var guolv = Context.Queryable<WarnInfo>();
  62. foreach (var warnGroupInfo in config)
  63. {
  64. if (warnGroupInfo.State)
  65. {
  66. foreach (var warnGroupSubset in warnGroupInfo.Subset)
  67. {
  68. if (!warnGroupSubset.State)
  69. {
  70. guolvIds.AddRange(await guolv
  71. .Where(x => warnGroupInfo.CameraId.Contains(x.CameraId) &&
  72. x.AlarmType == warnGroupSubset.Code).Select(x => x.Id).ToListAsync());
  73. }
  74. }
  75. }
  76. else
  77. {
  78. guolvIds.AddRange(await guolv
  79. .Where(x => warnGroupInfo.CameraId.Contains(x.CameraId)).Select(x => x.Id).ToListAsync());
  80. }
  81. }
  82. }
  83. var query = Context.Queryable<WarnInfo>()
  84. .WhereIF(search.WarnHand.HasValue, x => x.WarnHand == search.WarnHand)
  85. .WhereIF(!string.IsNullOrEmpty(search.AlarmType),x => x.AlarmType == search.AlarmType)
  86. .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
  87. .WhereIF(search.StartTick.HasValue,x => x.Tick >= search.StartTick)
  88. .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick)
  89. .WhereIF(guolvIds.Any(),x=>!guolvIds.Contains(x.Id));
  90. var list=await query.OrderByDescending(x=>x.Tick)
  91. .ToPagedListAsyncMapster<WarnInfo,WarnInfoList>(search.PageNum, search.PageSize);
  92. return list;
  93. }
  94. public async Task<bool> HandWarn(long id, string remark)
  95. {
  96. var model = await GetFirstAsync(p => p.Id == id);
  97. if (model == null)
  98. {
  99. throw Oops.Oh("信息不存在!");
  100. }
  101. model.Remark = remark;
  102. model.WarnHand = 1;
  103. model.HandTime = DateTime.Now;
  104. await UpdateAsync(model);
  105. return true;
  106. }
  107. public async Task<List<WarnInfo>> GetListNoPage(WarnInfoSearch search)
  108. {
  109. var query = Context.Queryable<WarnInfo>()
  110. .WhereIF(search.WarnHand.HasValue, x => x.WarnHand == search.WarnHand)
  111. .WhereIF(!string.IsNullOrEmpty(search.AlarmType), x => x.AlarmType == search.AlarmType)
  112. .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
  113. .WhereIF(search.StartTick.HasValue, x => x.Tick >= search.StartTick)
  114. .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick);
  115. var list = await query.OrderByDescending(x => x.Tick)
  116. .ToListAsync();
  117. return list;
  118. }
  119. public async Task<List<WarnType>>GetWarnType()
  120. {
  121. var query = Context.Queryable<WarnInfo>()
  122. .GroupBy(x => new { x.AlarmType, x.AlarmTypeDesc })
  123. .Select(xx => new WarnType
  124. {
  125. Name = xx.AlarmTypeDesc,
  126. Code = xx.AlarmType
  127. });
  128. return await query.ToListAsync();
  129. }
  130. }