terry пре 3 година
родитељ
комит
4427688ba7

+ 39 - 0
app/device/controller/IntelligentLightingStrategy.go

@@ -2,6 +2,11 @@ package controller
 
 import (
 	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/device/model"
+	"iot_manager_service/app/device/service"
+	"iot_manager_service/util"
+	"net/http"
+	"strconv"
 )
 
 var IntelligentLighting = new(intelligentLightingCtl)
@@ -9,11 +14,45 @@ var IntelligentLighting = new(intelligentLightingCtl)
 type intelligentLightingCtl struct{}
 
 func (c *intelligentLightingCtl) Detail(ctx *gin.Context) {
+	publicId, e := strconv.Atoi(ctx.Query("publicId"))
+	if e != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
+		return
+	}
+	if publicId == 0 {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.RelationIdInvalid, nil))
+		return
+	}
 
+	//当前类型1=灯杆2=灯杆分组
+	relationType, e := strconv.Atoi(ctx.Query("type"))
+	if e != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(e.Error(), nil))
+		return
+	}
+	if relationType == model.RelationTypeLight {
+		detail, err := service.IntelligentLightingService.GetDetailByLight(publicId)
+		if err != nil {
+			ctx.JSON(http.StatusOK, err)
+			return
+		}
+		ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, detail))
+	} else if relationType == model.RelationTypeLampPoleGroup {
+		detail, err := service.IntelligentLightingService.GetDetailByGroup(publicId)
+		if err != nil {
+			ctx.JSON(http.StatusOK, err)
+			return
+		}
+		ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, detail))
+	} else {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.RelationTypeInvalid, nil))
+	}
 }
+
 func (c *intelligentLightingCtl) List(ctx *gin.Context) {
 
 }
+
 func (c *intelligentLightingCtl) RelationList(ctx *gin.Context) {
 
 }

+ 0 - 26
app/device/controller/LightRelationStrategy.go

@@ -1,26 +0,0 @@
-package controller
-
-import "github.com/gin-gonic/gin"
-
-var LightRelation = new(lightRelationCtl)
-
-type lightRelationCtl struct{}
-
-func (c *lightRelationCtl) Detail(ctx *gin.Context) {
-
-}
-func (c *lightRelationCtl) List(ctx *gin.Context) {
-
-}
-func (c *lightRelationCtl) Remove(ctx *gin.Context) {
-
-}
-func (c *lightRelationCtl) RelationList(ctx *gin.Context) {
-
-}
-func (c *lightRelationCtl) Relation(ctx *gin.Context) {
-
-}
-func (c *lightRelationCtl) ChangeHandSwitch(ctx *gin.Context) {
-
-}

+ 2 - 0
app/device/dao/common.go

