vehicle.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 TVehicle struct {
  13. ID int64 `gorm:"column:id;PRIMARY_EKY" json:"id"`
  14. PlateNo string `gorm:"column:plate_no" json:"plate_no"`
  15. VehicleType int32 `gorm:"column:vehicle_type" json:"vehicle_type"`
  16. Vin string `gorm:"column:vin" json:"vin"`
  17. EngineNo string `gorm:"column:engine_no" json:"engine_no"`
  18. Brand string `gorm:"column:brand" json:"brand"`
  19. Color string `gorm:"column:color" json:"color"`
  20. SpaceId int64 `gorm:"column:space_id" json:"space_id"`
  21. ParkId int64 `gorm:"column:park_id" json:"park_id"`
  22. HouseholdUid int64 `gorm:"column:household_uid" json:"household_uid"`
  23. BindType int32 `gorm:"column:bind_type" json:"bind_type"`
  24. Displacement float64 `gorm:"column:displacement" json:"displacement"`
  25. DisplacementUnit string `gorm:"column:displacement_unit" json:"displacement_unit"`
  26. Seat int32 `gorm:"column:seat" json:"seat"`
  27. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  28. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  29. table string
  30. }
  31. func (p *TVehicle) TableName() string {
  32. return p.table
  33. }
  34. func NewVehicle(database string) *TVehicle {
  35. return &TVehicle{table: fmt.Sprintf("%s.%s", database, "t_vehicle")}
  36. }
  37. func (p *TVehicle) SetTable(database string) {
  38. p.table = fmt.Sprintf("%s.%s", database, "t_vehicle")
  39. }
  40. func (p *TVehicle) CreateTable(db *gorm.DB) error {
  41. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  42. "`id` int(10) NOT NULL AUTO_INCREMENT,"+
  43. "`plate_no` varchar(32) COLLATE utf8mb4_bin NOT NULL,"+
  44. " `vehicle_type` tinyint(1) NOT NULL COMMENT '1 小型车 2 大型车 3 新能源小车 4 新能源大车',"+
  45. "`vin` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '车架号',"+
  46. "`engine_no` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '发动机号',"+
  47. "`brand` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '品牌',"+
  48. "`color` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '颜色',"+
  49. "`space_id` int(10) NOT NULL COMMENT '停车位',"+
  50. "`park_id` int(10) NOT NULL COMMENT '停车场',"+
  51. "`household_uid` bigint(11) NOT NULL COMMENT '业主id',"+
  52. "`bind_type` tinyint(1) NOT NULL COMMENT '1 无固定车位 2 买车位 3 租车位',"+
  53. "`displacement` decimal(18,2) COLLATE utf8mb4_bin NOT NULL COMMENT '排量(费用配置可能按排量)',"+
  54. "`displacement_unit` varchar(1) NOT NULL COMMENT 'L/T(费用配置可能按排量)',"+
  55. "`seat` int(10) NOT NULL COMMENT '座位数 (费用配置可能按座位数)',"+
  56. "`created_at` datetime NOT NULL,"+
  57. " `updated_at` datetime NOT NULL,"+
  58. "PRIMARY KEY (`id`),"+
  59. " UNIQUE KEY `plate_no` (`plate_no`) USING BTREE,"+
  60. " KEY `household_uid` (`household_uid`) USING BTREE,"+
  61. " KEY `space_id` (`space_id`) USING BTREE,"+
  62. " KEY `park_id` (`park_id`) USING BTREE"+
  63. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  64. err := db.Exec(sql).Error
  65. if err != nil {
  66. logger.Error("mysql",
  67. zap.String("sql", "create table "+p.TableName()),
  68. zap.String("fields", ""),
  69. zap.String("error", err.Error()))
  70. }
  71. return err
  72. }
  73. func (p *TVehicle) Find(db *gorm.DB, where map[string]interface{}) error {
  74. err := db.Table(p.TableName()).Where(where).Find(p).Error
  75. if err != nil {
  76. fields, _ := util.MarshalToString(where)
  77. logger.Error("mysql",
  78. zap.String("sql", "select from "+p.TableName()),
  79. zap.String("fields", fields),
  80. zap.String("error", err.Error()))
  81. }
  82. return err
  83. }
  84. func (p *TVehicle) Last(db *gorm.DB) error {
  85. err := db.Table(p.TableName()).Last(p).Error
  86. if err != nil {
  87. logger.Error("mysql",
  88. zap.String("sql", "select last from "+p.TableName()),
  89. zap.String("fields", ""),
  90. zap.String("error", err.Error()))
  91. }
  92. return err
  93. }
  94. // Insert 插入一条记录
  95. func (p *TVehicle) Insert(db *gorm.DB) error {
  96. err := db.Table(p.TableName()).Create(p).Error
  97. if err != nil {
  98. fields, _ := util.MarshalToString(*p)
  99. logger.Error("mysql",
  100. zap.String("sql", "insert into "+p.TableName()),
  101. zap.String("fields", fields),
  102. zap.String("error", err.Error()))
  103. }
  104. return err
  105. }
  106. // Insert 插入多条记录
  107. func (p *TVehicle) InsertMulti(db *gorm.DB, values interface{}) error {
  108. err := db.Table(p.TableName()).Create(values).Error
  109. if err != nil {
  110. fields, _ := util.MarshalToString(*p)
  111. logger.Error("mysql",
  112. zap.String("sql", "insert into "+p.TableName()),
  113. zap.String("fields", fields),
  114. zap.String("error", err.Error()))
  115. }
  116. return err
  117. }
  118. func (p *TVehicle) Delete(db *gorm.DB, filter map[string]interface{}) error {
  119. cond, val, err := whereBuild(filter)
  120. if err != nil {
  121. return err
  122. }
  123. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  124. }
  125. func (p *TVehicle) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  126. cond, val, err := whereBuild(where)
  127. if err != nil {
  128. if err != nil {
  129. fields, _ := util.MarshalToString(values)
  130. logger.Error("mysql",
  131. zap.String("sql", "update "+p.TableName()),
  132. zap.String("fields", fields),
  133. zap.String("error", err.Error()))
  134. }
  135. return err
  136. }
  137. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  138. }
  139. func (p *TVehicle) UpdateByModel(db *gorm.DB) error {
  140. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  141. if err != nil {
  142. fields, _ := util.MarshalToString(*p)
  143. logger.Error("mysql",
  144. zap.String("sql", "update "+p.TableName()),
  145. zap.String("fields", fields),
  146. zap.String("error", err.Error()))
  147. }
  148. return err
  149. }
  150. func (p *TVehicle) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  151. cond, val, err := whereBuildAndOr(where, or)
  152. if err != nil {
  153. return 0, err
  154. }
  155. ret := int64(0)
  156. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  157. if err != nil {
  158. fields, _ := util.MarshalToString(where)
  159. logger.Error("mysql",
  160. zap.String("sql", "select count "+p.TableName()),
  161. zap.String("fields", fields),
  162. zap.String("error", err.Error()))
  163. }
  164. return ret, err
  165. }
  166. func (p *TVehicle) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TVehicle, err error) {
  167. cond, val, err := whereBuildAndOr(where, or)
  168. if err != nil {
  169. return list, err
  170. }
  171. if pageSize < 0 {
  172. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  173. if result.Error != nil {
  174. wherefields, _ := util.MarshalToString(where)
  175. logger.Error("mysql",
  176. zap.String("sql", "select * from "+p.TableName()),
  177. zap.String("where", wherefields),
  178. zap.String("error", result.Error.Error()))
  179. }
  180. return list, result.Error
  181. }
  182. offset := (page - 1) * pageSize
  183. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  184. if result.Error != nil {
  185. wherefields, _ := util.MarshalToString(where)
  186. logger.Error("mysql",
  187. zap.String("sql", "select * from "+p.TableName()),
  188. zap.String("where", wherefields),
  189. zap.String("error", result.Error.Error()))
  190. }
  191. return list, result.Error
  192. }