package service import ( "fmt" "iot_manager_service/app/device/dao" "iot_manager_service/app/device/model" "iot_manager_service/util" "time" ) // 中间件管理服务 var LightStrategyService = new(lightStrategyService) type lightStrategyService struct{} func (s *lightStrategyService) Get(id int) (*dao.LightStrategy, *util.Errors) { // 创建查询实例 device := &dao.LightStrategy{ ID: id, } err := device.GetDevice() if err != nil { return nil, util.FailResponse(err.Error(), nil) } return device, nil } func (s *lightStrategyService) CreateOrUpdate(req model.LightStrategyDetail) *util.Errors { // 创建查询实例 device := req if device.TenantID == "" { device.TenantID = "000000" // todo: 使用登录态 } device.UpdateUser = "TODO" // todo: 使用登录态 device.UpdateTime = time.Now() lightConditions := device.LightConditionList timeConditions := device.TimeConditionList if len(lightConditions) == 0 { lightConditionDetailList := device.LightConditionDetailList for i := 0; i < len(lightConditionDetailList); i++ { var detail = lightConditionDetailList[i] condition := dao.LightCondition{ ID: detail.ID, Remark: detail.Remark, LightId: detail.LightId, Luminance: detail.Luminance, ScopeStart: detail.ScopeStart, ScopeEnd: detail.ScopeEnd, } lightConditions = append(lightConditions, condition) } } if len(timeConditions) == 0 { timeConditionDetailList := device.TimeConditionDetailList for i := 0; i < len(timeConditionDetailList); i++ { var detail = timeConditionDetailList[i] condition := dao.TimeCondition{ ID: detail.ID, Remark: detail.Remark, LightId: detail.LightId, Luminance: detail.Luminance, StartTime: detail.StartTime, EndTime: detail.EndTime, Sunshine: detail.Sunshine, } timeConditions = append(timeConditions, condition) } } if req.ID == 0 { device.CreateTime = time.Now() device.CreateUser = "TODO" // todo: 使用登录态 if device.IsExistedBySN() { fmt.Printf("Create GetDeviceID err \n") return util.ParamsInvalidResponse("编码不能重复,请重新填写!", 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() { return util.ParamsInvalidResponse("编码不能重复,请重新填写!", 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 *lightStrategyService) List(searchValue string, current, size int) ([]dao.LightStrategy, *util.Errors) { device := dao.LightStrategy{} if searchValue != "" { device.LightSn = searchValue device.LightName = searchValue } 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 *lightStrategyService) Remove(id int) *util.Errors { // 创建查询实例 device := &dao.LightStrategy{ ID: id, IsDeleted: 1, UpdateUser: "TODO", // todo 使用登录态 UpdateTime: time.Now(), } //todo // service.transformerService.CountRelation() //todo operation record err := device.Delete() if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *lightStrategyService) GetOne(id int) (*dao.LightStrategy, *util.Errors) { device := &dao.LightStrategy{ ID: id, } err := device.GetDevice() if err != nil { return nil, nil } return device, nil }