package service import ( "fmt" "iot_manager_service/app/device/dao" "iot_manager_service/app/device/model" "iot_manager_service/util" "strings" "time" ) // 中间件管理服务 var LampPoleGroupService = new(lampPoleGroupService) type lampPoleGroupService struct{} func (s *lampPoleGroupService) Get(id int) (*dao.LampPoleGroup, *util.Errors) { // 创建查询实例 device := &dao.LampPoleGroup{ ID: id, } err := device.GetDevice() if err != nil { return nil, util.FailResponse(err.Error(), nil) } return device, nil } func (s *lampPoleGroupService) CreateOrUpdate(userId int64, tenantId int, req *dao.LampPoleGroup) *util.Errors { var build strings.Builder device := req device.TenantId = tenantId device.UpdateUser = userId device.UpdateTime = time.Now() if req.ID == 0 { device.CreateTime = time.Now() device.CreateUser = userId build.WriteString("新增(灯杆分组)【" + device.PoleGroupName + "】") if device.IsExistedByName() { fmt.Printf("Create IsExistedByName \n") return util.ParamsInvalidResponse(model.RepeatedName, nil) } if err := device.Create(); err != nil { fmt.Printf("Create err = %s \n", err.Error()) build.WriteString("失败") OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String()) return util.FailResponse(err.Error(), nil) } build.WriteString("成功") OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String()) return util.SuccessResponse(util.Succeeded, nil) } if device.IsExistedByNameAndCode() { fmt.Printf("Update IsExistedByNameAndCode \n") return util.ParamsInvalidResponse(model.RepeatedName, nil) } build.WriteString("修改(灯杆分组)【" + device.PoleGroupName + "】") if err := device.Update(); err != nil { fmt.Printf("Update err = %s \n", err.Error()) build.WriteString("失败") OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String()) return util.FailResponse(err.Error(), nil) } build.WriteString("成功") OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String()) //todo operation record return util.SuccessResponse(util.Succeeded, nil) } func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *util.Errors) { // 创建查询实例 device := &dao.LampPoleGroup{ PoleGroupName: poleGroupName, } 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 *lampPoleGroupService) Remove(userId int64, tenantId int, id int, name string) *util.Errors { var build strings.Builder device := &dao.LampPoleGroup{ ID: id, IsDeleted: 1, UpdateUser: userId, UpdateTime: time.Now(), } //todo // service.lampPoleService.CountRelation() build.WriteString("删除(灯杆分组)【" + name + "】") err := device.Delete() if err != nil { build.WriteString("失败") OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String()) return util.FailResponse(err.Error(), nil) } build.WriteString("成功") OperationHisService.Save(userId, tenantId, model.OperationRemove, model.ModuleTypeDevice, build.String()) return nil } func (s *lampPoleGroupService) GetList(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) { var devices []*dao.LampPoleGroup err := util.Redis.Get(getLampPoleGroupListRedisKey(tenantId)).Scan(&devices) if err == nil { return devices, nil } device := &dao.LampPoleGroup{ TenantId: tenantId, IsDeleted: 0, } devices, err = device.GetAllDevices() if err != nil { return nil, util.FailResponse(err.Error(), nil) } _ = util.Redis.Set(getLampPoleGroupListRedisKey(tenantId), devices, 0).Err() return devices, nil } func getLampPoleGroupListRedisKey(tenantId int) string { return fmt.Sprintf(model.LampPoleGroupList, tenantId) } func (s *lampPoleGroupService) GetFiltration(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) { var devices []*dao.LampPoleGroup err := util.Redis.Get(getGroupFiltrationListRedisKey(tenantId)).Scan(&devices) if err == nil { return devices, nil } device := &dao.LampPoleGroup{ TenantId: tenantId, IsDeleted: 0, } //todo // get t_dev_light_control group_id // groupIds := lightControl.GetGroupIds() devices, err = device.GetAllDevices() if err != nil { return nil, util.FailResponse(err.Error(), nil) } _ = util.Redis.Set(getGroupFiltrationListRedisKey(tenantId), devices, 0).Err() return devices, nil } func getGroupFiltrationListRedisKey(tenantId int) string { return fmt.Sprintf(model.GroupFiltrationList, tenantId) } func (s *lampPoleGroupService) GetTree(tenantId int) ([]*dao.LampPoleGroup, *util.Errors) { // todo use redis cache device := &dao.LampPoleGroup{ TenantId: tenantId, IsDeleted: 0, } //todo lampPole getALlDevice devices, err := device.GetAllDevices() if err != nil { return nil, util.FailResponse(err.Error(), nil) } return devices, nil }