package dao

import (
	"gorm.io/gorm"
	"time"
)

// Transformer 变压器
type Transformer struct {
	ID              int        `gorm:"primary_key" json:"id"`                             //编号
	TransName       string     `gorm:"type:varchar(60)" json:"transName"`                 //变压器名称
	TransSN         string     `gorm:"type:varchar(60)" json:"transSn"`                   //设备序列号
	RatedPower      float32    `gorm:"type:double(8, 2) default 0.00 " json:"ratedPower"` //功率
	ProvinceName    string     `gorm:" varchar(12)" json:"provinceName"`                  //省份
	CityName        string     `gorm:"type:varchar(12)" json:"cityName"`                  //市
	DistrictName    string     `gorm:"type:varchar(12)" json:"districtName"`              //区
	InstallLocation string     `gorm:"type:varchar(100)" json:"installLocation"`          //详细位置
	PoleLng         float64    `gorm:"type:double(17, 14) " json:"poleLng"`               //经度
	PoleLat         float64    `gorm:"type:double(17, 14)  " json:"poleLat"`              //纬度
	TransPhotoUrl   string     `gorm:"type:varchar(255)" json:"transPhotoUrl"`            //变压器照片地址
	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      int64      `gorm:"type:bigint" json:"createUser"`                     //新增记录操作用户ID
	UpdateTime      time.Time  `gorm:"type:datetime" json:"updateTime"`                   //修改时间
	UpdateUser      int64      `gorm:"type:bigint" json:"updateUser"`                     //修改用户
	IsDeleted       int        `gorm:"type:int;default 0" json:"isDeleted"`               //是否删除 0=未删除,1=删除
	Status          int        `gorm:"type:int" json:"status"`                            //状态 0=正常,1=异常
	CountNum        int64      `gorm:"-" json:"countNum"`                                 //关联供电箱数量
}

func (Transformer) TableName() string {
	return "device_transformer"
}

func (c Transformer) Delete() error {
	return Db.Debug().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 Transformer) IsExistedBySN() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where("  trans_sn = ? and is_deleted = ?",
		c.TransSN, c.IsDeleted).Count(&count).Error
	return count > 0
}

func (c Transformer) IsExistedByNameAndCode() bool {
	var devices []Transformer
	err := Db.Debug().Model(&c).Where(" trans_sn = ? and is_deleted = ?",
		c.TransSN, 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 *Transformer) Create() error {
	return Db.Debug().Model(&c).Save(&c).Error
}

func (c *Transformer) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
}

func (c *Transformer) GetDevice() error {
	err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
	return err
}

func (c Transformer) GetDevices(offset, limit int) ([]Transformer, int64, error) {
	var count int64
	var devices []Transformer
	db := Db.Debug().Model(&c).Where(" trans_name like ? and is_deleted = 0", "%"+c.TransName+"%")
	db.Count(&count)
	err := db.Offset(offset).Limit(limit).Find(&devices).Error
	return devices, count, err
}

func (c Transformer) GetAllDevices() ([]*Transformer, error) {
	var devices []*Transformer
	err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
	return devices, err
}
