123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package dao
- import (
- "gorm.io/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 int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
- UpdateUser int `gorm:"type:int" 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 "device_a_key_alarm_terminal"
- }
- func (c AlarmTerminal) Delete() error {
- return Db.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 int64
- _ = Db.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 := Db.Model(&c).Where(" terminal_sn = ? and is_deleted = ?",
- c.TerminalSN, c.IsDeleted).Find(&devices).Error
- //如果查询不到,返回相应的错误
- if gorm.ErrRecordNotFound == err {
- return false
- }
- for _, d := range devices {
- if d.ID != c.ID {
- return true
- }
- }
- return false
- }
- func (c *AlarmTerminal) Create() error {
- return Db.Model(&c).Save(&c).Error
- }
- func (c *AlarmTerminal) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *AlarmTerminal) GetDevice() error {
- err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
- return err
- }
- func (c AlarmTerminal) GetDevices(offset, limit int) ([]AlarmTerminal, int64, error) {
- var devices []AlarmTerminal
- var count int64
- db := Db.Model(&c).Where(" terminal_name like ? and is_deleted = 0", "%"+c.TerminalName+"%")
- db.Count(&count)
- err := db.Offset(offset).Limit(limit).Find(&devices).Error
- return devices, count, err
- }
- func (c AlarmTerminal) GetAllDevices() ([]*AlarmTerminal, error) {
- var devices []*AlarmTerminal
- err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
- func (c AlarmTerminal) GetDevicesByGateway() []AlarmTerminal {
- var devices []AlarmTerminal
- Db.Model(&c).Where(" gateway_id = ? and is_deleted = 0",
- c.GatewayId).Find(&devices)
- return devices
- }
- func (c AlarmTerminal) GetDevicesByLampPole() []AlarmTerminal {
- var devices []AlarmTerminal
- Db.Model(&c).Where("lamp_pole_id = ? and is_deleted = 0",
- c.LampPoleId).Find(&devices)
- return devices
- }
- func (c AlarmTerminal) GetTerminalCount(serverId int) int64 {
- var count int64
- Db.Model(&c).Where("serve_id = ? and is_deleted = 0",
- serverId).Count(&count)
- return count
- }
- func (c AlarmTerminal) DeviceCount1() (int64, int64) {
- var all, fault int64
- err := Db.Model(&c).Where("is_deleted = 0").Count(&all).Error
- if err != nil {
- all = -1
- }
- err = Db.Model(&c).Where("is_deleted = 0 and status = 0").Count(&fault).Error
- if err != nil {
- fault = -1
- }
- return all, fault
- }
- func (c AlarmTerminal) ListDevices1() []Device {
- var devices []AlarmTerminal
- Db.Model(&c).Where("is_deleted = 0").Find(&devices)
- var rsp = make([]Device, 0, len(devices))
- for _, v := range devices {
- var d = Device{
- Lng: v.PoleLng,
- Lat: v.PoleLat,
- Status: v.Status,
- Name: v.TerminalName,
- Code: v.TerminalSN,
- LampPoleName: v.LampPoleName,
- }
- rsp = append(rsp, d)
- }
- return rsp
- }
|