house_unit_building.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 THouseUnitBuilding struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  15. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  16. HouseNumber string `gorm:"column:house_number" json:"house_number"`
  17. Layer int64 `gorm:"column:layer" json:"layer"`
  18. RoomCount int64 `gorm:"column:room_count" json:"room_count"`
  19. HallCount int64 `gorm:"column:hall_count" json:"hall_count"`
  20. HouseType int64 `gorm:"column:house_type" json:"house_type"`
  21. HouseArea float64 `gorm:"column:house_area" json:"house_area"`
  22. HouseUsedArea float64 `gorm:"column:house_used_area" json:"house_used_area"`
  23. HasLift int64 `gorm:"column:has_lift" json:"has_lift"`
  24. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  25. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  26. Status int `gorm:"column:status" json:"status"`
  27. BuildingNumber string `gorm:"column:building_number" json:"building_number"`
  28. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  29. table string
  30. }
  31. func (p *THouseUnitBuilding) TableName() string {
  32. return p.table
  33. }
  34. func NewHouseUnitBuilding(database string) *THouseUnitBuilding {
  35. return &THouseUnitBuilding{table: fmt.Sprintf("%s.%s", database, "t_house_unit_building")}
  36. }
  37. func (p *THouseUnitBuilding) CreateView(db *gorm.DB, database string) error {
  38. sql := "CREATE VIEW "+database+".`t_house_unit_building` AS select t1.*,"+
  39. "`t2`.`unit_number` AS `unit_number`,`t3`.`building_number` AS `building_number` from "+
  40. "(("+database+".`t_house` `t1` left join "+database+".`t_unit` `t2` on((`t2`.`id` = `t1`.`unit_id`))) left join "+database+".`t_building` `t3` on((`t3`.`id` = `t2`.`building_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 *THouseUnitBuilding) Find(db *gorm.DB, where map[string]interface{}) error {
  51. err := db.Table(p.TableName()).Where(where).Find(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 *THouseUnitBuilding) 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 *THouseUnitBuilding) 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 *THouseUnitBuilding) InsertMulti(db *gorm.DB, values interface{}) error {
  84. err := db.Table(p.TableName()).Create(&values).Error
  85. if err != nil {
  86. fields, _ := util.MarshalToString(*p)
  87. logger.Error("mysql",
  88. zap.String("sql", "insert into "+p.TableName()),
  89. zap.String("fields", fields),
  90. zap.String("error", err.Error()))
  91. }
  92. return err
  93. }
  94. func (p *THouseUnitBuilding) Delete(db *gorm.DB, filter map[string]interface{}) error {
  95. cond, val, err := whereBuild(filter)
  96. if err != nil {
  97. return err
  98. }
  99. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  100. }
  101. func (p *THouseUnitBuilding) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  102. cond, val, err := whereBuild(where)
  103. if err != nil {
  104. if err != nil {
  105. fields, _ := util.MarshalToString(values)
  106. logger.Error("mysql",
  107. zap.String("sql", "update "+p.TableName()),
  108. zap.String("fields", fields),
  109. zap.String("error", err.Error()))
  110. }
  111. return err
  112. }
  113. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  114. }
  115. func (p *THouseUnitBuilding) UpdateByModel(db *gorm.DB) error {
  116. err := db.Model(p).Updates(p).Error
  117. if err != nil {
  118. fields, _ := util.MarshalToString(*p)
  119. logger.Error("mysql",
  120. zap.String("sql", "update "+p.TableName()),
  121. zap.String("fields", fields),
  122. zap.String("error", err.Error()))
  123. }
  124. return err
  125. }
  126. func (p *THouseUnitBuilding) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  127. cond, val, err := whereBuildAndOr(where, or)
  128. if err != nil {
  129. return 0, err
  130. }
  131. ret := int64(0)
  132. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  133. if err != nil {
  134. fields, _ := util.MarshalToString(where)
  135. logger.Error("mysql",
  136. zap.String("sql", "select count "+p.TableName()),
  137. zap.String("fields", fields),
  138. zap.String("error", err.Error()))
  139. }
  140. return ret, err
  141. }
  142. func (p *THouseUnitBuilding) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseUnitBuilding, err error) {
  143. cond, val, err := whereBuildAndOr(where, or)
  144. if err != nil {
  145. return list, err
  146. }
  147. if pageSize < 0 {
  148. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  149. if result.Error != nil {
  150. wherefields, _ := util.MarshalToString(where)
  151. logger.Error("mysql",
  152. zap.String("sql", "select * from "+p.TableName()),
  153. zap.String("where", wherefields),
  154. zap.String("error", result.Error.Error()))
  155. }
  156. return list, result.Error
  157. }
  158. offset := (page - 1) * pageSize
  159. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  160. if result.Error != nil {
  161. wherefields, _ := util.MarshalToString(where)
  162. logger.Error("mysql",
  163. zap.String("sql", "select * from "+p.TableName()),
  164. zap.String("where", wherefields),
  165. zap.String("error", result.Error.Error()))
  166. }
  167. return list, result.Error
  168. }