unit.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 TUnit struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  15. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  16. UnitName string `gorm:"column:unit_name" json:"unit_name"`
  17. UnitLayers int64 `gorm:"column:unit_layers" json:"unit_layers"`
  18. HasLift int64 `gorm:"column:has_lift" json:"has_lift"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  21. table string
  22. }
  23. func (p *TUnit) TableName() string {
  24. return p.table
  25. }
  26. func NewUnit(database string) *TUnit {
  27. return &TUnit{table: fmt.Sprintf("%s.%s", database, "t_unit")}
  28. }
  29. func (p *TUnit) SetTable(database string) {
  30. p.table = fmt.Sprintf("%s.%s", database, "t_unit")
  31. }
  32. func (p *TUnit) CreateTable(db *gorm.DB) error {
  33. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  34. "`id` int(10) NOT NULL AUTO_INCREMENT,"+
  35. "`building_id` int(10) NOT NULL COMMENT '楼栋id',"+
  36. "`unit_number` int(10) NOT NULL COMMENT '单元编号',"+
  37. "`unit_name` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '单元名称',"+
  38. "`unit_layers` int(10) NOT NULL COMMENT '单元楼层数',"+
  39. "`has_lift` tinyint(1) NOT NULL COMMENT '是否有电梯',"+
  40. "`created_at` datetime NOT NULL,"+
  41. " `updated_at` datetime NOT NULL,"+
  42. " PRIMARY KEY (`id`) USING BTREE,"+
  43. " UNIQUE KEY `number_index` (`building_id`,`unit_number`) USING BTREE"+
  44. ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
  45. err := db.Exec(sql).Error
  46. if err != nil {
  47. logger.Error("mysql",
  48. zap.String("sql", "create table "+p.TableName()),
  49. zap.String("fields", ""),
  50. zap.String("error", err.Error()))
  51. }
  52. return err
  53. }
  54. func (p *TUnit) Find(db *gorm.DB, where map[string]interface{}) error {
  55. err := db.Table(p.TableName()).Where(where).Find(p).Error
  56. if err != nil {
  57. fields, _ := util.MarshalToString(where)
  58. logger.Error("mysql",
  59. zap.String("sql", "select from "+p.TableName()),
  60. zap.String("fields", fields),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. func (p *TUnit) Last(db *gorm.DB) error {
  66. err := db.Table(p.TableName()).Last(p).Error
  67. if err != nil {
  68. logger.Error("mysql",
  69. zap.String("sql", "select last from "+p.TableName()),
  70. zap.String("fields", ""),
  71. zap.String("error", err.Error()))
  72. }
  73. return err
  74. }
  75. // Insert 插入一条记录
  76. func (p *TUnit) Insert(db *gorm.DB) error {
  77. err := db.Table(p.TableName()).Create(p).Error
  78. if err != nil {
  79. fields, _ := util.MarshalToString(*p)
  80. logger.Error("mysql",
  81. zap.String("sql", "insert into "+p.TableName()),
  82. zap.String("fields", fields),
  83. zap.String("error", err.Error()))
  84. }
  85. return err
  86. }
  87. // Insert 插入多条记录
  88. func (p *TUnit) InsertMulti(db *gorm.DB, values interface{}) error {
  89. err := db.Table(p.TableName()).Create(values).Error
  90. if err != nil {
  91. fields, _ := util.MarshalToString(*p)
  92. logger.Error("mysql",
  93. zap.String("sql", "insert into "+p.TableName()),
  94. zap.String("fields", fields),
  95. zap.String("error", err.Error()))
  96. }
  97. return err
  98. }
  99. func (p *TUnit) Delete(db *gorm.DB, filter map[string]interface{}) error {
  100. cond, val, err := whereBuild(filter)
  101. if err != nil {
  102. return err
  103. }
  104. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  105. }
  106. func (p *TUnit) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  107. cond, val, err := whereBuild(where)
  108. if err != nil {
  109. if err != nil {
  110. fields, _ := util.MarshalToString(values)
  111. logger.Error("mysql",
  112. zap.String("sql", "update "+p.TableName()),
  113. zap.String("fields", fields),
  114. zap.String("error", err.Error()))
  115. }
  116. return err
  117. }
  118. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  119. }
  120. func (p *TUnit) UpdateByModel(db *gorm.DB) error {
  121. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  122. if err != nil {
  123. fields, _ := util.MarshalToString(*p)
  124. logger.Error("mysql",
  125. zap.String("sql", "update "+p.TableName()),
  126. zap.String("fields", fields),
  127. zap.String("error", err.Error()))
  128. }
  129. return err
  130. }
  131. func (p *TUnit) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  132. cond, val, err := whereBuildAndOr(where, or)
  133. if err != nil {
  134. return 0, err
  135. }
  136. ret := int64(0)
  137. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  138. if err != nil {
  139. fields, _ := util.MarshalToString(where)
  140. logger.Error("mysql",
  141. zap.String("sql", "select count "+p.TableName()),
  142. zap.String("fields", fields),
  143. zap.String("error", err.Error()))
  144. }
  145. return ret, err
  146. }
  147. func (p *TUnit) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUnit, err error) {
  148. cond, val, err := whereBuildAndOr(where, or)
  149. if err != nil {
  150. return list, err
  151. }
  152. if pageSize < 0 {
  153. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  154. if result.Error != nil {
  155. wherefields, _ := util.MarshalToString(where)
  156. logger.Error("mysql",
  157. zap.String("sql", "select * from "+p.TableName()),
  158. zap.String("where", wherefields),
  159. zap.String("error", result.Error.Error()))
  160. }
  161. return list, result.Error
  162. }
  163. offset := (page - 1) * pageSize
  164. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  165. if result.Error != nil {
  166. wherefields, _ := util.MarshalToString(where)
  167. logger.Error("mysql",
  168. zap.String("sql", "select * from "+p.TableName()),
  169. zap.String("where", wherefields),
  170. zap.String("error", result.Error.Error()))
  171. }
  172. return list, result.Error
  173. }