package dao

import (
	"gorm.io/gorm"
	"iot_manager_service/util/common"
	"time"
)

//LampPoleGroup 灯杆分组
type LampPoleGroup struct {
	ID            int         `gorm:"primary_key" json:"id"`                             //编号
	PoleGroupName string      `gorm:"type:varchar(64)" json:"poleGroupName"`             //分组名称
	TenantId      string      `gorm:"type:varchar(12)" json:"tenantId"`                  //租户ID
	CreateTime    common.Time `gorm:"type:datetime;not null'" json:"createTime"`         //新增时间
	CreateUser    int64       `gorm:"type:bigint" json:"createUser"`                     //新增记录操作用户ID
	UpdateTime    common.Time `gorm:"type:datetime" json:"updateTime"`                   //修改时间
	UpdateUser    int64       `gorm:"type:bigint" json:"updateUser"`                     //修改用户
	IsDeleted     int         `gorm:"type:int;default 0" json:"isDeleted"`               //是否删除 0=未删除,1=删除
	Tag           string      `gorm:"type:varchar(255)" json:"tag"`                      //标签，(备用，逗号区分)
	Remark        string      `gorm:"type:varchar(255)" json:"remark"`                   //备注
	CountLampPole int         `gorm:"type:int;default 0" json:"countLampPole,omitempty"` //灯杆数
}

func (LampPoleGroup) TableName() string {
	return "device_lamp_pole_group"
}

func (c *LampPoleGroup) 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 LampPoleGroup) IsExistedByName() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where(" pole_group_name = ? and tenant_id = ? and is_deleted = ?",
		c.PoleGroupName, c.TenantId, c.IsDeleted).Count(&count).Error
	return count > 0
}

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

func (c *LampPoleGroup) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"pole_group_name": c.PoleGroupName, "remark": c.Remark,
		"tenant_id": c.TenantId, "update_time": time.Now(), "update_user": c.UpdateUser, "is_deleted": 0}).Error
}

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

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

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

func (c LampPoleGroup) BatchGet(ids []int) []*LampPoleGroup {
	var devices []*LampPoleGroup
	Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? and id in (?)", c.TenantId,
		c.IsDeleted, ids).Scan(&devices)
	return devices
}
