dws6.go 4.0 KB

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