Browse Source

灯杆分组get remove等接口

terry 2 years ago
parent
commit
7053d8f760

+ 92 - 5
app/controller/lampPoleGroupController.go

@@ -2,10 +2,12 @@ package controller
 
 import (
 	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/dao"
 	"iot_manager_service/app/model"
 	"iot_manager_service/app/service"
 	"iot_manager_service/app/utils"
 	"net/http"
+	"strconv"
 )
 
 // 灯杆分组管理对象
@@ -13,9 +15,23 @@ var LampPoleGroup = new(lampPoleGroupCtl)
 
 type lampPoleGroupCtl struct{}
 
+// Detail 获取详情
 func (c *lampPoleGroupCtl) Detail(ctx *gin.Context) {
+	id, e := strconv.Atoi(ctx.Query("id"))
+	if e != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil))
+		return
+	}
+
+	device, err := service.LampPoleGroupService.Get(id)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
 }
 
+// CreateOrUpdate 新增、更新
 func (c *lampPoleGroupCtl) CreateOrUpdate(ctx *gin.Context) {
 	var req *model.ReqLampPoleGroupSubmit
 	if err := ctx.ShouldBindJSON(&req); err != nil {
@@ -26,27 +42,98 @@ func (c *lampPoleGroupCtl) CreateOrUpdate(ctx *gin.Context) {
 	ctx.JSON(http.StatusOK, err)
 }
 
+// List 获取分页列表
 func (c *lampPoleGroupCtl) List(ctx *gin.Context) {
-	var req *model.ReqLampPoleGroupList
+	poleGroupName := ctx.Query("poleGroupName")
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+
+	devices, err := service.LampPoleGroupService.List(poleGroupName, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	rsp := model.RspLampPoleGroupList{
+		Current: current,
+		Size:    size,
+		Total:   len(devices),
+	}
+	for _, device := range devices {
+		rsp.Records = append(rsp.Records, lampPoleGroupDaoToModel(device))
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp))
+}
+
+// Remove 删除
+func (c *lampPoleGroupCtl) Remove(ctx *gin.Context) {
+	var req *model.ReqLampPoleGroupRemove
 	if err := ctx.ShouldBindJSON(&req); err != nil {
 		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
 		return
 	}
-	devices, err := service.LampPoleGroupService.List(req)
+	err := service.LampPoleGroupService.Remove(req.IDs)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
+		return
 	}
-	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }
 
-func (c *lampPoleGroupCtl) Remove(ctx *gin.Context) {
+// GetList 获取下拉列表
+func (c *lampPoleGroupCtl) GetList(ctx *gin.Context) {
+	devices, err := service.LampPoleGroupService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
 }
 
+// FiltrationList 获取智能照明灯杆分组下拉(过滤掉没有灯控的分组显示)
 func (c *lampPoleGroupCtl) FiltrationList(ctx *gin.Context) {
+	devices, err := service.LampPoleGroupService.GetFiltration()
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
 }
 
+// GetTree 获取所有的灯杆分组所属灯杆
 func (c *lampPoleGroupCtl) GetTree(ctx *gin.Context) {
+	//todo 灯杆列表赋值
+	// var rsp model.ReqLampPoleGroupSubmit
+	devices, err := service.LampPoleGroupService.GetTree()
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
 }
 
-func (c *lampPoleGroupCtl) GetList(ctx *gin.Context) {
+func lampPoleGroupDaoToModel(device dao.LampPoleGroup) model.ReqLampPoleGroupSubmit {
+	return model.ReqLampPoleGroupSubmit{
+		ID:             device.ID,
+		PoleGroupName:  device.PoleGroupName,
+		CountLampPole:  device.CountLampPole,
+		Remark:         device.Remark,
+		TenantId:       device.TenantId,
+		CreateTime:     device.CreateTime,
+		CreateUser:     device.CreateUser,
+		UpdateTime:     device.UpdateTime,
+		UpdateUser:     device.UpdateUser,
+		IsDeleted:      device.IsDeleted,
+		Tag:            device.Tag,
+		PublicName:     "",
+		LampPoleVOList: nil,
+		CameraList:     nil,
+		SwitchBoxList:  nil,
+		InfoBoardList:  nil,
+	}
 }

+ 1 - 1
app/dao/cameraDto.go

@@ -4,7 +4,7 @@ import "time"
 
 //CameraDevice 网关下挂载的设备, 摄像头
 type CameraDevice struct {
-	ID               string    `gorm:"type:int;primary_key"`         //编号
+	ID               int       `gorm:"primary_key"`                  //编号
 	DeviceName       string    `gorm:"type:varchar(64)"`             //设备名称
 	DeviceSN         string    `gorm:"type:varchar(64)"`             //设备序列号
 	CameraType       int       `gorm:"type:int"`                     //摄像机类型 0=枪机,1=球机

+ 1 - 1
app/dao/common.go

@@ -27,6 +27,6 @@ func InitDB() {
 		GDb.DB().SetMaxOpenConns(32)
 		GDb.DB().SetMaxIdleConns(5)
 		GDb.LogMode(false)
-		GDb.AutoMigrate(&CameraDevice{})
+		GDb.AutoMigrate(&CameraDevice{}, &LampPoleGroup{})
 	}
 }

+ 79 - 0
app/dao/lampPoleGroupDao.go

@@ -0,0 +1,79 @@
+package dao
+
+import (
+	"github.com/jinzhu/gorm"
+	"time"
+)
+
+//LampPoleGroup 灯杆分组
+type LampPoleGroup struct {
+	ID            int       `gorm:"primary_key" `       //编号
+	PoleGroupName string    `gorm:"type:varchar(64)"`   //分组名称
+	TenantId      string    `gorm:"type:varchar(12)"`   //租户ID
+	CreateTime    time.Time `gorm:"type:datetime"`      //新增时间
+	CreateUser    string    `gorm:"type:varchar(60)"`   //新增记录操作用户ID
+	UpdateTime    time.Time `gorm:"type:datetime"`      //修改时间
+	UpdateUser    string    `gorm:"type:varchar(60)"`   //修改用户
+	IsDeleted     int       `gorm:"type:int;default 0"` //是否删除 0=未删除,1=删除
+	Tag           string    `gorm:"type:varchar(255)"`  //标签,(备用,逗号区分)
+	Remark        string    `gorm:"type:varchar(255)"`  //备注
+	CountLampPole int       `gorm:"type:int"`           //灯杆数
+}
+
+func (LampPoleGroup) TableName() string {
+	return "t_dev_lamp_pole_group"
+}
+
+func (c *LampPoleGroup) Delete() error {
+	return GDb.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
+		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
+}
+
+func (c LampPoleGroup) IsExistedByName() bool {
+	var count = 0
+	_ = GDb.Model(&c).Where(" pole_group_name = ? and tenant_id = ? and is_deleted = ?",
+		c.PoleGroupName, c.TenantId, c.IsDeleted).Count(&count).Error
+	return count > 0
+}
+
+func (c LampPoleGroup) IsExistedByNameAndCode() bool {
+	var devices []LampPoleGroup
+	err := GDb.Model(&c).Where("pole_group_name = ? and tenant_id = ? and is_deleted = ?",
+		c.PoleGroupName, c.TenantId, c.IsDeleted).Find(&devices).Error
+	if gorm.IsRecordNotFoundError(err) {
+		return false
+	}
+	for _, d := range devices {
+		if d.ID != c.ID {
+			return true
+		}
+	}
+	return false
+}
+
+func (c *LampPoleGroup) Create() error {
+	return GDb.Model(&c).Save(&c).Error
+}
+
+func (c *LampPoleGroup) Update() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"pole_group_name": c.PoleGroupName,
+		"tenant_id": c.TenantId, "update_time": time.Now(), "update_user": "TODO", "is_deleted": 0}).Error
+}
+
+func (c *LampPoleGroup) GetDevice() error {
+	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
+	return err
+}
+
+func (c LampPoleGroup) GetDevices(offset, limit int) ([]LampPoleGroup, error) {
+	var devices []LampPoleGroup
+	err := GDb.Model(&c).Where(" pole_group_name like ? ", "%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Scan(
+		&devices).Error
+	return devices, err
+}
+
+func (c LampPoleGroup) GetAllDevices() ([]LampPoleGroup, error) {
+	var devices []LampPoleGroup
+	err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
+	return devices, err
+}

