building_unit.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 TBuildingUnit struct {
  13. ID int64 `gorm:"column:id" json:"id"`
  14. BuildingNumber string `gorm:"column:building_number" json:"building_number"`
  15. BuildingName string `gorm:"column:building_name" json:"building_name"`
  16. BuildingArea float64 `gorm:"column:building_area" json:"building_area"`
  17. BuildingUsedArea float64 `gorm:"column:building_used_area" json:"building_used_area"`
  18. Comment string `gorm:"column:comment" json:"comment"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  21. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  22. UnitName string `gorm:"column:unit_name" json:"unit_name"`
  23. UnitLayers int64 `gorm:"column:unit_layers" json:"unit_layers"`
  24. HasLift int64 `gorm:"column:has_lift" json:"has_lift"`
  25. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  26. table string
  27. }
  28. func (p *TBuildingUnit) TableName() string {
  29. return p.table
  30. }
  31. func NewBuildingUnit(database string) *TBuildingUnit {
  32. return &TBuildingUnit{table: fmt.Sprintf("%s.%s", database, "t_building_unit")}
  33. }
  34. func (p *TBuildingUnit) CreateView(db *gorm.DB, database string) error {
  35. sql := "CREATE VIEW "+database+".`t_building_unit` AS select t1.*,"+
  36. "`t2`.`unit_name` AS `unit_name`,`t2`.`has_lift` AS `has_lift`, t2.unit_number,"+
  37. "`t2`.`unit_layers` AS `unit_layers`,`t2`.`id` AS `unit_id` from "+
  38. "("+database+".`t_building` `t1` left join "+database+".`t_unit` `t2` on((`t1`.`id` = `t2`.`building_id`)))"
  39. err := db.Exec(sql).Error
  40. if err != nil {
  41. logger.Error("mysql",
  42. zap.String("sql", "create view "+p.TableName()),
  43. zap.String("fields", ""),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. func (p *TBuildingUnit) Find(db *gorm.DB, where map[string]interface{}) error {
  49. err := db.Table(p.TableName()).Where(where).Find(p).Error
  50. if err != nil {
  51. fields, _ := util.MarshalToString(where)
  52. logger.Error("mysql",
  53. zap.String("sql", "select from "+p.TableName()),
  54. zap.String("fields", fields),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TBuildingUnit) Last(db *gorm.DB) error {
  60. err := db.Table(p.TableName()).Last(p).Error
  61. if err != nil {
  62. logger.Error("mysql",
  63. zap.String("sql", "select last from "+p.TableName()),
  64. zap.String("fields", ""),
  65. zap.String("error", err.Error()))
  66. }
  67. return err
  68. }
  69. // Insert 插入一条记录
  70. func (p *TBuildingUnit) Insert(db *gorm.DB) error {
  71. err := db.Create(p).Error
  72. if err != nil {
  73. fields, _ := util.MarshalToString(*p)
  74. logger.Error("mysql",
  75. zap.String("sql", "insert into "+p.TableName()),
  76. zap.String("fields", fields),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *TBuildingUnit) Delete(db *gorm.DB, filter map[string]interface{}) error {
  82. cond, val, err := whereBuild(filter)
  83. if err != nil {
  84. return err
  85. }
  86. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  87. }
  88. func (p *TBuildingUnit) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  89. cond, val, err := whereBuild(where)
  90. if err != nil {
  91. if err != nil {
  92. fields, _ := util.MarshalToString(values)
  93. logger.Error("mysql",
  94. zap.String("sql", "update "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  101. }
  102. func (p *TBuildingUnit) UpdateByModel(db *gorm.DB) error {
  103. err := db.Model(p).Updates(p).Error
  104. if err != nil {
  105. fields, _ := util.MarshalToString(*p)
  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. func (p *TBuildingUnit) BuildingCount(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...).Group("id").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. type BuildingItem struct {
  130. TBuildingUnit
  131. UnitCount int64 `gorm:"column:unit_count" json:"unit_count"`
  132. }
  133. func (p *TBuildingUnit) BuildingList(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []BuildingItem, err error) {
  134. cond, val, err := whereBuildAndOr(where, or)
  135. if err != nil {
  136. return list, err
  137. }
  138. if pageSize < 0 {
  139. result := db.Table(p.TableName()).Select([]string{"*, sum(unit_id > 0) as unit_count"}).Where(cond, val...).Group("id").Order("building_number").Find(&list)
  140. if result.Error != nil {
  141. wherefields, _ := util.MarshalToString(where)
  142. logger.Error("mysql",
  143. zap.String("sql", "select * from "+p.TableName()),
  144. zap.String("where", wherefields),
  145. zap.String("error", result.Error.Error()))
  146. }
  147. return list, result.Error
  148. }
  149. offset := (page - 1) * pageSize
  150. result := db.Table(p.TableName()).Select([]string{"*, sum(unit_id > 0) as unit_count"}).Where(cond, val...).Group("id").Order("building_number").Limit(pageSize).Offset(offset).Find(&list)
  151. if result.Error != nil {
  152. wherefields, _ := util.MarshalToString(where)
  153. logger.Error("mysql",
  154. zap.String("sql", "select * from "+p.TableName()),
  155. zap.String("where", wherefields),
  156. zap.String("error", result.Error.Error()))
  157. }
  158. return list, result.Error
  159. }
  160. func (p *TBuildingUnit) UnitCount(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  161. cond, val, err := whereBuildAndOr(where, or)
  162. if err != nil {
  163. return 0, err
  164. }
  165. ret := int64(0)
  166. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  167. if err != nil {
  168. fields, _ := util.MarshalToString(where)
  169. logger.Error("mysql",
  170. zap.String("sql", "select count "+p.TableName()),
  171. zap.String("fields", fields),
  172. zap.String("error", err.Error()))
  173. }
  174. return ret, err
  175. }
  176. func (p *TBuildingUnit) UnitList(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TBuildingUnit, err error) {
  177. cond, val, err := whereBuildAndOr(where, or)
  178. if err != nil {
  179. return list, err
  180. }
  181. if pageSize < 0 {
  182. result := db.Table(p.TableName()).Where(cond, val...).Order("building_number").Order("unit_number").Find(&list)
  183. if result.Error != nil {
  184. wherefields, _ := util.MarshalToString(where)
  185. logger.Error("mysql",
  186. zap.String("sql", "select * from "+p.TableName()),
  187. zap.String("where", wherefields),
  188. zap.String("error", result.Error.Error()))
  189. }
  190. return list, result.Error
  191. }
  192. offset := (page - 1) * pageSize
  193. result := db.Table(p.TableName()).Where(cond, val...).Order("building_number").Order("unit_number").Limit(pageSize).Offset(offset).Find(&list)
  194. if result.Error != nil {
  195. wherefields, _ := util.MarshalToString(where)
  196. logger.Error("mysql",
  197. zap.String("sql", "select * from "+p.TableName()),
  198. zap.String("where", wherefields),
  199. zap.String("error", result.Error.Error()))
  200. }
  201. return list, result.Error
  202. }