瀏覽代碼

一键报警服务接口

hxz 2 年之前
父節點
當前提交
544b77c5fd
共有 5 個文件被更改,包括 282 次插入0 次删除
  1. 69 0
      app/controller/alarmController.go
  2. 88 0
      app/dao/alarmDao.go
  3. 1 0
      app/dao/common.go
  4. 15 0
      app/model/alarn.go
  5. 109 0
      app/service/alarmService.go

+ 69 - 0
app/controller/alarmController.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 Alarm = new(alarmCtl)
 type alarmCtl struct{}
 
 func (c *alarmCtl) 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.AlarmService.Get(id)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
 }
 
 func (c *alarmCtl) 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.AlarmService.List(searchValue, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(devices)) / float64(size))
+	rsp := model.RsqAlarmList{
+		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 *alarmCtl) CreateOrUpdate(ctx *gin.Context) {
+	var req dao.Alarm
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.AlarmService.CreateOrUpdate(req)
+	ctx.JSON(http.StatusOK, err)
 }
 
 func (c *alarmCtl) Remove(ctx *gin.Context) {
+	var req *model.ReqAlarmRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.AlarmService.Remove(req.IDs)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }
 
 func (c *alarmCtl) ImportExcel(ctx *gin.Context) {
@@ -31,4 +94,10 @@ func (c *alarmCtl) ExportTemplate(ctx *gin.Context) {
 }
 
 func (c *alarmCtl) GetList(ctx *gin.Context) {
+	devices, err := service.AlarmService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
 }

+ 88 - 0
app/dao/alarmDao.go

@@ -0,0 +1,88 @@
+package dao
+
+import (
+	"github.com/jinzhu/gorm"
+	"time"
+)
+
+// Alarm 一键报警服务
+type Alarm struct {
+	ID          int        `gorm:"primary_key" json:"id"`              //编号
+	ServeName   string     `gorm:" varchar(64)" json:"serveName"`      //设备名称
+	ServeSN     string     `gorm:"type:varchar(60)" json:"serveSN"`    //设备序编码
+	BrandID     int        `gorm:"type:int" json:"brandID"`            //设备品牌ID
+	ModelID     int        `gorm:"type:int" json:"modelID"`            //设备型号ID
+	ServeType   int        `gorm:"type: int" json:"serveType"`         //服务
+	ServeIP     string     `gorm:"type:varchar(50)" json:"serveIP"`    //所属服务IP地址
+	ServeProt   int        `gorm:"type:int  " json:"serveProt"`        //端口
+	Account     string     `gorm:"type:varchar(4000) " json:"account"` //账号
+	Pwd         string     `gorm:"type:varchar(4000) " json:"pwd"`     //密码
+	Numeration  int        `gorm:"type:int" json:"numeration"`         //编号
+	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 (Alarm) TableName() string {
+	return "t_dev_a_key_alarm_serve"
+}
+
+func (c Alarm) 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 Alarm) IsExistedBySN() bool {
+	var count = 0
+	_ = GDb.Model(&c).Where("  serve_sn = ? and is_deleted = ?",
+		c.ServeSN, c.IsDeleted).Count(&count).Error
+	return count > 0
+}
+
+func (c Alarm) IsExistedByNameAndCode() bool {
+	var devices []Alarm
+	err := GDb.Model(&c).Where(" serve_sn = ? and is_deleted = ?",
+		c.ServeSN, 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 *Alarm) Create() error {
+	return GDb.Model(&c).Save(&c).Error
+}
+
+func (c *Alarm) Update() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *Alarm) GetDevice() error {
+	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
+	return err
+}
+
+func (c Alarm) GetDevices(offset, limit int) ([]Alarm, error) {
+	var devices []Alarm
+	err := GDb.Model(&c).Where(" serve_name like ? and is_deleted = 0", "%"+c.ServeName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	return devices, err
+}
+
+func (c Alarm) GetAllDevices() ([]*Alarm, error) {
+	var devices []*Alarm
+	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

@@ -35,6 +35,7 @@ func InitDB() {
 			&Zigbee{},
 			&CameraDevice{},
 			&InfoBoard{},
+			&Alarm{},
 		).Error
 		if err != nil {
 			panic(fmt.Sprintf("AutoMigrate err : %v", err))

+ 15 - 0
app/model/alarn.go

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

+ 109 - 0
app/service/alarmService.go

@@ -0,0 +1,109 @@
+package service
+
+import (
+	"fmt"
+	"iot_manager_service/app/dao"
+	"iot_manager_service/app/model"
+	"iot_manager_service/app/utils"
+	"time"
+)
+
+// 中间件管理服务
+var AlarmService = new(alarmService)
+
+type alarmService struct{}
+
+func (s *alarmService) Get(id int) (*dao.Alarm, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.Alarm{
+		ID: id,
+	}
+	err := device.GetDevice()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+	return device, nil
+}
+
+func (s *alarmService) CreateOrUpdate(req dao.Alarm) *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 *alarmService) List(poleGroupName string, current, size int) ([]dao.Alarm, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.Alarm{}
+	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 *alarmService) Remove(id int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.Alarm{
+		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
+}
+
+func (s *alarmService) GetList() ([]*dao.Alarm, *utils.Errors) {
+	// todo use redis cache
+	device := &dao.Alarm{
+		TenantId:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+	devices, err := device.GetAllDevices()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	return devices, nil
+}