瀏覽代碼

ip音柱接口

hxz 2 年之前
父節點
當前提交
f45971cb10

+ 0 - 22
app/controller/groupRelevanceLampPole.go

@@ -1,22 +0,0 @@
-package controller
-
-import (
-	"github.com/gin-gonic/gin"
-)
-
-// 灯随车走灯杆关联分组管理对象
-var GroupRelevanceLampPole = new(groupRelevanceLampPoleCtl)
-
-type groupRelevanceLampPoleCtl struct{}
-
-func (c *groupRelevanceLampPoleCtl) Detail(ctx *gin.Context) {
-}
-
-func (c *groupRelevanceLampPoleCtl) List(ctx *gin.Context) {
-}
-
-func (c *groupRelevanceLampPoleCtl) Setting(ctx *gin.Context) {
-}
-
-func (c *groupRelevanceLampPoleCtl) Remove(ctx *gin.Context) {
-}

+ 63 - 3
app/controller/ipBroadcastController.go

@@ -2,6 +2,13 @@ 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"
+	"math"
+	"net/http"
+	"strconv"
 )
 
 // IP广播基本信息管理对象
@@ -10,15 +17,71 @@ var IpBroadcast = new(ipBroadcastCtl)
 type ipBroadcastCtl struct{}
 
 func (c *ipBroadcastCtl) 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.IpBroadcastService.Get(id)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
 }
 
 func (c *ipBroadcastCtl) List(ctx *gin.Context) {
+	searchValue := ctx.Query("searchValue")
+	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.IpBroadcastService.List(searchValue, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(devices)) / float64(size))
+	rsp := model.RsqIpBroadcastList{
+		Current: current,
+		Size:    size,
+		Total:   len(devices),
+		Pages:   int(pages),
+	}
+	for _, device := range devices {
+
+		rsp.Records = append(rsp.Records, device)
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp))
 }
 
 func (c *ipBroadcastCtl) CreateOrUpdate(ctx *gin.Context) {
+	var req dao.IpBroadcast
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.IpBroadcastService.CreateOrUpdate(req)
+	ctx.JSON(http.StatusOK, err)
 }
 
 func (c *ipBroadcastCtl) Remove(ctx *gin.Context) {
+	var req *model.ReqIpBroadcastRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.IpBroadcastService.Remove(req.IDs)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }
 
 func (c *ipBroadcastCtl) ImportExcel(ctx *gin.Context) {
@@ -30,8 +93,5 @@ func (c *ipBroadcastCtl) ExportExcel(ctx *gin.Context) {
 func (c *ipBroadcastCtl) ExportTemplate(ctx *gin.Context) {
 }
 
-func (c *ipBroadcastCtl) GetList(ctx *gin.Context) {
-}
-
 func (c *ipBroadcastCtl) SettingVolume(ctx *gin.Context) {
 }

+ 0 - 3
app/controller/zigbeeController.go

@@ -101,6 +101,3 @@ func (c *zigbeeCtl) GetList(ctx *gin.Context) {
 	}
 	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
 }
-
-func (c *zigbeeCtl) Enable(ctx *gin.Context) {
-}

+ 1 - 0
app/dao/common.go

@@ -41,6 +41,7 @@ func InitDB() {
 			&AlarmTerminal{},
 			&Bridge{},
 			&BridgeSensor{},
+			&IpBroadcast{},
 		).Error
 		if err != nil {
 			panic(fmt.Sprintf("AutoMigrate err : %v", err))

+ 89 - 0
app/dao/ipBroadcastDao.go

@@ -0,0 +1,89 @@
+package dao
+
+import (
+	"github.com/jinzhu/gorm"
+	"time"
+)
+
+type IpBroadcast struct {
+	ID               int        `gorm:"primary_key" json:"id"`                            //编号
+	CastName         string     `gorm:"type:varchar(64)" json:"castName"`                 //设备名称
+	CastSN           string     `gorm:"type:varchar(60)" json:"castSN"`                   //设备序列号
+	LampPoleId       int        `gorm:"type:int" json:"LampPoleId"`                       //所属灯杆id
+	LampPoleSN       string     `gorm:"type: varchar(64)" json:"lampPoleSN"`              //所属灯杆sn
+	GroupId          int        `gorm:"type:int" json:"groupId"`                          //灯杆分组ID
+	GatewayID        int        `gorm:"type: int" json:"gatewayID"`                       //所属网关id
+	GatewaySN        string     `gorm:"type:varchar(64)" json:"gatewaySN"`                //所属网关sn
+	BrandID          int        `gorm:"type:int" json:"brandID"`                          //设备名称
+	ModelID          int        `gorm:"type:int" json:"modelID"`                          //设备型号
+	RatedPower       float32    `gorm:"type:float(8, 2); default 0.00" json:"ratedPower"` //额定功率(LED灯)
+	IPAddress        string     `gorm:"type:varchar(50)" json:"ipAddress"`                //IP地址
+	ServiceIPAddress string     `gorm:"type:varchar(50)" json:"serviceIPAddress"`         //IP地址         //服务IP地址
+	ServerPort       int        `gorm:"type: int" json:"serverPort"`                      //服务端口
+	SoundVolume      int        `gorm:"type: int" json:"soundVolume"`                     //音量0-100换算
+	InstallTime      *time.Time `gorm:"type:date" json:"installTime"`                     //安装时间
+	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"`                  //修改时间
+	UpdateUser       string     `gorm:"type:varchar(60)" json:"updateUser"`               //修改用户
+	IsDeleted        int        `gorm:"type:int;default 0" json:"isDeleted"`              //是否删除 0=未删除,1=删除
+	TenantID         string     `gorm:"type:varchar(12)" json:"tenantID"`                 //租户id
+
+}
+
+func (IpBroadcast) TableName() string {
+	return "t_dev_ip_broadcast"
+}
+
+func (c IpBroadcast) 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 IpBroadcast) IsExistedBySN() bool {
+	var count = 0
+	_ = GDb.Model(&c).Where("  cast_sn = ? and is_deleted = ?",
+		c.CastSN, c.IsDeleted).Count(&count).Error
+	return count > 0
+}
+
+func (c IpBroadcast) IsExistedByNameAndCode() bool {
+	var devices []IpBroadcast
+	err := GDb.Model(&c).Where(" cast_sn = ? and is_deleted = ?",
+		c.CastSN, 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 *IpBroadcast) Create() error {
+	return GDb.Model(&c).Save(&c).Error
+}
+
+func (c *IpBroadcast) Update() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *IpBroadcast) GetDevice() error {
+	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
+	return err
+}
+
+func (c IpBroadcast) GetDevices(offset, limit int) ([]IpBroadcast, error) {
+	var devices []IpBroadcast
+	err := GDb.Model(&c).Where(" cast_name like ? ", "%"+c.CastName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	return devices, err
+}
+
+func (c IpBroadcast) GetAllDevices() ([]*IpBroadcast, error) {
+	var devices []*IpBroadcast
+	err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantID, c.IsDeleted).Scan(&devices).Error
+	return devices, err
+}

+ 15 - 0
app/model/ipBroadcast.go

@@ -0,0 +1,15 @@
+package model
+
+import "iot_manager_service/app/dao"
+
+type RsqIpBroadcastList struct {
+	Records []dao.IpBroadcast `json:"records"` //记录列表
+	Current int               `json:"current"` //当前分页
+	Size    int               `json:"size"`    //每页数量
+	Pages   int               `json:"pages"`   //总页数
+	Total   int               `json:"total"`   //总数
+}
+
+type ReqIpBroadcastRemove struct {
+	IDs int `json:"ids"` //分组编码
+}

+ 102 - 0
app/service/ipBroadcastService.go

@@ -0,0 +1,102 @@
+package service
+
+import (
+	"fmt"
+	"iot_manager_service/app/dao"
+	"iot_manager_service/app/utils"
+	"time"
+)
+
+var IpBroadcastService = new(ipBroadcastService)
+
+type ipBroadcastService struct{}
+
+func (s *ipBroadcastService) Get(id int) (*dao.IpBroadcast, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.IpBroadcast{
+		ID: id,
+	}
+	err := device.GetDevice()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+	return device, nil
+
+}
+
+func (s *ipBroadcastService) CreateOrUpdate(req dao.IpBroadcast) *utils.Errors {
+	// 创建查询实例
+	device := req
+	device.UpdateUser = "TODO" // todo: 使用登录态
+	device.UpdateTime = time.Now()
+	if req.ID == 0 {
+		device.CreateTime = time.Now()
+		device.CreateUser = "TODO" // todo: 使用登录态
+		if device.IsExistedBySN() {
+			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)
+	}
+	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 *ipBroadcastService) List(searchValue string, current, size int) ([]dao.IpBroadcast, *utils.Errors) {
+	device := dao.IpBroadcast{}
+	if searchValue != "" {
+		device.CastSN = searchValue
+		device.CastName = searchValue
+	}
+
+	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 *ipBroadcastService) Remove(id int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.IpBroadcast{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: "TODO", // todo 使用登录态
+		UpdateTime: time.Now(),
+	}
+	//todo
+	// service.transformerService.CountRelation()
+
+	//todo operation record
+	err := device.Delete()
+	if err != nil {
+		return utils.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *ipBroadcastService) GetList() ([]*dao.IpBroadcast, *utils.Errors) {
+	device := &dao.IpBroadcast{
+		TenantID:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+	devices, err := device.GetAllDevices()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	return devices, nil
+}