package service import ( "fmt" "iot_manager_service/app/dao" "iot_manager_service/app/model" "iot_manager_service/app/utils" "time" ) // 中间件管理服务 var LightControlService = new(lightControlService) type lightControlService struct{} func (s *lightControlService) Get(id int) (*dao.LightControl, *utils.Errors) { // 创建查询实例 device := &dao.LightControl{ ID: id, } err := device.GetDevice() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } //todo runstate 需要使用各个设备的状态来处理 return device, nil } func (s *lightControlService) GetRelevanceDetail(id int) (*model.LightControlDetail, *utils.Errors) { // 创建查询实例 device := &dao.LightControl{ ID: id, } err := device.GetDevice() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } //todo get gateway ipBroadcast lightcontroller... list return &model.LightControlDetail{ LightControl: *device, AlarmTerminalList: nil, CameraList: nil, CaptureUnitList: nil, GatewayList: nil, InfoBoardList: nil, IpBroadcastList: nil, LightControlList: nil, SensorList: nil, ZigbeeList: nil, }, nil } func (s *lightControlService) CreateOrUpdate(req *dao.LightControl) *utils.Errors { device := req if device.TenantId == "" { device.TenantId = "000000" // todo: 使用登录态 } device.UpdateUser = "TODO" // todo: 使用登录态 device.UpdateTime = time.Now() if device.ID == 0 { device.CreateTime = time.Now() device.CreateUser = "TODO" // todo: 使用登录态 if device.IsExistedBySN() { fmt.Printf("Create IsExistedBySN \n") return utils.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 utils.FailResponse(err.Error(), nil) } return utils.SuccessResponse(utils.Succeeded, nil) } if err := device.Update(); err != nil { fmt.Printf("Update err = %s \n", err.Error()) return utils.FailResponse(err.Error(), nil) } //todo operation record return utils.SuccessResponse(utils.Succeeded, nil) } func (s *lightControlService) List(searchValue, groupId, boxId string, current, size int) ([]dao.LightControl, *utils.Errors) { device := dao.LightControl{} if searchValue != "" { device.SN = searchValue } device.GroupId = utils.StringToInt(groupId) offset := (current - 1) * size limit := size devices, err := device.GetDevices(offset, limit) if err != nil { return nil, utils.FailResponse(err.Error(), nil) } return devices, nil } func (s *lightControlService) Remove(id int) *utils.Errors { // 创建查询实例 device := &dao.LightControl{ ID: id, IsDeleted: 1, UpdateUser: "TODO", // todo 使用登录态 UpdateTime: time.Now(), } //todo // service.lampPoleService.CountRelation() //todo operation record err := device.Delete() if err != nil { return utils.FailResponse(err.Error(), nil) } return nil } func (s *lightControlService) GetList() ([]*dao.LightControl, *utils.Errors) { // todo use redis cache device := &dao.LightControl{ TenantId: "000000", // todo 使用登录态 IsDeleted: 0, } devices, err := device.GetAllDevices() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } return devices, nil } func (s *lightControlService) GetOne(id int) (*dao.LightControl, *utils.Errors) { device := &dao.LightControl{ ID: id, } err := device.GetDevice() if err != nil { return nil, utils.FailResponse(err.Error(), nil) } return device, nil }