123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- package service
- import (
- "fmt"
- "iot_manager_service/app/device/dao"
- "iot_manager_service/app/device/model"
- "iot_manager_service/util"
- "sort"
- "time"
- )
- // 中间件管理服务
- var LightStrategyService = new(lightStrategyService)
- type lightStrategyService struct{}
- func (s *lightStrategyService) Get(id int) (*model.LightStrategyDetail, *util.Errors) {
- // 创建查询实例
- strategy := &dao.LightStrategy{
- ID: id,
- }
- err := strategy.GetStrategy()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- lightStrategy := &model.LightStrategyDetail{
- LightStrategy: *strategy,
- }
- lightStrategy.LightConditionList = LightConditionService.BatchGet([]int{id})
- lightStrategy.TimeConditionList = TimeConditionService.BatchGet([]int{id})
- if len(lightStrategy.TimeConditionList) > 0 {
- lightStrategy.LightType = "时间"
- }
- lightStrategy.LightType = lightStrategy.LightType + "策略"
- lightingList := IntelligentLightingService.BatchGet([]int{id})
- for _, lighting := range lightingList {
- // 关联类型1=灯杆,2=灯杆分组
- if lighting.RelationType == 2 {
- lampPole, _ := LampPoleService.Get(lighting.Rid)
- lightStrategy.LampPoleDetailList = append(lightStrategy.LampPoleDetailList,
- model.LampPoleDetail{LampPole: *lampPole})
- } else {
- lampPoleGroup, _ := LampPoleGroupService.Get(lighting.Rid)
- lightStrategy.LampPoleGroupDetailList = append(lightStrategy.LampPoleGroupDetailList,
- model.LampPoleGroupDetail{LampPoleGroup: *lampPoleGroup})
- }
- }
- return lightStrategy, nil
- }
- func (s *lightStrategyService) CreateOrUpdate(req *model.LightStrategyDetail) *util.Errors {
- // 创建查询实例
- strategy := req
- if strategy.TenantID == "" {
- strategy.TenantID = "000000" // todo: 使用登录态
- }
- strategy.UpdateUser = "TODO" // todo: 使用登录态
- strategy.UpdateTime = time.Now()
- if len(strategy.LightConditionList) == 0 {
- for i := 0; i < len(strategy.LightConditionDetailList); i++ {
- var detail = strategy.LightConditionDetailList[i]
- condition := dao.LightCondition{
- ID: detail.ID,
- Remark: detail.Remark,
- LightId: detail.LightId,
- Luminance: detail.Luminance,
- ScopeStart: detail.ScopeStart,
- ScopeEnd: detail.ScopeEnd,
- }
- strategy.LightConditionList = append(strategy.LightConditionList, condition)
- }
- }
- if len(strategy.TimeConditionList) == 0 {
- for i := 0; i < len(strategy.TimeConditionDetailList); i++ {
- var detail = strategy.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,
- }
- strategy.TimeConditionList = append(strategy.TimeConditionList, condition)
- }
- }
- if req.ID == 0 {
- strategy.CreateTime = time.Now()
- strategy.CreateUser = "TODO" // todo: 使用登录态
- if strategy.IsExistedBySN() {
- fmt.Printf("Create GetstrategyID err \n")
- return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
- }
- //todo 不能先存
- if err := strategy.Create(); err != nil {
- fmt.Printf("Create err = %s \n", err.Error())
- return util.FailResponse(err.Error(), nil)
- }
- return util.SuccessResponse(util.Succeeded, nil)
- }
- //todo 不能先存
- if err := strategy.Update(); err != nil {
- fmt.Printf("Update err = %s \n", err.Error())
- return util.FailResponse(err.Error(), nil)
- }
- //用于比较光照度重叠
- var overlapList []dao.LightCondition
- for _, lightCondition := range strategy.LightConditionList {
- if (lightCondition.ScopeStart != 0 && lightCondition.ScopeEnd == 0) || (lightCondition.
- ScopeStart == 0 && lightCondition.ScopeEnd != 0) {
- //todo 光照度请填写完整
- return nil
- } else if lightCondition.ScopeEnd > 0 && lightCondition.ScopeStart >= lightCondition.ScopeEnd {
- //todo 光照度起始值不能大于结束值
- return nil
- }
- overlapList = append(overlapList, lightCondition)
- if lightCondition.ID == 0 {
- lightCondition.CreateUser = "TODO" //todo 登录态
- lightCondition.CreateTime = time.Now()
- } else {
- lightCondition.UpdateUser = "TODO" //todo 登录态
- lightCondition.UpdateTime = time.Now()
- }
- lightCondition.LightId = req.ID
- }
- //光照条件查询是否区间重叠
- if len(overlapList) > 0 {
- sort.SliceIsSorted(overlapList, func(i, j int) bool {
- return overlapList[i].
- ScopeStart < overlapList[j].ScopeStart
- })
- tmp := -1
- for _, overLap := range overlapList {
- if tmp >= overLap.ScopeStart {
- //todo 区间有重叠
- return nil
- }
- tmp = overLap.ScopeEnd
- }
- }
- // 时间策略参数校验
- for _, timeCondition := range strategy.TimeConditionList {
- if timeCondition.Sunshine == 0 {
- timeCondition.Sunshine = 1
- }
- if timeCondition.Sunshine == 1 && (timeCondition.StartTime == "" && timeCondition.EndTime == "") {
- //todo 请将时间区间填写完整!
- return nil
- }
- if (timeCondition.StartTime == "" && timeCondition.EndTime != "") || (timeCondition.
- StartTime != "" && timeCondition.EndTime == "") {
- //todo 请将时间区间填写完整!
- return nil
- } else if timeCondition.EndTime != "" && timeCondition.StartTime >= timeCondition.EndTime {
- //todo 时间区间开始时间不能大于结束时间,请重新调整!
- return nil
- }
- if timeCondition.ID == 0 {
- timeCondition.CreateUser = "TODO" //todo 登录态
- timeCondition.CreateTime = time.Now()
- } else {
- timeCondition.UpdateUser = "TODO" //todo 登录态
- timeCondition.UpdateTime = time.Now()
- }
- timeCondition.LightId = req.ID
- }
- //todo create or update
- strategyDao := &dao.LightStrategy{}
- if strategy.ID == 0 {
- strategyDao.Create()
- } else {
- strategyDao.Update()
- }
- TimeConditionService.Save(strategy.TimeConditionList)
- LightConditionService.Save(strategy.LightConditionList)
- //todo 调用设备云端的新增或修改
- //todo operation record
- return util.SuccessResponse(util.Succeeded, nil)
- }
- func (s *lightStrategyService) List(searchValue string, current, size int) ([]dao.LightStrategy, *util.Errors) {
- strategy := dao.LightStrategy{}
- if searchValue != "" {
- strategy.LightSn = searchValue
- strategy.LightName = searchValue
- }
- offset := (current - 1) * size
- limit := size
- strategies, err := strategy.GetStrategies(offset, limit)
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- return strategies, nil
- }
- func (s *lightStrategyService) Remove(id int) *util.Errors {
- // 创建查询实例
- strategy := &dao.LightStrategy{
- ID: id,
- IsDeleted: 1,
- UpdateUser: "TODO", // todo 使用登录态
- UpdateTime: time.Now(),
- }
- //todo
- // service.transformerService.CountRelation()
- //todo operation record
- err := strategy.Delete()
- if err != nil {
- return util.FailResponse(err.Error(), nil)
- }
- return nil
- }
- func (s *lightStrategyService) GetOne(id int) (*dao.LightStrategy, *util.Errors) {
- strategy := &dao.LightStrategy{
- ID: id,
- }
- err := strategy.GetStrategy()
- if err != nil {
- return nil, nil
- }
- return strategy, nil
- }
|