Browse Source

一键报警终端接口

hxz 2 years ago
parent
commit
eda6f3b508

+ 63 - 0
app/controller/alarmTerminalController.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"
 )
 
 // 一键报警终端表管理对象
@@ -10,15 +17,71 @@ var AlarmTerminal = new(alarmTerminalCtl)
 type alarmTerminalCtl struct{}
 
 func (c *alarmTerminalCtl) 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.AlarmTerminalService.Get(id)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
 }
 
 func (c *alarmTerminalCtl) 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.AlarmTerminalService.List(searchValue, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(devices)) / float64(size))
+	rsp := model.RsqAlarmTerminalList{
+		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 *alarmTerminalCtl) CreateOrUpdate(ctx *gin.Context) {
+	var req dao.AlarmTerminal
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.AlarmTerminalService.CreateOrUpdate(req)
+	ctx.JSON(http.StatusOK, err)
 }
 
 func (c *alarmTerminalCtl) Remove(ctx *gin.Context) {
+	var req *model.ReqAlarmTerminalRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.AlarmTerminalService.Remove(req.IDs)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }
 
 func (c *alarmTerminalCtl) ImportExcel(ctx *gin.Context) {

+ 91 - 0
app/dao/alarmTerminalDao.go

@@ -0,0 +1,91 @@
+package dao
+
+import (
+	"github.com/jinzhu/gorm"
+	"time"
+)
+
+// AlarmTerminal 一键报警服务
+type AlarmTerminal struct {
+	ID               int       `gorm:"primary_key" json:"id"`                     //编号
+	TerminalName     string    `gorm:" varchar(64)" json:"terminalName"`          //设备名称
+	TerminalSN       string    `gorm:"type:varchar(60)" json:"terminalSN"`        //设备序编码
+	BrandID          int       `gorm:"type:int" json:"brandID"`                   //设备品牌ID
+	ModelID          int       `gorm:"type:int" json:"modelID"`                   //设备型号ID
+	GatewayID        int       `gorm:"type: int" json:"gatewayID"`                //所属网关
+	LampPoleID       int       `gorm:"type: int" json:"lampPoleID"`               //所属灯杆 灯杆ID
+	LampPoleName     string    `gorm:"type: varchar(64)" json:"lampPoleName"`     //灯杆名称
+	LampPoleSN       string    `gorm:"type: varchar(64)" json:"lampPoleSN"`       //灯杆编码
+	LampPoleLocation string    `gorm:"type:varchar(255)" json:"lampPoleLocation"` //灯杆安装位置
+	PoleLng          float64   `gorm:"type:double(17, 14) " json:"poleLng"`       //经度
+	PoleLat          float64   `gorm:"type:double(17, 14)  " json:"poleLat"`      //纬度
+	Numeration       int       `gorm:"type: int" json:"numeration"`               //编号
+	ServeID          int       `gorm:"type:int" json:"serveID"`                   //所属服务ID
+	IPAddress        string    `gorm:"type:varchar(50)" json:"ipAddress"`         //IP地址
+	InstallTime      time.Time `gorm:"type:date" json:"installTime"`              //安装时间
+	TenantId         string    `gorm:"type:varchar(12)" json:"tenantId"`          //租户id
+	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" json:"isDeleted"`                 //是否删除 0=未删除,1=删除
+	Status           int       `gorm:"type:int  " json:"status"`                  //告警状态 1处理完成2待处理3无需处理
+	Tag              string    `gorm:"type:varchar(255)" json:"tag"`              //标签,(备用,逗号区分)
+}
+
+func (AlarmTerminal) TableName() string {
+	return "t_dev_a_key_alarm_terminal"
+}
+
+func (c AlarmTerminal) 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 AlarmTerminal) IsExistedBySN() bool {
+	var count = 0
+	_ = GDb.Model(&c).Where("  terminal_sn = ? and is_deleted = ?",
+		c.TerminalSN, c.IsDeleted).Count(&count).Error
+	return count > 0
+}
+
+func (c AlarmTerminal) IsExistedByNameAndCode() bool {
+	var devices []AlarmTerminal
+	err := GDb.Model(&c).Where(" terminal_sn = ? and is_deleted = ?",
+		c.TerminalSN, 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 *AlarmTerminal) Create() error {
+	return GDb.Model(&c).Save(&c).Error
+}
+
+func (c *AlarmTerminal) Update() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *AlarmTerminal) GetDevice() error {
+	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
+	return err
+}
+
+func (c AlarmTerminal) GetDevices(offset, limit int) ([]AlarmTerminal, error) {
+	var devices []AlarmTerminal
+	err := GDb.Model(&c).Where(" terminal_name like ? and is_deleted = 0", "%"+c.TerminalName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	return devices, err
+}
+
+func (c AlarmTerminal) GetAllDevices() ([]*AlarmTerminal, error) {
+	var devices []*AlarmTerminal
+	err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
+	return devices, err
+}

+ 1 - 0
app/dao/common.go

@@ -38,6 +38,7 @@ func InitDB() {
 			&Alarm{},
 			&CaptureUint{},
 			&CheckPoint{},
+			&AlarmTerminal{},
 		).Error
 		if err != nil {
 			panic(fmt.Sprintf("AutoMigrate err : %v", err))

+ 15 - 0
app/model/alarmTerminal.go

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

+ 95 - 0
app/service/alarmTerminalService.go

@@ -0,0 +1,95 @@
+package service
+
+import (
+	"fmt"
+	"iot_manager_service/app/dao"
+	"iot_manager_service/app/model"
+	"iot_manager_service/app/utils"
+	"time"
+)
+
+// 中间件管理服务
+var AlarmTerminalService = new(alarmTerminalService)
+
+type alarmTerminalService struct{}
+
+func (s *alarmTerminalService) Get(id int) (*dao.AlarmTerminal, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.AlarmTerminal{
+		ID: id,
+	}
+	err := device.GetDevice()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+	return device, nil
+}
+
+func (s *alarmTerminalService) CreateOrUpdate(req dao.AlarmTerminal) *utils.Errors {
+	device := req
+	if device.TenantId == "" {
+		device.TenantId = "000000" // todo: 使用登录态
+	}
+	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 IsExistedBySN \n")
+			return utils.ParamsInvalidResponse(model.RepeatedName, 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() {
+		fmt.Printf("Update IsExistedByNameAndCode \n")
+		return utils.ParamsInvalidResponse(model.RepeatedName, 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 *alarmTerminalService) List(poleGroupName string, current, size int) ([]dao.AlarmTerminal, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.AlarmTerminal{}
+	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 *alarmTerminalService) Remove(id int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.AlarmTerminal{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: "TODO", // todo 使用登录态
+		UpdateTime: time.Now(),
+	}
+
+	//todo
+	// service.lampPoleService.CountRelation()
+
+	//todo operation record
+	err := device.Delete()
+	if err != nil {
+		return utils.FailResponse(err.Error(), nil)
+	}
+	return nil
+}