garbageWayGroupDao.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package dao
  2. import (
  3. "gorm.io/gorm"
  4. "time"
  5. )
  6. // GarbageWayGroup 垃圾桶道路分组管理
  7. type GarbageWayGroup struct {
  8. ID int `gorm:"primary_key" json:"id"` //编号
  9. GroupName string `gorm:"type:varchar(64)" json:"groupName"` //分组名称
  10. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户id
  11. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  12. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  13. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  14. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  15. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  16. Remark string `gorm:"type:varchar(255)" json:"remark"` //备注
  17. }
  18. func (GarbageWayGroup) TableName() string {
  19. return "device_garbage_way_group"
  20. }
  21. func (c GarbageWayGroup) Delete() error {
  22. return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
  23. "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
  24. }
  25. func (c GarbageWayGroup) IsExistedByName() bool {
  26. var count int64
  27. _ = Db.Model(&c).Where(" group_name = ? and is_deleted = ?",
  28. c.GroupName, c.IsDeleted).Count(&count).Error
  29. return count > 0
  30. }
  31. func (c GarbageWayGroup) IsExistedByNameAndCode() bool {
  32. var devices []Garbage
  33. err := Db.Model(&c).Where(" group_name = ? and is_deleted = ?",
  34. c.GroupName, c.IsDeleted).Find(&devices).Error
  35. //如果查询不到,返回相应的错误
  36. if gorm.ErrRecordNotFound == err {
  37. return false
  38. }
  39. for _, d := range devices {
  40. if d.ID != c.ID {
  41. return true
  42. }
  43. }
  44. return false
  45. }
  46. func (c *GarbageWayGroup) Create() error {
  47. return Db.Model(&c).Save(&c).Error
  48. }
  49. func (c *GarbageWayGroup) Update() error {
  50. return Db.Model(&c).Where(" id = ? ", c.ID).Updates(&c).Error
  51. }
  52. func (c *GarbageWayGroup) GetDevice() error {
  53. err := Db.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
  54. return err
  55. }
  56. func (c GarbageWayGroup) GetDevices(offset, limit int) ([]GarbageWayGroup, int64, error) {
  57. var devices []GarbageWayGroup
  58. var count int64
  59. db := Db.Model(&c).Where(" group_name like ? and is_deleted = 0", "%"+c.GroupName+"%")
  60. db.Count(&count)
  61. err := db.Offset(offset).Limit(limit).Find(&devices).Error
  62. return devices, count, err
  63. }
  64. func (c GarbageWayGroup) GetAllDevices() ([]*GarbageWayGroup, error) {
  65. var devices []*GarbageWayGroup
  66. err := Db.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
  67. return devices, err
  68. }