ads25.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "time"
  6. "gorm.io/gorm"
  7. )
  8. type Dws19 struct {
  9. ID int64 `gorm:"primary_key" json:"-"`
  10. EngineType string `json:"engine_type"`
  11. Displacement string `json:"displacement"`
  12. RatedPower string `json:"rated_power"`
  13. CreatedAt int64 `json:"-"`
  14. UpdatedAt int64 `json:"-"`
  15. }
  16. func (Dws19) TableName() string {
  17. return "t_adm_dws19"
  18. }
  19. // Insert 插入一条记录
  20. func (p *Dws19) Insert(db *gorm.DB) error {
  21. timeNow := time.Now().Unix()
  22. p.CreatedAt = timeNow
  23. p.UpdatedAt = timeNow
  24. err := db.Create(p).Error
  25. return err
  26. }
  27. func (p *Dws19) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  28. data["updated_at"] = time.Now().Unix()
  29. err := db.Model(p).Create(data).Error
  30. return err
  31. }
  32. func (p *Dws19) Delete(db *gorm.DB, filter map[string]interface{}) error {
  33. err := db.Where(filter).Delete(p).Error
  34. return err
  35. }
  36. func (p *Dws19) Save(db *gorm.DB) error {
  37. timeNow := time.Now().Unix()
  38. p.UpdatedAt = timeNow
  39. //db.Model().Update()
  40. err := db.Save(p).Error
  41. return err
  42. }
  43. func (p *Dws19) Update(db *gorm.DB) error {
  44. timeNow := time.Now().Unix()
  45. p.CreatedAt = 0
  46. p.UpdatedAt = timeNow
  47. err := db.Model(p).Updates(p).Error
  48. return err
  49. }
  50. func (p *Dws19) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  51. data["updated_at"] = time.Now().Unix()
  52. err := db.Model(p).Updates(data).Error
  53. return err
  54. }
  55. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  56. func (p *Dws19) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  57. /*if filed == nil {
  58. return errors.ParamsError
  59. }*/
  60. timeNow := time.Now().Unix()
  61. filed["updated_at"] = timeNow
  62. err := db.Model(p).Updates(filed).Error
  63. return err
  64. }
  65. func (p *Dws19) Query(db *gorm.DB, filter map[string]interface{}) error {
  66. err := db.Where(filter).Find(p).Error
  67. if p.ID == 0 {
  68. return gorm.ErrRecordNotFound
  69. }
  70. return err
  71. }
  72. func (p *Dws19) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  73. err := db.Where(filter).Find(out).Error
  74. return err
  75. }