engine_info.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 EngineInfo struct {
  9. ID int64 `gorm:"primary_key" json:"-"`
  10. ModelNo string `json:"model_no"`
  11. EngineType string `json:"engine_type"`
  12. Displacement string `json:"displacement"`
  13. RatedPower string `json:"rated_power"`
  14. }
  15. func (EngineInfo) TableName() string {
  16. return "v_adm_engine_info"
  17. }
  18. // Insert 插入一条记录
  19. func (p *EngineInfo) Insert(db *gorm.DB) error {
  20. err := db.Create(p).Error
  21. return err
  22. }
  23. func (p *EngineInfo) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  24. data["updated_at"] = time.Now().Unix()
  25. err := db.Model(p).Create(data).Error
  26. return err
  27. }
  28. func (p *EngineInfo) Delete(db *gorm.DB, filter map[string]interface{}) error {
  29. err := db.Where(filter).Delete(p).Error
  30. return err
  31. }
  32. func (p *EngineInfo) Save(db *gorm.DB) error {
  33. //db.Model().Update()
  34. err := db.Save(p).Error
  35. return err
  36. }
  37. func (p *EngineInfo) Update(db *gorm.DB) error {
  38. err := db.Model(p).Updates(p).Error
  39. return err
  40. }
  41. func (p *EngineInfo) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  42. data["updated_at"] = time.Now().Unix()
  43. err := db.Model(p).Updates(data).Error
  44. return err
  45. }
  46. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  47. func (p *EngineInfo) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  48. /*if filed == nil {
  49. return errors.ParamsError
  50. }*/
  51. timeNow := time.Now().Unix()
  52. filed["updated_at"] = timeNow
  53. err := db.Model(p).Updates(filed).Error
  54. return err
  55. }
  56. func (p *EngineInfo) Query(db *gorm.DB, filter map[string]interface{}) error {
  57. err := db.Where(filter).Order("id DESC").Find(p).Error
  58. return err
  59. }
  60. func (p *EngineInfo) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  61. err := db.Where(filter).Find(out).Error
  62. return err
  63. }