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

348 lines
12 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SMS;
using DS.Module.UserModule;
using DS.WMS.Core.OpenApiModule.Entity;
using DS.WMS.Core.SecurityModule.Dtos;
using DS.WMS.Core.SecurityModule.Entity;
using DS.WMS.Core.SecurityModule.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.SecurityModule.Method;
/// <summary>
/// 海康开放服务
/// </summary>
public class HkOpenService : IHkOpenService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISendMessageService _messageService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public HkOpenService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
_messageService = _serviceProvider.GetRequiredService<ISendMessageService>();
}
/// <summary>
/// 海康高温报警列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<HkThermometryViewModel>> GetThermometryListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<tb_HKThermometry>()
.Select<HkThermometryViewModel>()
// .Where(x=>x.AlarmLevel == 1)
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 获取海康高温报警信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<HkThermometryViewModel> GetThermometryInfo(string id)
{
var data = db.Queryable<tb_HKThermometry>()
.Select<HkThermometryViewModel>()
.Where(x => x.RelationId == Guid.Parse(id)).First();
return DataResult<HkThermometryViewModel>.Success(data);
}
/// <summary>
/// 获取未处理高温报警数
/// </summary>
/// <returns></returns>
public DataResult<int> GetThermometryWarningCount()
{
var count = db.Queryable<tb_HKThermometry>().Where(x => x.AlarmLevel == 1 && x.Status == 0).Count();
return DataResult<int>.Success(count);
}
/// <summary>
/// 海康越界侦测列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<HkLineDetectionViewModel>> GetLineDetectionListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<tb_HKLineDetection>()
.Select<HkLineDetectionViewModel>()
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 获取越界侦测报警信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<HkLineDetectionViewModel> GetLineDetectionInfo(string id)
{
var data = db.Queryable<tb_HKLineDetection>()
.Select<HkLineDetectionViewModel>()
.Where(x => x.RelationId == Guid.Parse(id)).First();
return DataResult<HkLineDetectionViewModel>.Success(data);
}
/// <summary>
/// 获取未处理越界侦测报警数
/// </summary>
/// <returns></returns>
public DataResult<int> GetLineDetectionWarningCount()
{
var count = db.Queryable<tb_HKLineDetection>().Where(x => x.Status == 0).Count();
return DataResult<int>.Success(count);
}
/// <summary>
/// 高温预警短信
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<DataResult> SendThermometryMessage(AlarmMessageInput model)
{
var smsCode = AppSetting.app(new string[] { "AliSMS", "ThermometrySMSCode" });
var records = new List<tb_SmsRecord>();
var info = db.Queryable<tb_HKThermometry>().First(x => x.RelationId == Guid.Parse(model.Id));
// if (info.Status == 2)
// {
// return DataResult.Failed("高温报警短信已发送!");
// }
var phones = db.Queryable<tb_SecurityUser>().Where(x => model.UserIds.Contains(x.Id)).Select(x => x.Phone)
.ToList();
var users = db.Queryable<tb_SecurityUser>().Where(x => model.UserIds.Contains(x.Id)).ToList();
string temp = "{\"channelName\": \"" + info.ChannelName + "\"}";
var msg = new TemplateMessages()
{
PhoneNumbers = String.Join(",", phones),
SignName = "高速临空物流园",
TemplateCode = smsCode,
TemplateParam = temp
};
var res = await _messageService.SendAliYunMessage(msg);
if (res.Succeeded)
{
foreach (var item in users)
{
var record = new tb_SmsRecord()
{
RelationId = Guid.Parse(model.Id),
UserId = item.Id,
UserName = item.UserName,
Duty = item.Duty,
Phone = item.Phone,
SendTime = DateTime.Now
};
records.Add(record);
}
try
{
//开启事务
await db.Ado.BeginTranAsync();
if (records.Count > 0)
{
await db.Insertable(records).ExecuteCommandAsync();
}
info.Status = 2;
await db.Updateable(info).ExecuteCommandAsync();
await db.Ado.CommitTranAsync();
return DataResult.Successed("发送成功!");
}
catch (Exception ex)
{
await db.Ado.RollbackTranAsync();
return DataResult.Failed("发送成功,但更新相关记录失败!" + ",请联系管理员!");
}
}
else
{
return DataResult.Failed(res.Message);
}
}
/// <summary>
/// 越界侦测预警短信
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<DataResult> SendLineDetectionMessage(AlarmMessageInput model)
{
var smsCode = AppSetting.app(new string[] { "AliSMS", "LineDetectionSMSCode" });
var records = new List<tb_SmsRecord>();
var info = db.Queryable<tb_HKLineDetection>().First(x => x.RelationId == Guid.Parse(model.Id));
// if (info.Status == 2)
// {
// return DataResult.Failed("高温报警短信已发送!");
// }
var phones = db.Queryable<tb_SecurityUser>().Where(x => model.UserIds.Contains(x.Id)).Select(x => x.Phone)
.ToList();
var users = db.Queryable<tb_SecurityUser>().Where(x => model.UserIds.Contains(x.Id)).ToList();
string temp = "{\"channelName\": \"" + info.ChannelName + "\"}";
var msg = new TemplateMessages()
{
PhoneNumbers = String.Join(",", phones),
SignName = "高速临空物流园",
TemplateCode = smsCode,
TemplateParam = temp
};
var res = await _messageService.SendAliYunMessage(msg);
if (res.Succeeded)
{
foreach (var item in users)
{
var record = new tb_SmsRecord()
{
RelationId = Guid.Parse(model.Id),
UserId = item.Id,
UserName = item.UserName,
Duty = item.Duty,
Phone = item.Phone,
SendTime = DateTime.Now
};
records.Add(record);
}
try
{
//开启事务
await db.Ado.BeginTranAsync();
if (records.Count > 0)
{
await db.Insertable(records).ExecuteCommandAsync();
}
info.Status = 2;
await db.Updateable(info).ExecuteCommandAsync();
await db.Ado.CommitTranAsync();
return DataResult.Successed("发送成功!");
}
catch (Exception ex)
{
await db.Ado.RollbackTranAsync();
return DataResult.Failed("发送成功,但更新相关记录失败!" + ",请联系管理员!");
}
}
else
{
return DataResult.Failed(res.Message);
}
}
/// <summary>
/// 获取短信发送记录
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<List<tb_SmsRecord>> GetSmsRecords(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<tb_SmsRecord>()
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
// var data = db.Queryable<tb_SmsRecord>()
// .Where(x => x.RelationId == Guid.Parse(id)).ToList();
// return DataResult.Successed("获取数据成功",data);
}
public DataResult IgnoreThermometryWarning(string id)
{
var info = db.Queryable<tb_HKThermometry>().First(x => x.RelationId == Guid.Parse(id));
if (info.Status == 2)
{
return DataResult.Failed("高温报警已处理!");
}
info.Status = 1;
db.Updateable(info).ExecuteCommand();
return DataResult.Successed("高温报警已知悉!");
}
public DataResult IgnoreLineDetectionWarning(string id)
{
var info = db.Queryable<tb_HKLineDetection>().First(x => x.RelationId == Guid.Parse(id));
if (info.Status == 2)
{
return DataResult.Failed("越界侦测报警已处理!");
}
info.Status = 1;
db.Updateable(info).ExecuteCommand();
return DataResult.Successed("越界侦测报警已知悉!");
}
/// <summary>
/// 批量更新
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult BatchDealData(BatchDealInput model)
{
if (model.Ids.Length == 0)
{
return DataResult.Failed("未选中数据!");
}
if (model.Type == 1) //高温报警
{
if (db.Queryable<tb_HKThermometry>().Any(x => model.Ids.Contains(x.RelationId.ToString()) && x.Status == 2))
{
return DataResult.Failed("存在已经处理的高温报警!");
}
var list = db.Queryable<tb_HKThermometry>().Where(x => model.Ids.Contains(x.RelationId.ToString()))
.ToList();
foreach (var item in list)
{
item.Status = 1;
}
db.Updateable(list).ExecuteCommand();
}
else if (model.Type == 2) //越界侦测
{
if (db.Queryable<tb_HKLineDetection>().Any(x => model.Ids.Contains(x.RelationId.ToString()) && x.Status == 2))
{
return DataResult.Failed("存在已经处理的越界侦测!");
}
var list = db.Queryable<tb_HKLineDetection>().Where(x => model.Ids.Contains(x.RelationId.ToString()))
.ToList();
foreach (var item in list)
{
item.Status = 1;
}
db.Updateable(list).ExecuteCommand();
}
else if (model.Type == 3) //火警
{
if (db.Queryable<tb_FireAlarm>().Any(x => model.Ids.Contains(x.RelationId.ToString()) && x.Status == 2))
{
return DataResult.Failed("存在已经处理的火警警报!");
}
var list = db.Queryable<tb_FireAlarm>().Where(x => model.Ids.Contains(x.RelationId.ToString()))
.ToList();
foreach (var item in list)
{
item.Status = 1;
}
db.Updateable(list).ExecuteCommand();
}
return DataResult.Successed("批量处理成功!");
}
}