+ 0 - 58
app/dao/lampPoleGroupDto.go

@@ -1,58 +0,0 @@
-package dao
-
-import (
-	"time"
-)
-
-//LampPoleGroup 灯杆分组
-type LampPoleGroup struct {
-	ID            int       `gorm:"type:int;primary_key"` //编号
-	PoleGroupName string    `gorm:"type:varchar(64)"`     //分组名称
-	TenantId      string    `gorm:"type:varchar(12)"`     //租户ID
-	CreateTime    time.Time `gorm:"type:datetime"`        //新增时间
-	CreateUser    string    `gorm:"type:varchar(60)"`     //新增记录操作用户ID
-	UpdateTime    time.Time `gorm:"type:datetime"`        //修改时间
-	UpdateUser    string    `gorm:"type:varchar(60)"`     //修改用户
-	IsDeleted     int       `gorm:"type:int;default 0"`   //是否删除 0=未删除,1=删除
-	Tag           string    `gorm:"type:varchar(255)"`    //标签,(备用,逗号区分)
-	Remark        string    `gorm:"type:varchar(255)"`    //备注
-}
-
-func (LampPoleGroup) TableName() string {
-	return "t_dev_lamp_pole_group"
-}
-
-func (c LampPoleGroup) Delete() error {
-	return GDb.Model(&c).Updates(map[string]interface{}{"state": 0}).Error
-}
-
-func (c LampPoleGroup) IsExistedByCode() (bool, error) {
-	var count = 0
-	err := GDb.Model(&c).Where(" id = ? ", c.ID).Count(&count).Error
-	return count > 0, err
-}
-
-func (c LampPoleGroup) GetDeviceID() (int, error) {
-	err := GDb.Model(&c).Where(" pole_group_name = ? and tenant_id = ? and is_deleted = ?", c.PoleGroupName, c.TenantId, c.IsDeleted).First(&c).Error
-	return c.ID, err
-}
-
-func (c LampPoleGroup) Create() error {
-	return GDb.Create(&c).Error
-}
-
-func (c LampPoleGroup) Update() error {
-	return GDb.Model(&c).Updates(map[string]interface{}{"pole_group_name": c.PoleGroupName,
-		"tenant_id": c.TenantId, "update_time": time.Now(), "update_user": "TODO", "is_deleted": 0}).Error
-}
-
-func (c LampPoleGroup) GetDevice() error {
-	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
-	return err
-}
-
-func (c LampPoleGroup) GetDevices(offset, limit int) ([]LampPoleGroup, error) {
-	var devices []LampPoleGroup
-	err := GDb.Model(&c).Where(" id like '%?%'", c.PoleGroupName).Offset(offset).Limit(limit).Scan(&devices).Error
-	return devices, err
-}

