park.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 TPark struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. ParkNumber string `gorm:"column:park_number" json:"park_number"`
  15. ParkName string `gorm:"column:park_name" json:"park_name"`
  16. ParkType int32 `gorm:"column:park_type" json:"park_type"`
  17. SpaceCount int64 `gorm:"column:space_count" json:"park_type"`
  18. Comment string `gorm:"column:comment" json:"comment"`
  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. table string
  23. }
  24. func (p *TPark) TableName() string {
  25. return p.table
  26. }
  27. func NewPark(database string) *TPark {
  28. return &TPark{table: fmt.Sprintf("%s.%s", database, "t_park")}
  29. }
  30. func (p *TPark) SetTable(database string) {
  31. p.table = fmt.Sprintf("%s.%s", database, "t_park")
  32. }
  33. func (p *TPark) CreateTable(db *gorm.DB) error {
  34. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  35. "`id` int(10) NOT NULL AUTO_INCREMENT,"+
  36. "`park_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '停车场编号',"+
  37. "`park_name` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '停车场名称',"+
  38. "`park_type` tinyint(1) NOT NULL COMMENT '1 地面停车场 2 地下停车场',"+
  39. "`comment` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '备注',"+
  40. "`created_at` datetime NOT NULL,"+
  41. " `updated_at` datetime NOT NULL,"+
  42. " `space_count` int(10) NOT NULL COMMENT ' 车位数',"+
  43. "PRIMARY KEY (`id`) USING BTREE,"+
  44. " UNIQUE KEY `garden_park` (`park_number`) USING BTREE"+
  45. ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
  46. err := db.Exec(sql).Error
  47. if err != nil {
  48. logger.Error("mysql",
  49. zap.String("sql", "create table "+p.TableName()),
  50. zap.String("fields", ""),
  51. zap.String("error", err.Error()))
  52. }
  53. return err
  54. }
  55. func (p *TPark) Find(db *gorm.DB, where map[string]interface{}) error {
  56. err := db.Table(p.TableName()).Where(where).Find(p).Error
  57. if err != nil {
  58. fields, _ := util.MarshalToString(where)
  59. logger.Error("mysql",
  60. zap.String("sql", "select from "+p.TableName()),
  61. zap.String("fields", fields),
  62. zap.String("error", err.Error()))
  63. }
  64. return err
  65. }
  66. func (p *TPark) Last(db *gorm.DB) error {
  67. err := db.Table(p.TableName()).Last(p).Error
  68. if err != nil {
  69. logger.Error("mysql",
  70. zap.String("sql", "select last from "+p.TableName()),
  71. zap.String("fields", ""),
  72. zap.String("error", err.Error()))
  73. }
  74. return err
  75. }
  76. // Insert 插入一条记录
  77. func (p *TPark) Insert(db *gorm.DB) error {
  78. err := db.Table(p.TableName()).Create(p).Error
  79. if err != nil {
  80. fields, _ := util.MarshalToString(*p)
  81. logger.Error("mysql",
  82. zap.String("sql", "insert into "+p.TableName()),
  83. zap.String("fields", fields),
  84. zap.String("error", err.Error()))
  85. }
  86. return err
  87. }
  88. func (p *TPark) Delete(db *gorm.DB, filter map[string]interface{}) error {
  89. err := db.Table(p.TableName()).Where(filter).Delete(p).Error
  90. if err != nil {
  91. fields, _ := util.MarshalToString(filter)
  92. logger.Error("mysql",
  93. zap.String("sql", "delete from "+p.TableName()),
  94. zap.String("where", fields),
  95. zap.String("error", err.Error()))
  96. }
  97. return err
  98. }
  99. func (p *TPark) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  100. cond, val, err := whereBuild(where)
  101. if err != nil {
  102. if err != nil {
  103. fields, _ := util.MarshalToString(values)
  104. logger.Error("mysql",
  105. zap.String("sql", "update "+p.TableName()),
  106. zap.String("fields", fields),
  107. zap.String("error", err.Error()))
  108. }
  109. return err
  110. }
  111. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  112. }
  113. func (p *TPark) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  114. cond, val, err := whereBuildAndOr(where, or)
  115. if err != nil {
  116. return 0, err
  117. }
  118. ret := int64(0)
  119. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  120. if err != nil {
  121. fields, _ := util.MarshalToString(where)
  122. logger.Error("mysql",
  123. zap.String("sql", "select count "+p.TableName()),
  124. zap.String("fields", fields),
  125. zap.String("error", err.Error()))
  126. }
  127. return ret, err
  128. }
  129. func (p *TPark) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TPark, err error) {
  130. cond, val, err := whereBuildAndOr(where, or)
  131. if err != nil {
  132. return list, err
  133. }
  134. if pageSize < 0 {
  135. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  136. if result.Error != nil {
  137. wherefields, _ := util.MarshalToString(where)
  138. logger.Error("mysql",
  139. zap.String("sql", "select * from "+p.TableName()),
  140. zap.String("where", wherefields),
  141. zap.String("error", result.Error.Error()))
  142. }
  143. return list, result.Error
  144. }
  145. offset := (page - 1) * pageSize
  146. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  147. if result.Error != nil {
  148. wherefields, _ := util.MarshalToString(where)
  149. logger.Error("mysql",
  150. zap.String("sql", "select * from "+p.TableName()),
  151. zap.String("where", wherefields),
  152. zap.String("error", result.Error.Error()))
  153. }
  154. return list, result.Error
  155. }