dws14.go 3.9 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. "adm-dws/common.in/utils"
  6. "gorm.io/gorm"
  7. "go.uber.org/zap"
  8. "time"
  9. )
  10. type Dws14 struct {
  11. ID int64 `gorm:"primary_key" json:"-"`
  12. PlateNo string `json:"plate_no" gorm:"-"`
  13. IdCard string `json:"id_card" gorm:"-"`
  14. Name string `json:"name" gorm:"-"`
  15. Owner string `json:"owner" gorm:"-"`
  16. Type int `json:"type"`
  17. Rule string `json:"rule"`
  18. CreatedAt int64 `json:"-"`
  19. UpdatedAt int64 `json:"-"`
  20. }
  21. func (Dws14) TableName() string {
  22. return "t_adm_dws14"
  23. }
  24. // Insert 插入一条记录
  25. func (p *Dws14) Insert(db *gorm.DB) error {
  26. timeNow := time.Now().Unix()
  27. p.CreatedAt = timeNow
  28. p.UpdatedAt = 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 *Dws14)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 *Dws14) 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 *Dws14) Save(db *gorm.DB) error {
  60. timeNow := time.Now().Unix()
  61. p.UpdatedAt = timeNow
  62. //db.Model().Update()
  63. err := db.Save(p).Error
  64. if err != nil {
  65. l.Error("mysql",
  66. zap.String("sql", "save "+p.TableName()),
  67. zap.String("fields", utils.MarshalJsonString(*p)),
  68. zap.String("error", err.Error()))
  69. }
  70. return err
  71. }
  72. func (p *Dws14) Update(db *gorm.DB) error {
  73. timeNow := time.Now().Unix()
  74. p.CreatedAt = 0
  75. p.UpdatedAt = timeNow
  76. err := db.Model(p).Updates(p).Error
  77. if err != nil {
  78. l.Error("mysql",
  79. zap.String("sql", "update "+p.TableName()),
  80. zap.String("fields", utils.MarshalJsonString(*p)),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. func (p *Dws14)UpdateByMap(db *gorm.DB,data map[string]interface{}) error {
  86. data["updated_at"] = time.Now().Unix()
  87. err := db.Model(p).Updates(data).Error
  88. if err != nil{
  89. l.Error("mysql",
  90. zap.String("sql", "update "+p.TableName()),
  91. zap.String("fields", utils.MarshalJsonString(data)),
  92. zap.String("error", err.Error()))
  93. }
  94. return err
  95. }
  96. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  97. func (p *Dws14) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  98. /*if filed == nil {
  99. return errors.ParamsError
  100. }*/
  101. timeNow := time.Now().Unix()
  102. filed["updated_at"] = timeNow
  103. err := db.Model(p).Updates(filed).Error
  104. if err != nil {
  105. l.Error("mysql",
  106. zap.String("sql", "update "+p.TableName()),
  107. zap.String("fields", utils.MarshalJsonString(filed)),
  108. zap.String("error", err.Error()))
  109. }
  110. return err
  111. }
  112. func (p *Dws14) Query(db *gorm.DB, filter map[string]interface{}) error {
  113. err := db.Where(filter).Find(p).Error
  114. if err != nil {
  115. l.Error("mysql",
  116. zap.String("sql", "select from "+p.TableName()),
  117. zap.String("fields", utils.MarshalJsonString(filter)),
  118. zap.String("error", err.Error()))
  119. }
  120. if p.ID == 0 {
  121. return gorm.ErrRecordNotFound
  122. }
  123. return err
  124. }
  125. func (p *Dws14) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  126. err := db.Where(filter).Find(out).Error
  127. if err != nil {
  128. l.Error("mysql",
  129. zap.String("sql", "select from "+p.TableName()),
  130. zap.String("fields", utils.MarshalJsonString(filter)),
  131. zap.String("error", err.Error()))
  132. }
  133. return err
  134. }