123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package dao
- import (
- "gorm.io/gorm"
- "iot_manager_service/util/common"
- "time"
- )
- type SwitchBox struct {
- ID int `gorm:"primary_key" json:"id"`
- BoxName string `gorm:"type:varchar(64)" json:"boxName"`
- BoxSN string `gorm:"type:varchar(60)" json:"boxSn"`
- BrandId int `gorm:"type:int" json:"brandId"`
- ModelId int `gorm:"type:int" json:"modelId"`
- TransformerId int `gorm:"type:int" json:"transformerId"`
- TransformerNum string `gorm:"type:varchar(60)" json:"transformerNum"`
- RatedPower float32 `gorm:"type:double(8, 2) default 0.00 " json:"ratedPower"`
- ProvinceName string `gorm:" type:varchar(12)" json:"provinceName"`
- CityName string `gorm:"type:varchar(12)" json:"cityName"`
- DistrictName string `gorm:"type:varchar(12)" json:"districtName"`
- BoxLocation string `gorm:"type:varchar(100)" json:"boxLocation"`
- PoleLng float64 `gorm:"type:double(17, 14) " json:"poleLng"`
- PoleLat float64 `gorm:"type:double(17, 14) " json:"poleLat"`
- MonitorAddress string `gorm:"type:varchar(255) " json:"monitorAddress"`
- InstallTime common.Time `gorm:"type:date" json:"installTime"`
- 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"`
- Tag string `gorm:"type:varchar(255)" json:"tag"`
- BoxPhotoUrl string `gorm:"type:varchar(255)" json:"boxPhotoUrl"`
- CountLampPole int64 `gorm:"-" json:"countLampPole" comment:"关联灯杆数" default:0`
- }
- func (SwitchBox) TableName() string {
- return "device_switch_box"
- }
- func (c SwitchBox) 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 SwitchBox) IsExistedBySN() bool {
- var count int64
- _ = Db.Model(&c).Where(" box_sn = ? and is_deleted = ?",
- c.BoxSN, c.IsDeleted).Count(&count).Error
- return count > 0
- }
- func (c SwitchBox) IsExistedByNameAndCode() bool {
- var devices []SwitchBox
- err := Db.Model(&c).Where(" box_sn = ? and is_deleted = ?",
- c.BoxSN, 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 *SwitchBox) Create() error {
- return Db.Model(&c).Save(&c).Error
- }
- func (c *SwitchBox) Update() error {
- return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
- }
- func (c *SwitchBox) GetDevice() error {
- err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
- return err
- }
- func (c SwitchBox) GetDevices(offset, limit int) ([]SwitchBox, int64, error) {
- var devices []SwitchBox
- var count int64
- db := Db.Model(&c).Where("(box_name like ?) and is_deleted = 0",
- "%"+c.BoxName+"%")
- db.Count(&count)
- err := db.Offset(offset).Limit(limit).Find(&devices).Error
- return devices, count, err
- }
- func (c SwitchBox) GetDeviceCount() (int64, error) {
- var count int64
- err := Db.Model(&c).Where("(transformer_id = ?) and is_deleted = 0",
- c.TransformerId).Count(&count).Error
- return count, err
- }
- func (c SwitchBox) GetAllDevices() ([]*SwitchBox, error) {
- var devices []*SwitchBox
- err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
- func (c SwitchBox) CountFault() (int64, error) {
- var i int64
- err := Db.Model(&c).Where(map[string]interface{}{"is_deleted": "0", "status": "0"}).Count(&i).Error
- return i, err
- }
- func (c SwitchBox) ListDevices() ([]Device, int64, error) {
- var devices []SwitchBox
- result := 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.BoxName,
- Code: v.BoxSN,
- Model: "型号",
- }
- rsp = append(rsp, d)
- }
- var i int64
- result.Count(&i)
- return rsp, i, result.Error
- }
- func (c SwitchBox) 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 SwitchBox) ListDevices1() []Device {
- var devices []SwitchBox
- 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.BoxName,
- Code: v.BoxSN,
- Model: "模型",
- }
- rsp = append(rsp, d)
- }
- return rsp
- }
|