123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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"`
- CreateTime time.Time `gorm:"type:datetime" json:"createTime"`
- CreateUser int `gorm:"type:int" json:"createUser"`
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"`
- UpdateUser int `gorm:"type:int" json:"updateUser"`
- IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"`
- Status int `gorm:"type:int" json:"status"`
- }
- 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
- }
|