space_park.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TSpacePark struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. SpaceNumber string `gorm:"column:space_number" json:"space_number"`
  15. SpaceType int32 `gorm:"column:space_type" json:"space_type"`
  16. SpaceArea float64 `gorm:"column:space_area" json:"space_area"`
  17. Comment string `gorm:"column:comment" json:"comment"`
  18. ParkId int64 `gorm:"column:park_id" json:"park_id"`
  19. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  20. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  21. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  22. ParkNumber string `gorm:"column:park_number" json:"park_number"`
  23. ParkName string `gorm:"column:park_name" json:"park_name"`
  24. ParkType int32 `gorm:"column:park_type" json:"park_type"`
  25. BindStatus int32 `gorm:"column:bind_status" json:"bind_status"`
  26. VehicleId int64 `gorm:"column:vehicle_id" json:"vehicle_id"`
  27. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  28. Status int32 `gorm:"column:status" json:"status"`
  29. table string
  30. }
  31. func (p *TSpacePark) TableName() string {
  32. return p.table
  33. }
  34. func NewSpacePark(database string) *TSpacePark {
  35. return &TSpacePark{table: fmt.Sprintf("%s.%s", database, "t_space_park")}
  36. }
  37. func (p *TSpacePark) CreateView(db *gorm.DB, database string) error {
  38. sql := "CREATE VIEW "+database+".`t_space_park` AS select t1.*,"+
  39. "`t2`.`park_number` AS `park_number`,`t2`.`park_name` AS `park_name`,`t2`.`park_type` AS `park_type` from "+
  40. "("+database+".`t_park_space` `t1` left join "+database+".`t_park` `t2` on((`t1`.`park_id` = `t2`.`id`)))"
  41. err := db.Exec(sql).Error
  42. if err != nil {
  43. logger.Error("mysql",
  44. zap.String("sql", "create view "+p.TableName()),
  45. zap.String("fields", ""),
  46. zap.String("error", err.Error()))
  47. }
  48. return err
  49. }
  50. func (p *TSpacePark) Find(db *gorm.DB, where map[string]interface{}) error {
  51. err := db.Table(p.TableName()).Where(where).First(p).Error
  52. if err != nil {
  53. fields, _ := util.MarshalToString(where)
  54. logger.Error("mysql",
  55. zap.String("sql", "select from "+p.TableName()),
  56. zap.String("fields", fields),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func (p *TSpacePark) Last(db *gorm.DB) error {
  62. err := db.Table(p.TableName()).Last(p).Error
  63. if err != nil {
  64. logger.Error("mysql",
  65. zap.String("sql", "select last from "+p.TableName()),
  66. zap.String("fields", ""),
  67. zap.String("error", err.Error()))
  68. }
  69. return err
  70. }
  71. // Insert 插入一条记录
  72. func (p *TSpacePark) Insert(db *gorm.DB) error {
  73. err := db.Create(p).Error
  74. if err != nil {
  75. fields, _ := util.MarshalToString(*p)
  76. logger.Error("mysql",
  77. zap.String("sql", "insert into "+p.TableName()),
  78. zap.String("fields", fields),
  79. zap.String("error", err.Error()))
  80. }
  81. return err
  82. }
  83. func (p *TSpacePark) Delete(db *gorm.DB, filter map[string]interface{}) error {
  84. cond, val, err := whereBuild(filter)
  85. if err != nil {
  86. return err
  87. }
  88. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  89. }
  90. func (p *TSpacePark) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  91. cond, val, err := whereBuild(where)
  92. if err != nil {
  93. if err != nil {
  94. fields, _ := util.MarshalToString(values)
  95. logger.Error("mysql",
  96. zap.String("sql", "update "+p.TableName()),
  97. zap.String("fields", fields),
  98. zap.String("error", err.Error()))
  99. }
  100. return err
  101. }
  102. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  103. }
  104. func (p *TSpacePark) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  105. cond, val, err := whereBuildAndOr(where, or)
  106. if err != nil {
  107. return 0, err
  108. }
  109. ret := int64(0)
  110. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  111. if err != nil {
  112. fields, _ := util.MarshalToString(where)
  113. logger.Error("mysql",
  114. zap.String("sql", "select count "+p.TableName()),
  115. zap.String("fields", fields),
  116. zap.String("error", err.Error()))
  117. }
  118. return ret, err
  119. }
  120. func (p *TSpacePark) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TSpacePark, err error) {
  121. cond, val, err := whereBuildAndOr(where, or)
  122. if err != nil {
  123. return list, err
  124. }
  125. if pageSize < 0 {
  126. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  127. if result.Error != nil {
  128. wherefields, _ := util.MarshalToString(where)
  129. logger.Error("mysql",
  130. zap.String("sql", "select * from "+p.TableName()),
  131. zap.String("where", wherefields),
  132. zap.String("error", result.Error.Error()))
  133. }
  134. return list, result.Error
  135. }
  136. offset := (page - 1) * pageSize
  137. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  138. if result.Error != nil {
  139. wherefields, _ := util.MarshalToString(where)
  140. logger.Error("mysql",
  141. zap.String("sql", "select * from "+p.TableName()),
  142. zap.String("where", wherefields),
  143. zap.String("error", result.Error.Error()))
  144. }
  145. return list, result.Error
  146. }