package service import ( "fmt" "iot_manager_service/app/device/dao" "iot_manager_service/app/device/model" "iot_manager_service/util" "time" ) // 中间件管理服务 var AlarmService = new(alarmService) type alarmService struct{} func (s *alarmService) Get(id int) (*dao.Alarm, *util.Errors) { // 创建查询实例 device := &dao.Alarm{ ID: id, } err := device.GetDevice() if err != nil { return nil, util.FailResponse(err.Error(), nil) } return device, nil } func (s *alarmService) CreateOrUpdate(userId int64, tenantId int, req dao.Alarm) *util.Errors { device := req device.TenantId = tenantId device.UpdateUser = userId device.UpdateTime = time.Now() if req.ID == 0 { device.CreateTime = time.Now() device.CreateUser = userId if device.IsExistedBySN() { fmt.Printf("Create IsExistedBySN \n") return util.ParamsInvalidResponse(model.RepeatedName, nil) } if err := device.Create(); err != nil { fmt.Printf("Create err = %s \n", err.Error()) return util.FailResponse(err.Error(), nil) } return util.SuccessResponse(util.Succeeded, nil) } if device.IsExistedByNameAndCode() { fmt.Printf("Update IsExistedByNameAndCode \n") return util.ParamsInvalidResponse(model.RepeatedName, nil) } if err := device.Update(); err != nil { fmt.Printf("Update err = %s \n", err.Error()) return util.FailResponse(err.Error(), nil) } //todo operation record return util.SuccessResponse(util.Succeeded, nil) } func (s *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, *util.Errors) { // 创建查询实例 device := &dao.Alarm{} offset := (current - 1) * size limit := size devices, err := device.GetDevices(offset, limit) if err != nil { return nil, util.FailResponse(err.Error(), nil) } return devices, nil } func (s *alarmService) Remove(userId int64, id int) *util.Errors { // 创建查询实例 device := &dao.Alarm{ ID: id, IsDeleted: 1, UpdateUser: userId, UpdateTime: time.Now(), } //todo // service.lampPoleService.CountRelation() //todo operation record err := device.Delete() if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *alarmService) GetList(tenantId int) ([]*dao.Alarm, *util.Errors) { var devices []*dao.Alarm err := util.Redis.Get(getAlarmListRedisKey(tenantId)).Scan(&devices) if err == nil { return devices, nil } device := &dao.Alarm{ TenantId: tenantId, IsDeleted: 0, } devices, err = device.GetAllDevices() if err != nil { return nil, util.FailResponse(err.Error(), nil) } _ = util.Redis.Set(getAlarmListRedisKey(tenantId), devices, 0).Err() return devices, nil } func getAlarmListRedisKey(tenantId int) string { return fmt.Sprintf(model.AlarmList, tenantId) }