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

171 lines
6.2 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. using SafeCampus.Core.Utils.TXYSMS;
  8. namespace SafeCampus.Web.Core.Controllers.Application.Business.Warn.Service;
  9. public class WarnInfoService:DbRepository<WarnInfo>, IWarnInfoService,ITransient
  10. {
  11. private readonly ISimpleCacheService _simpleCacheService;
  12. public WarnInfoService(ISimpleCacheService simpleCacheService)
  13. {
  14. _simpleCacheService = simpleCacheService;
  15. }
  16. public async Task<bool> Add(WarnInfoDto vm)
  17. {
  18. var model = vm.Adapt<WarnInfo>();
  19. await InsertAsync(model);
  20. //TODO 发送短信
  21. TxySmsUtil.SendSms(new[] { "" }, new[] { "" });
  22. return true;
  23. }
  24. public async Task<bool> Delete(long Id)
  25. {
  26. var model = await GetFirstAsync(x=>x.Id==Id);
  27. if (model == null)
  28. {
  29. throw Oops.Oh("信息不存在");
  30. }
  31. await DeleteAsync(model);
  32. return true;
  33. }
  34. public async Task<bool> Update(WarnInfoDto vm)
  35. {
  36. if (!vm.ID.HasValue)
  37. {
  38. throw Oops.Oh("请填写ID");
  39. }
  40. var model = await GetFirstAsync(p => p.Id == vm.ID);
  41. if (model == null)
  42. {
  43. throw Oops.Oh("信息不存在");
  44. }
  45. var res = vm.Adapt(model);
  46. await UpdateAsync(res);
  47. return true;
  48. }
  49. public async Task<WarnInfoDto> GetInfo(long Id)
  50. {
  51. var model = await Context.Queryable<WarnInfo>()
  52. .Includes(x=>x.CameraInfoItem)
  53. .Includes(x => x.PersonInfoItem)
  54. .Includes(x => x.PersonSetInfoItem)
  55. .FirstAsync(x => x.Id == Id);
  56. if (model == null)
  57. {
  58. throw Oops.Oh("信息不存在!");
  59. }
  60. return model.Adapt<WarnInfoDto>();
  61. }
  62. public async Task<SqlSugarPagedList<WarnInfoList>> GetList(WarnInfoSearch search)
  63. {
  64. var config = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  65. var guolvIds = new List<long>();
  66. if (config!=null)
  67. {
  68. var guolv = Context.Queryable<WarnInfo>();
  69. foreach (var warnGroupInfo in config)
  70. {
  71. if (warnGroupInfo.State)
  72. {
  73. foreach (var warnGroupSubset in warnGroupInfo.Subset)
  74. {
  75. if (!warnGroupSubset.State)
  76. {
  77. guolvIds.AddRange(await guolv
  78. .Where(x => warnGroupInfo.CameraId.Contains(x.CameraId) &&
  79. x.AlarmType == warnGroupSubset.Code).Select(x => x.Id).ToListAsync());
  80. }
  81. }
  82. }
  83. else
  84. {
  85. guolvIds.AddRange(await guolv
  86. .Where(x => warnGroupInfo.CameraId.Contains(x.CameraId)).Select(x => x.Id).ToListAsync());
  87. }
  88. }
  89. }
  90. var query = Context.Queryable<WarnInfo>()
  91. .Includes(x=>x.CameraInfoItem)
  92. .Includes(x=>x.PersonInfoItem)
  93. .Includes(x=>x.PersonSetInfoItem,x=>x.MajorInfoItem,x=>x.DepartmentInfoItem)
  94. .WhereIF(search.WarnHand.HasValue, x => x.WarnHand == search.WarnHand)
  95. .WhereIF(!string.IsNullOrEmpty(search.AlarmType),x => x.AlarmType == search.AlarmType)
  96. .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
  97. .WhereIF(search.StartTick.HasValue,x => x.Tick >= search.StartTick)
  98. .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick)
  99. .WhereIF(guolvIds.Any(),x=>!guolvIds.Contains(x.Id));
  100. var list=await query.OrderByDescending(x=>x.Tick)
  101. .ToPagedListAsyncMapster<WarnInfo,WarnInfoList>(search.PageNum, search.PageSize);
  102. return list;
  103. }
  104. public async Task<bool> HandWarn(HandWarnInput input)
  105. {
  106. var model = await GetFirstAsync(p => p.Id == input.Id);
  107. if (model == null)
  108. {
  109. throw Oops.Oh("信息不存在!");
  110. }
  111. model.Remark = input.Remark;
  112. model.WarnHand = 1;
  113. model.HandTime = DateTime.Now;
  114. await UpdateAsync(model);
  115. return true;
  116. }
  117. public async Task<List<WarnInfoList>> GetListNoPage(WarnInfoSearch search)
  118. {
  119. var query = Context.Queryable<WarnInfo>()
  120. .Includes(x => x.CameraInfoItem,x=>x.CameraGroupItem)
  121. .Includes(x => x.PersonInfoItem)
  122. .Includes(x => x.PersonSetInfoItem)
  123. .WhereIF(search.WarnHand.HasValue, x => x.WarnHand == search.WarnHand)
  124. .WhereIF(!string.IsNullOrEmpty(search.AlarmType), x => x.AlarmType == search.AlarmType)
  125. .WhereIF(search.AlarmTypes!=null&&search.AlarmTypes.Any(),x=>search.AlarmTypes.Contains(x.AlarmType))
  126. .WhereIF(search.PersonSetIds != null&&search.PersonSetIds.Any(),x=>search.PersonSetIds.Contains(x.PersonSetId))
  127. .WhereIF(search.CameraIds != null&&search.CameraIds.Any(),x=>search.CameraIds.Contains(x.CameraId))
  128. .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
  129. .WhereIF(!string.IsNullOrEmpty(search.PersonId), x => x.PersonId == search.PersonId)
  130. .WhereIF(search.StartTick.HasValue, x => x.Tick >= search.StartTick)
  131. .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick);
  132. var list = await query.OrderByDescending(x => x.Tick)
  133. .ToListAsync();
  134. return list.Adapt<List<WarnInfoList>>();
  135. }
  136. public async Task<List<WarnType>>GetWarnType()
  137. {
  138. var query = Context.Queryable<WarnInfo>()
  139. .GroupBy(x => new { x.AlarmType, x.AlarmTypeDesc })
  140. .Select(xx => new WarnType
  141. {
  142. Name = xx.AlarmTypeDesc,
  143. Code = xx.AlarmType
  144. });
  145. return await query.ToListAsync();
  146. }
  147. public async Task<bool> DeleteBatch(List<long> Ids)
  148. {
  149. await Context.Deleteable<WarnInfo>().In(Ids).ExecuteCommandAsync();
  150. return true;
  151. }
  152. }