+ 1 - 1
app/model/camera.go

@@ -1,7 +1,7 @@
 package model
 
 type ReqCamera struct {
-	Code    string `json:"code"`    //设备编号,禁止修改
+	Code    int    `json:"code"`    //设备编号,禁止修改
 	GID     string `json:"gid"`     //网关ID
 	Tenant  string `json:"tenant"`  //租户ID
 	Name    string `json:"name"`    //设备名称

+ 22 - 7
app/model/lampPoleGroup.go

@@ -5,14 +5,14 @@ import "time"
 type ReqLampPoleGroupSubmit struct {
 	ID             int          `json:"id"`             //分组编码
 	PoleGroupName  string       `json:"poleGroupName"`  //分组名
-	CountLampPole  string       `json:"countLampPole"`  //灯杆数
+	CountLampPole  int          `json:"countLampPole"`  //灯杆数
 	Remark         string       `json:"remark"`         //备注
-	TenantId       string       `json:"tenant_id"`      //租户ID
-	CreateTime     time.Time    `json:"create_time"`    //新增时间
-	CreateUser     string       `json:"create_user"`    //新增记录操作用户ID
-	UpdateTime     time.Time    `json:"update_time"`    //修改时间
-	UpdateUser     string       `json:"update_user"`    //修改用户
-	IsDeleted      int          `json:"is_deleted"`     //是否删除 0=未删除,1=删除
+	TenantId       string       `json:"tenantId"`       //租户ID
+	CreateTime     time.Time    `json:"createTime"`     //新增时间
+	CreateUser     string       `json:"createUser"`     //新增记录操作用户ID
+	UpdateTime     time.Time    `json:"updateTime"`     //修改时间
+	UpdateUser     string       `json:"updateUser"`     //修改用户
+	IsDeleted      int          `json:"isDeleted"`      //是否删除 0=未删除,1=删除
 	Tag            string       `json:"tag"`            //标签,(备用,逗号区分)
 	PublicName     string       `json:"publicName"`     //标节目发布时使用
 	LampPoleVOList []lampPoleVO `json:"lampPoleVOList"` //分组下的所有灯杆集合
@@ -38,3 +38,18 @@ type ReqLampPoleGroupList struct {
 	Current       int    `json:"current"`       //当前分页
 	Size          int    `json:"size"`          //每页数量
 }
+
+type RspLampPoleGroupList struct {
+	Records []ReqLampPoleGroupSubmit `json:"records"` //记录列表
+	Current int                      `json:"current"` //当前分页
+	Size    int                      `json:"size"`    //每页数量
+	Total   int                      `json:"total"`   //总数
+}
+
+type ReqLampPoleGroupDetail struct {
+	ID int `json:"id"` //分组编码
+}
+
+type ReqLampPoleGroupRemove struct {
+	IDs int `json:"ids"` //分组编码
+}

+ 105 - 16
app/service/lampPoleGroupService.go

@@ -1,6 +1,7 @@
 package service
 
 import (
+	"fmt"
 	"iot_manager_service/app/dao"
 	"iot_manager_service/app/model"
 	"iot_manager_service/app/utils"
@@ -12,50 +13,138 @@ var LampPoleGroupService = new(lampPoleGroupService)
 
 type lampPoleGroupService struct{}
 
+func (s *lampPoleGroupService) Get(id int) (*dao.LampPoleGroup, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.LampPoleGroup{
+		ID: id,
+	}
+	err := device.GetDevice()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+	return device, nil
+}
+
 func (s *lampPoleGroupService) CreateOrUpdate(req *model.ReqLampPoleGroupSubmit) *utils.Errors {
 	// 创建查询实例
 	device := dao.LampPoleGroup{
 		PoleGroupName: req.PoleGroupName,
 		TenantId:      "000000", // todo: 使用登录态
 		IsDeleted:     0,
-	}
-
-	if id, err := device.GetDeviceID(); err != nil || id == req.ID {
-		return utils.ParamsInvalidResponse("列表中存在重名,请更改灯杆分组名称!", nil)
+		UpdateUser:    "TODO", // todo: 使用登录态
+		UpdateTime:    time.Now(),
 	}
 
 	if req.ID == 0 {
 		device.CreateTime = time.Now()
 		device.CreateUser = "TODO" // todo: 使用登录态
+
+		if device.IsExistedByName() {
+			fmt.Printf("Create GetDeviceID err \n")
+			return utils.ParamsInvalidResponse("列表中存在重名,请更改灯杆分组名称!", nil)
+		}
 		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)
 	}
-	device.UpdateTime = time.Now()
-	device.UpdateUser = "TODO" // todo: 使用登录态
+
+	device.ID = req.ID
+	if device.IsExistedByNameAndCode() {
+		return utils.ParamsInvalidResponse("列表中存在重名,请更改灯杆分组名称!", 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 *lampPoleGroupService) List(req *model.ReqLampPoleGroupList) ([]dao.LampPoleGroup, *utils.Errors) {
+func (s *lampPoleGroupService) List(poleGroupName string, current, size int) ([]dao.LampPoleGroup, *utils.Errors) {
 	// 创建查询实例
-	device := dao.LampPoleGroup{
-		PoleGroupName: req.PoleGroupName,
+	device := &dao.LampPoleGroup{
+		PoleGroupName: poleGroupName,
 	}
-	if req.Current == 0 {
-		req.Current = 1
+
+	offset := (current - 1) * size
+	limit := size
+	devices, err := device.GetDevices(offset, limit)
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
 	}
-	if req.Size < 0 || req.Size > 100 {
-		req.Size = 10
+	return devices, nil
+}
+
+func (s *lampPoleGroupService) Remove(id int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.LampPoleGroup{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: "TODO", // todo 使用登录态
+		UpdateTime: time.Now(),
 	}
-	offset := (req.Current - 1) * req.Size
-	limit := req.Size
-	devices, err := device.GetDevices(offset, limit)
+
+	//todo
+	// service.lampPoleService.CountRelation()
+
+	//todo operation record
+	err := device.Delete()
+	if err != nil {
+		return utils.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *lampPoleGroupService) GetList() ([]dao.LampPoleGroup, *utils.Errors) {
+	// todo use redis cache
+	device := &dao.LampPoleGroup{
+		TenantId:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+	devices, err := device.GetAllDevices()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	return devices, nil
+}
+
+func (s *lampPoleGroupService) GetFiltration() ([]dao.LampPoleGroup, *utils.Errors) {
+	// todo use redis cache
+	device := &dao.LampPoleGroup{
+		TenantId:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+
+	//todo
+	// get t_dev_light_control group_id
+	// groupIds := lightControl.GetGroupIds()
+
+	devices, err := device.GetAllDevices()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	return devices, nil
+}
+
+func (s *lampPoleGroupService) GetTree() ([]dao.LampPoleGroup, *utils.Errors) {
+	// todo use redis cache
+	device := &dao.LampPoleGroup{
+		TenantId:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+
+	//todo lampPole getALlDevice
+
+	devices, err := device.GetAllDevices()
 	if err != nil {
 		return nil, utils.FailResponse(err.Error(), nil)
 	}
+
 	return devices, nil
 }