dws1.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "adm-dws/common.in/utils"
  6. "gorm.io/gorm"
  7. "go.uber.org/zap"
  8. "time"
  9. )
  10. type Dws1 struct {
  11. ID int64 `gorm:"primary_key" json:"-"`
  12. PlateNo string `json:"plate_no"`
  13. PlateType string `json:"plate_type"`
  14. Vin string `json:"vin"`
  15. Long string `json:"long" gorm:"-"`
  16. ApprovedNumber string `json:"approved_number" gorm:"-"`
  17. GrossMass string `json:"gross_mass" gorm:"-"`
  18. //Source int `json:"source"`
  19. CreatedAt int64 `json:"-"`
  20. UpdatedAt int64 `json:"-"`
  21. }
  22. func (Dws1) TableName() string {
  23. return "t_adm_dws1"
  24. }
  25. // Insert 插入一条记录
  26. func (p *Dws1) Insert(db *gorm.DB) error {
  27. if p.CreatedAt == 0 {
  28. timeNow := time.Now().Unix()
  29. p.CreatedAt = timeNow
  30. p.UpdatedAt = timeNow
  31. }
  32. err := db.Create(p).Error
  33. if err != nil {
  34. l.Error("mysql",
  35. zap.String("sql", "insert into "+p.TableName()),
  36. zap.String("fields", utils.MarshalJsonString(*p)),
  37. zap.String("error", err.Error()))
  38. }
  39. return err
  40. }
  41. func (p *Dws1)InsertByMap(db *gorm.DB,data map[string]interface{}) error {
  42. data["updated_at"] = time.Now().Unix()
  43. err := db.Model(p).Create(data).Error
  44. if err != nil{
  45. l.Error("mysql",
  46. zap.String("sql", "insert into "+p.TableName()),
  47. zap.String("fields", utils.MarshalJsonString(data)),
  48. zap.String("error", err.Error()))
  49. }
  50. return err
  51. }
  52. func (p *Dws1) Delete(db *gorm.DB, filter map[string]interface{}) error {
  53. err := db.Where(filter).Delete(p).Error
  54. if err != nil {
  55. l.Error("mysql",
  56. zap.String("sql", "delete from "+p.TableName()),
  57. zap.String("fields", utils.MarshalJsonString(filter)),
  58. zap.String("error", err.Error()))
  59. }
  60. return err
  61. }
  62. func (p *Dws1) Save(db *gorm.DB) error {
  63. timeNow := time.Now().Unix()
  64. p.UpdatedAt = timeNow
  65. //db.Model().Update()
  66. err := db.Save(p).Error
  67. if err != nil {
  68. l.Error("mysql",
  69. zap.String("sql", "save "+p.TableName()),
  70. zap.String("fields", utils.MarshalJsonString(*p)),
  71. zap.String("error", err.Error()))
  72. }
  73. return err
  74. }
  75. func (p *Dws1) Update(db *gorm.DB) error {
  76. p.CreatedAt = 0
  77. if p.UpdatedAt == 0 {
  78. timeNow := time.Now().Unix()
  79. p.UpdatedAt = timeNow
  80. }
  81. err := db.Model(p).UpdateColumns(p).Error
  82. if err != nil {
  83. l.Error("mysql",
  84. zap.String("sql", "update "+p.TableName()),
  85. zap.String("fields", utils.MarshalJsonString(*p)),
  86. zap.String("error", err.Error()))
  87. }
  88. return err
  89. }
  90. func (p *Dws1)UpdateByMap(db *gorm.DB,data map[string]interface{}) error {
  91. data["updated_at"] = time.Now().Unix()
  92. err := db.Model(p).Updates(data).Error
  93. if err != nil{
  94. l.Error("mysql",
  95. zap.String("sql", "update "+p.TableName()),
  96. zap.String("fields", utils.MarshalJsonString(data)),
  97. zap.String("error", err.Error()))
  98. }
  99. return err
  100. }
  101. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  102. func (p *Dws1) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  103. /*if filed == nil {
  104. return errors.ParamsError
  105. }*/
  106. timeNow := time.Now().Unix()
  107. filed["updated_at"] = timeNow
  108. err := db.Model(p).Updates(filed).Error
  109. if err != nil {
  110. l.Error("mysql",
  111. zap.String("sql", "update "+p.TableName()),
  112. zap.String("fields", utils.MarshalJsonString(filed)),
  113. zap.String("error", err.Error()))
  114. }
  115. return err
  116. }
  117. func (p *Dws1) Query(db *gorm.DB, filter map[string]interface{}) error {
  118. err := db.Where(filter).Find(p).Error
  119. if err != nil {
  120. l.Error("mysql",
  121. zap.String("sql", "select from "+p.TableName()),
  122. zap.String("fields", utils.MarshalJsonString(filter)),
  123. zap.String("error", err.Error()))
  124. }
  125. if p.ID == 0 {
  126. return gorm.ErrRecordNotFound
  127. }
  128. return err
  129. }
  130. func (p *Dws1) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  131. err := db.Where(filter).Find(out).Error
  132. if err != nil {
  133. l.Error("mysql",
  134. zap.String("sql", "select from "+p.TableName()),
  135. zap.String("fields", utils.MarshalJsonString(filter)),
  136. zap.String("error", err.Error()))
  137. }
  138. return err
  139. }