package service import ( "fmt" "iot_manager_service/app/device/dao" "iot_manager_service/app/device/model" "iot_manager_service/app/system/service" "iot_manager_service/util/cache" "iot_manager_service/util/common" "iot_manager_service/util/logger" "time" ) var ZigbeeService = new(zigbeeService) type zigbeeService struct{} func (s *zigbeeService) Get(id int) (*model.ZigbeeDetail, *common.Errors) { // 创建查询实例 device := &dao.Zigbee{ ID: id, } err := device.GetDevice() if err != nil { return nil, common.FailResponse(err.Error(), nil) } endTime, state := cache.GetDeviceState(device.Sn) return &model.ZigbeeDetail{ Zigbee: *device, RunState: state, NetworkState: state, EndLineTime: endTime, }, nil } func (s *zigbeeService) CreateOrUpdate(userId int64, tenantId int, req dao.Zigbee) *common.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() { logger.Logger.Errorf("Create GetDeviceID err \n") return common.ParamsInvalidResponse("编码不能重复,请重新填写!", 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, common.DeviceTypeZigbee, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess) return common.SuccessResponse(common.Succeeded, nil) } if device.IsExistedByNameAndCode() { return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil) } fmt.Printf("device = %v", device) 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, common.DeviceTypeZigbee, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess) return common.SuccessResponse(common.Succeeded, nil) } func (s *zigbeeService) List(searchValue string, current, size int) ([]model.ZigbeeDetail, *common.Errors) { device := dao.Zigbee{} if searchValue != "" { device.Sn = searchValue device.Name = searchValue } offset := (current - 1) * size limit := size devices, err := device.GetDevices(offset, limit) if err != nil { return nil, common.FailResponse(err.Error(), nil) } var details []model.ZigbeeDetail for _, d := range devices { endTime, state := cache.GetDeviceState(d.Sn) details = append(details, model.ZigbeeDetail{ Zigbee: d, RunState: state, NetworkState: state, EndLineTime: endTime, }) } return details, nil } func (s *zigbeeService) Remove(userId int64, tenantId int, id int) *common.Errors { // 创建查询实例 device := &dao.Zigbee{ ID: id, IsDeleted: 1, UpdateUser: userId, UpdateTime: time.Now(), } err := device.Delete() if err != nil { return common.FailResponse(err.Error(), nil) } service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice, common.DeviceTypeZigbee, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess) return nil } func (s *zigbeeService) GetList(tenantId int) ([]dao.Zigbee, *common.Errors) { device := &dao.Zigbee{ TenantId: tenantId, IsDeleted: 0, } devices, err := device.GetAllDevices() if err != nil { return nil, common.FailResponse(err.Error(), nil) } return devices, nil } func (s *zigbeeService) GetByGateway(id int) []dao.Zigbee { // 创建查询实例 device := &dao.Zigbee{ GatewayId: id, } return device.GetDevicesByGateway() } func (s *zigbeeService) GetByLampPole(id int) []dao.Zigbee { // 创建查询实例 device := &dao.Zigbee{ LampPoleId: id, } return device.GetDevicesByLampPole() }