park_space.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 TParkSpace 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. Status int32 `gorm:"column:status" json:"status"`
  20. //GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  21. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  22. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  23. BindStatus int64 `gorm:"column:bind_status" json:"bind_status"`
  24. VehicleId int64 `gorm:"column:vehicle_id" json:"vehicle_id"`
  25. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  26. table string
  27. }
  28. func (p *TParkSpace) TableName() string {
  29. return p.table
  30. }
  31. func NewParkSpace(database string) *TParkSpace {
  32. return &TParkSpace{table: fmt.Sprintf("%s.%s", database, "t_park_space")}
  33. }
  34. func (p *TParkSpace) SetTable(database string) {
  35. p.table = fmt.Sprintf("%s.%s", database, "t_park_space")
  36. }
  37. func (p *TParkSpace) CreateTable(db *gorm.DB) error {
  38. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  39. "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  40. "`space_number` varchar(32) COLLATE utf8mb4_bin NOT NULL,"+
  41. "`space_type` tinyint(1) NOT NULL COMMENT '1 标准车位 2 大型车位 3 小型车位 4 特殊车位',"+
  42. "`space_area` decimal(18,2) NOT NULL,"+
  43. "`comment` varchar(512) COLLATE utf8mb4_bin NOT NULL,"+
  44. "`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 空闲 2 已售 3 已租',"+
  45. "`park_id` int(10) NOT NULL,"+
  46. "`created_at` datetime NOT NULL,"+
  47. "`updated_at` datetime NOT NULL,"+
  48. " `bind_status` tinyint(1) NOT NULL COMMENT '1 空闲 2 已占用',"+
  49. "`vehicle_id` int(10) NOT NULL,"+
  50. " `house_id` bigint(11) NOT NULL COMMENT '房屋id',"+
  51. "PRIMARY KEY (`id`) USING BTREE,"+
  52. " UNIQUE KEY `park_space` (`park_id`,`space_number`) USING BTREE,"+
  53. " KEY `vehicle_id` (`vehicle_id`) USING BTREE,"+
  54. " KEY `house_id` (`house_id`) USING BTREE"+
  55. ") ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT COMMENT='停车位';"
  56. err := db.Exec(sql).Error
  57. if err != nil {
  58. logger.Error("mysql",
  59. zap.String("sql", "create table "+p.TableName()),
  60. zap.String("fields", ""),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. func (p *TParkSpace) Find(db *gorm.DB, where map[string]interface{}) error {
  66. err := db.Table(p.TableName()).Where(where).Find(p).Error
  67. if err != nil {
  68. fields, _ := util.MarshalToString(where)
  69. logger.Error("mysql",
  70. zap.String("sql", "select from "+p.TableName()),
  71. zap.String("fields", fields),
  72. zap.String("error", err.Error()))
  73. }
  74. return err
  75. }
  76. func (p *TParkSpace) Last(db *gorm.DB) error {
  77. err := db.Table(p.TableName()).Last(p).Error
  78. if err != nil {
  79. logger.Error("mysql",
  80. zap.String("sql", "select last from "+p.TableName()),
  81. zap.String("fields", ""),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. // Insert 插入一条记录
  87. func (p *TParkSpace) Insert(db *gorm.DB) error {
  88. err := db.Table(p.TableName()).Create(p).Error
  89. if err != nil {
  90. fields, _ := util.MarshalToString(*p)
  91. logger.Error("mysql",
  92. zap.String("sql", "insert into "+p.TableName()),
  93. zap.String("fields", fields),
  94. zap.String("error", err.Error()))
  95. }
  96. return err
  97. }
  98. func (p *TParkSpace) Delete(db *gorm.DB, filter map[string]interface{}) error {
  99. cond, val, err := whereBuild(filter)
  100. if err != nil {
  101. return err
  102. }
  103. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  104. }
  105. func (p *TParkSpace) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  106. cond, val, err := whereBuild(where)
  107. if err != nil {
  108. if err != nil {
  109. fields, _ := util.MarshalToString(values)
  110. logger.Error("mysql",
  111. zap.String("sql", "update "+p.TableName()),
  112. zap.String("fields", fields),
  113. zap.String("error", err.Error()))
  114. }
  115. return err
  116. }
  117. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  118. }
  119. func (p *TParkSpace) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  120. cond, val, err := whereBuildAndOr(where, or)
  121. if err != nil {
  122. return 0, err
  123. }
  124. ret := int64(0)
  125. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  126. if err != nil {
  127. fields, _ := util.MarshalToString(where)
  128. logger.Error("mysql",
  129. zap.String("sql", "select count "+p.TableName()),
  130. zap.String("fields", fields),
  131. zap.String("error", err.Error()))
  132. }
  133. return ret, err
  134. }
  135. func (p *TParkSpace) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TParkSpace, err error) {
  136. cond, val, err := whereBuildAndOr(where, or)
  137. if err != nil {
  138. return list, err
  139. }
  140. if pageSize < 0 {
  141. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  142. if result.Error != nil {
  143. wherefields, _ := util.MarshalToString(where)
  144. logger.Error("mysql",
  145. zap.String("sql", "select * from "+p.TableName()),
  146. zap.String("where", wherefields),
  147. zap.String("error", result.Error.Error()))
  148. }
  149. return list, result.Error
  150. }
  151. offset := (page - 1) * pageSize
  152. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  153. if result.Error != nil {
  154. wherefields, _ := util.MarshalToString(where)
  155. logger.Error("mysql",
  156. zap.String("sql", "select * from "+p.TableName()),
  157. zap.String("where", wherefields),
  158. zap.String("error", result.Error.Error()))
  159. }
  160. return list, result.Error
  161. }
  162. func (p *TParkSpace) GetIds(db *gorm.DB, where map[string]interface{}) ([]int64, map[int64]int64, error) {
  163. cond, val, err := whereBuildAndOr(where, nil)
  164. if err != nil {
  165. return nil, nil, err
  166. }
  167. type Result struct {
  168. Id int64
  169. ParkId int64
  170. }
  171. array := []Result{}
  172. err = db.Table(p.TableName()).Select("id").Where(cond, val...).Find(&array).Error
  173. if err != nil {
  174. wherefields, _ := util.MarshalToString(where)
  175. logger.Error("mysql",
  176. zap.String("sql", "select id from "+p.TableName()),
  177. zap.String("where", wherefields),
  178. zap.String("error", err.Error()))
  179. }
  180. ret := make([]int64, len(array))
  181. m := map[int64]int64{}
  182. for i, v := range array {
  183. ret[i] = v.Id
  184. m[v.Id] = v.ParkId
  185. }
  186. return ret, m, err
  187. }