package service import ( "fmt" "iot_manager_service/app/device/dao" "iot_manager_service/app/device/model" "iot_manager_service/util" "strconv" "time" ) // 中间件管理服务 var CameraService = new(cameraService) type cameraService struct{} func (s *cameraService) CreateOrUpdate(userId int64, tenantId int, req *model.CameraDetail) *util.Errors { device := req.CameraDevice device.TenantId = tenantId device.UpdateUser = userId device.UpdateTime = time.Now() if device.LampPoleId != 0 { lampPole, err := LampPoleService.GetOne(device.LampPoleId) if err == nil { device.LampLat = lampPole.PoleLat device.LampLng = lampPole.PoleLng device.LampPoleName = lampPole.PoleName device.LampPoleSn = lampPole.PoleSN device.LampPoleLocation = lampPole.InstallLocation device.GroupId = lampPole.GroupId } else { fmt.Printf("LampPoleService.GetOne err = %v \n", err) } } if device.ID == 0 { device.CreateTime = time.Now() device.CreateUser = userId if device.IsExistedBySN() { fmt.Printf("Create IsExistedBySN \n") return util.ParamsInvalidResponse(model.RepeatedPrompts, nil) } fmt.Printf("device = %+v \n", device) 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 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 *cameraService) Get(id int) (*model.CameraDetail, *util.Errors) { // 创建查询实例 device := &dao.CameraDevice{ ID: id, } err := device.GetDevice() if err != nil { return nil, util.FailResponse(err.Error(), nil) } detail := &model.CameraDetail{CameraDevice: *device} if device.CameraType == model.CameraTypeBall { detail.CameraTypeName = model.CameraTypeBallName } else if device.CameraType == model.CameraTypeGun { detail.CameraTypeName = model.CameraTypeGunName } //todo 获取运行状态 // runState = forVidiconService.queryRtStatus(); return detail, nil } func (s *cameraService) List(searchValue, cameraType string, current, size int) ([]model.CameraDetail, *util.Errors) { var details []model.CameraDetail device := dao.CameraDevice{} if searchValue != "" { device.DeviceSN = searchValue } if cameraType != "" { t, _ := strconv.Atoi(cameraType) device.CameraType = t } else { device.CameraType = -1 } offset := (current - 1) * size limit := size devices, err := device.GetDevices(offset, limit) if err != nil { return nil, util.FailResponse(err.Error(), nil) } for _, d := range devices { details = append(details, model.CameraDetail{ CameraDevice: d, RunState: "", // todo queryRtStatus }) } return details, nil } func (s *cameraService) Remove(userId int64, id int) *util.Errors { // 创建查询实例 device := &dao.CameraDevice{ ID: id, IsDeleted: 1, UpdateUser: userId, UpdateTime: time.Now(), } //todo operation record err := device.Delete() if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *cameraService) GetList(tenantId int) ([]dao.CameraDevice, *util.Errors) { device := &dao.CameraDevice{ TenantId: tenantId, IsDeleted: 0, } devices, err := device.GetAllDevices() var results []dao.CameraDevice // todo 不要返回多余字段,只需要id和name for _, device := range devices { results = append(results, dao.CameraDevice{ID: device.ID, DeviceName: device.DeviceName + "(" + device.DeviceSN + ")"}) } if err != nil { return nil, util.FailResponse(err.Error(), nil) } return results, nil } func (s *cameraService) Enable(id, status int) *util.Errors { // 创建查询实例 device := &dao.CameraDevice{ ID: id, IsEnable: status, } err := device.UpdateEnable() if err != nil { return util.FailResponse(err.Error(), nil) } return nil }