device.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "cp-organization-management/errors"
  6. "fmt"
  7. "time"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "github.com/jaryhe/gopkgs/util"
  10. "go.uber.org/zap"
  11. "github.com/jinzhu/gorm"
  12. )
  13. type Device struct {
  14. Id int64 `json:"id" gorm:"primary_key;index:idx"`
  15. Name string `json:"name" gorm:"name;comment:'设备名';size:128;not null;default:''"`
  16. DeviceCode string `json:"device_code" gorm:"device_code;comment:'充电桩设备编码';not null;default:''"`
  17. StationCode string `json:"station_code" gorm:"station_code;comment:'充电站编码';not null;default:''"`
  18. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  19. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  20. table string `json:"table"`
  21. }
  22. func (p *Device) TableName() string {
  23. return p.table
  24. }
  25. func NewDevice(dbname string) *Device {
  26. return &Device{table: fmt.Sprintf("%s.%s", dbname, "t_device")}
  27. }
  28. func (p *Device) CreateTable(db *gorm.DB) error {
  29. err := db.CreateTable(p).Error
  30. if err != nil {
  31. logger.Error("mysql",
  32. zap.String("sql", "create table "+p.TableName()),
  33. zap.String("fields", ""),
  34. zap.String("error", err.Error()))
  35. }
  36. return nil
  37. }
  38. func (p *Device) Find(db *gorm.DB, where map[string]interface{}) error {
  39. err := db.Table(p.TableName()).Where(where).Find(p).Error
  40. if err != nil {
  41. fields, _ := util.MarshalToString(where)
  42. logger.Error("mysql",
  43. zap.String("sql", "select from "+p.TableName()),
  44. zap.String("fields", fields),
  45. zap.String("error", err.Error()))
  46. }
  47. return err
  48. }
  49. func (p *Device) Last(db *gorm.DB) error {
  50. err := db.Table(p.TableName()).Last(p).Error
  51. if err != nil {
  52. logger.Error("mysql",
  53. zap.String("sql", "select last from "+p.TableName()),
  54. zap.String("fields", ""),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. // Insert 插入一条记录
  60. func (p *Device) Insert(db *gorm.DB) error {
  61. timeNow := time.Now()
  62. p.CreatedAt = timeNow
  63. p.UpdatedAt = timeNow
  64. err := db.Create(p).Error
  65. if err != nil {
  66. fields, _ := util.MarshalToString(*p)
  67. logger.Error("mysql",
  68. zap.String("sql", "insert into "+p.TableName()),
  69. zap.String("fields", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *Device) Delete(db *gorm.DB, filter map[string]interface{}) error {
  75. err := db.Where(filter).Delete(p).Error
  76. if err != nil {
  77. fields, _ := util.MarshalToString(filter)
  78. logger.Error("mysql",
  79. zap.String("sql", "delete from "+p.TableName()),
  80. zap.String("fields", fields),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. func (p *Device) Save(db *gorm.DB) error {
  86. p.UpdatedAt = time.Now()
  87. err := db.Save(p).Error
  88. if err != nil {
  89. fields, _ := util.MarshalToString(*p)
  90. logger.Error("mysql",
  91. zap.String("sql", "save "+p.TableName()),
  92. zap.String("fields", fields),
  93. zap.String("error", err.Error()))
  94. }
  95. return err
  96. }
  97. func (p *Device) Update(db *gorm.DB) error {
  98. p.UpdatedAt = time.Now()
  99. err := db.Model(p).Updates(p).Error
  100. if err != nil {
  101. fields, _ := util.MarshalToString(*p)
  102. logger.Error("mysql",
  103. zap.String("sql", "update "+p.TableName()),
  104. zap.String("fields", fields),
  105. zap.String("error", err.Error()))
  106. }
  107. return err
  108. }
  109. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  110. func (p *Device) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  111. if filed == nil {
  112. return errors.ParamsError
  113. }
  114. filed["updated_at"] = time.Now()
  115. err := db.Model(p).Updates(filed).Error
  116. if err != nil {
  117. fields, _ := util.MarshalToString(filed)
  118. logger.Error("mysql",
  119. zap.String("sql", "update "+p.TableName()),
  120. zap.String("fields", fields),
  121. zap.String("error", err.Error()))
  122. }
  123. return err
  124. }
  125. func (p *Device) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  126. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  127. if err != nil {
  128. fields, _ := util.MarshalToString(filter)
  129. logger.Error("mysql",
  130. zap.String("sql", "select from "+p.TableName()),
  131. zap.String("fields", fields),
  132. zap.String("error", err.Error()))
  133. }
  134. return err
  135. }
  136. func (p *Device) List(db *gorm.DB, limit, offset int, out interface{}) error {
  137. err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error
  138. if err != nil {
  139. logger.Error("mysql",
  140. zap.String("sql", "select from "+p.TableName()),
  141. zap.String("fields", ""),
  142. zap.String("error", err.Error()))
  143. }
  144. return err
  145. }
  146. func (p *Device) Count(db *gorm.DB) (int64, error) {
  147. var count int64
  148. err := db.Table(p.TableName()).Count(&count).Error
  149. if err != nil {
  150. logger.Error("mysql",
  151. zap.String("sql", "select count from "+p.TableName()),
  152. zap.String("fields", ""),
  153. zap.String("error", err.Error()))
  154. }
  155. return count, err
  156. }