|
@@ -16,56 +16,58 @@ var AlarmService = new(alarmService)
|
|
|
|
|
|
|
|
type alarmService struct{}
|
|
type alarmService struct{}
|
|
|
|
|
|
|
|
|
|
+// 根据id获取设备信息
|
|
|
func (s *alarmService) Get(id int) (*dao.Alarm, *common.Errors) {
|
|
func (s *alarmService) Get(id int) (*dao.Alarm, *common.Errors) {
|
|
|
// 创建查询实例
|
|
// 创建查询实例
|
|
|
device := &dao.Alarm{
|
|
device := &dao.Alarm{
|
|
|
ID: id,
|
|
ID: id,
|
|
|
}
|
|
}
|
|
|
- err := device.GetDevice()
|
|
|
|
|
|
|
+ err := device.GetDevice() //调用device的GetDevice方法,将返回值赋给err
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return nil, common.FailResponse(err.Error(), nil)
|
|
|
|
|
|
|
+ return nil, common.FailResponse(err.Error(), nil) //如果err不为nil,则返回一个空指针和错误信息
|
|
|
}
|
|
}
|
|
|
- device.CountRel = int(dao.AlarmTerminal{}.GetTerminalCount(device.ID))
|
|
|
|
|
- return device, nil
|
|
|
|
|
|
|
+ device.CountRel = int(dao.AlarmTerminal{}.GetTerminalCount(device.ID)) //调用一键报警服务中的获取终端计数的方法,将返回值转换为整数并赋值给device.CountRel
|
|
|
|
|
+ return device, nil //返回设备和nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (s *alarmService) CreateOrUpdate(userId int64, tenantId string, req dao.Alarm) *common.Errors {
|
|
|
|
|
- device := req
|
|
|
|
|
|
|
+// 添加设备或者修改设备
|
|
|
|
|
+func (s *alarmService) CreateOrUpdate(userId int, tenantId string, req dao.Alarm) *common.Errors {
|
|
|
|
|
+ device := req //创建一个名为device的变量,将传入的req赋值给它
|
|
|
device.TenantId = tenantId
|
|
device.TenantId = tenantId
|
|
|
device.UpdateUser = userId
|
|
device.UpdateUser = userId
|
|
|
device.UpdateTime = time.Now()
|
|
device.UpdateTime = time.Now()
|
|
|
|
|
+ if req.ID == 0 { //判断传入的req是否没有ID
|
|
|
|
|
+ device.CreateTime = time.Now() //如果req.ID为0,则为device的成员变量CreateTime赋值当前时间
|
|
|
|
|
+ device.CreateUser = userId //device的成员变量CreateUser为传入的userid
|
|
|
|
|
|
|
|
- if req.ID == 0 {
|
|
|
|
|
- device.CreateTime = time.Now()
|
|
|
|
|
- device.CreateUser = userId
|
|
|
|
|
-
|
|
|
|
|
- if device.IsExistedBySN() {
|
|
|
|
|
- logger.Logger.Errorf("Create IsExistedBySN \n")
|
|
|
|
|
- return common.ParamsInvalidResponse(model.RepeatedName, nil)
|
|
|
|
|
|
|
+ if device.IsExistedBySN() { //判断设备是否已经存在
|
|
|
|
|
+ logger.Logger.Errorf("Create IsExistedBySN \n") //如果设备已经存在,则记录错误日志
|
|
|
|
|
+ return common.ParamsInvalidResponse(model.RepeatedName, nil) //返回参数无效的错误响应
|
|
|
}
|
|
}
|
|
|
- if err := device.Create(); err != nil {
|
|
|
|
|
- logger.Logger.Errorf("Create err = %s \n", err.Error())
|
|
|
|
|
- return common.FailResponse(err.Error(), nil)
|
|
|
|
|
|
|
+ if err := device.Create(); err != nil { //尝试创建设备
|
|
|
|
|
+ logger.Logger.Errorf("Create err = %s \n", err.Error()) //如果创建失败,则记录错误日志
|
|
|
|
|
+ return common.FailResponse(err.Error(), nil) //返回创建失败的错误响应
|
|
|
}
|
|
}
|
|
|
service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
|
|
service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
|
|
|
- common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
|
|
|
|
|
- return common.SuccessResponse(common.Succeeded, nil)
|
|
|
|
|
|
|
+ common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess) //保存操作历史记录
|
|
|
|
|
+ return common.SuccessResponse(common.Succeeded, nil) //返回成功响应
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if device.IsExistedByNameAndCode() {
|
|
|
|
|
- logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
|
|
|
|
|
- return common.ParamsInvalidResponse(model.RepeatedName, nil)
|
|
|
|
|
|
|
+ if device.IsExistedByNameAndCode() { //判断设备是否已经存在
|
|
|
|
|
+ logger.Logger.Errorf("Update IsExistedByNameAndCode \n") //如果设备已经存在,则记录错误日志
|
|
|
|
|
+ return common.ParamsInvalidResponse(model.RepeatedName, nil) //返回参数无效的错误响应
|
|
|
}
|
|
}
|
|
|
- if err := device.Update(); err != nil {
|
|
|
|
|
- logger.Logger.Errorf("Update err = %s \n", err.Error())
|
|
|
|
|
- return common.FailResponse(err.Error(), nil)
|
|
|
|
|
|
|
+ if err := device.Update(); err != nil { //尝试更新设备
|
|
|
|
|
+ logger.Logger.Errorf("Update err = %s \n", err.Error()) //如果更新失败,则记录错误日志
|
|
|
|
|
+ return common.FailResponse(err.Error(), nil) //返回更新失败的错误响应
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
|
|
service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
|
|
|
- common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess)
|
|
|
|
|
- return common.SuccessResponse(common.Succeeded, nil)
|
|
|
|
|
|
|
+ common.DeviceTypeAlarmServer, common.GetDeviceObject(device.ID, device.ServeName), common.OperationSuccess) //保存操作历史记录
|
|
|
|
|
+ return common.SuccessResponse(common.Succeeded, nil) //返回成功响应
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 获取所有设备
|
|
|
func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, int64, *common.Errors) {
|
|
func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, int64, *common.Errors) {
|
|
|
// 创建查询实例
|
|
// 创建查询实例
|
|
|
device := &dao.Alarm{}
|
|
device := &dao.Alarm{}
|
|
@@ -81,7 +83,8 @@ func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alar
|
|
|
return devices, total, nil
|
|
return devices, total, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (s *alarmService) Remove(userId int64, tenantId string, id int) *common.Errors {
|
|
|
|
|
|
|
+// 移除设备
|
|
|
|
|
+func (s *alarmService) Remove(userId int, tenantId string, id int) *common.Errors {
|
|
|
// 创建查询实例
|
|
// 创建查询实例
|
|
|
device := &dao.Alarm{
|
|
device := &dao.Alarm{
|
|
|
ID: id,
|
|
ID: id,
|
|
@@ -98,36 +101,40 @@ func (s *alarmService) Remove(userId int64, tenantId string, id int) *common.Err
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 获取设备列表
|
|
|
func (s *alarmService) GetList(tenantId string) ([]*dao.Alarm, *common.Errors) {
|
|
func (s *alarmService) GetList(tenantId string) ([]*dao.Alarm, *common.Errors) {
|
|
|
- var devices []*dao.Alarm
|
|
|
|
|
- err := cache.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices)
|
|
|
|
|
- if err == nil {
|
|
|
|
|
- return devices, nil
|
|
|
|
|
|
|
+ var devices []*dao.Alarm //声明一个名为devices的变量,类型为指向dao.Alarm结构体的指针的切片
|
|
|
|
|
+ err := cache.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices) //从Redis缓存中获取指定租户ID的设备列表,并将结果存储在devices变量中
|
|
|
|
|
+ if err == nil { //判断是否成功从Redis缓存中获取设备列表 //
|
|
|
|
|
+ return devices, nil //如果成功获取设备列表,则返回设备列表和nil错误
|
|
|
}
|
|
}
|
|
|
- device := &dao.Alarm{
|
|
|
|
|
|
|
+ device := &dao.Alarm{ //创建一个新的dao.Alarm结构体实例,并将其赋值给device变量
|
|
|
TenantId: tenantId,
|
|
TenantId: tenantId,
|
|
|
IsDeleted: 0,
|
|
IsDeleted: 0,
|
|
|
}
|
|
}
|
|
|
- devices, err = device.GetAllDevices()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil, common.FailResponse(err.Error(), nil)
|
|
|
|
|
|
|
+ devices, err = device.GetAllDevices() //调用device的GetAllDevices方法,获取所有设备列表,并将结果存储在devices变量中
|
|
|
|
|
+ if err != nil { //判断是否获取设备列表时发生错误
|
|
|
|
|
+ return nil, common.FailResponse(err.Error(), nil) //如果发生错误,则返回nil和错误响应
|
|
|
}
|
|
}
|
|
|
- _ = cache.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err()
|
|
|
|
|
- return devices, nil
|
|
|
|
|
|
|
+ _ = cache.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err() //将获取到的设备列表存储到Redis缓存中,并设置过期时间为0
|
|
|
|
|
+ return devices, nil //返回设备列表和nil错误
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 获取报警列表redis密钥
|
|
|
func getAlarmListRedisKey(tenantId string) string {
|
|
func getAlarmListRedisKey(tenantId string) string {
|
|
|
- return fmt.Sprintf(model.AlarmList, tenantId)
|
|
|
|
|
|
|
+ return fmt.Sprintf(model.AlarmList, tenantId) //使用fmt.Sprintf函数将model.AlarmList和tenantId格式化为一个字符串,并将其作为方法的返回值
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 获取设备的数量
|
|
|
func (s *alarmService) DeviceCount() dao.DeviceStatus {
|
|
func (s *alarmService) DeviceCount() dao.DeviceStatus {
|
|
|
- var rsp dao.DeviceStatus
|
|
|
|
|
- d := dao.Alarm{}
|
|
|
|
|
- rsp.Set(d.DeviceCount1())
|
|
|
|
|
- return rsp
|
|
|
|
|
|
|
+ var rsp dao.DeviceStatus //声明一个名为rsp的变量,类型为指向dao.DeviceStatus结构体的指针
|
|
|
|
|
+ d := dao.Alarm{} //声明一个名为d的变量,类型为dao.Alarm结构体
|
|
|
|
|
+ rsp.Set(d.DeviceCount1()) //调用rsp的Set方法,将d.DeviceCount1()的结果作为参数传入
|
|
|
|
|
+ return rsp //返回rsp变量
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 设备列表
|
|
|
func (s *alarmService) ListDevices() []dao.Device {
|
|
func (s *alarmService) ListDevices() []dao.Device {
|
|
|
- d := dao.Alarm{}
|
|
|
|
|
- return d.ListDevices1()
|
|
|
|
|
|
|
+ d := dao.Alarm{} //声明一个d的变量,类型为dao.Alarm结构体
|
|
|
|
|
+ return d.ListDevices1() //返回设备的列表
|
|
|
}
|
|
}
|