package dao import ( "gorm.io/gorm" "time" ) type Bridge struct { ID int `gorm:"primary_key" json:"id"` //编号 Name string `gorm:"type:varchar(64)" json:"name"` //名称 SN string `gorm:"type:varchar(60)" json:"sn"` //唯一编码 Lng float64 `gorm:"type:double(17, 14)" json:"lng"` //经度 Lat float64 `gorm:"type:double(17, 14)" json:"lat"` //纬度 ProvinceName string `gorm:"type:varchar(60)" json:"provinceName"` //省份 CityName string `gorm:"type:varchar(60)" json:"cityName"` //市 DistrictName string `gorm:"type:varchar(60)" json:"districtName"` //区 InstallPosition string `gorm:"type:varchar(100)" json:"installLocation"` //位置 PurposeType int `gorm:"type:int" json:"purposeType"` //用途类型 StraddlesType int `gorm:"type:int" json:"straddlesType"` //跨越类型 StuffType int `gorm:"type:int" json:"stuffType"` //材料类型 StructureType int `gorm:"type:int" json:"structureType"` //结构类型 TechniqueGrade int `gorm:"type:int" json:"techniqueGrade"` //技术等级 CompletionTime string `gorm:"type:date" json:"completionTime"` //建成时间 Length int `gorm:"type:int" json:"length"` //长度 Width int `gorm:"type:int" json:"width"` //宽度 Height int `gorm:"type:int" json:"height"` //高度 TrafficNum int `gorm:"type:int" json:"trafficNum"` //车道数 IsOpenTraffic int `gorm:"type:int" json:"isOpenTraffic"` //是否已通车 DesignSpeed int `gorm:"type:int" json:"designSpeed"` //设计时速 BriefIntroduction string `gorm:"type:varchar(255)" json:"briefIntroduction"` //简介 Photo string `gorm:"type:varchar(255)" json:"photo"` //照片 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;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除 Status int `gorm:"type:int" json:"status"` //状态 0=正常,1=异常 } func (Bridge) TableName() string { return "device_bridge" } func (c Bridge) 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 Bridge) IsExistedBySN() bool { var count int64 _ = Db.Model(&c).Where(" tsn = ? and is_deleted = ?", c.SN, c.IsDeleted).Count(&count).Error return count > 0 } func (c Bridge) IsExistedByNameAndCode() bool { var devices []Bridge err := Db.Model(&c).Where(" sn = ? and is_deleted = ?", c.SN, 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 *Bridge) Create() error { return Db.Model(&c).Save(&c).Error } func (c *Bridge) Update() error { return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error } func (c *Bridge) GetDevice() error { err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error return err } func (c Bridge) GetDevices(offset, limit int) ([]Bridge, int64, error) { var devices []Bridge var count int64 db := Db.Model(&c).Where(" name like ? and is_deleted = 0", "%"+c.Name+"%") db.Count(&count) err := db.Offset(offset).Limit(limit).Find(&devices).Error return devices, count, err } func (c Bridge) GetAllDevices() ([]*Bridge, error) { var devices []*Bridge err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error return devices, err } func (c Bridge) 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 Bridge) ListDevices1() []Device { var devices []Bridge 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.Lng, Lat: v.Lat, Status: v.Status, Name: v.Name, Code: v.SN, LampPoleName: v.DistrictName, Model: "模型", } rsp = append(rsp, d) } return rsp }