@@ -36,6 +36,8 @@ func InitDB(db *gorm.DB) {
 		&IpBroadcast{},
 		&ManholeCover{},
 		&OptoSensor{},
+		&IntelligentLight{},
+		&TimeCondition{},
 	).Error
 	if err != nil {
 		panic(fmt.Sprintf("AutoMigrate err : %v", err))

+ 16 - 9
app/device/dao/intelligentLightingDao.go

@@ -4,10 +4,10 @@ import (
 	"time"
 )
 
-type IntelligentLighting struct {
+type IntelligentLight struct {
 	ID           int       `gorm:"primary key" json:"id"`               //编号
-	LightID      int       `gorm:"type: int" json:"lightID"`            //照明策略id
-	RelationType int       `gorm:"type: int" json:"relationType"`       //关联类型1=灯控,2=灯杆分组
+	LightID      int       `gorm:"type:int" json:"lightID"`             //照明策略id
+	RelationType int       `gorm:"type:int" json:"relationType"`        //关联类型1=灯控,2=灯杆分组
 	Rid          int       `gorm:"type:int" json:"rid"`                 //关联外键ID 根据type类型关联至其表
 	CreateTime   time.Time `gorm:"type:timestamp" json:"createTime"`    //新增时间
 	CreateUser   string    `gorm:"type:varchar(60)" json:"createUser"`  //新增记录操作用户ID
@@ -17,12 +17,19 @@ type IntelligentLighting struct {
 	TenantID     string    `gorm:"type:varchar(12)" json:"tenantID"`    //租户id
 }
 
-func (IntelligentLighting) Table() string {
-	return "t_str_intelligent_lighting"
+func (IntelligentLight) TableName() string {
+	return "t_strategy_intelligent_light"
 }
 
-func (c *IntelligentLighting) BatchGet(ids []int) ([]IntelligentLighting, error) {
-	var intelligentLightings []IntelligentLighting
-	err := Db.Model(&c).Where("light_id in ?", ids).Find(&intelligentLightings).Error
-	return intelligentLightings, err
+func (c *IntelligentLight) BatchGet(ids []int) ([]IntelligentLight, error) {
+	var intelligentLights []IntelligentLight
+	err := Db.Model(&c).Where("light_id in ? and is_deleted = 0", ids).Find(&intelligentLights).Error
+	return intelligentLights, err
+}
+
+func (c *IntelligentLight) GetByRidAndType() ([]IntelligentLight, error) {
+	var intelligentLights []IntelligentLight
+	err := Db.Model(&c).Where("rid = ? and relation_type = ? and is_deleted = 0", c.Rid,
+		c.RelationType).Find(&intelligentLights).Error
+	return intelligentLights, err
 }

+ 7 - 0
app/device/dao/lampPoleDao.go

@@ -103,3 +103,10 @@ func (c LampPole) GetAllDevices() ([]*LampPole, error) {
 	err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
 	return devices, err
 }
+
+func (c LampPole) Count() int {
+	var count = 0
+	_ = Db.Model(&c).Where(" group_id = ? and is_deleted = ?",
+		c.GroupId, c.IsDeleted).Count(&count).Error
+	return count
+}

+ 6 - 0
app/device/dao/lightControlDao.go

@@ -89,6 +89,12 @@ func (c *LightControl) GetDevice() error {
 	return err
 }
 
+func (c *LightControl) GetDevicesByGroup() ([]LightControl, error) {
+	var devices []LightControl
+	err := Db.Model(&c).Where(" group_id = ? and is_deleted = 0", c.GroupId).Find(&devices).Error
+	return devices, err
+}
+
 func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
 	var devices []LightControl
 

+ 3 - 3
app/device/dao/timeConditionDao.go

@@ -6,12 +6,12 @@ type TimeCondition struct {
 	ID         int       `gorm:"primary key" json:"id"`               //编号
 	LightId    int       `gorm:"type: int" json:"lightId"`            //照明策略id
 	StartTime  string    `gorm:"type:time" json:"startTime"`          //开始时间
-	EndTime    string    `gorm:"type: time" json:"endTime"`           //结束时间
+	EndTime    string    `gorm:"type:time" json:"endTime"`            //结束时间
 	Luminance  int       `gorm:"type:int " json:"luminance"`          //开灯亮度
 	Remark     string    `gorm:"type:varchar(255)" json:"remark"`     //备注
-	CreateTime time.Time `gorm:"type: datetime" json:"createTime"`    //新增时间
+	CreateTime time.Time `gorm:"type:datetime" json:"createTime"`     //新增时间
 	CreateUser string    `gorm:"type:varchar(60)" json:"createUser"`  //新增记录操作用户ID
-	UpdateTime time.Time `gorm:"type: datetime" json:"updateTime"`    //修改时间
+	UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"`     //修改时间
 	UpdateUser string    `gorm:"type:varchar(60)" json:"updateUser"`  //修改用户
 	IsDeleted  int       `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
 	Sunshine   int       `gorm:"type:int;default 1" json:"sunshine"`  //日出日落 设置 2=开启,1=不开启

+ 34 - 0
app/device/model/intelligentLighting.go

@@ -1 +1,35 @@
 package model
+
+import "iot_manager_service/app/device/dao"
+
+const (
+	RelationIdInvalid         = "该记录未关联策略无法查看,请关联策略!"
+	RelationTypeInvalid       = "查询参数(关联类型)异常"
+	RelationTypeLight         = 1 //关联类型 灯控
+	RelationTypeLampPoleGroup = 2 //关联类型 灯杆分组
+)
+
+type RspIntelligentLightDetail struct {
+	LightControlList []dao.LightControl     `json:"lightControlList"`
+	LampPoleName     string                 `json:"lampPoleName"`
+	LightRelMap      IntelligentLightSimple `json:"lightRelMap"`
+}
+
+type RspIntelligentLightDetailByGroup struct {
+	LightControlList  []dao.LightControl     `json:"lightControlList"`
+	LampPoleGroupName string                 `json:"lampPoleGroupName"`
+	CountLampPole     int                    `json:"countLampPole"`
+	LightRelMap       IntelligentLightSimple `json:"lightRelMap"`
+}
+
+type IntelligentLightSimple struct {
+	TimeConditionList []TimeConditionSimple `json:"timeConditionList"` //时间条件列表
+	LightName         string                `json:"lightName"`         //灯控名称
+	LightSn           string                `json:"lightSn"`           //灯控SN
+	EffectiveDate     string                `json:"effectiveDate"`     //生效日期
+}
+
+type TimeConditionSimple struct {
+	Luminance string `json:"luminance"`
+	Times     string `json:"times"`
+}

+ 42 - 2
app/device/service/intelligentLightingService.go

@@ -2,15 +2,55 @@ package service
 
 import (
 	"iot_manager_service/app/device/dao"
+	"iot_manager_service/app/device/model"
+	"iot_manager_service/util"
 )
 
 var IntelligentLightingService = new(intelligentLightingService)
 
 type intelligentLightingService struct{}
 
-func (s *intelligentLightingService) BatchGet(ids []int) []dao.IntelligentLighting {
+func (s *intelligentLightingService) GetDetailByLight(id int) (model.RspIntelligentLightDetail, *util.Errors) {
+	detail := model.RspIntelligentLightDetail{}
+	lightControl, _ := LightControlService.GetOne(id)
+	if lightControl != nil {
+		lightControl.LampPoleName = lightControl.LampPoleName + "(" + lightControl.LampPoleSN + ")"
+		detail.LightControlList = []dao.LightControl{*lightControl}
+		detail.LampPoleName = lightControl.LampPoleName
+	}
+	detail.LightRelMap = s.GetLightRelation(id, model.RelationTypeLight)
+	return detail, nil
+}
+
+func (s *intelligentLightingService) GetDetailByGroup(id int) (model.RspIntelligentLightDetailByGroup, *util.Errors) {
+	detail := model.RspIntelligentLightDetailByGroup{}
+	lampPoleGroup, _ := LightControlService.GetByGroupId(id)
+	if len(lampPoleGroup) > 0 {
+		detail.LightControlList = lampPoleGroup
+		detail.LampPoleGroupName = lampPoleGroup[0].LampPoleName
+	}
+	detail.CountLampPole = LampPoleService.CountLampPole(id)
+	detail.LightRelMap = s.GetLightRelation(id, model.RelationTypeLampPoleGroup)
+	return detail, nil
+}
+
+func (s *intelligentLightingService) GetLightRelation(id, relationType int) model.IntelligentLightSimple {
+	detail := model.IntelligentLightSimple{}
+	intelligentLight := dao.IntelligentLight{Rid: id, RelationType: relationType}
+	relations, _ := intelligentLight.GetByRidAndType()
+	if len(relations) > 0 {
+		light, _ := LightControlService.Get(relations[0].LightID)
+		if light != nil {
+			detail.LightName = light.Name
+			detail.LightSn = light.SN
+		}
+	}
+	return detail
+}
+
+func (s *intelligentLightingService) BatchGet(ids []int) []dao.IntelligentLight {
 	// 创建查询实例
-	intelligent := &dao.IntelligentLighting{}
+	intelligent := &dao.IntelligentLight{}
 	conditions, err := intelligent.BatchGet(ids)
 	if err != nil {
 		return nil

+ 7 - 0
app/device/service/lampPoleService.go

@@ -148,3 +148,10 @@ func (s *lampPoleService) GetOne(id int) (*dao.LampPole, *util.Errors) {
 	}
 	return device, nil
 }
+
+func (s *lampPoleService) CountLampPole(groupId int) int {
+	device := &dao.LampPole{
+		GroupId: groupId,
+	}
+	return device.Count()
+}

+ 12 - 0
app/device/service/lightControlService.go

@@ -205,6 +205,18 @@ func (s *lightControlService) GetOne(id int) (*dao.LightControl, *util.Errors) {
 	return device, nil
 }
 
+func (s *lightControlService) GetByGroupId(groupId int) ([]dao.LightControl, *util.Errors) {
+	// 创建查询实例
+	device := &dao.LightControl{
+		GroupId: groupId,
+	}
+	devices, err := device.GetDevicesByGroup()
+	if err != nil {
+		return nil, util.FailResponse(err.Error(), nil)
+	}
+	return devices, nil
+}
+
 //检查灯控编号是否合规1-1——255-255
 func checkControlNoIsCompliance(controlNo string) bool {
 	arr := strings.Split(controlNo, "-")

+ 12 - 7
app/system/model/role.go

@@ -21,14 +21,19 @@ type ReqRoleGrant struct {
 	DataScopeIds string `json:"dataScopeIds"` //数据权限ID
 	ApiScopeIds  string `json:"apiScopeIds"`  //接口权限ID
 }
+
 type RspRoleTree struct {
-	HasChildren bool          `json:"hasChildren"`
-	ID          string        `json:"id"`
-	ParentId    string        `json:"parentId"`
-	Title       string        `json:"title"`
-	Key         string        `json:"key"`
-	Value       string        `json:"value"`
-	Children    []RspRoleTree `json:"children"`
+	RoleDetails []RoleDetail
+}
+
+type RoleDetail struct {
+	HasChildren bool         `json:"hasChildren"`
+	ID          string       `json:"id"`
+	ParentId    string       `json:"parentId"`
+	Title       string       `json:"title"`
+	Key         string       `json:"key"`
+	Value       string       `json:"value"`
+	Children    []RoleDetail `json:"children"`
 }
 
 const ExistChild = "需要删除的记录中存在子角色,请先删除子角色!"

+ 48 - 9
app/system/service/roleService.go

@@ -4,6 +4,7 @@ import (
 	"iot_manager_service/app/system/dao"
 	"iot_manager_service/app/system/model"
 	"iot_manager_service/util"
+	"strconv"
 )
 
 // 角色管理服务
@@ -43,24 +44,62 @@ func (s *roleService) List(roleName, tenantId, roleAlias string, current, size i
 	return roles, nil
 }
 
-func (s *roleService) Tree() ([]dao.Role, error) {
+func (s *roleService) Tree() (*model.RspRoleTree, error) {
 	role := &dao.Role{}
 	roles, err := role.GetAll()
+
 	if err != nil {
 		return nil, err
 	}
-	rsp := model.RspRoleTree{
-		HasChildren: false,
-		ID:          ,
-		ParentId:    "",
-		Title:       "",
-		Key:         "",
-		Value:       "",
-		Children:    nil,
+	rsp := &model.RspRoleTree{}
+
+	tmp := make(map[int64][]dao.Role)
+	for _, role := range roles {
+		childes, isExist := tmp[role.ParentId]
+		if isExist {
+			childes = append(childes, role)
+			tmp[role.ParentId] = childes
+		} else {
+			tmp[role.ParentId] = []dao.Role{role}
+		}
+	}
+
+	if len(tmp) == 0 {
+		return nil, nil
 	}
+	roots, _ := tmp[0]
+	rsp.RoleDetails = dfs(rsp.RoleDetails, roots, tmp)
+
 	return rsp, nil
 }
 
+func dfs(result []model.RoleDetail, roles []dao.Role, tmp map[int64][]dao.Role) []model.RoleDetail {
+	if len(roles) == 0 {
+		return nil
+	}
+	for _, child := range roles {
+		id := strconv.FormatInt(child.ID, 10)
+		parentId := strconv.FormatInt(child.ParentId, 10)
+		detail := model.RoleDetail{
+			ID:       id,
+			ParentId: parentId,
+			Title:    child.RoleName,
+			Key:      id,
+			Value:    id,
+		}
+		childes, isExist := tmp[child.ID]
+		if isExist {
+			detail.HasChildren = true
+			detail.Children = dfs([]model.RoleDetail{}, childes, tmp)
+			result = append(result, detail)
+		} else {
+			detail.HasChildren = false
+			result = append(result, detail)
+		}
+	}
+	return result
+}
+
 func (s *roleService) Submit(req dao.Role) *util.Errors {
 	role := &req
 	if role.ID == 0 {

+ 29 - 24
router/router.go

@@ -295,35 +295,40 @@ func InitRouter(engine *gin.Engine) {
 		zigbee.GET("/export-excel", controller.Zigbee.ExportExcel)
 		zigbee.GET("/export-template", controller.Zigbee.ExportTemplate)
 	}
-	strategy := device.Group("/api/longchi/strategy")
-	//智能照明(灯杆分组/灯控树) 控制器
-	intelligentLighting := strategy.Group("IntelligentLighting")
-	{
-		intelligentLighting.GET("/detail", controller.IntelligentLighting.Detail)
-		intelligentLighting.GET("/list", controller.IntelligentLighting.List)
-		intelligentLighting.POST("/relationList", controller.IntelligentLighting.RelationList)
-		intelligentLighting.POST("/relation", controller.IntelligentLighting.Relation)
-		intelligentLighting.POST("/remove", controller.IntelligentLighting.Remove)
-		intelligentLighting.POST("/changHandSwitch", controller.IntelligentLighting.ChangeHandSwitch)
-		intelligentLighting.POST("/get_status", controller.IntelligentLighting.GetStatus)
-	}
+
+	// 设备控制相关
+	strategy := engine.Group("/api/longchi/strategy")
+
+	////智能照明(灯杆分组/灯控树) 控制器
+	//intelligentLighting := strategy.Group("IntelligentLighting")
+	//{
+	//	intelligentLighting.GET("/detail", controller.IntelligentLighting.Detail)
+	//	intelligentLighting.GET("/list", controller.IntelligentLighting.List)
+	//	intelligentLighting.POST("/relationList", controller.IntelligentLighting.RelationList)
+	//	intelligentLighting.POST("/relation", controller.IntelligentLighting.Relation)
+	//	intelligentLighting.POST("/remove", controller.IntelligentLighting.Remove)
+	//	intelligentLighting.POST("/changHandSwitch", controller.IntelligentLighting.ChangeHandSwitch)
+	//	intelligentLighting.POST("/get_status", controller.IntelligentLighting.GetStatus)
+	//}
+
 	//照明策略 控制器
-	lightstartegy := strategy.Group("lightstrategy")
+	lightStartegy := strategy.Group("lightstrategy")
 	{
-		lightstartegy.GET("/detail", controller.LightStrategy.Detail)
-		lightstartegy.GET("/list", controller.LightStrategy.List)
-		lightstartegy.POST("/submit", controller.LightStrategy.CreateOrUpdate)
-		lightstartegy.POST("/remove", controller.LightStrategy.Remove)
+		lightStartegy.GET("/detail", controller.LightStrategy.Detail)
+		lightStartegy.GET("/list", controller.LightStrategy.List)
+		lightStartegy.POST("/submit", controller.LightStrategy.CreateOrUpdate)
+		lightStartegy.POST("/remove", controller.LightStrategy.Remove)
 	}
-	//照明策略略关联(灯杆分组/灯控树形态) 控制器(作废)
+
+	//照明策略略关联(灯杆分组/灯控树形态) 控制器
 	lightRelation := strategy.Group("lightrelation")
 	{
-		lightRelation.GET("/detail", controller.LightRelation.Detail)
-		lightRelation.GET("/list", controller.LightRelation.List)
-		lightRelation.POST("/remove", controller.LightRelation.Remove)
-		lightRelation.POST("/relationList", controller.LightRelation.RelationList)
-		lightRelation.POST("/relation", controller.LightRelation.Relation)
-		lightRelation.POST("/changeHandSwitch", controller.LightRelation.ChangeHandSwitch)
+		lightRelation.GET("/detail", controller.IntelligentLighting.Detail)
+		lightRelation.GET("/list", controller.IntelligentLighting.List)
+		lightRelation.POST("/remove", controller.IntelligentLighting.Remove)
+		lightRelation.POST("/relationList", controller.IntelligentLighting.RelationList)
+		lightRelation.POST("/relation", controller.IntelligentLighting.Relation)
+		lightRelation.POST("/changeHandSwitch", controller.IntelligentLighting.ChangeHandSwitch)
 	}
 	//灯随车走照明控制 控制器
 	onDemandLightingControl := strategy.Group("onDemandLightingControl")