station.go 4.3 KB

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